try to listen

This commit is contained in:
Jeff Carr 2025-03-09 09:41:53 -05:00
parent 2495995e6e
commit 31c8c96c82
2 changed files with 23 additions and 12 deletions

View File

@ -5,7 +5,8 @@ BUILDTIME = $(shell date +%Y.%m.%d_%H%M)
all: portmap.pb.go goimports vet build all: portmap.pb.go goimports vet build
./gus --version ./gus --version
./gus --gui gocui >/tmp/gocui.log 2>&1 ./gus --no-gui
# ./gus --gui gocui >/tmp/gocui.log 2>&1
build: goimports build: goimports
GO111MODULE=off go build -v -x \ GO111MODULE=off go build -v -x \

32
main.go
View File

@ -5,6 +5,7 @@ package main
import ( import (
"embed" "embed"
"fmt"
"io" "io"
"net" "net"
"os" "os"
@ -50,24 +51,32 @@ func main() {
} }
// go NewWatchdog() // go NewWatchdog()
go listen3000()
go startHTTP() go startHTTP()
if gui.NoGui() { 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) os.Exit(0)
} }
doGui() doGui()
} }
func listen3000() { func gus3000(port int, connect string) {
// Listen on local port 3000 // 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 { 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() defer listener.Close()
log.Println("Listening on port 3000...") log.Info("Listening on ", s)
for { for {
// Accept incoming connection // Accept incoming connection
@ -79,17 +88,18 @@ func listen3000() {
log.Printf("Client connected: %s", clientConn.RemoteAddr()) log.Printf("Client connected: %s", clientConn.RemoteAddr())
// Handle the connection in a separate goroutine // 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() defer clientConn.Close()
// Connect to the target server // 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 { 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 return
} }
defer targetConn.Close() defer targetConn.Close()