[Fixed] auto-route support use ip route

This commit is contained in:
Skyxim 2022-01-22 13:24:31 +08:00
parent e5c99cbee7
commit 03b956b7a3
2 changed files with 21 additions and 2 deletions

View file

@ -208,7 +208,7 @@ func UnmarshalRawConfig(buf []byte) (*RawConfig, error) {
Tun: Tun{
Enable: false,
Stack: "gvisor",
DnsHijack: []string{},
DnsHijack: []string{"198.18.0.2:53"},
AutoRoute: false,
},
DNS: RawDNS{

View file

@ -63,8 +63,9 @@ func OpenTunDevice(tunAddress string, autoRoute bool) (TunDevice, error) {
}
if autoRoute {
log.Warnln("linux unsupported automatic route")
addRoute(tunAddress)
}
return dev, nil
case "fd":
fd, err := strconv.ParseInt(deviceURL.Host, 10, 32)
@ -328,3 +329,21 @@ func GetAutoDetectInterface() (string, error) {
}
return out.String(), nil
}
func addRoute(gateway string) {
cmd := exec.Command("route", "add", "default", "gw", gateway)
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())
}
}
func delRoute(gateway string) {
cmd := exec.Command("ip", "route", "delete", "gw")
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
log.Errorln("[auto route] Failed to delete system route: %s: %s , cmd: %s", err.Error(), stderr.String(), cmd.String())
}
}