From 110e0014d17156f18281abd04a3ed6ff3391a6c6 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Thu, 6 Oct 2022 22:18:49 +0800 Subject: [PATCH] chore: fix sing-tun's BuildAndroidRules --- config/config.go | 31 +++++++++++++++++++++++++++-- listener/sing_tun/server.go | 26 +++++++++++++++++++----- listener/sing_tun/server_android.go | 23 +++++++++++++++++++++ listener/sing_tun/server_other.go | 11 ++++++++++ 4 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 listener/sing_tun/server_android.go create mode 100644 listener/sing_tun/server_other.go diff --git a/config/config.go b/config/config.go index ae24d9ac..1969bc27 100644 --- a/config/config.go +++ b/config/config.go @@ -257,6 +257,20 @@ type RawTun struct { AutoRoute bool `yaml:"auto-route" json:"auto-route"` AutoDetectInterface bool `yaml:"auto-detect-interface"` RedirectToTun []string `yaml:"-" json:"-"` + + MTU uint32 `yaml:"mtu" json:"mtu,omitempty"` + //Inet4Address []ListenPrefix `yaml:"inet4-address" json:"inet4_address,omitempty"` + Inet6Address []ListenPrefix `yaml:"inet6-address" json:"inet6_address,omitempty"` + StrictRoute bool `yaml:"strict-route" json:"strict_route,omitempty"` + IncludeUID []uint32 `yaml:"include-uid" json:"include_uid,omitempty"` + IncludeUIDRange []string `yaml:"include-uid-range" json:"include_uid_range,omitempty"` + ExcludeUID []uint32 `yaml:"exclude-uid" json:"exclude_uid,omitempty"` + ExcludeUIDRange []string `yaml:"exclude-uid-range" json:"exclude_uid_range,omitempty"` + IncludeAndroidUser []int `yaml:"include-android-user" json:"include_android_user,omitempty"` + IncludePackage []string `yaml:"include-package" json:"include_package,omitempty"` + ExcludePackage []string `yaml:"exclude-package" json:"exclude_package,omitempty"` + EndpointIndependentNat bool `yaml:"endpoint-independent-nat" json:"endpoint_independent_nat,omitempty"` + UDPTimeout int64 `yaml:"udp-timeout" json:"udp_timeout,omitempty"` } type RawConfig struct { @@ -361,6 +375,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) { DNSHijack: []string{"0.0.0.0:53"}, // default hijack all dns query AutoRoute: false, AutoDetectInterface: false, + Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, }, EBpf: EBpf{ RedirectToTun: []string{}, @@ -1132,8 +1147,20 @@ func parseTun(rawTun RawTun, general *General, dnsCfg *DNS) (*Tun, error) { AutoRoute: rawTun.AutoRoute, AutoDetectInterface: rawTun.AutoDetectInterface, RedirectToTun: rawTun.RedirectToTun, - Inet4Address: []ListenPrefix{ListenPrefix(tunAddressPrefix)}, - Inet6Address: []ListenPrefix{ListenPrefix(netip.MustParsePrefix("fdfe:dcba:9876::1/126"))}, + + MTU: rawTun.MTU, + Inet4Address: []ListenPrefix{ListenPrefix(tunAddressPrefix)}, + Inet6Address: rawTun.Inet6Address, + StrictRoute: rawTun.StrictRoute, + IncludeUID: rawTun.IncludeUID, + IncludeUIDRange: rawTun.IncludeUIDRange, + ExcludeUID: rawTun.ExcludeUID, + ExcludeUIDRange: rawTun.ExcludeUIDRange, + IncludeAndroidUser: rawTun.IncludeAndroidUser, + IncludePackage: rawTun.IncludePackage, + ExcludePackage: rawTun.ExcludePackage, + EndpointIndependentNat: rawTun.EndpointIndependentNat, + UDPTimeout: rawTun.UDPTimeout, }, nil } diff --git a/listener/sing_tun/server.go b/listener/sing_tun/server.go index 82ad6138..3730a86a 100644 --- a/listener/sing_tun/server.go +++ b/listener/sing_tun/server.go @@ -3,6 +3,7 @@ package sing_tun import ( "context" "net/netip" + "runtime" "strconv" "strings" @@ -25,19 +26,31 @@ var InterfaceName = "Meta" type Listener struct { closed bool options config.Tun - handler tun.Handler + handler *ListenerHandler tunIf tun.Tun tunStack tun.Stack networkUpdateMonitor tun.NetworkUpdateMonitor defaultInterfaceMonitor tun.DefaultInterfaceMonitor + packageManager tun.PackageManager +} + +func CalculateInterfaceName(name string) (tunName string) { + if runtime.GOOS == "darwin" { + tunName = "utun" + } else if name != "" { + tunName = name + } else { + tunName = "tun" + } + return } func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (l *Listener, err error) { tunName := options.Device if tunName == "" { - tunName = tun.CalculateInterfaceName(InterfaceName) + tunName = CalculateInterfaceName(InterfaceName) } tunMTU := options.MTU if tunMTU == 0 { @@ -157,9 +170,11 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P TableIndex: 2022, } - //if C.IsAndroid { - // t.tunOptions.BuildAndroidRules(t.router.PackageManager(), t) - //} + err = l.buildAndroidRules(&tunOptions) + if err != nil { + err = E.Cause(err, "build android rules") + return + } tunIf, err := tun.Open(tunOptions) if err != nil { err = E.Cause(err, "configure tun interface") @@ -229,6 +244,7 @@ func (l *Listener) Close() { l.tunIf, l.defaultInterfaceMonitor, l.networkUpdateMonitor, + l.packageManager, ) } diff --git a/listener/sing_tun/server_android.go b/listener/sing_tun/server_android.go new file mode 100644 index 00000000..e9d36f66 --- /dev/null +++ b/listener/sing_tun/server_android.go @@ -0,0 +1,23 @@ +package sing_tun + +import ( + tun "github.com/sagernet/sing-tun" +) + +func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error { + packageManager, err := tun.NewPackageManager(l.handler) + if err != nil { + return err + } + err = packageManager.Start() + if err != nil { + return err + } + l.packageManager = packageManager + tunOptions.BuildAndroidRules(packageManager, l.handler) + return nil +} + +func (h *ListenerHandler) OnPackagesUpdated(packages int, sharedUsers int) { + return +} diff --git a/listener/sing_tun/server_other.go b/listener/sing_tun/server_other.go new file mode 100644 index 00000000..f621c71e --- /dev/null +++ b/listener/sing_tun/server_other.go @@ -0,0 +1,11 @@ +//go:build !android + +package sing_tun + +import ( + tun "github.com/sagernet/sing-tun" +) + +func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error { + return nil +}