support adding new subdomains too
parent
525fd1c139
commit
2554efc2ea
93
main.go
93
main.go
|
|
@ -5,12 +5,11 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"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")
|
subdomain := flag.String("prefix", "*.home.", "subdomain to point to this IP, empty str ok")
|
||||||
apikey := flag.String("apikey", "7a26bd4b7aedbc6c44f56af38", "namesilo api key")
|
apikey := flag.String("apikey", "7a26bd4b7aedbc6c44f56af38", "namesilo api key")
|
||||||
flag.Parse()
|
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)
|
log.Printf("uri=%s", uri)
|
||||||
resp, err := http.Get(uri)
|
resp, err := http.Get(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
b, _ := ioutil.ReadAll(resp.Body)
|
defer resp.Body.Close()
|
||||||
log.Printf("%d: %s", resp.StatusCode, b)
|
b, _ := io.ReadAll(resp.Body)
|
||||||
buff := bytes.NewBuffer(b)
|
if resp.StatusCode != http.StatusOK {
|
||||||
js, err := xj.Convert(buff)
|
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 {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
var rawraw struct {
|
defer resp.Body.Close()
|
||||||
Namesilo struct {
|
if resp.StatusCode != http.StatusOK {
|
||||||
Request struct {
|
panic(resp.StatusCode)
|
||||||
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(strings.NewReader(js.String())).Decode(&rawraw); err != nil {
|
b, _ = ioutil.ReadAll(resp.Body)
|
||||||
panic(err)
|
log.Printf("%v: %s", err, b)
|
||||||
}
|
return
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue