diff --git a/dns_handler.go b/dns_handler.go index 813c3a1..7f07f9a 100644 --- a/dns_handler.go +++ b/dns_handler.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "net/http" "net/url" + "strconv" "strings" ) @@ -70,8 +71,6 @@ func get_domain(name string) int64 { if sjson.Get("status").Get("code").MustString() == "1" { domains, _ := sjson.Get("domains").Array() - fmt.Println(domains) - for _, d := range domains { m := d.(map[string]interface{}) if m["name"] == name { @@ -81,15 +80,80 @@ func get_domain(name string) int64 { case json.Number: ret, _ = t.Int64() } + + break } } } - fmt.Printf("Domain id is: %d", ret) return ret } -func get_subdomain(name string) int64 { +func get_subdomain(domain_id int64, name string) (string, string) { + var ret, ip string + value := url.Values{} + value.Add("domain_id", strconv.FormatInt(domain_id, 10)) + value.Add("offset", "0") + value.Add("length", "1") + value.Add("sub_domain", name) + + response, err := post_data("/Record.List", value) + + if err != nil { + fmt.Println("Failed to get domain list...") + return "", "" + } + + sjson, parse_err := simplejson.NewJson([]byte(response)) + + if parse_err != nil { + fmt.Println(parse_err.Error()) + return "", "" + } + + if sjson.Get("status").Get("code").MustString() == "1" { + records, _ := sjson.Get("records").Array() + + for _, d := range records { + m := d.(map[string]interface{}) + if m["name"] == name { + ret = m["id"].(string) + ip = m["value"].(string) + break + } + } + } + + return ret, ip +} + +func update_ip(domain_id int64, sub_domain_id string, sub_domain_name string, ip string) { + value := url.Values{} + value.Add("domain_id", strconv.FormatInt(domain_id, 10)) + value.Add("record_id", sub_domain_id) + value.Add("sub_domain", sub_domain_name) + value.Add("record_type", "A") + value.Add("record_line", "默认") + value.Add("value", ip) + + response, err := post_data("/Record.Modify", value) + + if err != nil { + fmt.Println("Failed to update record to new IP!") + fmt.Println(err.Error()) + return + } + + sjson, parse_err := simplejson.NewJson([]byte(response)) + + if parse_err != nil { + fmt.Println(parse_err.Error()) + return + } + + if sjson.Get("status").Get("code").MustString() == "1" { + fmt.Println("New IP updated!") + } } @@ -112,6 +176,5 @@ func post_data(url string, content url.Values) (string, error) { resp, _ := ioutil.ReadAll(response.Body) - fmt.Println(string(resp)) return string(resp), nil } diff --git a/godns.go b/godns.go index 857a22e..dc107bb 100644 --- a/godns.go +++ b/godns.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "strings" "time" ) @@ -30,14 +31,22 @@ func dns_loop(loop chan bool) { fmt.Println("Inside the loop...") time.Sleep(time.Second * 2) - // currentIP, _ := get_currentIP(Configuration.IP_Url) - // fmt.Println("Current IP is" + currentIP) + domain_id := get_domain(Configuration.Domain) + + currentIP, _ := get_currentIP(Configuration.IP_Url) + sub_domain_id, ip := get_subdomain(domain_id, Configuration.Sub_domain) + + fmt.Printf("currentIp is:%s\n", currentIP) //Continue to check the IP of sub-domain - // if len(currentIP) > 0 { - domain_id := get_domain(Configuration.Domain) - // } + if len(ip) > 0 && !strings.Contains(currentIP, ip) { + + fmt.Println("Start to update record IP...") + update_ip(domain_id, sub_domain_id, Configuration.Sub_domain, currentIP) + + } else { + fmt.Println("Current IP is same as domain IP, no need to update...") + } - api_version() loop <- false }