github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/ip/fallbacks.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package ip 19 20 import ( 21 "errors" 22 "io" 23 "net" 24 "strings" 25 26 "github.com/mysteriumnetwork/node/requests" 27 "github.com/mysteriumnetwork/node/utils/random" 28 ) 29 30 // IPFallbackAddresses represents the various services we can use to fetch our public IP. 31 var IPFallbackAddresses = []string{ 32 "https://api.ipify.org", 33 "https://ip2location.io/ip", 34 "https://ipinfo.io/ip", 35 "https://api.ipify.org", 36 "https://ifconfig.me", 37 "https://www.trackip.net/ip", 38 "https://checkip.amazonaws.com/", 39 "https://icanhazip.com", 40 "https://ipecho.net/plain", 41 "https://ident.me/", 42 "http://whatismyip.akamai.com/", 43 } 44 45 var rng = random.NewTimeSeededRand() 46 47 func shuffleStringSlice(slice []string) []string { 48 tmp := make([]string, len(slice)) 49 copy(tmp, slice) 50 rng.Shuffle(len(tmp), func(i, j int) { 51 tmp[i], tmp[j] = tmp[j], tmp[i] 52 }) 53 return tmp 54 } 55 56 // RequestAndParsePlainIPResponse requests and parses a plain IP response. 57 func RequestAndParsePlainIPResponse(c *requests.HTTPClient, url string) (string, error) { 58 req, err := requests.NewGetRequest(url, "", nil) 59 if err != nil { 60 return "", err 61 } 62 63 res, err := c.Do(req) 64 if err != nil { 65 return "", err 66 } 67 defer res.Body.Close() 68 69 r, err := io.ReadAll(res.Body) 70 if err != nil { 71 return "", err 72 } 73 74 ipv4addr := net.ParseIP(strings.TrimSpace(string(r))) 75 if ipv4addr == nil { 76 return "", errors.New("could not parse ip response") 77 } 78 return ipv4addr.String(), err 79 }