-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipcidr.go
70 lines (57 loc) · 1.1 KB
/
ipcidr.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 rules
import (
"net"
"net/netip"
)
type IPCIDR struct {
ipnet *net.IPNet
adapter string
isSourceIP bool
noResolveIP bool
}
func (p *IPCIDR) Clash() (string, bool) {
return "IP-CIDR", true
}
func (p *IPCIDR) QuanX() (string, bool) {
if netip.MustParsePrefix(p.Payload()).Addr().Is6() {
return "IP6-CIDR", true
} else {
return "IP-CIDR", true
}
}
func (i *IPCIDR) RuleType() RuleType {
if i.isSourceIP {
return RuleTypeSrcIPCIDR
}
return RuleTypeIPCIDR
}
func (i *IPCIDR) Match(metadata *Metadata) bool {
ip := metadata.DstIP
if i.isSourceIP {
ip = metadata.SrcIP
}
return ip != nil && i.ipnet.Contains(ip)
}
func (i *IPCIDR) Adapter() string {
return i.adapter
}
func (i *IPCIDR) Payload() string {
return i.ipnet.String()
}
func (i *IPCIDR) ShouldResolveIP() bool {
return !i.noResolveIP
}
func (i *IPCIDR) ShouldFindProcess() bool {
return false
}
func NewIPCIDR(s string, adapter string) (*IPCIDR, error) {
_, ipnet, err := net.ParseCIDR(s)
if err != nil {
return nil, err
}
ipcidr := &IPCIDR{
ipnet: ipnet,
adapter: adapter,
}
return ipcidr, nil
}