浏览代码

fix ip address parsing of loopback address

Marcus Efraimsson 7 年之前
父节点
当前提交
0495499b4f
共有 2 个文件被更改,包括 6 次插入2 次删除
  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")
 	})
 }