summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/helper/darwin.go5
-rw-r--r--pkg/helper/helper.go4
-rw-r--r--pkg/helper/linux.go5
-rw-r--r--pkg/helper/ports.go49
-rw-r--r--pkg/helper/windows.go6
-rw-r--r--pkg/helper/windows_service.go5
6 files changed, 63 insertions, 11 deletions
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 {