fix: hotspot for android

This commit is contained in:
adlyq 2022-04-22 22:25:45 +08:00
parent de4341c8cd
commit 2e1d9a4f2e
2 changed files with 24 additions and 8 deletions

View file

@ -398,7 +398,10 @@ func Cleanup() {
if tunStackListener != nil {
_ = tunStackListener.Close()
if runtime.GOOS == "android" {
_, _ = cmd.ExecCmd("ip rule del pref 5000")
prefs := []int{9000, 9001, 9002, 9003}
for _, pref := range prefs {
_, _ = cmd.ExecCmd(fmt.Sprintf("ip rule del pref %d", pref))
}
}
}
}

View file

@ -7,6 +7,7 @@ import (
"github.com/Dreamacro/clash/log"
"net/netip"
"runtime"
"strconv"
)
func GetAutoDetectInterface() (string, error) {
@ -39,21 +40,33 @@ func ConfigInterfaceAddress(dev device.Device, addr netip.Prefix, forceMTU int,
}
func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
linkIP := addr.Masked().Addr().Next()
if runtime.GOOS == "android" {
tableId := 1981801
const tableId = 1981801
for _, route := range defaultRoutes {
if _, err := cmd.ExecCmd(fmt.Sprintf("ip route add %s dev %s scope link table %d", route, interfaceName, tableId)); err != nil {
if err := execRouterCmd("add", route, interfaceName, linkIP.String(), strconv.Itoa(tableId)); err != nil {
return err
}
}
_, err := cmd.ExecCmd(fmt.Sprintf("ip rule add lookup %d suppress_prefixlength 0 pref 5000", tableId))
_, err := cmd.ExecCmd(fmt.Sprintf("ip rule add from 0.0.0.0 iif lo uidrange 0-4294967294 lookup %d pref 9000", tableId))
if err != nil {
log.Warnln("%s", err)
}
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add from %s iif lo uidrange 0-4294967294 lookup %d pref 9001", linkIP, tableId))
if err != nil {
log.Warnln("%s", err)
}
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add from all iif %s lookup main suppress_prefixlength 0 pref 9002", interfaceName))
if err != nil {
log.Warnln("%s", err)
}
_, err = cmd.ExecCmd(fmt.Sprintf("ip rule add not from all iif lo lookup %d pref 9003", tableId))
if err != nil {
log.Warnln("%s", err)
}
} else {
linkIP := addr.Masked().Addr().Next()
for _, route := range defaultRoutes {
if err := execRouterCmd("add", route, interfaceName, linkIP.String()); err != nil {
if err := execRouterCmd("add", route, interfaceName, linkIP.String(), "main"); err != nil {
return err
}
}
@ -64,8 +77,8 @@ func configInterfaceRouting(interfaceName string, addr netip.Prefix) error {
return nil
}
func execRouterCmd(action, route string, interfaceName string, linkIP string) error {
cmdStr := fmt.Sprintf("ip route %s %s dev %s proto kernel scope link src %s", action, route, interfaceName, linkIP)
func execRouterCmd(action, route, interfaceName, linkIP, table string) error {
cmdStr := fmt.Sprintf("ip route %s %s dev %s proto kernel scope link src %s table %s", action, route, interfaceName, linkIP, table)
_, err := cmd.ExecCmd(cmdStr)
return err