Created
November 1, 2019 12:02
-
-
Save naoki9911/58cc370dc7cde9afacc4df788b077fe8 to your computer and use it in GitHub Desktop.
SoftEther Wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
"net" | |
) | |
func main(){ | |
flag.Parse() | |
if flag.Arg(0) == "" { | |
fmt.Println("Specify Connection Name") | |
os.Exit(1) | |
} | |
out, err := exec.Command("vpncmd", "/client", "localhost", "/cmd", "AccountGet", flag.Arg(0)).Output() | |
if err != nil { | |
fmt.Printf("接続 %v は存在しません\n", flag.Arg(0)) | |
log.Fatal(err) | |
} | |
out_list := strings.Split(string(out), "\n") | |
var server_address string = "" | |
for i := 0; i < len(out_list); i++ { | |
if strings.Index(out_list[i], "VPN Client>AccountGet") != -1 { | |
server_address = out_list[i+5] | |
break | |
} | |
} | |
server_address = strings.Split(server_address, "|")[1] | |
if net.ParseIP(server_address) != nil { | |
fmt.Println("IP") | |
} else { | |
addr, err := net.ResolveIPAddr("ip", server_address) | |
if err != nil { | |
fmt.Println("Resolve error") | |
os.Exit(1) | |
} | |
fmt.Printf("VPN Server URI: %v\n", server_address) | |
server_address = addr.String() | |
} | |
fmt.Printf("VPN Server IP : %v\n", server_address) | |
_ = exec.Command("ip", "route", "del", server_address).Run() | |
_ = exec.Command("dhcpcd", "-x", "vpn_"+strings.ToLower(flag.Arg(0))).Run() | |
out, err = exec.Command("sh", "-c", "ip route | grep default").Output() | |
if err != nil { | |
fmt.Println("Failed to exec ip route") | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
if len(out) < 1 { | |
fmt.Println("Failed to get default gateway") | |
os.Exit(1) | |
} | |
gateway_string := strings.Split(string(out), "\n")[0] | |
gateway_ip := strings.Split(string(gateway_string), " ")[2] | |
gateway_dev := strings.Split(string(gateway_string), " ")[4] | |
fmt.Printf("Default Gateway IP : %v\n", gateway_ip) | |
fmt.Printf("Default Gateway Dev: %v\n", gateway_dev) | |
fmt.Printf("Adding VPN Server Route...\n") | |
out, err = exec.Command("ip", "route", "add", server_address, "via", gateway_ip, "dev", gateway_dev).Output() | |
if err != nil { | |
fmt.Println("Failed to exec ip route") | |
log.Fatal(err) | |
os.Exit(1) | |
} | |
fmt.Printf("Successfully Added VPN Server Route\n") | |
out, err = exec.Command("vpncmd", "/client", "localhost", "/cmd", "AccountConnect", flag.Arg(0)).Output() | |
if err != nil { | |
fmt.Printf("Failed to Connect\n", flag.Arg(0)) | |
log.Fatal(err) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment