-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsniff.go
70 lines (62 loc) · 1.76 KB
/
sniff.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
package protocol
import (
"github.com/layou233/zbproxy/v3/adapter"
"github.com/layou233/zbproxy/v3/common/bufio"
"github.com/layou233/zbproxy/v3/protocol/minecraft"
"github.com/phuslu/log"
)
const (
SniffTypeUndefined = ""
SniffTypeAll = "all"
SniffTypeMinecraft = "minecraft"
SniffTypeTLS = "tls"
)
type SnifferFunc = func(logger *log.Logger, conn bufio.PeekConn, metadata *adapter.Metadata) error
func Sniff(logger *log.Logger, conn bufio.PeekConn, metadata *adapter.Metadata, registry map[string]SnifferFunc, protocols ...string) {
startPosition := conn.CurrentPosition()
if startPosition < 0 {
startPosition = 0
}
for _, protocol := range protocols {
var err error
sniffAll := protocol == SniffTypeAll
switch protocol {
case SniffTypeAll:
fallthrough
case SniffTypeMinecraft:
if metadata.Minecraft == nil {
err = minecraft.SniffClientHandshake(conn, metadata)
if err != nil {
logger.Trace().Str("protocol", protocol).Err(err).Msg("Sniff error")
}
}
if !sniffAll {
break
}
fallthrough
//case SniffTypeTLS: // TODO
default:
if sniffAll {
for _, snifferFunc := range registry {
err = snifferFunc(logger, conn, metadata)
if err != nil {
logger.Trace().Str("protocol", protocol).Err(err).Msg("Sniff error")
}
}
return
} else if len(registry) > 0 {
if snifferFunc := registry[protocol]; snifferFunc != nil {
err = snifferFunc(logger, conn, metadata)
if err != nil {
logger.Trace().Str("protocol", protocol).Err(err).Msg("Sniff error")
}
} else {
logger.Fatal().Str("protocol", protocol).Msg("Unsupported protocol")
}
} else {
logger.Fatal().Str("protocol", protocol).Msg("Unsupported protocol")
}
}
conn.Rewind(startPosition)
}
}