Skip to content

Commit 089a14d

Browse files
Fndroidshawn1m
Fndroid
authored andcommitted
add a mix matcher (#153)
1 parent 8cfc9e9 commit 089a14d

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

core/config/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/shawn1m/overture/core/matcher/full"
1313
"github.com/shawn1m/overture/core/matcher/regex"
1414
"github.com/shawn1m/overture/core/matcher/suffix"
15+
"github.com/shawn1m/overture/core/matcher/mix"
1516

1617
"io/ioutil"
1718
"net"
@@ -170,6 +171,8 @@ func getDomainMatcher(name string) (m matcher.Matcher) {
170171
return &full.List{DataList: []string{}}
171172
case "regex-list":
172173
return &regex.List{RegexList: []string{}}
174+
case "mix-list":
175+
return &mix.List{DataList: make([]mix.Data, 0)}
173176
default:
174177
log.Warn("There is no such matcher: "+name, ", use regex-list matcher as default")
175178
return &regex.List{RegexList: []string{}}

core/matcher/mix/list.go

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2019 shawn1m. All rights reserved.
3+
* Use of this source code is governed by The MIT License (MIT) that can be
4+
* found in the LICENSE file..
5+
*/
6+
7+
package mix
8+
9+
import (
10+
"errors"
11+
"regexp"
12+
"strings"
13+
)
14+
15+
type Data struct {
16+
Type string
17+
Content string
18+
}
19+
20+
type List struct {
21+
DataList []Data
22+
}
23+
24+
func (s *List) Insert(str string) error {
25+
kv := strings.Split(str, ":")
26+
if len(kv) > 2 {
27+
return errors.New("Invalid format: " + str)
28+
}
29+
if len(kv) == 1 {
30+
s.DataList = append(s.DataList,
31+
Data{
32+
Type: "domain",
33+
Content: strings.ToLower(kv[0])})
34+
}
35+
if len(kv) == 2 {
36+
s.DataList = append(s.DataList,
37+
Data{
38+
Type: strings.ToLower(kv[0]),
39+
Content: strings.ToLower(kv[1])})
40+
}
41+
42+
return nil
43+
}
44+
45+
func (s *List) Has(str string) bool {
46+
for _, data := range s.DataList {
47+
switch data.Type {
48+
case "domain":
49+
idx := len(str) - len(data.Content)
50+
if idx > 0 && data.Content == str[idx:] {
51+
return true
52+
}
53+
case "regex":
54+
reg := regexp.MustCompile(data.Content)
55+
if reg.MatchString(str) {
56+
return true
57+
}
58+
case "keyword":
59+
if strings.Contains(str, data.Content) {
60+
return true
61+
}
62+
case "full":
63+
if data.Content == str {
64+
return true
65+
}
66+
}
67+
}
68+
return false
69+
}
70+
71+
func (s *List) Name() string {
72+
return "mix-list"
73+
}

0 commit comments

Comments
 (0)