-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
67 lines (60 loc) · 1.52 KB
/
handler.go
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
package main
import (
"fmt"
"io"
"log"
"net"
)
// handleConnection is a function that handles a new connection
func handleConnection(inbound net.Conn, config *Config) {
// Create log files
inboundLog, err := createLogFile(config, "inbound")
if err != nil {
log.Printf("Failed to create inbound log: %v", err)
return
}
defer inboundLog.Close()
outboundLog, err := createLogFile(config, "outbound")
if err != nil {
log.Printf("Failed to create outbound log: %v", err)
return
}
defer outboundLog.Close()
// Log connection details
logConnection(inboundLog, "New connection from %s", inbound.RemoteAddr())
outbound, err := net.Dial("tcp", fmt.Sprintf("%s:%s", config.OutboundAddress, config.OutboundPort))
if err != nil {
logConnection(inboundLog, "Failed to connect to target: %v", err)
inbound.Close()
return
}
defer outbound.Close()
defer inbound.Close()
// Modified copy function with logging
go func() {
buffer := make([]byte, 1024)
for {
n, err := inbound.Read(buffer)
if err != nil {
if err != io.EOF {
logConnection(inboundLog, "Error reading from inbound: %v", err)
}
return
}
logConnection(inboundLog, "Received: %s", string(buffer[:n]))
outbound.Write(buffer[:n])
}
}()
buffer := make([]byte, 1024)
for {
n, err := outbound.Read(buffer)
if err != nil {
if err != io.EOF {
logConnection(outboundLog, "Error reading from outbound: %v", err)
}
return
}
logConnection(outboundLog, "Sent: %s", string(buffer[:n]))
inbound.Write(buffer[:n])
}
}