修订tun自动路由代码

This commit is contained in:
e1732a364fed
2000-01-01 00:00:00 +00:00
parent 58d93eb09a
commit abf53be491
3 changed files with 45 additions and 34 deletions

View File

@@ -1,6 +1,7 @@
package tun
import (
"log"
"os/exec"
"strings"
@@ -8,8 +9,6 @@ import (
"go.uber.org/zap"
)
var rememberedRouterIP string
func init() {
autoRouteFunc = func(tunDevName, tunGateway, tunIP string, directList []string) {
if len(directList) == 0 {
@@ -103,30 +102,31 @@ func init() {
}
autoRouteDownFunc = func(tunDevName, tunGateway, tunIP string, directList []string) {
if rememberedRouterIP == "" {
return
}
if len(directList) == 0 {
utils.Warn("tun auto route down called, but no direct list given. auto route will not run.")
}
//恢复路由表
params := "delete -host default"
out1, _ := exec.Command("route", strings.Split(params, " ")...).Output()
if ce := utils.CanLogInfo("auto route delete tun route"); ce != nil {
ce.Write(zap.String("output", string(out1)))
}
params = "add default " + rememberedRouterIP
out1, _ = exec.Command("route", strings.Split(params, " ")...).Output()
if ce := utils.CanLogInfo("auto route recover default route"); ce != nil {
ce.Write(zap.String("output", string(out1)))
strs := []string{
"route delete delete -host default",
"route add default " + rememberedRouterIP,
}
for _, v := range directList {
params = "delete -host " + v
out1, _ = exec.Command("route", strings.Split(params, " ")...).Output()
strs = append(strs, "route delete -host "+v)
}
if ce := utils.CanLogInfo("auto route delete direct"); ce != nil {
ce.Write(zap.String("output", string(out1)))
if manualRoute {
promptManual(strs)
} else {
log.Println("running these commands", strs)
if e := utils.ExecCmdList(strs); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
}
}
}
}

View File

@@ -11,8 +11,6 @@ import (
"go.uber.org/zap"
)
var rememberedRouterIP string
func init() {
/*
经过测试发现,完全一样的路由命令,自动执行和 手动在控制台输入执行,效果竟然不一样; 手动的能正常运行, 自动的就不行, 怪
@@ -87,14 +85,7 @@ func init() {
strs = append(strs, fmt.Sprintf("route add 0.0.0.0 mask 0.0.0.0 %s metric 6", tunGateway))
if manualRoute {
utils.Warn("Please try run these commands manually(Administrator):")
for _, s := range strs {
utils.Warn(s)
}
if AddManualRunCmdsListFunc != nil {
AddManualRunCmdsListFunc(strs)
}
promptManual(strs)
} else {
if e := utils.ExecCmdList(strs[:len(strs)-1]); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
@@ -127,12 +118,17 @@ func init() {
strs = append(strs, "route delete "+v+" "+rememberedRouterIP)
}
log.Println("running these commands", strs)
if manualRoute {
promptManual(strs)
} else {
log.Println("running these commands", strs)
if e := utils.ExecCmdList(strs); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
if e := utils.ExecCmdList(strs); e != nil {
if ce := utils.CanLogErr("recover auto route failed"); ce != nil {
ce.Write(zap.Error(e))
}
}
}
}
}

View File

@@ -22,10 +22,25 @@ import (
"go.uber.org/zap"
)
const name = "tun"
var AddManualRunCmdsListFunc func([]string)
var manualRoute bool
const name = "tun"
const manualPrompt = "Please try run these commands manually(Administrator):"
var rememberedRouterIP string
func promptManual(strs []string) {
utils.Warn(manualPrompt)
for _, s := range strs {
utils.Warn(s)
}
if AddManualRunCmdsListFunc != nil {
AddManualRunCmdsListFunc(strs)
}
}
func init() {
proxy.RegisterServer(name, &ServerCreator{})
@@ -97,7 +112,7 @@ func (ServerCreator) AfterCommonConfServer(ps proxy.Server) (err error) {
const defaultSelfIP = "10.1.0.10"
const defaultRealIP = "10.1.0.20"
const defaultMask = "255.255.255.0"
//const defaultMask = "255.255.255.0"
//上面两个默认ip取自water项目给出的示例