tailscale: Use system dialer for system interface
* Revert "Fix netstack TCP connections with system interface
This commit is contained in:
@@ -110,6 +110,7 @@ type Endpoint struct {
|
||||
systemInterfaceName string
|
||||
systemInterfaceMTU uint32
|
||||
systemTun tun.Tun
|
||||
systemDialer *dialer.DefaultDialer
|
||||
fallbackTCPCloser func()
|
||||
}
|
||||
|
||||
@@ -324,8 +325,19 @@ func (t *Endpoint) Start(stage adapter.StartStage) error {
|
||||
_ = systemTun.Close()
|
||||
return err
|
||||
}
|
||||
systemDialer, err := dialer.NewDefault(t.ctx, option.DialerOptions{
|
||||
BindInterface: tunName,
|
||||
})
|
||||
if err != nil {
|
||||
_ = systemTun.Close()
|
||||
return err
|
||||
}
|
||||
t.systemTun = systemTun
|
||||
t.systemDialer = systemDialer
|
||||
t.server.TunDevice = wgTunDevice
|
||||
t.server.RouterWrapper = func(inner router.Router) router.Router {
|
||||
return &exitRouteFilteringRouter{Router: inner}
|
||||
}
|
||||
}
|
||||
err := t.server.Start()
|
||||
if err != nil {
|
||||
@@ -459,6 +471,10 @@ func (t *Endpoint) Close() error {
|
||||
t.fallbackTCPCloser()
|
||||
t.fallbackTCPCloser = nil
|
||||
}
|
||||
if t.systemTun != nil {
|
||||
_ = t.systemTun.Close()
|
||||
t.systemTun = nil
|
||||
}
|
||||
return common.Close(common.PtrOrNil(t.server))
|
||||
}
|
||||
|
||||
@@ -476,6 +492,9 @@ func (t *Endpoint) DialContext(ctx context.Context, network string, destination
|
||||
}
|
||||
return N.DialSerial(ctx, t, network, destination, destinationAddresses)
|
||||
}
|
||||
if t.systemDialer != nil {
|
||||
return t.systemDialer.DialContext(ctx, network, destination)
|
||||
}
|
||||
addr4, addr6 := t.server.TailscaleIPs()
|
||||
remoteAddr := tcpip.FullAddress{
|
||||
NIC: 1,
|
||||
@@ -522,6 +541,9 @@ func (t *Endpoint) DialContext(ctx context.Context, network string, destination
|
||||
}
|
||||
|
||||
func (t *Endpoint) listenPacketWithAddress(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
|
||||
if t.systemDialer != nil {
|
||||
return t.systemDialer.ListenPacket(ctx, destination)
|
||||
}
|
||||
addr4, addr6 := t.server.TailscaleIPs()
|
||||
bind := tcpip.FullAddress{
|
||||
NIC: 1,
|
||||
@@ -679,19 +701,29 @@ func (t *Endpoint) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
||||
}
|
||||
|
||||
func (t *Endpoint) NewDirectRouteConnection(metadata adapter.InboundContext, routeContext tun.DirectRouteContext, timeout time.Duration) (tun.DirectRouteDestination, error) {
|
||||
inet4Address, inet6Address := t.server.TailscaleIPs()
|
||||
if metadata.Destination.Addr.Is4() && !inet4Address.IsValid() || metadata.Destination.Addr.Is6() && !inet6Address.IsValid() {
|
||||
return nil, E.New("Tailscale is not ready yet")
|
||||
}
|
||||
ctx := log.ContextWithNewID(t.ctx)
|
||||
destination, err := ping.ConnectGVisor(
|
||||
ctx, t.logger,
|
||||
metadata.Source.Addr, metadata.Destination.Addr,
|
||||
routeContext,
|
||||
t.stack,
|
||||
inet4Address, inet6Address,
|
||||
timeout,
|
||||
)
|
||||
var destination tun.DirectRouteDestination
|
||||
var err error
|
||||
if t.systemDialer != nil {
|
||||
destination, err = ping.ConnectDestination(
|
||||
ctx, t.logger,
|
||||
t.systemDialer.DialerForICMPDestination(metadata.Destination.Addr).Control,
|
||||
metadata.Destination.Addr, routeContext, timeout,
|
||||
)
|
||||
} else {
|
||||
inet4Address, inet6Address := t.server.TailscaleIPs()
|
||||
if metadata.Destination.Addr.Is4() && !inet4Address.IsValid() || metadata.Destination.Addr.Is6() && !inet6Address.IsValid() {
|
||||
return nil, E.New("Tailscale is not ready yet")
|
||||
}
|
||||
destination, err = ping.ConnectGVisor(
|
||||
ctx, t.logger,
|
||||
metadata.Source.Addr, metadata.Destination.Addr,
|
||||
routeContext,
|
||||
t.stack,
|
||||
inet4Address, inet6Address,
|
||||
timeout,
|
||||
)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -808,3 +840,17 @@ func (c *dnsConfigurtor) GetBaseConfig() (tsDNS.OSConfig, error) {
|
||||
func (c *dnsConfigurtor) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type exitRouteFilteringRouter struct {
|
||||
router.Router
|
||||
}
|
||||
|
||||
func (r *exitRouteFilteringRouter) Set(config *router.Config) error {
|
||||
if config != nil {
|
||||
config = config.Clone()
|
||||
config.Routes = common.Filter(config.Routes, func(prefix netip.Prefix) bool {
|
||||
return !tsaddr.IsExitRoute(prefix)
|
||||
})
|
||||
}
|
||||
return r.Router.Set(config)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user