Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dyn
- import (
- "crypto/tls"
- "github.com/go-resty/resty"
- "log"
- "net"
- "strings"
- )
- type Dyn interface {
- GetIp() string
- UpdateIp()
- }
- type He struct {
- Hostname string
- Password string
- Ip net.IP
- }
- func GetIp(uri string) net.IP {
- resp, err := resty.R().Get(uri)
- if err != nil {
- log.Fatal("ERROR: %s", err)
- }
- ip := net.ParseIP(strings.TrimSpace(string(resp.Body())))
- if ip == nil {
- log.Fatal("ERROR: Failed to parse the IP.")
- }
- return ip
- }
- func (h *He) UpdateIp(uri string) {
- log.Println("INFO: updating the ip")
- log.Printf("INFO: URI: %s nDomain: %s nPassword: %s nIP: %vn", uri, h.Hostname, h.Password, h.Ip)
- resty.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
- resp, err := resty.R().
- SetFormData(map[string]string{
- "hostname": h.Hostname,
- "password": h.Password,
- "myip": h.Ip.String(),
- }).
- Post(uri)
- if err != nil {
- log.Fatal("ERROR: ", err)
- }
- log.Printf("INFO: resp: %v", string(resp.Body()))
- }
- package main
- import (
- "fmt"
- "github.com/Adamar/dyn-he-updater/dyn"
- "gopkg.in/alecthomas/kingpin.v2"
- "net"
- "os"
- )
- var (
- app = kingpin.New("dyn-he-updater", "A commandline updater for dynamic ip's from dyn.dns.he.net")
- debug = app.Flag("debug", "Enable debug mode.").Bool()
- get = app.Command("get", "Get the raw ip from an external service.")
- getV4 = get.Flag("ipv4", "Get only the IPv4 ip address.").Bool()
- getV6 = get.Flag("ipv6", "Get only the IPv6 ip address.").Bool()
- update = app.Command("update", "Update the dynamic ip.")
- domain = update.Flag("domain", "Domain you want to update.").Required().String()
- pass = update.Flag("password", "Password to update the domain.").Required().String()
- v4 = update.Flag("ipv4", "Update only the IPv4 ip address. (default)").Bool()
- v6 = update.Flag("ipv6", "Update only the IPv6 ip address.").Bool()
- CheckIPv4Uri = "http://ipv4.myexternalip.com/raw"
- CheckIPv6Uri = "http://ipv6.myexternalip.com/raw"
- UpdateIpUri = "https://dyn.dns.he.net/nic/update"
- )
- func main() {
- kingpin.Version("0.0.1")
- switch kingpin.MustParse(app.Parse(os.Args[1:])) {
- case get.FullCommand():
- if *getV4 == true {
- r := dyn.GetIp(CheckIPv4Uri)
- fmt.Printf("IPv4: %sn", r)
- } else if *getV6 {
- r := dyn.GetIp(CheckIPv6Uri)
- fmt.Printf("IPv6: %sn", r)
- } else {
- r := dyn.GetIp(CheckIPv4Uri)
- fmt.Printf("IPv4: %s (default)n", r)
- }
- case update.FullCommand():
- if *domain != "" && *pass != "" {
- var r net.IP
- if *v4 == true || *v6 == false {
- r = dyn.GetIp(CheckIPv4Uri)
- } else if *v6 == true {
- r = dyn.GetIp(CheckIPv6Uri)
- }
- d := dyn.He{Hostname: *domain, Password: *pass, Ip: r}
- d.UpdateIp(UpdateIpUri)
- }
- }
- }
- Autodetect my IPv4/IPv6 address:
- % curl -4 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com"
- % curl -6 "http://dyn.example.com:password@dyn.dns.he.net/nic/update?hostname=dyn.example.com"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement