go-nsupdate/main.go

98 lines
2.3 KiB
Go
Raw Normal View History

package main
import (
"github.com/jessevdk/go-flags"
"log"
"os"
"time"
)
type Options struct {
Verbose bool `long:"verbose" short:"v"`
Watch bool `long:"watch" description:"Watch for interface changes"`
Interface string `long:"interface" short:"i" value-name:"IFACE" description:"Use address from interface"`
2016-06-19 13:56:06 -05:00
InterfaceFamily Family `long:"interface-family" value-name:"ipv4|ipv6|all"`
Server string `long:"server" value-name:"HOST[:PORT]"`
Timeout time.Duration `long:"timeout" value-name:"DURATION" default:"10s"`
Retry time.Duration `long:"retry" value-name:"DURATION" default:"30s"`
2016-06-19 13:56:06 -05:00
TSIGName string `long:"tsig-name" value-name:"FQDN"`
TSIGSecret string `long:"tsig-secret" value-name:"BASE-64" env:"TSIG_SECRET"`
TSIGAlgorithm TSIGAlgorithm `long:"tsig-algorithm" value-name:"hmac-{md5,sha1,sha256,sha512}" default:"hmac-sha1."`
2016-06-19 13:56:06 -05:00
Zone string `long:"zone" value-name:"FQDN" description:"Zone to update"`
TTL time.Duration `long:"ttl" value-name:"DURATION" default:"60s"`
Args struct {
Name string `description:"DNS Name to update"`
} `positional-args:"yes"`
}
func main() {
var options Options
if _, err := flags.Parse(&options); err != nil {
log.Fatalf("flags.Parse: %v", err)
os.Exit(1)
}
2016-06-19 11:46:23 -05:00
var update = Update{
ttl: int(options.TTL.Seconds()),
timeout: options.Timeout,
retry: options.Retry,
verbose: options.Verbose,
}
if err := update.Init(options.Args.Name, options.Zone, options.Server); err != nil {
2016-06-19 11:46:23 -05:00
log.Fatalf("init: %v", err)
}
if options.TSIGSecret != "" {
var name = options.TSIGName
if name == "" {
name = options.Args.Name
}
log.Printf("using TSIG: %v (algo=%v)", name, options.TSIGAlgorithm)
update.InitTSIG(name, options.TSIGSecret, options.TSIGAlgorithm)
}
2016-06-19 11:46:23 -05:00
// addrs
addrs, err := InterfaceAddrs(options.Interface, options.InterfaceFamily)
if err != nil {
2016-06-19 11:46:23 -05:00
log.Fatalf("addrs scan: %v", err)
}
2016-06-19 11:46:23 -05:00
// update
update.Start()
for {
log.Printf("update...")
if err := update.Update(addrs); err != nil {
log.Fatalf("update: %v", err)
}
if !options.Watch {
break
}
if err := addrs.Read(); err != nil {
log.Fatalf("addrs read: %v", err)
} else {
log.Printf("addrs update...")
}
}
log.Printf("wait...")
if err := update.Done(); err != nil {
log.Printf("update done: %v", err)
2016-06-19 12:05:55 -05:00
} else {
log.Printf("update done")
}
}