/
packet_filter.go
60 lines (54 loc) · 1.18 KB
/
packet_filter.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
package game
import (
"strings"
"github.com/rs/zerolog/log"
"github.com/lotusmomo/hk4e-emu/pkg/config"
"github.com/lotusmomo/hk4e-proto/pb"
)
type PacketFilter struct {
config *config.FilterConfig
policy bool
packet map[pb.ProtoMessageType]bool
}
func NewPacketFilter(cfg *config.Config) *PacketFilter {
f := &PacketFilter{
config: &cfg.GameServer.Filter,
policy: true,
packet: make(map[pb.ProtoMessageType]bool),
}
f.init()
return f
}
func (f *PacketFilter) init() {
if f.config == nil {
return
}
for _, rule := range f.config.Rules {
params := strings.Split(rule, ":")
if len(params) != 2 {
continue
}
policy := true
switch params[0] {
case "allow":
policy = false
case "block":
policy = true
}
name := pb.ProtoMessageType(params[1])
if name == "*" {
f.policy = policy
log.Debug().Str("policy", params[0]).Msg("PacketFilter policy updated")
} else {
f.packet[name] = policy
log.Debug().Str("policy", params[0]).Str("packet", string(name)).Msg("PacketFilter packet updated")
}
}
}
func (f *PacketFilter) Map(ctx *Context, name pb.ProtoMessageType) bool {
block, ok := f.packet[name]
if !ok {
return f.policy
}
return block
}