From 6ec63e44992017af99fac8afa2c73a11c72a7ff7 Mon Sep 17 00:00:00 2001 From: Timothy Date: Thu, 30 Jan 2020 23:54:47 +0800 Subject: [PATCH] add IPV6 support --- settings.go | 4 ++-- utils.go | 35 +++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/settings.go b/settings.go index 1c3411c..cf59b7b 100644 --- a/settings.go +++ b/settings.go @@ -30,14 +30,14 @@ type Settings struct { LoginToken string `json:"login_token"` Domains []Domain `json:"domains"` IPUrl string `json:"ip_url"` + IPV6Url string `json:"ipv6_url"` Interval int `json:"interval"` UserAgent string `json:"user_agent,omitempty"` LogPath string `json:"log_path"` Socks5Proxy string `json:"socks5_proxy"` Notify Notify `json:"notify"` IPInterface string `json:"ip_interface"` - //the code is not ready to update AAAA record - //IPType string `json:"ip_type"` + Mode string `json:"mode"` } // LoadSettings -- Load settings from config file diff --git a/utils.go b/utils.go index b3fc85f..5bb312d 100644 --- a/utils.go +++ b/utils.go @@ -46,6 +46,10 @@ const ( GOOGLE = "Google" // DUCK for Duck DNS DUCK = "DuckDNS" + // IPV4 for IPV4 mode + IPV4 = "IPV4" + // IPV6 for IPV6 mode + IPV6 = "IPV6" ) //GetIPFromInterface gets IP address from the specific interface @@ -84,17 +88,17 @@ func GetIPFromInterface(configuration *Settings) (string, error) { continue } - //the code is not ready for updating an AAAA record - /* - if (isIPv4(ip.String())){ - if (configuration.IPType=="IPv6"){ - continue; - } - }else{ - if (configuration.IPType!="IPv6"){ - continue; - } - } */ + //the code is not ready for updating the 'AAAA' record + if isIPv4(ip.String()) { + if strings.ToUpper(configuration.Mode) == IPV4 { + continue + } + } else { + if configuration.Mode != IPV6 { + continue + } + } + if !isIPv4(ip.String()) { continue } @@ -172,7 +176,14 @@ func GetIPOnline(configuration *Settings) (string, error) { httpTransport.Dial = dialer.Dial } - response, err := client.Get(configuration.IPUrl) + var response *http.Response + var err error + + if configuration.Mode == IPV4 { + response, err = client.Get(configuration.IPUrl) + } else { + response, err = client.Get(configuration.IPV6Url) + } if err != nil { log.Println("Cannot get IP...")