You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
3.7 KiB
Go

package main
import (
"fmt"
"os"
"strings"
"sync"
"git.xbudex.com/buddy/update-dns/dns"
"git.xbudex.com/buddy/update-dns/ip"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var validTypes = []string{"A", "AAAA", "TXT"}
type typeFlag struct {
value string
}
func (f *typeFlag) Set(v string) error {
value := strings.ToUpper(v)
for _, val := range validTypes {
if val == value {
f.value = val
return nil
}
}
return errors.Errorf("invalid type %s, expected %v", v, validTypes)
}
func (f *typeFlag) String() string {
return f.value
}
func main() {
app := cli.NewApp()
8 years ago
app.Name = "update-dns"
8 years ago
app.Usage = "update dns using dnsimple with public ip address"
app.UsageText = "update-dns [options] host"
app.Version = "0.3.1"
app.Action = action
8 years ago
app.HideHelp = true
app.Authors = []cli.Author{
cli.Author{
Name: "Buddy Sandidge",
Email: "buddy@sandidge.lol",
},
}
app.Flags = []cli.Flag{
cli.GenericFlag{
Name: "type",
Value: &typeFlag{value: "AAAA"},
Usage: "type of dns record to create",
},
cli.IntFlag{
Name: "ttl",
Value: 600,
Usage: "time to live in seconds for dns record",
},
cli.StringFlag{
Name: "content",
Value: "",
Usage: "content to set manually, defaults to fetching ip address if not set",
},
cli.StringFlag{
Name: "get-ipv4-url",
Value: "https://v4.buddy.wtf/json",
Usage: "url to use to get public IPv4 address",
},
8 years ago
cli.StringFlag{
Name: "get-ipv6-url",
Value: "https://v6.buddy.wtf/json",
8 years ago
Usage: "url to use to get public IPv6 address",
},
cli.StringFlag{
EnvVar: "DNSIMPLE_TOKEN",
Name: "dnsimple-token",
Usage: "dnsimple token",
},
}
app.Run(os.Args)
}
type runtimeError string
func (e runtimeError) Error() string {
return string(e)
}
func action(context *cli.Context) error {
var wg sync.WaitGroup
if context.NArg() < 1 {
return runtimeError("requires host argument")
}
host := context.Args().Get(0)
kind := context.Generic("type").(*typeFlag).value
ttl := context.Int("ttl")
var content string
var getContentError error
wg.Add(1)
go func() {
defer wg.Done()
ipClient := &ip.Client{
URLv4: context.String("get-ipv4-url"),
URLv6: context.String("get-ipv6-url"),
}
content, getContentError = getContent(ipClient, kind, context.String("content"))
}()
dnsClient := dns.New(context.String("dnsimple-token"))
records := []dns.Record{}
var getRecordsError error
wg.Add(1)
go func() {
defer wg.Done()
records, getRecordsError = dnsClient.GetRecords(host, kind)
}()
wg.Wait()
if getContentError != nil {
return errors.Wrap(getContentError, "could no get ip address")
}
if getRecordsError != nil {
return errors.Wrap(getRecordsError, "could no get record")
}
if len(records) == 1 && records[0].Content == content {
fmt.Printf("%s record for %s already set as %s\n", kind, host, content)
return nil
}
newRecord := dns.Record{Type: kind, Content: content, TTL: ttl}
if err := dnsClient.CreateRecord(host, newRecord); err != nil {
return errors.Wrap(err, "could not create record")
}
fmt.Printf("%s record created for %s as %s\n", kind, host, content)
var deleteWG sync.WaitGroup
for _, record := range records {
deleteWG.Add(1)
go func(r dns.Record) {
defer deleteWG.Done()
fmt.Printf("%s record for %s being deleting %s\n", r.Type, host, r.Content)
dnsClient.DeleteRecord(r)
}(record)
}
deleteWG.Wait()
return nil
}
func getContent(client *ip.Client, kind, content string) (string, error) {
if content != "" {
return content, nil
}
switch kind {
case "A":
return client.GetIPv4()
case "AAAA":
return client.GetIPv6()
default:
return "", errors.Errorf("could not get content for type %s", kind)
}
}