forked from Kuingsmile/clash-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipset.go
57 lines (46 loc) · 1.01 KB
/
ipset.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
package rules
import (
"github.com/Dreamacro/clash/component/ipset"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
)
// Implements C.Rule
var _ C.Rule = (*IPSet)(nil)
type IPSet struct {
name string
adapter string
noResolveIP bool
}
func (f *IPSet) RuleType() C.RuleType {
return C.IPSet
}
func (f *IPSet) Match(metadata *C.Metadata) bool {
exist, err := ipset.Test(f.name, metadata.DstIP)
if err != nil {
log.Warnln("check ipset '%s' failed: %s", f.name, err.Error())
return false
}
return exist
}
func (f *IPSet) Adapter() string {
return f.adapter
}
func (f *IPSet) Payload() string {
return f.name
}
func (f *IPSet) ShouldResolveIP() bool {
return !f.noResolveIP
}
func (f *IPSet) ShouldFindProcess() bool {
return false
}
func NewIPSet(name string, adapter string, noResolveIP bool) (*IPSet, error) {
if err := ipset.Verify(name); err != nil {
return nil, err
}
return &IPSet{
name: name,
adapter: adapter,
noResolveIP: noResolveIP,
}, nil
}