-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevilproxy.go
79 lines (64 loc) · 1.99 KB
/
evilproxy.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
package main
import (
"bytes"
"flag"
"io"
"log"
"net"
"runtime/pprof"
"sync"
"time"
"github.com/efarrer/evilproxy/connection"
"github.com/efarrer/evilproxy/debug"
"github.com/efarrer/evilproxy/parser"
)
func main() {
var client = flag.String("client", ":80", "Client connection address")
var server = flag.String("server", ":8080", "Server connection address")
var connections = flag.Int("connections", -1, "Number of connections to allow")
var debugEnabled = flag.Bool("debug", false, "Enable additional debug functionality")
flag.Parse()
var outstandingConns sync.WaitGroup
serv, err := net.Listen("tcp", *server)
if err != nil {
log.Fatalf("Unable start server on \"%s\". %v\n", *server, err)
}
for i := 0; i != *connections; i++ {
ssock, err := serv.Accept()
if err != nil {
log.Fatalf("Unable accept client connection. %v\n", err)
}
outstandingConns.Add(1)
// Complete the connection
go func(client string) {
defer outstandingConns.Done()
csock, err := net.DialTimeout("tcp", client, time.Second*3)
if err != nil {
log.Printf("Unable to connect to \"%s\". %v\n", client, err)
}
rule := ""
cconn, sconn, err := parser.ConstructConnections(rule)
if err != nil {
log.Printf("Error (%v) unable to parse rule. \"%v\"\n", rule, err)
}
go io.Copy(csock, connection.ConnectionReaderAdaptor(cconn))
go io.Copy(connection.ConnectionWriterAdaptor(cconn), csock)
go io.Copy(ssock, connection.ConnectionReaderAdaptor(sconn))
io.Copy(connection.ConnectionWriterAdaptor(sconn), ssock)
defer ssock.Close()
defer csock.Close()
defer cconn.Close()
defer sconn.Close()
// TODO Make sure all socket/connections get closed
}(*client)
outstandingConns.Wait()
if *debugEnabled {
time.Sleep(1 * time.Second)
buffer := &bytes.Buffer{}
pprof.Lookup("goroutine").WriteTo(buffer, 2)
if cnt, value := debug.OutstandingGoRoutines(buffer.String()); cnt != 1 {
log.Printf("%v\n\nOutstanding goroutines %v", value, cnt)
}
}
}
}