Last active
February 14, 2019 17:14
-
-
Save GeoffreyMetais/d791198d1c4681d590edeb5da8cde172 to your computer and use it in GitHub Desktop.
Update your IPv4 + IPv6 addresses only when they change. With several domains support.
This file contains 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 ( | |
b64 "encoding/base64" | |
"io/ioutil" | |
"net" | |
"net/http" | |
"strings" | |
) | |
var nddservice = "netlib.re" | |
var domains = map[string]string{"netlib.re": "testsubdomain", "codelib.re": "testsubdomain"} | |
var login = "testlogin" | |
var pass = b64.StdEncoding.EncodeToString([]byte("testpwd")) | |
func getIPv6() string { | |
conn, err := net.Dial("tcp6", "google.com:http") | |
defer conn.Close() | |
if err != nil { | |
return "" | |
} | |
localAddr := conn.LocalAddr().String() | |
idx := strings.LastIndex(localAddr, ":") | |
return localAddr[1 : idx-1] | |
} | |
func getIPv4() string { | |
client := &http.Client{} | |
client.Transport = &http.Transport{ | |
Dial: (func(network, addr string) (net.Conn, error) { | |
return (&net.Dialer{ | |
Timeout: 3 * time.Second, | |
}).Dial("tcp4", addr) | |
}), | |
} | |
resp, err := client.Get("http://t.karchnu.fr/ip.php") | |
if err == nil { | |
contents, err := ioutil.ReadAll(resp.Body) | |
if err == nil { | |
defer resp.Body.Close() | |
return strings.TrimSpace(string(contents)) | |
} | |
} | |
return "" | |
} | |
func filterIps(domain string, ip4, ip6 *string) { | |
ips, err := net.LookupIP(domain) | |
if err == nil { | |
for _, ip := range ips { | |
ipaddr := ip.String() | |
if ipaddr == *ip4 { | |
*ip4 = "" | |
} | |
if ipaddr == *ip6 { | |
*ip6 = "" | |
} | |
} | |
} | |
} | |
func main() { | |
ipv4 := getIPv4() | |
ipv6 := getIPv6() | |
if len(ipv4) == 0 && len(ipv6) == 0 { | |
return | |
} | |
for service, subdomain := range domains { | |
ip4 := string(ipv4) | |
ip6 := string(ipv6) | |
domain := subdomain + "." + service | |
filterIps(domain, &ip4, &ip6) | |
baseURL := "https://" + nddservice + "/domain/cliup/" + login + "/" + pass + "/" + domain + "/@" | |
if len(ip4) > 0 { | |
resp, err := http.Get(baseURL + "/A/" + ip4) | |
if err != nil { | |
defer resp.Body.Close() | |
} | |
} | |
if len(ip6) > 0 { | |
resp, err := http.Get(baseURL + "/AAAA/" + ip6) | |
if err != nil { | |
defer resp.Body.Close() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment