-
Notifications
You must be signed in to change notification settings - Fork 21
/
replicator.go
125 lines (104 loc) · 2.65 KB
/
replicator.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
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
package main
import (
"fmt"
"net"
"strings"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
debug = kingpin.Flag("debug", "Enable debug mode").Envar("DEBUG").Bool()
listenIP = kingpin.Flag("listen-ip", "IP to listen in").Default("0.0.0.0").Envar("LISTEN_IP").IP()
listenPort = kingpin.Flag("listen-port", "Port to listen on").Default("9000").Envar("LISTEN_PORT").Int()
bodySize = kingpin.Flag("body-size", "Size of body to read").Default("4096").Envar("BODY_SIZE").Int()
forwards = kingpin.Flag("forward", "ip:port to forward traffic to (port defaults to listen-port)").PlaceHolder("ip:port").Envar("FORWARD").Strings()
pretty = kingpin.Flag("pretty", "").Default("true").Envar("PRETTY").Hidden().Bool()
targets []*net.UDPConn
)
func main() {
// CLI
kingpin.Parse()
// Log setup
if *debug {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
if !*pretty {
log.SetFormatter(&log.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}
if len(*forwards) <= 0 {
log.Fatal("Must specify at least one forward target")
}
// Clients
for _, forward := range *forwards {
// Check for port
if strings.Index(forward, ":") < 0 {
forward = fmt.Sprintf("%s:%d", forward, *listenPort)
}
// Resolve
addr, err := net.ResolveUDPAddr("udp", forward)
if err != nil {
log.Fatalf("Could not ResolveUDPAddr: %s (%s)", forward, err)
}
// Setup conn
conn, err := net.DialUDP("udp", nil, addr)
if err != nil {
log.Fatalf("Could not DialUDP: %+v (%s)", addr, err)
}
defer conn.Close()
targets = append(targets, conn)
}
// Server
conn, err := net.ListenUDP("udp", &net.UDPAddr{
Port: *listenPort,
IP: *listenIP,
})
if err != nil {
log.Fatal(err)
}
defer conn.Close()
// Startup status
log.WithFields(log.Fields{
"ip": *listenIP,
"port": *listenPort,
}).Infof("Server started")
for i, target := range targets {
log.WithFields(log.Fields{
"num": i + 1,
"total": len(targets),
"addr": target.RemoteAddr(),
}).Info("Forwarding target configured")
}
for {
// Read
b := make([]byte, *bodySize)
n, addr, err := conn.ReadFromUDP(b)
if err != nil {
log.Error(err)
continue
}
// Log receive
ctxLog := log.WithFields(log.Fields{
"source": addr.String(),
"body": string(b[:n]),
})
ctxLog.Debugf("Recieved packet")
// Proxy
for _, target := range targets {
_, err := target.Write(b[:n])
// Log proxy
ctxLog := ctxLog.WithFields(log.Fields{
"target": target.RemoteAddr(),
})
if err != nil {
ctxLog.Warn("Could not forward packet", err)
} else {
ctxLog.Debug("Wrote to target")
}
}
}
}