Skip to content

Commit dfb9976

Browse files
committed
fix: assign server first IP from CIDR range instead of entire subnet
- Fixed WireGuard server interface to use 10.100.0.1/32 instead of 10.100.0.0/24 - Server now gets first usable IP with /32 mask, matching pyjam.as implementation - Resolves routing conflicts where server claimed entire subnet vs client IPs - This should fix connectivity issues between server and client tunnels
1 parent 4a13189 commit dfb9976

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

internal/tunnel/tunnel.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8+
"net"
89

910
"github.com/vishvananda/netlink"
1011
"github.com/zerodha/logf"
@@ -58,15 +59,27 @@ func New(opts PeerOpts) (*Tunnel, error) {
5859
return nil, fmt.Errorf("error while getting link: %w", err)
5960
}
6061

61-
// Parse the CIDR.
62-
addr, err := netlink.ParseAddr(opts.CIDR)
62+
// Parse the CIDR to get the network range.
63+
_, cidrNet, err := net.ParseCIDR(opts.CIDR)
6364
if err != nil {
64-
return nil, fmt.Errorf("error parsing ip address: %w", err)
65+
return nil, fmt.Errorf("error parsing CIDR: %w", err)
6566
}
6667

67-
// Add the address to the interface.
68+
// Calculate the first usable IP in the range (network + 1).
69+
serverIP := make(net.IP, len(cidrNet.IP))
70+
copy(serverIP, cidrNet.IP)
71+
serverIP[len(serverIP)-1] += 1 // Increment last octet to get .1
72+
73+
// Create server address with /32 (single host).
74+
serverAddr := fmt.Sprintf("%s/32", serverIP.String())
75+
addr, err := netlink.ParseAddr(serverAddr)
76+
if err != nil {
77+
return nil, fmt.Errorf("error parsing server address: %w", err)
78+
}
79+
80+
// Add the server address to the interface.
6881
if err = netlink.AddrAdd(link, addr); err != nil {
69-
return nil, fmt.Errorf("error assigning ip address: %w", err)
82+
return nil, fmt.Errorf("error assigning server address: %w", err)
7083
}
7184

7285
// Start the interface.

0 commit comments

Comments
 (0)