Backend: fix IPv6 address parsing erroneous (#28585)

* Backend: Fix parsing of client IP address

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
taciomcosta
2020-11-25 07:55:22 +01:00
committed by GitHub
co-authored by Arve Knudsen
parent 3d33de1751
commit 10ff4eecef
15 changed files with 228 additions and 147 deletions
-32
View File
@@ -8,30 +8,6 @@ import (
"github.com/grafana/grafana/pkg/util/errutil"
)
// ParseIPAddress parses an IP address and removes port and/or IPV6 format
func ParseIPAddress(input string) (string, error) {
addr, err := SplitHostPort(input)
if err != nil {
return "", errutil.Wrapf(err, "failed to split network address %q by host and port",
input)
}
ip := net.ParseIP(addr.Host)
if ip == nil {
return addr.Host, nil
}
if ip.IsLoopback() {
if strings.Contains(addr.Host, ":") {
// IPv6
return "::1", nil
}
return "127.0.0.1", nil
}
return ip.String(), nil
}
type NetworkAddress struct {
Host string
Port string
@@ -79,11 +55,3 @@ func SplitHostPortDefault(input, defaultHost, defaultPort string) (NetworkAddres
return addr, nil
}
// SplitHostPort splits ip address/hostname string by host and port
func SplitHostPort(input string) (NetworkAddress, error) {
if len(input) == 0 {
return NetworkAddress{}, fmt.Errorf("input is empty")
}
return SplitHostPortDefault(input, "", "")
}