2016-06-19 11:33:25 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jessevdk/go-flags"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Options struct {
|
|
|
|
Verbose bool `long:"verbose" short:"v"`
|
2016-06-19 13:30:44 -05:00
|
|
|
Watch bool `long:"watch" description:"Watch for interface changes"`
|
2016-06-19 11:33:25 -05:00
|
|
|
|
|
|
|
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"`
|
2016-06-19 11:33:25 -05:00
|
|
|
|
|
|
|
Server string `long:"server" value-name:"HOST[:PORT]"`
|
|
|
|
Timeout time.Duration `long:"timeout" value-name:"DURATION" default:"10s"`
|
2016-06-19 13:30:44 -05:00
|
|
|
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 11:33:25 -05:00
|
|
|
|
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"`
|
2016-06-19 12:10:19 -05:00
|
|
|
|
|
|
|
Args struct {
|
|
|
|
Name string `description:"DNS Name to update"`
|
|
|
|
} `positional-args:"yes"`
|
2016-06-19 11:33:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var options Options
|
|
|
|
|
2016-06-19 12:10:19 -05:00
|
|
|
if _, err := flags.Parse(&options); err != nil {
|
2016-06-19 11:33:25 -05:00
|
|
|
log.Fatalf("flags.Parse: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2016-06-19 11:46:23 -05:00
|
|
|
var update = Update{
|
2016-06-19 13:30:44 -05:00
|
|
|
ttl: int(options.TTL.Seconds()),
|
2016-06-19 11:33:25 -05:00
|
|
|
timeout: options.Timeout,
|
2016-06-19 13:30:44 -05:00
|
|
|
retry: options.Retry,
|
|
|
|
verbose: options.Verbose,
|
2016-06-19 11:33:25 -05:00
|
|
|
}
|
|
|
|
|
2016-06-19 12:10:19 -05:00
|
|
|
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)
|
2016-06-19 11:33:25 -05:00
|
|
|
}
|
|
|
|
|
2016-06-19 12:10:19 -05:00
|
|
|
if options.TSIGSecret != "" {
|
|
|
|
var name = options.TSIGName
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
name = options.Args.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("using TSIG: %v (algo=%v)", name, options.TSIGAlgorithm)
|
2016-06-19 11:33:25 -05:00
|
|
|
|
2016-06-19 12:10:19 -05:00
|
|
|
update.InitTSIG(name, options.TSIGSecret, options.TSIGAlgorithm)
|
2016-06-19 11:33:25 -05:00
|
|
|
}
|
|
|
|
|
2016-06-19 11:46:23 -05:00
|
|
|
// addrs
|
2016-06-19 13:30:44 -05:00
|
|
|
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:33:25 -05:00
|
|
|
}
|
|
|
|
|
2016-06-19 11:46:23 -05:00
|
|
|
// update
|
2016-06-19 13:30:44 -05:00
|
|
|
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 {
|
2016-06-19 13:30:44 -05:00
|
|
|
log.Printf("update done")
|
2016-06-19 11:33:25 -05:00
|
|
|
}
|
|
|
|
}
|