Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0987281

Browse files
chore: add a tailscale router that uses the CoderVPN protocol (#15391)
Closes #14732.
1 parent 6e18742 commit 0987281

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

tailnet/conn.go

+3
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ type Options struct {
110110
// DNSConfigurator is optional, and is passed to the underlying wireguard
111111
// engine.
112112
DNSConfigurator dns.OSConfigurator
113+
// Router is optional, and is passed to the underlying wireguard engine.
114+
Router router.Router
113115
}
114116

115117
// TelemetrySink allows tailnet.Conn to send network telemetry to the Coder
@@ -183,6 +185,7 @@ func NewConn(options *Options) (conn *Conn, err error) {
183185
ListenPort: options.ListenPort,
184186
SetSubsystem: sys.Set,
185187
DNS: options.DNSConfigurator,
188+
Router: options.Router,
186189
})
187190
if err != nil {
188191
return nil, xerrors.Errorf("create wgengine: %w", err)

vpn/router.go

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package vpn
2+
3+
import (
4+
"net"
5+
"net/netip"
6+
7+
"tailscale.com/wgengine/router"
8+
)
9+
10+
func NewRouter(t *Tunnel) router.Router {
11+
return &vpnRouter{tunnel: t}
12+
}
13+
14+
type vpnRouter struct {
15+
tunnel *Tunnel
16+
}
17+
18+
func (*vpnRouter) Up() error {
19+
// On macOS, the Desktop app will handle turning the VPN on and off.
20+
// On Windows, this is a no-op.
21+
return nil
22+
}
23+
24+
func (v *vpnRouter) Set(cfg *router.Config) error {
25+
req := convertRouterConfig(cfg)
26+
return v.tunnel.ApplyNetworkSettings(v.tunnel.ctx, req)
27+
}
28+
29+
func (*vpnRouter) Close() error {
30+
// There's no cleanup that we need to initiate from within the dylib.
31+
return nil
32+
}
33+
34+
func convertRouterConfig(cfg *router.Config) *NetworkSettingsRequest {
35+
v4LocalAddrs := make([]string, 0)
36+
v6LocalAddrs := make([]string, 0)
37+
for _, addrs := range cfg.LocalAddrs {
38+
if addrs.Addr().Is4() {
39+
v4LocalAddrs = append(v4LocalAddrs, addrs.String())
40+
} else if addrs.Addr().Is6() {
41+
v6LocalAddrs = append(v6LocalAddrs, addrs.String())
42+
} else {
43+
continue
44+
}
45+
}
46+
v4Routes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
47+
v6Routes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
48+
for _, route := range cfg.Routes {
49+
if route.Addr().Is4() {
50+
v4Routes = append(v4Routes, convertToIPV4Route(route))
51+
} else if route.Addr().Is6() {
52+
v6Routes = append(v6Routes, convertToIPV6Route(route))
53+
} else {
54+
continue
55+
}
56+
}
57+
v4ExcludedRoutes := make([]*NetworkSettingsRequest_IPv4Settings_IPv4Route, 0)
58+
v6ExcludedRoutes := make([]*NetworkSettingsRequest_IPv6Settings_IPv6Route, 0)
59+
for _, route := range cfg.LocalRoutes {
60+
if route.Addr().Is4() {
61+
v4ExcludedRoutes = append(v4ExcludedRoutes, convertToIPV4Route(route))
62+
} else if route.Addr().Is6() {
63+
v6ExcludedRoutes = append(v6ExcludedRoutes, convertToIPV6Route(route))
64+
} else {
65+
continue
66+
}
67+
}
68+
69+
return &NetworkSettingsRequest{
70+
Mtu: uint32(cfg.NewMTU),
71+
Ipv4Settings: &NetworkSettingsRequest_IPv4Settings{
72+
Addrs: v4LocalAddrs,
73+
IncludedRoutes: v4Routes,
74+
ExcludedRoutes: v4ExcludedRoutes,
75+
},
76+
Ipv6Settings: &NetworkSettingsRequest_IPv6Settings{
77+
Addrs: v6LocalAddrs,
78+
IncludedRoutes: v6Routes,
79+
ExcludedRoutes: v6ExcludedRoutes,
80+
},
81+
TunnelOverheadBytes: 0, // N/A
82+
TunnelRemoteAddress: "", // N/A
83+
}
84+
}
85+
86+
func convertToIPV4Route(route netip.Prefix) *NetworkSettingsRequest_IPv4Settings_IPv4Route {
87+
return &NetworkSettingsRequest_IPv4Settings_IPv4Route{
88+
Destination: route.Addr().String(),
89+
Mask: prefixToSubnetMask(route),
90+
Router: "", // N/A
91+
}
92+
}
93+
94+
func convertToIPV6Route(route netip.Prefix) *NetworkSettingsRequest_IPv6Settings_IPv6Route {
95+
return &NetworkSettingsRequest_IPv6Settings_IPv6Route{
96+
Destination: route.Addr().String(),
97+
PrefixLength: uint32(route.Bits()),
98+
Router: "", // N/A
99+
}
100+
}
101+
102+
func prefixToSubnetMask(prefix netip.Prefix) string {
103+
maskBytes := net.CIDRMask(prefix.Masked().Bits(), net.IPv4len*8)
104+
return net.IP(maskBytes).String()
105+
}

vpn/router_internal_test.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package vpn
2+
3+
import (
4+
"net/netip"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
"tailscale.com/wgengine/router"
9+
)
10+
11+
func TestConvertRouterConfig(t *testing.T) {
12+
t.Parallel()
13+
14+
tests := []struct {
15+
name string
16+
cfg *router.Config
17+
expected *NetworkSettingsRequest
18+
}{
19+
{
20+
name: "IPv4 and IPv6 configuration",
21+
cfg: &router.Config{
22+
LocalAddrs: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32"), netip.MustParsePrefix("fd7a:115c:a1e0::1/128")},
23+
Routes: []netip.Prefix{netip.MustParsePrefix("192.168.0.0/24"), netip.MustParsePrefix("fd00::/64")},
24+
LocalRoutes: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8"), netip.MustParsePrefix("2001:db8::/32")},
25+
NewMTU: 1500,
26+
},
27+
expected: &NetworkSettingsRequest{
28+
Mtu: 1500,
29+
Ipv4Settings: &NetworkSettingsRequest_IPv4Settings{
30+
Addrs: []string{"100.64.0.1/32"},
31+
IncludedRoutes: []*NetworkSettingsRequest_IPv4Settings_IPv4Route{
32+
{Destination: "192.168.0.0", Mask: "255.255.255.0", Router: ""},
33+
},
34+
ExcludedRoutes: []*NetworkSettingsRequest_IPv4Settings_IPv4Route{
35+
{Destination: "10.0.0.0", Mask: "255.0.0.0", Router: ""},
36+
},
37+
},
38+
Ipv6Settings: &NetworkSettingsRequest_IPv6Settings{
39+
Addrs: []string{"fd7a:115c:a1e0::1/128"},
40+
IncludedRoutes: []*NetworkSettingsRequest_IPv6Settings_IPv6Route{
41+
{Destination: "fd00::", PrefixLength: 64, Router: ""},
42+
},
43+
ExcludedRoutes: []*NetworkSettingsRequest_IPv6Settings_IPv6Route{
44+
{Destination: "2001:db8::", PrefixLength: 32, Router: ""},
45+
},
46+
},
47+
},
48+
},
49+
{
50+
name: "Empty",
51+
cfg: &router.Config{},
52+
expected: &NetworkSettingsRequest{
53+
Ipv4Settings: &NetworkSettingsRequest_IPv4Settings{
54+
Addrs: []string{},
55+
IncludedRoutes: []*NetworkSettingsRequest_IPv4Settings_IPv4Route{},
56+
ExcludedRoutes: []*NetworkSettingsRequest_IPv4Settings_IPv4Route{},
57+
},
58+
Ipv6Settings: &NetworkSettingsRequest_IPv6Settings{
59+
Addrs: []string{},
60+
IncludedRoutes: []*NetworkSettingsRequest_IPv6Settings_IPv6Route{},
61+
ExcludedRoutes: []*NetworkSettingsRequest_IPv6Settings_IPv6Route{},
62+
},
63+
},
64+
},
65+
}
66+
//nolint:paralleltest // outdated rule
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
t.Parallel()
70+
result := convertRouterConfig(tt.cfg)
71+
require.Equal(t, tt.expected, result)
72+
})
73+
}
74+
}

0 commit comments

Comments
 (0)