forked from mushorg/glutton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocols.go
95 lines (79 loc) · 2.9 KB
/
protocols.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
package glutton
import (
"context"
"fmt"
"net"
"strings"
"time"
)
// mapProtocolHandlers map protocol handlers to corresponding protocol
func (g *Glutton) mapProtocolHandlers() {
g.protocolHandlers["smtp"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleSMTP(ctx, conn)
}
g.protocolHandlers["rdp"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleRDP(ctx, conn)
}
g.protocolHandlers["smb"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleSMB(ctx, conn)
}
g.protocolHandlers["ftp"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleFTP(ctx, conn)
}
g.protocolHandlers["sip"] = func(ctx context.Context, conn net.Conn) (err error) {
// TODO: remove 'context.TODO()' when handler code start using context.
ctx = context.TODO()
return g.HandleSIP(ctx, conn)
}
g.protocolHandlers["rfb"] = func(ctx context.Context, conn net.Conn) (err error) {
// TODO: remove 'context.TODO()' when handler code start using context.
ctx = context.TODO()
return g.HandleRFB(ctx, conn)
}
g.protocolHandlers["proxy_tcp"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.tcpProxy(ctx, conn)
}
g.protocolHandlers["telnet"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleTelnet(ctx, conn)
}
g.protocolHandlers["proxy_ssh"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.sshProxy.handle(ctx, conn)
}
g.protocolHandlers["jabber"] = func(ctx context.Context, conn net.Conn) (err error) {
return g.HandleJabber(ctx, conn)
}
g.protocolHandlers["default"] = func(ctx context.Context, conn net.Conn) (err error) {
// TODO: remove 'context.TODO()' when handler code start using context.
ctx = context.TODO()
snip, bufConn, err := g.Peek(conn, 4)
g.onErrorClose(err, conn)
httpMap := map[string]bool{"GET ": true, "POST": true, "HEAD": true, "OPTI": true}
if _, ok := httpMap[strings.ToUpper(string(snip))]; ok == true {
return g.HandleHTTP(ctx, bufConn)
}
return g.HandleTCP(ctx, bufConn)
}
}
// closeOnShutdown close all connections before system shutdown
func (g *Glutton) closeOnShutdown(conn net.Conn, done <-chan struct{}) {
select {
case <-g.ctx.Done():
if err := conn.Close(); err != nil {
g.logger.Error(fmt.Sprintf("[glutton ] error: %v", err))
}
return
case <-done:
return
}
}
// Drive child context from parent context with additional value required for sepcific handler
func (g *Glutton) contextWithTimeout(timeInSeconds uint8) context.Context {
limit := time.Duration(timeInSeconds) * time.Second
return context.WithValue(g.ctx, "timeout", time.Now().Add(limit))
}
// updateConnectionTimeout increase connection timeout limit on connection I/O operation
func (g *Glutton) updateConnectionTimeout(ctx context.Context, conn net.Conn) {
if timeout, ok := ctx.Value("timeout").(time.Time); ok {
conn.SetDeadline(timeout)
}
}