Skip to content

Commit 8b7a212

Browse files
committed
nat: make Port methods more self-contained
- Use strings.Cut directly instead of using SplitProtoPort - Use parsePortNumber instead of the SplitProtoPort wrapper Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent 2433ffa commit 8b7a212

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

nat/nat.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,35 @@ func ParsePortRangeToInt(rawPort string) (startPort, endPort int, _ error) {
6161

6262
// Proto returns the protocol of a Port
6363
func (p Port) Proto() string {
64-
proto, _ := SplitProtoPort(string(p))
64+
_, proto, _ := strings.Cut(string(p), "/")
65+
if proto == "" {
66+
proto = "tcp"
67+
}
6568
return proto
6669
}
6770

6871
// Port returns the port number of a Port
6972
func (p Port) Port() string {
70-
_, port := SplitProtoPort(string(p))
73+
port, _, _ := strings.Cut(string(p), "/")
7174
return port
7275
}
7376

74-
// Int returns the port number of a Port as an int
77+
// Int returns the port number of a Port as an int. It assumes [Port]
78+
// is valid, and returns 0 otherwise.
7579
func (p Port) Int() int {
76-
portStr := p.Port()
7780
// We don't need to check for an error because we're going to
78-
// assume that any error would have been found, and reported, in NewPort()
79-
port, _ := ParsePort(portStr)
81+
// assume that any error would have been found, and reported, in [NewPort]
82+
port, _ := parsePortNumber(p.Port())
8083
return port
8184
}
8285

8386
// Range returns the start/end port numbers of a Port range as ints
8487
func (p Port) Range() (int, int, error) {
85-
return ParsePortRangeToInt(p.Port())
88+
portRange := p.Port()
89+
if portRange == "" {
90+
return 0, 0, nil
91+
}
92+
return parsePortRange(portRange)
8693
}
8794

8895
// SplitProtoPort splits a port(range) and protocol, formatted as "<portnum>/[<proto>]"

0 commit comments

Comments
 (0)