Browse Source

fix ip address parsing of loopback address

Marcus Efraimsson 7 years ago
parent
commit
0495499b4f
2 changed files with 6 additions and 2 deletions
  1. 4 2
      pkg/util/ip_address.go
  2. 2 0
      pkg/util/ip_address_test.go

+ 4 - 2
pkg/util/ip_address.go

@@ -7,11 +7,13 @@ import (
 
 // ParseIPAddress parses an IP address and removes port and/or IPV6 format
 func ParseIPAddress(input string) string {
-	var s string
+	s := input
 	lastIndex := strings.LastIndex(input, ":")
 
 	if lastIndex != -1 {
-		s = input[:lastIndex]
+		if lastIndex > 0 && input[lastIndex-1:lastIndex] != ":" {
+			s = input[:lastIndex]
+		}
 	}
 
 	s = strings.Replace(s, "[", "", -1)

+ 2 - 0
pkg/util/ip_address_test.go

@@ -10,5 +10,7 @@ func TestParseIPAddress(t *testing.T) {
 	Convey("Test parse ip address", t, func() {
 		So(ParseIPAddress("192.168.0.140:456"), ShouldEqual, "192.168.0.140")
 		So(ParseIPAddress("[::1:456]"), ShouldEqual, "127.0.0.1")
+		So(ParseIPAddress("[::1]"), ShouldEqual, "127.0.0.1")
+		So(ParseIPAddress("192.168.0.140"), ShouldEqual, "192.168.0.140")
 	})
 }