1
0
mirror of https://github.com/taigrr/godns synced 2025-01-18 04:03:25 -08:00

Finish the main logic, make it run.

This commit is contained in:
Timothy 2014-05-13 23:16:56 +08:00
parent aca4a9ca27
commit 9a20a5354d
2 changed files with 83 additions and 11 deletions

View File

@ -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
}

View File

@ -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
}