Skip to content

Commit f6164ac

Browse files
committed
feat: add fake-ip-filter-mode in dns
#1479
1 parent 08ac9a3 commit f6164ac

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

component/fakeip/pool.go

+11
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Pool struct {
3636
cycle bool
3737
mux sync.Mutex
3838
host []C.DomainMatcher
39+
mode C.FilterMode
3940
ipnet netip.Prefix
4041
store store
4142
}
@@ -66,6 +67,14 @@ func (p *Pool) LookBack(ip netip.Addr) (string, bool) {
6667

6768
// ShouldSkipped return if domain should be skipped
6869
func (p *Pool) ShouldSkipped(domain string) bool {
70+
should := p.shouldSkipped(domain)
71+
if p.mode == C.FilterWhiteList {
72+
return !should
73+
}
74+
return should
75+
}
76+
77+
func (p *Pool) shouldSkipped(domain string) bool {
6978
for _, matcher := range p.host {
7079
if matcher.MatchDomain(domain) {
7180
return true
@@ -157,6 +166,7 @@ func (p *Pool) restoreState() {
157166
type Options struct {
158167
IPNet netip.Prefix
159168
Host []C.DomainMatcher
169+
Mode C.FilterMode
160170

161171
// Size sets the maximum number of entries in memory
162172
// and does not work if Persistence is true
@@ -187,6 +197,7 @@ func New(options Options) (*Pool, error) {
187197
offset: first.Prev(),
188198
cycle: false,
189199
host: options.Host,
200+
mode: options.Mode,
190201
ipnet: options.IPNet,
191202
}
192203
if options.Persistence {

component/fakeip/pool_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ func TestPool_Skip(t *testing.T) {
164164
for _, pool := range pools {
165165
assert.True(t, pool.ShouldSkipped("example.com"))
166166
assert.False(t, pool.ShouldSkipped("foo.com"))
167+
assert.False(t, pool.shouldSkipped("baz.com"))
168+
}
169+
}
170+
171+
func TestPool_SkipWhiteList(t *testing.T) {
172+
ipnet := netip.MustParsePrefix("192.168.0.1/29")
173+
tree := trie.New[struct{}]()
174+
assert.NoError(t, tree.Insert("example.com", struct{}{}))
175+
assert.False(t, tree.IsEmpty())
176+
pools, tempfile, err := createPools(Options{
177+
IPNet: ipnet,
178+
Size: 10,
179+
Host: []C.DomainMatcher{tree.NewDomainSet()},
180+
Mode: C.FilterWhiteList,
181+
})
182+
assert.Nil(t, err)
183+
defer os.Remove(tempfile)
184+
185+
for _, pool := range pools {
186+
assert.False(t, pool.ShouldSkipped("example.com"))
187+
assert.True(t, pool.ShouldSkipped("foo.com"))
188+
assert.True(t, pool.ShouldSkipped("baz.com"))
167189
}
168190
}
169191

config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ type RawDNS struct {
205205
EnhancedMode C.DNSMode `yaml:"enhanced-mode" json:"enhanced-mode"`
206206
FakeIPRange string `yaml:"fake-ip-range" json:"fake-ip-range"`
207207
FakeIPFilter []string `yaml:"fake-ip-filter" json:"fake-ip-filter"`
208+
FakeIPFilterMode C.FilterMode `yaml:"fake-ip-filter-mode" json:"fake-ip-filter-mode"`
208209
DefaultNameserver []string `yaml:"default-nameserver" json:"default-nameserver"`
209210
CacheAlgorithm string `yaml:"cache-algorithm" json:"cache-algorithm"`
210211
NameServerPolicy *orderedmap.OrderedMap[string, any] `yaml:"nameserver-policy" json:"nameserver-policy"`
@@ -474,6 +475,7 @@ func DefaultRawConfig() *RawConfig {
474475
"www.msftnsci.com",
475476
"www.msftconnecttest.com",
476477
},
478+
FakeIPFilterMode: C.FilterBlackList,
477479
},
478480
NTP: RawNTP{
479481
Enable: false,
@@ -1458,6 +1460,7 @@ func parseDNS(rawCfg *RawConfig, hosts *trie.DomainTrie[resolver.HostValue], rul
14581460
IPNet: fakeIPRange,
14591461
Size: 1000,
14601462
Host: host,
1463+
Mode: cfg.FakeIPFilterMode,
14611464
Persistence: rawCfg.Profile.StoreFakeIP,
14621465
})
14631466
if err != nil {

constant/dns.go

+61-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ func (e DNSMode) MarshalYAML() (any, error) {
4343
// UnmarshalJSON unserialize EnhancedMode with json
4444
func (e *DNSMode) UnmarshalJSON(data []byte) error {
4545
var tp string
46-
json.Unmarshal(data, &tp)
46+
if err := json.Unmarshal(data, &tp); err != nil {
47+
return err
48+
}
4749
mode, exist := DNSModeMapping[tp]
4850
if !exist {
4951
return errors.New("invalid mode")
@@ -115,6 +117,64 @@ func NewDNSPrefer(prefer string) DNSPrefer {
115117
}
116118
}
117119

120+
// FilterModeMapping is a mapping for FilterMode enum
121+
var FilterModeMapping = map[string]FilterMode{
122+
FilterBlackList.String(): FilterBlackList,
123+
FilterWhiteList.String(): FilterWhiteList,
124+
}
125+
126+
type FilterMode int
127+
128+
const (
129+
FilterBlackList FilterMode = iota
130+
FilterWhiteList
131+
)
132+
133+
func (e FilterMode) String() string {
134+
switch e {
135+
case FilterBlackList:
136+
return "blacklist"
137+
case FilterWhiteList:
138+
return "whitelist"
139+
default:
140+
return "unknown"
141+
}
142+
}
143+
144+
func (e FilterMode) MarshalYAML() (interface{}, error) {
145+
return e.String(), nil
146+
}
147+
148+
func (e *FilterMode) UnmarshalYAML(unmarshal func(interface{}) error) error {
149+
var tp string
150+
if err := unmarshal(&tp); err != nil {
151+
return err
152+
}
153+
mode, exist := FilterModeMapping[tp]
154+
if !exist {
155+
return errors.New("invalid mode")
156+
}
157+
*e = mode
158+
return nil
159+
}
160+
161+
func (e FilterMode) MarshalJSON() ([]byte, error) {
162+
return json.Marshal(e.String())
163+
}
164+
165+
func (e *FilterMode) UnmarshalJSON(data []byte) error {
166+
var tp string
167+
if err := json.Unmarshal(data, &tp); err != nil {
168+
return err
169+
}
170+
mode, exist := FilterModeMapping[tp]
171+
if !exist {
172+
return errors.New("invalid mode")
173+
}
174+
*e = mode
175+
return nil
176+
}
177+
118178
type HTTPVersion string
119179

120180
const (

docs/config.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ dns:
249249
- rule-set:fakeip-filter
250250
# fakeip-filter 为 geosite 中名为 fakeip-filter 的分类(需要自行保证该分类存在)
251251
- geosite:fakeip-filter
252+
# 配置fake-ip-filter的匹配模式,默认为blacklist,即如果匹配成功不返回fake-ip
253+
# 可设置为whitelist,即只有匹配成功才返回fake-ip
254+
fake-ip-filter-mode: blacklist
252255

253256
# use-hosts: true # 查询 hosts
254257

0 commit comments

Comments
 (0)