[Style] Adjust the routing table of tun on mac
This commit is contained in:
parent
d96180dc60
commit
27f0f65bfb
1 changed files with 37 additions and 11 deletions
|
@ -5,11 +5,13 @@ package dev
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/Dreamacro/clash/log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
@ -176,7 +178,7 @@ func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if autoRoute {
|
if autoRoute {
|
||||||
SetLinuxAutoRoute()
|
setAutoRoute(tunAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
return tun, nil
|
return tun, nil
|
||||||
|
@ -480,15 +482,39 @@ func (t *tunDarwin) attachLinkLocal() error {
|
||||||
|
|
||||||
// GetAutoDetectInterface get ethernet interface
|
// GetAutoDetectInterface get ethernet interface
|
||||||
func GetAutoDetectInterface() (string, error) {
|
func GetAutoDetectInterface() (string, error) {
|
||||||
cmd := exec.Command("bash", "-c", "netstat -rnf inet | grep 'default' | awk -F ' ' 'NR==1{print $6}' | xargs echo -n")
|
cmd := exec.Command("route", "-n", "get", "default")
|
||||||
var out bytes.Buffer
|
if result, err := cmd.Output(); err != nil {
|
||||||
cmd.Stdout = &out
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
return "", err
|
||||||
|
} else {
|
||||||
|
resultString := string(result)
|
||||||
|
reg, err := regexp.Compile("(interface:)(.*)")
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
matchResult := reg.FindStringSubmatch(resultString)
|
||||||
|
interfaceName := strings.TrimSpace(matchResult[len(matchResult)-1])
|
||||||
|
return interfaceName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func setAutoRoute(tunGateway string) {
|
||||||
|
addRoute("1", tunGateway)
|
||||||
|
addRoute("2/7", tunGateway)
|
||||||
|
addRoute("4/6", tunGateway)
|
||||||
|
addRoute("8/5", tunGateway)
|
||||||
|
addRoute("16/4", tunGateway)
|
||||||
|
addRoute("32/3", tunGateway)
|
||||||
|
addRoute("64/2", tunGateway)
|
||||||
|
addRoute("128.0/1", tunGateway)
|
||||||
|
addRoute("198.18.0/16", tunGateway)
|
||||||
|
}
|
||||||
|
|
||||||
|
func addRoute(net, name string) {
|
||||||
|
cmd := exec.Command("route", "add", "-net", net, name)
|
||||||
|
var stderr bytes.Buffer
|
||||||
|
cmd.Stderr = &stderr
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Errorln("[auto route] Failed to add system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String())
|
||||||
}
|
}
|
||||||
if out.Len() == 0 {
|
|
||||||
return "", errors.New("interface not found by default route")
|
|
||||||
}
|
|
||||||
return out.String(), nil
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue