diff --git a/Makefile b/Makefile index 9ded075..466aee7 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M) all: portmap.pb.go goimports vet build ./gus --version - ./gus --gui gocui >/tmp/gocui.log 2>&1 + ./gus --no-gui + # ./gus --gui gocui >/tmp/gocui.log 2>&1 build: goimports GO111MODULE=off go build -v -x \ diff --git a/main.go b/main.go index 5b58e05..6d02a2b 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "embed" + "fmt" "io" "net" "os" @@ -50,24 +51,32 @@ func main() { } // go NewWatchdog() - - go listen3000() - go startHTTP() + if gui.NoGui() { + all := me.portmaps.All() + for all.Scan() { + pm := all.Next() + if !pm.Enabled { + continue + } + log.Info("portmap enabled for port", pm.Listen, "to", pm.Connect) + gus3000(int(pm.Listen), pm.Connect) + } os.Exit(0) } doGui() } -func listen3000() { +func gus3000(port int, connect string) { // Listen on local port 3000 - listener, err := net.Listen("tcp", "0.0.0.0:3000") + s := fmt.Sprintf("0.0.0.0:%d", port) + listener, err := net.Listen("tcp", s) if err != nil { - log.Fatalf("Failed to listen on port 3000: %v", err) + log.Fatalf("Failed to listen on %s: %v", s, err) } defer listener.Close() - log.Println("Listening on port 3000...") + log.Info("Listening on ", s) for { // Accept incoming connection @@ -79,17 +88,18 @@ func listen3000() { log.Printf("Client connected: %s", clientConn.RemoteAddr()) // Handle the connection in a separate goroutine - go handleConnection(clientConn) + go handleConnection(clientConn, s) } } -func handleConnection(clientConn net.Conn) { +func handleConnection(clientConn net.Conn, where string) { defer clientConn.Close() // Connect to the target server - targetConn, err := net.Dial("tcp", "go.wit.com:44355") + // targetConn, err := net.Dial("tcp", "go.wit.com:80") + targetConn, err := net.Dial("tcp", where) if err != nil { - log.Printf("Failed to connect to go.wit.com %v", err) + log.Printf("Failed to connect to %s %v", where, err) return } defer targetConn.Close()