gus/main.go

84 lines
1.6 KiB
Go

// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"io"
"net"
"os"
"time"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/log"
)
var VERSION string
var BUILDTIME string
func main() {
var pp *arg.Parser
pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
me := new(gusconf)
me.pollDelay = 10 * time.Second
if argv.Daemon {
// turn off timestamps for STDOUT (systemd adds them)
log.DaemonMode(true)
startHTTP()
os.Exit(0)
}
// go NewWatchdog()
go listen3000()
startHTTP()
}
func listen3000() {
// Listen on local port 3000
listener, err := net.Listen("tcp", "0.0.0.0:3000")
if err != nil {
log.Fatalf("Failed to listen on port 3000: %v", err)
}
defer listener.Close()
log.Println("Listening on port 3000...")
for {
// Accept incoming connection
clientConn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept client connection: %v", err)
continue
}
log.Printf("Client connected: %s", clientConn.RemoteAddr())
// Handle the connection in a separate goroutine
go handleConnection(clientConn)
}
}
func handleConnection(clientConn net.Conn) {
defer clientConn.Close()
// Connect to the target server
targetConn, err := net.Dial("tcp", "go.wit.com:44355")
if err != nil {
log.Printf("Failed to connect to go.wit.com %v", err)
return
}
defer targetConn.Close()
log.Printf("Connected to target server: %s", targetConn.RemoteAddr())
// Bidirectional copy of data
go io.Copy(targetConn, clientConn) // Client -> Target
io.Copy(clientConn, targetConn) // Target -> Client
}