-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
glutton.go
380 lines (332 loc) · 10.6 KB
/
glutton.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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package glutton
import (
"bytes"
"context"
_ "embed"
"fmt"
"io"
"log/slog"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"time"
"github.com/mushorg/glutton/connection"
"github.com/mushorg/glutton/producer"
"github.com/mushorg/glutton/protocols"
"github.com/mushorg/glutton/rules"
"github.com/google/uuid"
"github.com/seud0nym/tproxy-go/tproxy"
"github.com/spf13/viper"
)
// Glutton struct
type Glutton struct {
id uuid.UUID
Logger *slog.Logger
Server *Server
rules rules.Rules
Producer *producer.Producer
connTable *connection.ConnTable
tcpProtocolHandlers map[string]protocols.TCPHandlerFunc
udpProtocolHandlers map[string]protocols.UDPHandlerFunc
ctx context.Context
cancel context.CancelFunc
publicAddrs []net.IP
}
//go:embed config/rules.yaml
var defaultRules []byte
func (g *Glutton) initConfig() error {
viper.SetConfigName("config")
viper.AddConfigPath(viper.GetString("confpath"))
if _, err := os.Stat(viper.GetString("confpath")); !os.IsNotExist(err) {
if err := viper.ReadInConfig(); err != nil {
return err
}
}
// If no config is found, use the defaults
viper.SetDefault("ports.tcp", 5000)
viper.SetDefault("ports.udp", 5001)
viper.SetDefault("ports.ssh", 22)
viper.SetDefault("max_tcp_payload", 4096)
viper.SetDefault("conn_timeout", 45)
viper.SetDefault("rules_path", "rules/rules.yaml")
g.Logger.Debug("configuration set successfully", slog.String("reporter", "glutton"))
return nil
}
// New creates a new Glutton instance
func New(ctx context.Context) (*Glutton, error) {
g := &Glutton{
tcpProtocolHandlers: make(map[string]protocols.TCPHandlerFunc),
udpProtocolHandlers: make(map[string]protocols.UDPHandlerFunc),
connTable: connection.New(),
}
g.ctx, g.cancel = context.WithCancel(ctx)
if err := g.makeID(); err != nil {
return nil, err
}
g.Logger = producer.NewLogger(g.id.String())
// Loading the configuration
g.Logger.Info("Loading configurations from: config/config.yaml", slog.String("reporter", "glutton"))
if err := g.initConfig(); err != nil {
return nil, err
}
var rulesFile io.ReadCloser
rulesPath := viper.GetString("rules_path")
if _, err := os.Stat(rulesPath); !os.IsNotExist(err) {
rulesFile, err = os.Open(rulesPath)
if err != nil {
return nil, err
}
defer rulesFile.Close()
} else {
g.Logger.Warn("No rules file found, using default rules", slog.String("reporter", "glutton"))
rulesFile = io.NopCloser(bytes.NewBuffer(defaultRules))
}
var err error
g.rules, err = rules.Init(rulesFile)
if err != nil {
return nil, err
}
return g, nil
}
// Init initializes server and handles
func (g *Glutton) Init() error {
var err error
g.publicAddrs, err = getNonLoopbackIPs(viper.GetString("interface"))
if err != nil {
return err
}
for _, sIP := range viper.GetStringSlice("addresses") {
if ip := net.ParseIP(sIP); ip != nil {
g.publicAddrs = append(g.publicAddrs, ip)
}
}
// Start the Glutton server
tcpServerPort := uint(viper.GetInt("ports.tcp"))
udpServerPort := uint(viper.GetInt("ports.udp"))
g.Server = NewServer(tcpServerPort, udpServerPort)
if err := g.Server.Start(); err != nil {
return err
}
// Initiating log producers
if viper.GetBool("producers.enabled") {
g.Producer, err = producer.New(g.id.String())
if err != nil {
return err
}
}
// Initiating protocol handlers
g.tcpProtocolHandlers = protocols.MapTCPProtocolHandlers(g.Logger, g)
g.udpProtocolHandlers = protocols.MapUDPProtocolHandlers(g.Logger, g)
return nil
}
func (g *Glutton) udpListen(wg *sync.WaitGroup) {
defer func() {
wg.Done()
}()
buffer := make([]byte, 1024)
for {
select {
case <-g.ctx.Done():
if err := g.Server.udpConn.Close(); err != nil {
g.Logger.Error("Failed to close UDP listener", producer.ErrAttr(err))
}
return
default:
}
g.Server.udpConn.SetReadDeadline(time.Now().Add(1 * time.Second))
n, srcAddr, dstAddr, err := tproxy.ReadFromUDP(g.Server.udpConn, buffer)
if err != nil {
if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
continue
}
g.Logger.Error("Failed to read UDP packet", producer.ErrAttr(err))
}
rule, err := g.applyRules("udp", srcAddr, dstAddr)
if err != nil {
g.Logger.Error("Failed to apply rules", producer.ErrAttr(err))
}
if rule == nil {
rule = &rules.Rule{Target: "udp"}
}
md, err := g.connTable.Register(srcAddr.IP.String(), strconv.Itoa(int(srcAddr.AddrPort().Port())), dstAddr.AddrPort().Port(), rule)
if err != nil {
g.Logger.Error("Failed to register UDP packet", producer.ErrAttr(err))
}
if hfunc, ok := g.udpProtocolHandlers[rule.Target]; ok {
data := buffer[:n]
go func() {
if err := hfunc(g.ctx, srcAddr, dstAddr, data, md); err != nil {
g.Logger.Error("Failed to handle UDP payload", producer.ErrAttr(err))
}
}()
}
}
}
func (g *Glutton) tcpListen() {
for {
select {
case <-g.ctx.Done():
if err := g.Server.tcpListener.Close(); err != nil {
g.Logger.Error("Failed to close TCP listener", producer.ErrAttr(err))
}
return
default:
}
conn, err := g.Server.tcpListener.Accept()
if err != nil {
g.Logger.Error("Failed to accept connection", producer.ErrAttr(err))
continue
}
rule, err := g.applyRulesOnConn(conn)
if err != nil {
g.Logger.Error("Failed to apply rules", producer.ErrAttr(err))
continue
}
if rule == nil {
rule = &rules.Rule{Target: "default"}
}
md, err := g.connTable.RegisterConn(conn, rule)
if err != nil {
g.Logger.Error("Failed to register connection", producer.ErrAttr(err))
continue
}
g.Logger.Debug("new connection", slog.String("addr", conn.LocalAddr().String()), slog.String("handler", rule.Target))
g.ctx = context.WithValue(g.ctx, ctxTimeout("timeout"), int64(viper.GetInt("conn_timeout")))
if err := g.UpdateConnectionTimeout(g.ctx, conn); err != nil {
g.Logger.Error("Failed to set connection timeout", producer.ErrAttr(err))
}
if hfunc, ok := g.tcpProtocolHandlers[rule.Target]; ok {
go func() {
if err := hfunc(g.ctx, conn, md); err != nil {
g.Logger.Error("Failed to handle TCP connection", producer.ErrAttr(err), slog.String("handler", rule.Target))
}
}()
}
}
}
// Start the listener, this blocks for new connections
func (g *Glutton) Start() error {
g.startMonitor()
sshPort := viper.GetUint32("ports.ssh")
if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), sshPort); err != nil {
return err
}
if err := setTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), sshPort); err != nil {
return err
}
wg := &sync.WaitGroup{}
wg.Add(1)
go g.udpListen(wg)
go g.tcpListen()
wg.Wait()
return nil
}
func (g *Glutton) makeID() error {
filePath := filepath.Join(viper.GetString("var-dir"), "glutton.id")
if err := os.MkdirAll(viper.GetString("var-dir"), 0744); err != nil {
return fmt.Errorf("failed to create var-dir: %w", err)
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
g.id = uuid.New()
data, err := g.id.MarshalBinary()
if err != nil {
return fmt.Errorf("failed to marshal UUID: %w", err)
}
if err := os.WriteFile(filePath, data, 0744); err != nil {
return fmt.Errorf("failed to create new PID file: %w", err)
}
} else {
if err != nil {
return fmt.Errorf("failed to access PID file: %w", err)
}
f, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open PID file: %w", err)
}
buff, err := io.ReadAll(f)
if err != nil {
return fmt.Errorf("failed to read PID file: %w", err)
}
g.id, err = uuid.FromBytes(buff)
if err != nil {
return fmt.Errorf("failed to create UUID from PID filed content: %w", err)
}
}
return nil
}
type ctxTimeout string
// UpdateConnectionTimeout increase connection timeout limit on connection I/O operation
func (g *Glutton) UpdateConnectionTimeout(ctx context.Context, conn net.Conn) error {
if timeout, ok := ctx.Value(ctxTimeout("timeout")).(int64); ok {
if err := conn.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil {
return err
}
}
return nil
}
// ConnectionByFlow returns connection metadata by connection key
func (g *Glutton) ConnectionByFlow(ckey [2]uint64) connection.Metadata {
return g.connTable.Get(ckey)
}
// MetadataByConnection returns connection metadata by connection
func (g *Glutton) MetadataByConnection(conn net.Conn) (connection.Metadata, error) {
host, port, err := net.SplitHostPort(conn.RemoteAddr().String())
if err != nil {
return connection.Metadata{}, fmt.Errorf("faild to split remote address: %w", err)
}
ckey, err := connection.NewConnKeyByString(host, port)
if err != nil {
return connection.Metadata{}, err
}
md := g.ConnectionByFlow(ckey)
return md, nil
}
func (g *Glutton) sanitizePayload(payload []byte) []byte {
for _, ip := range g.publicAddrs {
payload = []byte(strings.ReplaceAll(string(payload), ip.String(), "1.2.3.4"))
}
return payload
}
func (g *Glutton) ProduceTCP(handler string, conn net.Conn, md connection.Metadata, payload []byte, decoded interface{}) error {
if g.Producer != nil {
payload = g.sanitizePayload(payload)
return g.Producer.LogTCP(handler, conn, md, payload, decoded)
}
return nil
}
func (g *Glutton) ProduceUDP(handler string, srcAddr, dstAddr *net.UDPAddr, md connection.Metadata, payload []byte, decoded interface{}) error {
if g.Producer != nil {
payload = g.sanitizePayload(payload)
return g.Producer.LogUDP("udp", srcAddr, md, payload, decoded)
}
return nil
}
// Shutdown the packet processor
func (g *Glutton) Shutdown() {
g.cancel() // close all connection
g.Logger.Info("Flushing TCP iptables")
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "tcp", uint32(g.Server.tcpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
g.Logger.Error("Failed to drop tcp iptables", producer.ErrAttr(err))
}
g.Logger.Info("Flushing UDP iptables")
if err := flushTProxyIPTables(viper.GetString("interface"), g.publicAddrs[0].String(), "udp", uint32(g.Server.udpPort), uint32(viper.GetInt("ports.ssh"))); err != nil {
g.Logger.Error("Failed to drop udp iptables", producer.ErrAttr(err))
}
g.Logger.Info("All done")
}
func (g *Glutton) applyRules(network string, srcAddr, dstAddr net.Addr) (*rules.Rule, error) {
match, err := g.rules.Match(network, srcAddr, dstAddr)
if err != nil {
return nil, err
}
if match != nil {
return match, err
}
return nil, nil
}
func (g *Glutton) applyRulesOnConn(conn net.Conn) (*rules.Rule, error) {
return g.applyRules("tcp", conn.RemoteAddr(), conn.LocalAddr())
}