Skip to content
Snippets Groups Projects
Unverified Commit 79b2cf15 authored by Kali Kaneko's avatar Kali Kaneko
Browse files

[feat] listen on available port

parent d1bbf960
No related branches found
No related tags found
No related merge requests found
......@@ -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)
}
......@@ -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 {
......
......@@ -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) {
......
......@@ -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 {
......
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
}
......@@ -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
}
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment