diff --git a/main.go b/main.go index 93e409d..431edd0 100755 --- a/main.go +++ b/main.go @@ -5,12 +5,11 @@ import ( "encoding/json" "flag" "fmt" + "io" "io/ioutil" "log" "net/http" "strings" - - xj "github.com/basgys/goxml2json" ) /* @@ -53,49 +52,67 @@ func main() { subdomain := flag.String("prefix", "*.home.", "subdomain to point to this IP, empty str ok") apikey := flag.String("apikey", "7a26bd4b7aedbc6c44f56af38", "namesilo api key") flag.Parse() - uri := "https://www.namesilo.com/api/dnsListRecords?version=1&type=xml&key=" + *apikey + "&domain=" + *domain + uri := "https://www.namesilo.com/api/dnsListRecords?version=1&type=json&key=" + *apikey + "&domain=" + *domain log.Printf("uri=%s", uri) resp, err := http.Get(uri) if err != nil { panic(err) } - b, _ := ioutil.ReadAll(resp.Body) - log.Printf("%d: %s", resp.StatusCode, b) - buff := bytes.NewBuffer(b) - js, err := xj.Convert(buff) + defer resp.Body.Close() + b, _ := io.ReadAll(resp.Body) + if resp.StatusCode != http.StatusOK { + panic(fmt.Sprintf("(%d) %s", resp.StatusCode, b)) + } + var rawraw struct { + //Namesilo struct { + Request struct { + IP string `json:"ip"` + } `json:"request"` + Reply struct { + ResourceRecord []struct { + Host string `json:"host"` + RecordID string `json:"record_id"` + } `json:"resource_record"` + } `json:"reply"` + //} `json:"namesilo"` + } + if err := json.NewDecoder(bytes.NewReader(b)).Decode(&rawraw); err != nil { + panic(err) + } + ip := rawraw.Request.IP + log.Printf("%d: %+v (%s)", resp.StatusCode, rawraw, b) + log.Printf("looking for host == %s", *subdomain+*domain) + for _, v := range rawraw.Reply.ResourceRecord { + if v.Host != *subdomain+*domain { + continue + } + rrid := v.RecordID + log.Printf("found %s%s: %v %v", *subdomain, *domain, rrid, ip) + url := fmt.Sprintf("https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=json&key="+*apikey+"&domain="+*domain+"&rrid=%s&rrhost="+strings.TrimRight(*subdomain, ".")+"&rrvalue=%s&rrttl=3600", rrid, ip) + log.Printf("updating via %s", url) + resp, err := http.Get(url) + if err != nil { + panic(err) + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + panic(resp.StatusCode) + } + b, _ := ioutil.ReadAll(resp.Body) + log.Printf("%v: %s", err, b) + return + } + url := fmt.Sprintf("https://www.namesilo.com/api/dnsAddRecord?version=1&type=json&key="+*apikey+"&domain="+*domain+"&rrtype=A&rrhost="+strings.TrimRight(*subdomain, ".")+"&rrvalue=%s&rrttl=3600", ip) + log.Printf("creating via %s", url) + resp, err = http.Get(url) if err != nil { panic(err) } - var rawraw struct { - Namesilo struct { - Request struct { - IP string `json:"ip"` - } `json:"request"` - Reply struct { - ResourceRecord []struct { - Host string `json:"host"` - RecordID string `json:"record_id"` - } `json:"resource_record"` - } `json:"reply"` - } `json:"namesilo"` + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + panic(resp.StatusCode) } - if err := json.NewDecoder(strings.NewReader(js.String())).Decode(&rawraw); err != nil { - panic(err) - } - log.Printf("looking for host == %s", *subdomain+*domain) - for _, v := range rawraw.Namesilo.Reply.ResourceRecord { - if v.Host == *subdomain+*domain { - rrid := v.RecordID - ip := rawraw.Namesilo.Request.IP - log.Println(rrid, ip) - url := fmt.Sprintf("https://www.namesilo.com/api/dnsUpdateRecord?version=1&type=xml&key="+*apikey+"&domain="+*domain+"&rrid=%s&rrhost="+strings.TrimRight(*subdomain, ".")+"&rrvalue=%s&rrttl=3600", rrid, ip) - log.Printf("updating via %s", url) - resp, err := http.Get(url) - b, _ := ioutil.ReadAll(resp.Body) - js, _ := xj.Convert(bytes.NewBuffer(b)) - log.Printf("%v: %s", err, js) - return - } - } - log.Println(rawraw) + b, _ = ioutil.ReadAll(resp.Body) + log.Printf("%v: %s", err, b) + return } diff --git a/namesilo-amd64 b/namesilo-amd64 new file mode 100755 index 0000000..9dafaff Binary files /dev/null and b/namesilo-amd64 differ diff --git a/namesilo-arm64 b/namesilo-arm64 new file mode 100755 index 0000000..8069c27 Binary files /dev/null and b/namesilo-arm64 differ