From 936efcbdca17fe6382489133bb7f25480a06731f Mon Sep 17 00:00:00 2001 From: Timothy Date: Mon, 12 May 2014 23:19:32 +0800 Subject: [PATCH] refactor post logic --- dns_handler.go | 28 +++++++++++++++++++--------- godns.go | 20 ++++++++++++++------ settings.go | 2 -- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/dns_handler.go b/dns_handler.go index 836dc05..66eeae5 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -8,7 +8,7 @@ import ( "strings" ) -func GetCurrentIP(url string) (string, error) { +func get_currentIP(url string) (string, error) { response, err := http.Get(url) defer response.Body.Close() @@ -21,37 +21,47 @@ func GetCurrentIP(url string) (string, error) { return string(body), nil } -func generate_header(content url.Values, setting Settings) url.Values { +func generate_header(content url.Values) url.Values { header := url.Values{} - header.Add("login_email", setting.Email) - header.Add("login_password", setting.Password) + header.Add("login_email", Configuration.Email) + header.Add("login_password", Configuration.Password) header.Add("format", "json") header.Add("lang", "en") header.Add("error_on_empty", "no") - for k, _ := range content { - header.Add(k, content.Get(k)) + if content != nil { + for k, _ := range content { + header.Add(k, content.Get(k)) + } } return header } -func post_data(url string, content url.Values, setting Settings) (string, error) { +func api_version() { + fmt.Println(Configuration.Email) + post_data("/Info.Version", nil) +} + +func post_data(url string, content url.Values) (string, error) { client := &http.Client{} - req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(content.Encode())) + values := generate_header(content) + req, _ := http.NewRequest("POST", "https://dnsapi.cn"+url, strings.NewReader(values.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", setting.Email)) + req.Header.Set("User-Agent", fmt.Sprintf("GoDNS/0.1 (%s)", Configuration.Email)) response, err := client.Do(req) defer response.Body.Close() if err != nil { fmt.Println("Post failed...") + fmt.Println(err.Error()) return "", err } resp, _ := ioutil.ReadAll(response.Body) + fmt.Println(string(resp)) return string(resp), nil } diff --git a/godns.go b/godns.go index 380b358..fe6d64f 100644 --- a/godns.go +++ b/godns.go @@ -6,14 +6,15 @@ import ( "time" ) +var Configuration Settings + func main() { fmt.Println("Starting...") - setting := LoadSettings() - fmt.Println(setting.IP_Url) + Configuration = LoadSettings() loop := make(chan bool) - go dns_loop(setting, loop) + go dns_loop(loop) ret := <-loop @@ -25,11 +26,18 @@ func main() { } } -func dns_loop(setting Settings, loop chan bool) { +func dns_loop(loop chan bool) { fmt.Println("Inside the loop...") //time.Sleep(time.Second * 60 * 5) - time.Sleep(time.Second * 5) - fmt.Println(GetCurrentIP(setting.IP_Url)) + currentIP, _ := get_currentIP(Configuration.IP_Url) + fmt.Println("Current IP is" + currentIP) + + //Continue to check the IP of sub-domain + if len(currentIP) > 0 { + + } + + api_version() loop <- false } diff --git a/settings.go b/settings.go index 1efb191..de07d0d 100644 --- a/settings.go +++ b/settings.go @@ -28,7 +28,5 @@ func LoadSettings() Settings { var setting Settings json.Unmarshal(file, &setting) - fmt.Println(setting.Email) - return setting }