-
Notifications
You must be signed in to change notification settings - Fork 1
/
flags.go
125 lines (99 loc) · 2.68 KB
/
flags.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 (
"flag"
"log"
"net"
"strconv"
"strings"
"syscall"
)
// -- uint64 Value
type uint16Value uint16
func newUint16Value(val uint16, p *uint16) *uint16Value {
*p = val
return (*uint16Value)(p)
}
func Uint16Var(p *uint16, name string, value uint16, usage string) {
flag.Var(newUint16Value(value, p), name, usage)
}
func (i *uint16Value) Set(s string) error {
v, err := strconv.ParseUint(s, 0, 16)
*i = uint16Value(v)
return err
}
func (i *uint16Value) Get() any { return uint16(*i) }
func (i *uint16Value) String() string { return strconv.FormatUint(uint64(*i), 10) }
type Flags struct {
KernelBTF string
FilterInterface string
FilterProto string
FilterSrcIP string
FilterDstIP string
FilterSrcPort uint16
FilterDstPort uint16
FilterPort uint16
DropPackage bool
}
type FilterConfig struct {
// Filter l3
FilterSrcIP [4]byte
FilterDstIP [4]byte
// Filter l4
FilterProto uint8
FilterSrcPort uint16
FilterDstPort uint16
FilterPort uint16
IsDrop byte
}
func SetFlags() *Flags {
f := &Flags{}
flag.StringVar(&f.KernelBTF, "kernel-btf", "", "specify kernel BTF file")
flag.StringVar(&f.FilterInterface, "filter-if", "", "filter net interface")
flag.StringVar(&f.FilterProto, "filter-proto", "", "filter L4 protocol (tcp, udp, icmp)")
flag.StringVar(&f.FilterSrcIP, "filter-src-ip", "", "filter source IP addr")
flag.StringVar(&f.FilterDstIP, "filter-dst-ip", "", "filter destination IP addr")
Uint16Var(&f.FilterSrcPort, "filter-src-port", 0, "filter source port")
Uint16Var(&f.FilterDstPort, "filter-dst-port", 0, "filter destination port")
Uint16Var(&f.FilterPort, "filter-port", 0, "filter either destination or source port")
flag.BoolVar(&f.DropPackage, "drop-skb", false, "drop filtered skb")
return f
}
func GetConfig(flags *Flags) FilterConfig {
cfg := FilterConfig{}
if flags.FilterPort > 0 {
cfg.FilterPort = flags.FilterPort
} else {
if flags.FilterSrcPort > 0 {
cfg.FilterSrcPort = flags.FilterSrcPort
}
if flags.FilterDstPort > 0 {
cfg.FilterDstPort = flags.FilterDstPort
}
}
switch strings.ToLower(flags.FilterProto) {
case "tcp":
cfg.FilterProto = syscall.IPPROTO_TCP
case "udp":
cfg.FilterProto = syscall.IPPROTO_UDP
case "icmp":
cfg.FilterProto = syscall.IPPROTO_ICMP
}
if flags.FilterDstIP != "" {
ip := net.ParseIP(flags.FilterDstIP)
if ip == nil {
log.Fatalf("Failed to parse --filter-dst-ip")
}
copy(cfg.FilterDstIP[:], ip.To4()[:])
}
if flags.FilterSrcIP != "" {
ip := net.ParseIP(flags.FilterSrcIP)
if ip == nil {
log.Fatalf("Failed to parse --filter-src-ip")
}
copy(cfg.FilterSrcIP[:], ip.To4()[:])
}
if flags.DropPackage {
cfg.IsDrop = 1
}
return cfg
}