chore: fix sing-tun's BuildAndroidRules

This commit is contained in:
wwqgtxx 2022-10-06 22:18:49 +08:00
parent 94246104b8
commit 6c82e98bbc
4 changed files with 84 additions and 7 deletions

View File

@ -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
}

View File

@ -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,
)
}

View File

@ -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
}

View File

@ -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
}