From 20642dc3965af89003642fe1a2666e1bb17a5043 Mon Sep 17 00:00:00 2001 From: wh1te909 Date: Mon, 8 Mar 2021 23:44:04 -0800 Subject: [PATCH] prefer ipv4 over ipv6 wh1te909/tacticalrmm#304 --- agent/utils.go | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/agent/utils.go b/agent/utils.go index 086dc51..490c902 100644 --- a/agent/utils.go +++ b/agent/utils.go @@ -25,6 +25,7 @@ func (a *WindowsAgent) PublicIP() string { client := resty.New() client.SetTimeout(4 * time.Second) urls := []string{"https://icanhazip.tacticalrmm.io/", "https://icanhazip.com", "https://ifconfig.co/ip"} + ip := "error" for _, url := range urls { r, err := client.R().Get(url) @@ -32,15 +33,28 @@ func (a *WindowsAgent) PublicIP() string { a.Logger.Debugln("PublicIP err", err) continue } - ip := StripAll(r.String()) + ip = StripAll(r.String()) if !IsValidIP(ip) { a.Logger.Debugln("PublicIP not valid", ip) continue } + v4 := net.ParseIP(ip) + if v4.To4() == nil { + r1, err := client.R().Get("https://ifconfig.me/ip") + if err != nil { + return ip + } + ipv4 := StripAll(r1.String()) + if !IsValidIP(ipv4) { + continue + } + a.Logger.Debugln("Forcing ipv4:", ipv4) + return ipv4 + } a.Logger.Debugln("PublicIP return: ", ip) - return ip + break } - return "error" + return ip } // GenerateAgentID creates and returns a unique agent id