Skip to content

Commit

Permalink
add a mix matcher (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
Fndroid authored and shawn1m committed Apr 19, 2019
1 parent 8cfc9e9 commit 089a14d
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/shawn1m/overture/core/matcher/full"
"github.com/shawn1m/overture/core/matcher/regex"
"github.com/shawn1m/overture/core/matcher/suffix"
"github.com/shawn1m/overture/core/matcher/mix"

"io/ioutil"
"net"
Expand Down Expand Up @@ -170,6 +171,8 @@ func getDomainMatcher(name string) (m matcher.Matcher) {
return &full.List{DataList: []string{}}
case "regex-list":
return &regex.List{RegexList: []string{}}
case "mix-list":
return &mix.List{DataList: make([]mix.Data, 0)}
default:
log.Warn("There is no such matcher: "+name, ", use regex-list matcher as default")
return &regex.List{RegexList: []string{}}
Expand Down
73 changes: 73 additions & 0 deletions core/matcher/mix/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2019 shawn1m. All rights reserved.
* Use of this source code is governed by The MIT License (MIT) that can be
* found in the LICENSE file..
*/

package mix

import (
"errors"
"regexp"
"strings"
)

type Data struct {
Type string
Content string
}

type List struct {
DataList []Data
}

func (s *List) Insert(str string) error {
kv := strings.Split(str, ":")
if len(kv) > 2 {
return errors.New("Invalid format: " + str)
}
if len(kv) == 1 {
s.DataList = append(s.DataList,
Data{
Type: "domain",
Content: strings.ToLower(kv[0])})
}
if len(kv) == 2 {
s.DataList = append(s.DataList,
Data{
Type: strings.ToLower(kv[0]),
Content: strings.ToLower(kv[1])})
}

return nil
}

func (s *List) Has(str string) bool {
for _, data := range s.DataList {
switch data.Type {
case "domain":
idx := len(str) - len(data.Content)
if idx > 0 && data.Content == str[idx:] {
return true
}
case "regex":
reg := regexp.MustCompile(data.Content)
if reg.MatchString(str) {
return true
}
case "keyword":
if strings.Contains(str, data.Content) {
return true
}
case "full":
if data.Content == str {
return true
}
}
}
return false
}

func (s *List) Name() string {
return "mix-list"
}

0 comments on commit 089a14d

Please sign in to comment.