support adding new subdomains too

master
Bel LaPointe 2024-05-19 15:13:25 -04:00
parent 525fd1c139
commit 2554efc2ea
3 changed files with 55 additions and 38 deletions

93
main.go
View File

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

BIN
namesilo-amd64 Executable file

Binary file not shown.

BIN
namesilo-arm64 Executable file

Binary file not shown.