mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-05-14 10:00:34 +00:00
TUN inbound: Add gateway, dns, autoSystemRoutingTable, autoOutboundsInterface for Windows (#5887)
And refactor `mtu` to support setting IPv4/v6 separately Example: https://github.com/XTLS/Xray-core/pull/5887#issue-4198837696
This commit is contained in:
@@ -2,6 +2,7 @@ package internet
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -11,6 +12,8 @@ import (
|
||||
"github.com/xtls/xray-core/features/outbound"
|
||||
)
|
||||
|
||||
var Controllers []func(network, address string, c syscall.RawConn) error
|
||||
var ControllersLock sync.Mutex
|
||||
var effectiveSystemDialer SystemDialer = &DefaultSystemDialer{}
|
||||
|
||||
type SystemDialer interface {
|
||||
@@ -19,9 +22,8 @@ type SystemDialer interface {
|
||||
}
|
||||
|
||||
type DefaultSystemDialer struct {
|
||||
controllers []func(network, address string, c syscall.RawConn) error
|
||||
dns dns.Client
|
||||
obm outbound.Manager
|
||||
dns dns.Client
|
||||
obm outbound.Manager
|
||||
}
|
||||
|
||||
func resolveSrcAddr(network net.Network, src net.Address) net.Addr {
|
||||
@@ -63,7 +65,7 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
|
||||
return nil, err
|
||||
}
|
||||
lc.Control = func(network, address string, c syscall.RawConn) error {
|
||||
for _, ctl := range d.controllers {
|
||||
for _, ctl := range Controllers {
|
||||
if err := ctl(network, address, c); err != nil {
|
||||
errors.LogInfoInner(ctx, err, "failed to apply external controller")
|
||||
}
|
||||
@@ -115,12 +117,12 @@ func (d *DefaultSystemDialer) Dial(ctx context.Context, src net.Address, dest ne
|
||||
KeepAliveConfig: keepAliveConfig,
|
||||
}
|
||||
|
||||
if sockopt != nil || len(d.controllers) > 0 {
|
||||
if sockopt != nil || len(Controllers) > 0 {
|
||||
if sockopt != nil && sockopt.TcpMptcp {
|
||||
dialer.SetMultipathTCP(true)
|
||||
}
|
||||
dialer.Control = func(network, address string, c syscall.RawConn) error {
|
||||
for _, ctl := range d.controllers {
|
||||
for _, ctl := range Controllers {
|
||||
if err := ctl(network, address, c); err != nil {
|
||||
errors.LogInfoInner(ctx, err, "failed to apply external controller")
|
||||
}
|
||||
@@ -208,12 +210,15 @@ func RegisterDialerController(ctl func(network, address string, c syscall.RawCon
|
||||
return errors.New("nil listener controller")
|
||||
}
|
||||
|
||||
dialer, ok := effectiveSystemDialer.(*DefaultSystemDialer)
|
||||
ControllersLock.Lock()
|
||||
Controllers = append(Controllers, ctl)
|
||||
ControllersLock.Unlock()
|
||||
|
||||
_, ok := effectiveSystemDialer.(*DefaultSystemDialer)
|
||||
if !ok {
|
||||
return errors.New("RegisterListenerController not supported in custom dialer")
|
||||
}
|
||||
|
||||
dialer.controllers = append(dialer.controllers, ctl)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user