diff --git a/.gitignore b/.gitignore index 902e4e7..464ff4e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ test.sh openvpn-dm-mgt-server openvpn-dm-mgt-server.conf +iproute/iproute diff --git a/iproute/main.go b/iproute/main.go new file mode 100644 index 0000000..9240523 --- /dev/null +++ b/iproute/main.go @@ -0,0 +1,49 @@ +package main + +import ( + "errors" + "fmt" + "net" + "os" + "os/exec" + "regexp" +) + +func checkArg(args []string, pos int, value string) { + if args[pos] != value { + fmt.Println(errors.New("invalid command : " + args[pos])) + os.Exit(10 + pos) + } +} + +func main() { + if len(os.Args) != 6 { + fmt.Println(errors.New("missing args"), len(os.Args)) + os.Exit(1) + } + + args := os.Args[1:] + + r := regexp.MustCompile("[^a-zA-Z0-9.-]") + var ip string + + checkArg(args, 0, "route") + checkArg(args, 1, "replace") + + if testip := net.ParseIP(args[2]); testip != nil { + ip = testip.String() + } else { + fmt.Println(errors.New("invalid ip")) + os.Exit(2) + } + + checkArg(args, 3, "dev") + + dev := r.ReplaceAllString(args[4], "") + + cmd := exec.Command("/bin/ip", "route", "replace", ip, "dev", dev) + if err := cmd.Run(); err != nil { + fmt.Println(err) + os.Exit(3) + } +}