58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"net"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gokrazy/gokrazy"
|
|
"github.com/rtr7/dyndns"
|
|
"github.com/vishvananda/netlink"
|
|
)
|
|
|
|
func main() {
|
|
gokrazy.WaitFor("net-route")
|
|
route, err := netlink.RouteGet(net.ParseIP("192.0.2.0" /* RFC5737 TEST-NET-1 */))
|
|
if err != nil {
|
|
log.Fatal("Unable to find route", err)
|
|
}
|
|
if got, want := len(route), 1; got != want {
|
|
log.Fatalf("Unable to find route get 0.0.0.0: got %v, want %v", got, want)
|
|
}
|
|
ipv4 := route[0].Src
|
|
ips := []net.IP{ipv4}
|
|
log.Print("IPs:", ips)
|
|
setNames(ips, os.Args[1:])
|
|
for {
|
|
time.Sleep(30)
|
|
setNames(ips, os.Args[1:])
|
|
}
|
|
}
|
|
|
|
func setNames(ips []net.IP, names []string) {
|
|
for _, ip := range ips {
|
|
if ip.IsMulticast() {
|
|
continue
|
|
}
|
|
if ip.IsLoopback() {
|
|
continue
|
|
}
|
|
name:
|
|
for _, name := range names {
|
|
if lookup_ips, err := net.LookupIP(name); err == nil && len(lookup_ips) > 0 {
|
|
for _, l_ip := range lookup_ips {
|
|
if ip.Equal(l_ip) {
|
|
continue name
|
|
}
|
|
}
|
|
}
|
|
if err := dyndns.SetSubname(name, ip); err != nil {
|
|
log.Printf("dyndns.SetSubname(%q, %v): %v", name, ip, err)
|
|
} else {
|
|
log.Printf("dyndns.SetSubname(%q, %v)", name, ip)
|
|
}
|
|
}
|
|
}
|
|
}
|