From b74b8bdc6f105e2faabe7acd1dcd81cbfc8a204f Mon Sep 17 00:00:00 2001 From: e1732a364fed <75717694+e1732a364fed@users.noreply.github.com> Date: Sat, 1 Jan 2000 00:00:00 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E8=AE=A2=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- netLayer/tproxy/route_linux.go | 34 ++-------------------- proxy/tun/route_windows.go | 53 +++++++++++++++++++--------------- utils/cmd.go | 41 ++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 55 deletions(-) create mode 100644 utils/cmd.go diff --git a/netLayer/tproxy/route_linux.go b/netLayer/tproxy/route_linux.go index e926c4b..7cd679a 100644 --- a/netLayer/tproxy/route_linux.go +++ b/netLayer/tproxy/route_linux.go @@ -3,40 +3,12 @@ package tproxy import ( "fmt" "os/exec" - "strings" "github.com/e1732a364fed/v2ray_simple/utils" - "go.uber.org/zap" ) //配置iptables -func execCmd(cmdStr string) (err error) { - utils.ZapLogger.Info("tproxy run cmd", zap.String("cmd", cmdStr)) - - strs := strings.Split(cmdStr, " ") - - cmd1 := exec.Command(strs[0], strs[1:]...) - if err = cmd1.Run(); err != nil { - utils.ZapLogger.Error("tproxy run cmd failed", zap.Error(err)) - } - - return -} - -func execCmdList(cmdStr string) (err error) { - - strs := strings.Split(cmdStr, "\n") - - for _, str := range strs { - if err = execCmd(str); err != nil { - return - } - } - - return -} - const toutyRaterIptableCmdList = `ip rule add fwmark 1 table 100 ip route add local 0.0.0.0/0 dev lo table 100 iptables -t mangle -N V2RAY @@ -92,7 +64,7 @@ func SetRouteByPort(port int) error { } lastPortSet = port - return execCmdList(fmt.Sprintf(toutyRaterIptableCmdList, port, port)) + return utils.ExecCmdMultilineList(fmt.Sprintf(toutyRaterIptableCmdList, port, port)) } // port 12345 @@ -103,13 +75,13 @@ func SetIPTablesByDefault() error { // port 12345 func CleanupIPTablesByDefault() { - execCmdList(fmt.Sprintf(iptableRMCmdList, 12345, 12345)) + utils.ExecCmdMultilineList(fmt.Sprintf(iptableRMCmdList, 12345, 12345)) } // clear iptables set by the last SetRouteByPort call func CleanupRoutes() { if lastPortSet != 0 { - execCmdList(fmt.Sprintf(iptableRMCmdList, lastPortSet, lastPortSet)) + utils.ExecCmdMultilineList(fmt.Sprintf(iptableRMCmdList, lastPortSet, lastPortSet)) lastPortSet = 0 } } diff --git a/proxy/tun/route_windows.go b/proxy/tun/route_windows.go index 158b860..d96e41e 100644 --- a/proxy/tun/route_windows.go +++ b/proxy/tun/route_windows.go @@ -1,7 +1,6 @@ package tun import ( - "fmt" "os/exec" "strings" @@ -12,10 +11,17 @@ import ( var rememberedRouterIP string func init() { + //经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪 + /* + netsh interface ip set address name="vs_wintun" source=static addr=192.168.123.1 mask=255.255.255.0 gateway=none + + route add vps_ip router_ip + route add 0.0.0.0 mask 0.0.0.0 vps_ip metric 5 + */ autoRouteFunc = func(tunDevName, tunGateway, tunIP string, directList []string) { - params := fmt.Sprintf(`interface ip set address name="%s" source=static addr=%s mask=255.255.255.0 gateway=%s`, tunDevName, tunIP, tunGateway) - _, err := exec.Command("netsh", strings.Split(params, " ")...).Output() + params := "netstat -nr" + out, err := exec.Command("", params).Output() if err != nil { if ce := utils.CanLogErr("auto route failed"); ce != nil { ce.Write(zap.Error(err)) @@ -23,15 +29,6 @@ func init() { return } - params = "-nr" - out, err := exec.Command("netstat", params).Output() - if err != nil { - if ce := utils.CanLogErr("auto route failed"); ce != nil { - ce.Write(zap.Error(err)) - } - return - } - //log.Println(string(out)) lines := strings.Split(string(out), "\n") startLineIndex := -1 for i, l := range lines { @@ -67,16 +64,16 @@ func init() { } rememberedRouterIP = routerIP - params1 := "delete 0.0.0.0 mask 0.0.0.0" - out1, err := exec.Command("route", strings.Split(params1, " ")...).Output() + // params1 := "delete 0.0.0.0 mask 0.0.0.0" + // out1, err := exec.Command("route", strings.Split(params1, " ")...).Output() //这里err只能捕获没有权限运行等错误; 如果路由表修改失败,是不会返回err的 - checkErrStep: - if ce := utils.CanLogInfo("auto route delete default"); ce != nil { - ce.Write(zap.String("output", string(out1))) - } + // if ce := utils.CanLogInfo("auto route delete default"); ce != nil { + // ce.Write(zap.String("output", string(out1))) + // } + _, err = exec.Command("netsh", "interface", "ip", "set", "address", `name="`+tunGateway+`"`, "source=static", "addr="+tunGateway, "mask=255.255.255.0", "gateway=none").Output() if err != nil { if ce := utils.CanLogErr("auto route failed"); ce != nil { ce.Write(zap.Error(err)) @@ -84,20 +81,28 @@ func init() { return } - params1 = "add 0.0.0.0 mask 0.0.0.0 " + tunGateway + " metric 6" - out1, err = exec.Command("route", strings.Split(params1, " ")...).Output() + out1, err := exec.Command("route", "add", "0.0.0.0", "mask", "0.0.0.0", tunGateway, "metric", "6").Output() if err != nil { - goto checkErrStep + if err != nil { + if ce := utils.CanLogErr("auto route failed"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } } if ce := utils.CanLogInfo("auto route add tun"); ce != nil { ce.Write(zap.String("output", string(out1))) } for _, v := range directList { - params1 = "add " + v + " " + rememberedRouterIP + " metric 5" - out1, err = exec.Command("route", strings.Split(params1, " ")...).Output() + out1, err = exec.Command("route", "add", v, rememberedRouterIP, "metric", "5").Output() if err != nil { - goto checkErrStep + if err != nil { + if ce := utils.CanLogErr("auto route failed"); ce != nil { + ce.Write(zap.Error(err)) + } + return + } } if ce := utils.CanLogInfo("auto route add direct"); ce != nil { ce.Write(zap.String("output", string(out1))) diff --git a/utils/cmd.go b/utils/cmd.go new file mode 100644 index 0000000..f9d0db6 --- /dev/null +++ b/utils/cmd.go @@ -0,0 +1,41 @@ +package utils + +import ( + "os/exec" + "strings" + + "go.uber.org/zap" +) + +func ExecCmd(cmdStr string) (err error) { + ZapLogger.Info("run cmd", zap.String("cmd", cmdStr)) + + strs := strings.Split(cmdStr, " ") + + cmd1 := exec.Command(strs[0], strs[1:]...) + if err = cmd1.Run(); err != nil { + ZapLogger.Error("run cmd failed", zap.Error(err)) + } + + return +} + +func ExecCmdMultilineList(cmdStr string) (err error) { + + strs := strings.Split(cmdStr, "\n") + + err = ExecCmdList(strs) + + return +} + +func ExecCmdList(strs []string) (err error) { + + for _, str := range strs { + if err = ExecCmd(str); err != nil { + return + } + } + + return +}