forked from praetorian-inc/trudy
/
main.go
90 lines (72 loc) · 2.07 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"net"
"log"
"github.com/kelbyludwig/trudy/pipe"
"github.com/kelbyludwig/trudy/module"
)
var connection_count uint
func main(){
tcpAddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:6666")
tcpListener,err := net.ListenTCP("tcp", tcpAddr)
defer tcpListener.Close()
if err != nil {
log.Println("[FATAL] Failed to setup listeners. Did you run this as root? (You should!)")
panic(err)
}
log.Println("[INFO] Trudy lives!")
for {
conn, err := tcpListener.AcceptTCP()
if err != nil {
log.Println("[ERR] Error reading from connection. Moving along.")
continue
}
tcppipe,err := pipe.NewTCPPipe(connection_count, *conn)
if err != nil {
log.Println("[ERR] Error creating new TCPPipe.")
continue
}
log.Printf("[INFO] TCP Connection %v accepted!\n", connection_count)
go clientHandler(tcppipe)
go serverHandler(tcppipe)
connection_count++
}
}
func clientHandler(tcppipe pipe.TCPPipe) {
defer tcppipe.Close()
buffer := make([]byte, 65535)
//TODO: Timeouts!
for {
bytesRead,err := tcppipe.ReadSource(buffer)
if err != nil {
continue
}
if module.Drop(buffer[:bytesRead]){
continue
}
if !module.Pass(buffer[:bytesRead]) {
//TODO: This won't work when Mangle returns a different sized buffer.
buffer = module.Mangle(buffer)
}
log.Println(module.PrettyPrint(buffer))
_, err = tcppipe.WriteDestination(buffer[:bytesRead])
if err != nil {
continue
}
}
}
func serverHandler(tcppipe pipe.TCPPipe) {
defer tcppipe.Close()
buffer := make([]byte, 65535)
//TODO: Timeouts!
for {
bytesReadFromDestination,err := tcppipe.ReadDestination(buffer)
if err != nil {
continue
}
_,err = tcppipe.WriteSource(buffer[:bytesReadFromDestination])
if err != nil {
continue
}
}
}