Skip to content

Commit 5384124

Browse files
committed
refactor: centralize CIDR whitespace normalization
1 parent f589a4f commit 5384124

5 files changed

Lines changed: 13 additions & 9 deletions

File tree

internal/helper/helper_darwin.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"net/netip"
99
"os"
1010
"os/exec"
11-
"strings"
1211
"sync"
1312
"syscall"
1413
"time"
@@ -82,7 +81,6 @@ func (c *DarwinConfigurator) SetupRoutes(ctx context.Context, gateways []*pb.Gat
8281
routesAdded := 0
8382
for _, gw := range gateways {
8483
for _, cidr := range gw.GetRoutesIPv4() {
85-
cidr = strings.TrimSpace(cidr)
8684
if IsTunnelRoute(tunnelNet, cidr) {
8785
continue
8886
}
@@ -93,7 +91,6 @@ func (c *DarwinConfigurator) SetupRoutes(ctx context.Context, gateways []*pb.Gat
9391
}
9492

9593
for _, cidr := range gw.GetRoutesIPv6() {
96-
cidr = strings.TrimSpace(cidr)
9794
if IsTunnelRoute(tunnelNet, cidr) {
9895
continue
9996
}

internal/helper/helper_linux.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"fmt"
77
"net"
88
"net/netip"
9-
"strings"
109
"sync"
1110
"syscall"
1211

@@ -75,8 +74,6 @@ func (c *LinuxConfigurator) SetupRoutes(ctx context.Context, gateways []*pb.Gate
7574
routesAdded := 0
7675
for _, gw := range gateways {
7776
for _, cidr := range append(gw.GetRoutesIPv4(), gw.GetRoutesIPv6()...) {
78-
cidr = strings.TrimSpace(cidr)
79-
8077
if IsTunnelRoute(tunnelNet, cidr) {
8178
c.log.WithFields(logrus.Fields{
8279
"cidr": cidr,

internal/helper/helper_windows.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"net"
88
"net/netip"
99
"os"
10-
"strings"
1110
"sync"
1211

1312
"github.com/sirupsen/logrus"
@@ -66,8 +65,6 @@ func (c *WindowsConfigurator) SetupRoutes(ctx context.Context, gateways []*pb.Ga
6665
routesAdded := 0
6766
for _, gw := range gateways {
6867
for _, cidr := range append(gw.GetRoutesIPv4(), gw.GetRoutesIPv6()...) {
69-
cidr = strings.TrimSpace(cidr)
70-
7168
if IsTunnelRoute(c.tunnelNet, cidr) {
7269
continue
7370
}

internal/iputil/iputil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ package iputil
44
import (
55
"fmt"
66
"net/netip"
7+
"strings"
78
)
89

910
// ParsePrefix parses s as a CIDR prefix.
1011
// Bare IP addresses without a prefix length are treated as host addresses (/32 or /128).
1112
func ParsePrefix(s string) (netip.Prefix, error) {
13+
s = strings.TrimSpace(s)
14+
1215
prefix, err := netip.ParsePrefix(s)
1316
if err == nil {
1417
return prefix, nil

internal/iputil/iputil_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ func TestParsePrefix(t *testing.T) {
4040
input: "fd00::1",
4141
want: netip.MustParsePrefix("fd00::1/128"),
4242
},
43+
{
44+
name: "CIDR with surrounding whitespace",
45+
input: " 10.0.0.0/24 ",
46+
want: netip.MustParsePrefix("10.0.0.0/24"),
47+
},
48+
{
49+
name: "bare IP with surrounding whitespace",
50+
input: " 10.43.0.60 ",
51+
want: netip.MustParsePrefix("10.43.0.60/32"),
52+
},
4353
{
4454
name: "garbage input",
4555
input: "not-an-ip",

0 commit comments

Comments
 (0)