Initial commit
This commit is contained in:
commit
a20eeeecc9
7
go.mod
Normal file
7
go.mod
Normal file
@ -0,0 +1,7 @@
|
||||
module github.com/FooSoft/dynhost
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/nrdcg/namesilo v0.2.1
|
||||
|
||||
require github.com/google/go-querystring v1.0.0 // indirect
|
11
go.sum
Normal file
11
go.sum
Normal file
@ -0,0 +1,11 @@
|
||||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/nrdcg/namesilo v0.2.1 h1:kLjCjsufdW/IlC+iSfAqj0iQGgKjlbUUeDJio5Y6eMg=
|
||||
github.com/nrdcg/namesilo v0.2.1/go.mod h1:lwMvfQTyYq+BbjJd30ylEG4GPSS6PII0Tia4rRpRiyw=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
132
main.go
Normal file
132
main.go
Normal file
@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/nrdcg/namesilo"
|
||||
)
|
||||
|
||||
func updateDns(token, domain, host, address string) error {
|
||||
transport, err := namesilo.NewTokenTransport(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := namesilo.NewClient(transport.Client())
|
||||
|
||||
var record *namesilo.ResourceRecord
|
||||
{
|
||||
log.Println("querying DNS records...")
|
||||
|
||||
params := &namesilo.DnsListRecordsParams{
|
||||
Domain: domain,
|
||||
}
|
||||
|
||||
dnsListRecords, err := client.DnsListRecords(params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, r := range dnsListRecords.Reply.ResourceRecord {
|
||||
if r.Host == fmt.Sprintf("%s.%s", host, domain) {
|
||||
record = &r
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if record == nil {
|
||||
log.Println("adding new DNS record...")
|
||||
|
||||
params := &namesilo.DnsAddRecordParams{
|
||||
Distance: 0,
|
||||
Domain: domain,
|
||||
Host: host,
|
||||
TTL: 7200,
|
||||
Type: "A",
|
||||
Value: address,
|
||||
}
|
||||
|
||||
if _, err := client.DnsAddRecord(params); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if record.Value != address {
|
||||
log.Println("updating DNS record...")
|
||||
|
||||
params := &namesilo.DnsUpdateRecordParams{
|
||||
Distance: 0,
|
||||
Domain: domain,
|
||||
Host: host,
|
||||
ID: record.RecordID,
|
||||
TTL: 7200,
|
||||
Value: address,
|
||||
}
|
||||
|
||||
if _, err := client.DnsUpdateRecord(params); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
log.Println("DNS record is current")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func queryAddress() (string, error) {
|
||||
log.Println("querying current address...")
|
||||
|
||||
type response struct {
|
||||
Query string
|
||||
}
|
||||
|
||||
req, err := http.Get("http://ip-api.com/json/")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer req.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var ip response
|
||||
json.Unmarshal(body, &ip)
|
||||
|
||||
return ip.Query, nil
|
||||
}
|
||||
|
||||
func exec() error {
|
||||
var (
|
||||
token = os.Getenv("DYN_TOKEN")
|
||||
domain = os.Getenv("DYN_DOMAIN")
|
||||
host = os.Getenv("DYN_HOST")
|
||||
)
|
||||
|
||||
if len(token) == 0 || len(domain) == 0 || len(host) == 0 {
|
||||
return errors.New("undefined environment")
|
||||
}
|
||||
|
||||
address, err := queryAddress()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := updateDns(token, domain, host, address); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := exec(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user