From 089a14d8b0804be61d9cad0755d2d30c40a1a85a Mon Sep 17 00:00:00 2001 From: Fndroid <18825176954@163.com> Date: Fri, 19 Apr 2019 23:50:09 +0800 Subject: [PATCH] add a mix matcher (#153) --- core/config/config.go | 3 ++ core/matcher/mix/list.go | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 core/matcher/mix/list.go diff --git a/core/config/config.go b/core/config/config.go index 2c4aa2a..8390d82 100644 --- a/core/config/config.go +++ b/core/config/config.go @@ -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" @@ -170,6 +171,8 @@ func getDomainMatcher(name string) (m matcher.Matcher) { return &full.List{DataList: []string{}} case "regex-list": return ®ex.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 ®ex.List{RegexList: []string{}} diff --git a/core/matcher/mix/list.go b/core/matcher/mix/list.go new file mode 100644 index 0000000..bca94bb --- /dev/null +++ b/core/matcher/mix/list.go @@ -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" +}