summaryrefslogtreecommitdiff
path: root/main.go
blob: dab140e073ee16323e56f48584bcc3c7ef089f67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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.Connect = "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.Listen, "to", pm.Connect)
			go gus3000(int(pm.Listen), pm.Connect)
		}
		startHTTP()
		os.Exit(0)
	}
	// go NewWatchdog()
	go startHTTP()
	doGui()
}

func gus3000(port int, connect string) {
	// 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)

	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)
	}
}

func handleConnection(clientConn net.Conn, where string) {
	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()
	// log.Printf("Connected to target server: %s where = %s\n", targetConn.RemoteAddr(), where)
	log.Printf("Connected to client: %s where = %s\n", clientConn.RemoteAddr(), where)

	e := new(Event)
	e.Address = fmt.Sprintf("%s\n", clientConn.RemoteAddr())
	e.Where = where
	e.Ctime = timestamppb.New(time.Now())

	me.events.Append(e)

	// Bidirectional copy of data
	go io.Copy(targetConn, clientConn) // Client -> Target
	io.Copy(clientConn, targetConn)    // Target -> Client
	e.Etime = timestamppb.New(time.Now())
}