gus/main.go

196 lines
4.4 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 (
"embed"
"fmt"
"io"
"net"
"os"
"time"
"github.com/google/uuid"
"go.wit.com/dev/alexflint/arg"
"go.wit.com/gui"
"go.wit.com/log"
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
)
var VERSION string
var BUILDTIME string
//go:embed resources/*
var resources embed.FS
func main() {
var pp *arg.Parser
gui.InitArg()
pp = arg.MustParse(&argv)
if pp == nil {
pp.WriteHelp(os.Stdout)
os.Exit(0)
}
log.Info("tmp hack", uuid.New().String())
me = new(gusconf)
me.pollDelay = 10 * time.Second
me.portmaps = ConfigLoad()
me.events = EventLoad()
if me.portmaps == nil {
me.portmaps = NewPortmaps()
p := new(Portmap)
p.Dest = "testing:323"
me.portmaps.Append(p)
}
if argv.Daemon {
// turn off timestamps for STDOUT (systemd adds them)
log.DaemonMode(true)
startHTTP()
os.Exit(0)
}
if gui.NoGui() {
all := me.portmaps.All()
for all.Scan() {
pm := all.Next()
if !pm.Enabled {
continue
}
log.Info("portmap enabled for port", pm.Localport, "to", pm.Dest)
go gus3000(pm)
}
startHTTP()
os.Exit(0)
}
all := me.portmaps.All()
for all.Scan() {
pm := all.Next()
if !pm.Enabled {
continue
}
log.Info("portmap enabled for port", pm.Localport, "to", pm.Dest)
go gus3000(pm)
}
// go NewWatchdog()
go startHTTP()
doGui()
}
// func doME(pm *Portmap, gus listener.Accept) {
func doME(pm *Portmap, gus net.Listener) {
localport := int(pm.Localport)
where := pm.Dest
/*
// Listen on local port 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 %s: %v", s, err)
}
defer listener.Close()
log.Info("Listening on ", s)
*/
// Accept incoming connection
clientConn, err := gus.Accept()
if err != nil {
log.Printf("Failed to accept client connection: %v", err)
return
}
// log.Printf("Client connected: %s", clientConn.RemoteAddr())
// make a new event from this new connection
log.Printf("Connected on port %d from client: %s to where = %s\n", localport, clientConn.RemoteAddr(), where)
// Handle the connection in a separate goroutine
// go handleConnection(clientConn, connect, port)
}
func gus3000(pm *Portmap) {
port := int(pm.Localport)
connect := pm.Dest
// Listen on local port 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 %s: %v", s, err)
}
defer listener.Close()
log.Info("Listening on ", s)
if pm.UseME {
doME(pm, listener)
return
}
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, connect, port)
}
}
func handleConnection(clientConn net.Conn, where string, localport int) {
defer clientConn.Close()
// Connect to the target server
// targetConn, err := net.Dial("tcp", "go.wit.com:80")
targetConn, err := net.Dial("tcp", where)
if err != nil {
log.Printf("Failed to connect to %s %v", where, err)
return
}
defer targetConn.Close()
// make a new event from this new connection
log.Printf("Connected on port %d from client: %s to where = %s\n", localport, clientConn.RemoteAddr(), where)
e := new(Event)
e.Etype = GusEventType_Connect
e.LocalPort = int64(localport)
e.Sock = new(GusSocket)
e.Sock.SrcIp = fmt.Sprintf("%s", clientConn.RemoteAddr())
e.Sock.DestIp = where
e.Ctime = timestamppb.New(time.Now())
me.events.Append(e)
me.eventsChanged = true
// Bidirectional copy of data
go io.Copy(targetConn, clientConn) // Client -> Target
io.Copy(clientConn, targetConn) // Target -> Client
// if the socket closes, record the close time
e.Etime = timestamppb.New(time.Now())
me.eventsChanged = true
log.Printf("Connection closed on port %d from client: %s to where = %s\n", localport, clientConn.RemoteAddr(), where)
}
func enablePort(port int, dest string) {
all := me.portmaps.All()
for all.Scan() {
pm := all.Next()
if int(pm.Localport) == port {
log.Info("Found port!", port)
}
if pm.Enabled {
log.Info("portmap already enabled for", pm.Localport, "to", pm.Dest)
} else {
log.Info("portmap not enabled for", pm.Localport, "to", pm.Dest)
}
// go gus3000(pm)
}
}