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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
import (
"bufio"
"embed"
"fmt"
"io"
"net"
"os"
"strings"
sync "sync"
"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
if argv.UseME {
connectME()
okExit("")
}
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)
startGus()
startHTTP()
os.Exit(0)
}
if gui.NoGui() {
startGus()
startHTTP()
os.Exit(0)
}
startGus()
// go NewWatchdog()
go startHTTP()
doGui()
}
func startGus() {
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)
}
}
// 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)
*/
for {
// Accept incoming connection
src, err := gus.Accept()
if err != nil {
log.Printf("Failed to accept client connection: %v\n", err)
pm.Enabled = false
return
}
// make a new event from this new connection
log.Printf("/me Connected on port %d from client: %s to where = %s\n", localport, src.RemoteAddr(), where)
fmt.Fprintln(src, "/me hello")
reader := bufio.NewReader(src)
// Read one line
line, err := reader.ReadString('\n')
if err != nil {
log.Info("gus src", src.RemoteAddr(), "read error:", err)
continue
}
if !strings.HasPrefix(line, "/me") {
log.Printf("gus Received %d invalid bytes\n", len(line))
continue
}
parts := strings.Fields(line)
if len(parts) != 3 {
continue
}
if parts[0] != "/me" {
continue
}
if parts[1] != "hostname" {
continue
}
hostname := parts[2]
msg := fmt.Sprintf("got hostname %s for %s", hostname, src.RemoteAddr())
log.Info("gus:", msg)
fmt.Fprintln(src, msg)
if hostname == "framebook.wit.com" {
// Handle the connection in a separate goroutine
log.Info("RUNNING DIAL")
dest, err := net.Dial("tcp", where)
if err != nil {
log.Printf("Failed to connect to %s %v", where, err)
continue
}
defer dest.Close()
log.Info("IOCOPY START")
ioCopy(src, dest)
log.Info("IOCOPY END")
}
}
}
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
ioCopy(clientConn, targetConn)
// 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) {
var found bool
all := me.portmaps.All()
for all.Scan() {
pm := all.Next()
if int(pm.Localport) == port {
found = true
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)
}
if !found {
log.Info("Did not find port =", port)
}
}
// should cleanly close both
func ioCopy(clientConn, targetConn net.Conn) {
defer clientConn.Close()
defer targetConn.Close()
var wg sync.WaitGroup
wg.Add(2)
// Copy data in both directions
go func() {
defer wg.Done()
io.Copy(targetConn, clientConn) // Client -> Target
targetConn.Close() // Ensure closure on EOF/error
}()
go func() {
defer wg.Done()
io.Copy(clientConn, targetConn) // Target -> Client
clientConn.Close() // Ensure closure on EOF/error
}()
wg.Wait() // Wait for both copies to complete
}
func connectME() {
hostname, err := os.Hostname()
if err != nil {
fmt.Println("Error:", err)
return
}
log.Println("Hostname:", hostname)
if fqdn, err := getFQDN(hostname); err == nil {
hostname = fqdn
} else {
log.Printf("Your hostname (%s) is not in DNS correctly: %v\n", hostname, err)
}
localport := "25910"
// where := "104.48.38.253:25910"
where := "104.48.38.253:8081"
dest, err := net.Dial("tcp", where)
if err != nil {
log.Printf("Failed to connect to %s %v\n", where, err)
return
}
defer dest.Close()
// make a new event from this new connection
log.Printf("Connected on port %s from client: %s to where = %s\n", localport, "tbd", where)
reader := bufio.NewReader(dest)
// Read one line
line, err := reader.ReadString('\n')
if err != nil {
log.Info("Error reading line:", err)
return
}
log.Info("gus Received:", line)
fmt.Fprintf(dest, "/me hostname %s\n", hostname)
// Read one line
line, err = reader.ReadString('\n')
if err != nil {
log.Info("Error reading line:", err)
return
}
log.Info("gus Received:", line)
// Listen on local port
s := fmt.Sprintf("0.0.0.0:%s", localport)
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)
log.Printf("Try: remmina -c spice://localhost:%s\n", localport)
// Accept incoming connection
src, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept client connection: %v", err)
return
}
log.Printf("Client connected: %s\n", src.RemoteAddr())
ioCopy(src, dest)
}
func getFQDN(hostname string) (string, error) {
// Perform a reverse lookup to get the FQDN
addrs, err := net.LookupAddr(hostname)
if err != nil {
return "", fmt.Errorf("failed to resolve FQDN: %w", err)
}
// Choose the first valid result
for _, addr := range addrs {
if strings.HasSuffix(addr, ".") { // FQDNs often end with a dot
return strings.TrimSuffix(addr, "."), nil
}
return addr, nil
}
return "", fmt.Errorf("no FQDN found for hostname: %s", hostname)
}
|