From 03b956b7a31311bece31d02098ed51b77e3e6dee Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sat, 22 Jan 2022 13:24:31 +0800 Subject: [PATCH] [Fixed] auto-route support use ip route --- config/config.go | 2 +- listener/tun/dev/dev_linux.go | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 1a2d218f..d4437865 100644 --- a/config/config.go +++ b/config/config.go @@ -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{ diff --git a/listener/tun/dev/dev_linux.go b/listener/tun/dev/dev_linux.go index aeb90d4e..d43433ad 100644 --- a/listener/tun/dev/dev_linux.go +++ b/listener/tun/dev/dev_linux.go @@ -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()) + } +}