Skip to content

Commit

Permalink
pkg/portutil: use net.SplitHostPort to parse ip address with port
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda authored and THLIVSQAZ committed Jul 14, 2024
1 parent d9d133c commit 99b05e6
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions pkg/portutil/portutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,19 @@ import (

// return respectively ip, hostPort, containerPort
func splitParts(rawport string) (string, string, string) {
parts := strings.Split(rawport, ":")
n := len(parts)
containerport := parts[n-1]
lastIndex := strings.LastIndex(rawport, ":")
containerPort := rawport[lastIndex+1:]
if lastIndex == -1 {
return "", "", containerPort
}

switch n {
case 1:
return "", "", containerport
case 2:
return "", parts[0], containerport
case 3:
return parts[0], parts[1], containerport
default:
return strings.Join(parts[:n-2], ":"), parts[n-2], containerport
hostAddrPort := rawport[:lastIndex]
addr, port, err := net.SplitHostPort(hostAddrPort)
if err != nil {
return "", hostAddrPort, containerPort
}

return addr, port, containerPort
}

// ParseFlagP parse port mapping pair, like "127.0.0.1:3000:8080/tcp",
Expand Down

0 comments on commit 99b05e6

Please sign in to comment.