fix: try to support android hotspot when using tun

This commit is contained in:
wwqgtxx 2022-11-03 12:58:21 +08:00
parent 508e257543
commit 3373b62b02
3 changed files with 31 additions and 1 deletions

View file

@ -203,11 +203,14 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
if err != nil {
return
}
err = l.tunStack.Start()
err = l.tunStack.Start()
if err != nil {
return
}
l.openAndroidHotspot(tunOptions)
log.Infoln("[TUN] Tun adapter listening at: %s(%s,%s), mtu: %d, auto route: %v, ip stack: %s",
tunName, tunOptions.Inet4Address, tunOptions.Inet6Address, tunMTU, options.AutoRoute, options.Stack)
return

View file

@ -1,7 +1,11 @@
package sing_tun
import (
"github.com/Dreamacro/clash/log"
"github.com/sagernet/netlink"
tun "github.com/sagernet/sing-tun"
"golang.org/x/sys/unix"
"runtime"
)
func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error {
@ -21,3 +25,25 @@ func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error {
func (h *ListenerHandler) OnPackagesUpdated(packages int, sharedUsers int) {
return
}
func (l *Listener) openAndroidHotspot(tunOptions tun.Options) {
if runtime.GOOS == "android" && tunOptions.AutoRoute {
priority := 9000
if len(tunOptions.ExcludedRanges()) > 0 {
priority++
}
if tunOptions.InterfaceMonitor.AndroidVPNEnabled() {
priority++
}
it := netlink.NewRule()
it.Priority = priority
it.IifName = tunOptions.Name
it.Table = 254 //main
it.Family = unix.AF_INET
it.SuppressPrefixlen = 0
err := netlink.RuleAdd(it)
if err != nil {
log.Warnln("[TUN] add AndroidHotspot rule error")
}
}
}

View file

@ -9,3 +9,4 @@ import (
func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error {
return nil
}
func (l *Listener) openAndroidHotspot(tunOptions tun.Options) {}