summaryrefslogtreecommitdiff
path: root/main.go
blob: e5ea9272bb4824a91db9f59a69199dd95e6e419c (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
// 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
}