Skip to content

Commit

Permalink
try to support android hotspot when using tun
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Nov 3, 2022
1 parent be8da8a commit dc44598
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions listener/sing_tun/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,14 @@ func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
if err != nil {
return
}

err = l.tunStack.Start()
if err != nil {
return
}

l.openAndroidHotspot(tunOptions)

log.Infoln("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
Expand Down
26 changes: 26 additions & 0 deletions listener/sing_tun/server_android.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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")
}
}
}
1 change: 1 addition & 0 deletions listener/sing_tun/server_notandroid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ import (
func (l *Listener) buildAndroidRules(tunOptions *tun.Options) error {
return nil
}
func (l *Listener) openAndroidHotspot(tunOptions tun.Options) {}

0 comments on commit dc44598

Please sign in to comment.