From 79b2cf15c8d563dc5a3ea589ea1c4a29ee1315e8 Mon Sep 17 00:00:00 2001 From: "kali kaneko (leap communications)" Date: Thu, 27 Feb 2020 18:06:22 +0100 Subject: [feat] listen on available port --- cmd/bitmask-helper/main.go | 7 +++---- pkg/helper/darwin.go | 5 +++-- pkg/helper/helper.go | 4 ++-- pkg/helper/linux.go | 5 +++-- pkg/helper/ports.go | 49 +++++++++++++++++++++++++++++++++++++++++++ pkg/helper/windows.go | 6 +++++- pkg/helper/windows_service.go | 5 +---- 7 files changed, 66 insertions(+), 15 deletions(-) create mode 100644 pkg/helper/ports.go diff --git a/cmd/bitmask-helper/main.go b/cmd/bitmask-helper/main.go index ce32b4e..de180ab 100644 --- a/cmd/bitmask-helper/main.go +++ b/cmd/bitmask-helper/main.go @@ -24,8 +24,8 @@ import ( ) const ( - bindAddr = "localhost:7171" - logFile = "helper.log" + preferredPort = 7171 + logFile = "helper.log" ) func main() { @@ -36,6 +36,5 @@ func main() { defer logger.Close() } - helper.ServeHTTP(bindAddr) - + helper.ServeHTTP(preferredPort) } diff --git a/pkg/helper/darwin.go b/pkg/helper/darwin.go index f65012d..7f68243 100644 --- a/pkg/helper/darwin.go +++ b/pkg/helper/darwin.go @@ -33,6 +33,7 @@ import ( "os" "os/exec" "path" + "strconv" "strings" "0xacab.org/leap/bitmask-vpn/pkg/config" @@ -86,8 +87,8 @@ func daemonize() { log.Print("bitmask-helper daemon started") } -func doHandleCommands(bindAddr string) { - runCommandServer(bindAddr) +func doHandleCommands(port int) { + runCommandServer("localhost:" + strconv.Itoa(port)) } func getOpenvpnPath() string { diff --git a/pkg/helper/helper.go b/pkg/helper/helper.go index ab1894e..b5404c2 100644 --- a/pkg/helper/helper.go +++ b/pkg/helper/helper.go @@ -37,10 +37,10 @@ func runCommandServer(bindAddr string) { log.Fatal(http.ListenAndServe(bindAddr, nil)) } -func ServeHTTP(bindAddr string) { +func ServeHTTP(port int) { parseCliArgs() daemonize() - doHandleCommands(bindAddr) + doHandleCommands(port) } func (openvpn *openvpnT) start(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/helper/linux.go b/pkg/helper/linux.go index 3aaa0fe..5aa7e14 100644 --- a/pkg/helper/linux.go +++ b/pkg/helper/linux.go @@ -20,6 +20,7 @@ import ( "log" "os" "os/exec" + "strconv" "0xacab.org/leap/bitmask-vpn/pkg/config" ) @@ -46,8 +47,8 @@ func parseCliArgs() { func daemonize() {} -func doHandleCommands(bindAddr string) { - runCommandServer(bindAddr) +func doHandleCommands(port int) { + runCommandServer("localhost:" + strconv.Itoa(port)) } func getOpenvpnPath() string { diff --git a/pkg/helper/ports.go b/pkg/helper/ports.go new file mode 100644 index 0000000..58e5856 --- /dev/null +++ b/pkg/helper/ports.go @@ -0,0 +1,49 @@ +package helper + +import ( + "io/ioutil" + "net" + "os" + "path" + "strconv" +) + +func getFirstAvailablePortFrom(port int) int { + for { + if isPortAvailable(port) { + return port + } + if port > 65535 { + return 0 + } + port += 1 + } +} + +func isPortAvailable(port int) bool { + conn, err := net.Dial("tcp", "127.0.0.1:"+strconv.Itoa(port)) + if err != nil { + return true + } else { + defer conn.Close() + return false + } +} + +func writePortToFile(port int) error { + exeDir, err := getExecutableDir() + if err != nil { + return err + } + portFile := path.Join(exeDir, "port") + return ioutil.WriteFile(portFile, []byte(strconv.Itoa(port)+"\n"), 0644) + +} + +func getExecutableDir() (string, error) { + ex, err := os.Executable() + if err != nil { + return "", err + } + return path.Dir(ex), nil +} diff --git a/pkg/helper/windows.go b/pkg/helper/windows.go index fc80853..376b376 100644 --- a/pkg/helper/windows.go +++ b/pkg/helper/windows.go @@ -21,6 +21,7 @@ import ( "log" "os" "os/exec" + "strconv" "strings" "0xacab.org/leap/bitmask-vpn/pkg/config" @@ -97,7 +98,10 @@ func usage(errmsg string) { func daemonize() {} // http server is called from within Execute in windows -func doHandleCommands(bindAddr string) { +func doHandleCommands(preferredPort int) { + port := getFirstAvailablePortFrom(preferredPort) + writePortToFile(port) + bindAddr := "localhost:" + strconv.Itoa(port) httpBindAddr = bindAddr } diff --git a/pkg/helper/windows_service.go b/pkg/helper/windows_service.go index b35ba19..82f6e2b 100644 --- a/pkg/helper/windows_service.go +++ b/pkg/helper/windows_service.go @@ -8,8 +8,6 @@ package helper import ( "fmt" - //"strings" - //"time" "golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc/debug" @@ -24,8 +22,7 @@ func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes c const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} - // TODO use httpBindAddr - go runCommandServer("localhost:7171") + go runCommandServer(httpBindAddr) loop: for { select { -- cgit v1.2.3