Skip to content

Commit

Permalink
[PSL-1092] normalise ipinfo response
Browse files Browse the repository at this point in the history
  • Loading branch information
j-rafique committed Dec 12, 2023
1 parent 0b49ddb commit ce31e45
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions common/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"math"
"math/big"
"net"
Expand All @@ -31,6 +30,7 @@ const (
semaphoreWeight = 1
highCompressTimeout = 30 * time.Minute
highCompressionLevel = 4
iPCheckURL = "http://ipinfo.io/ip"
)

var sem = semaphore.NewWeighted(maxParallelHighCompressCalls)
Expand Down Expand Up @@ -114,24 +114,31 @@ func IsContextErr(err error) bool {

// GetExternalIPAddress returns external IP address
func GetExternalIPAddress() (externalIP string, err error) {

resp, err := http.Get("http://ipinfo.io/ip")
resp, err := http.Get(iPCheckURL)
if err != nil {
return "", err
}

defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}

if net.ParseIP(string(body)) == nil {
return "", errors.Errorf("invalid IP response from %s", "ipconf.ip")
ipString := normalizeString(string(body))
if net.ParseIP(ipString) == nil {
return "", errors.Errorf("invalid IP response from %s", iPCheckURL)
}

return string(body), nil
return ipString, nil
}

func normalizeString(s string) string {
stringWithoutEscapes := strings.Replace(s, "\n", "", -1)
stringWithoutEscapes = strings.Replace(stringWithoutEscapes, "\t", "", -1)
stringWithoutEscapes = strings.Replace(stringWithoutEscapes, "\r", "", -1)

return stringWithoutEscapes
}

// B64Encode base64 encodes
Expand Down

0 comments on commit ce31e45

Please sign in to comment.