Created
November 28, 2016 00:13
-
-
Save surma/19203114902e235d214228e7e16ab666 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"log" | |
"io/ioutil" | |
"net/http" | |
"time" | |
"github.com/weppos/dnsimple-go/dnsimple" | |
) | |
var ( | |
token = flag.String("token", "", "Domain token") | |
entryID = flag.Int("entry", 0, "ID of the entry") | |
domain = flag.String("domain", "", "Domain name") | |
interval = flag.Duration("interval", 60*time.Minute, "Interval of update") | |
) | |
func main() { | |
flag.Parse() | |
checkFlags() | |
for { | |
api := dnsimple.NewAuthenticatedClient(dnsimple.NewDomainTokenCredentials(*token)) | |
ip, err := getIP() | |
if err != nil { | |
log.Printf("Could not get public IP: %s", err) | |
continue | |
} | |
log.Printf("Updating IP to %s", ip) | |
_, _, err = api.Domains.UpdateRecord(*domain, *entryID, dnsimple.Record{ | |
Content: ip, | |
}) | |
if err != nil { | |
log.Printf("Could not update DNS record: %s", err) | |
continue | |
} | |
time.Sleep(*interval) | |
} | |
} | |
func checkFlags() { | |
if *token == "" { | |
log.Fatalf("Must provide -token") | |
} | |
if *entryID == 0 { | |
log.Fatalf("Must provide -entry") | |
} | |
if *domain == "" { | |
log.Fatalf("Must provide -domain") | |
} | |
} | |
func getIP() (string, error) { | |
resp, err := http.Get("https://icanhazip.com") | |
if err != nil { | |
return "", err | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return "", err | |
} | |
return string(body), err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment