mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
v1.0.0
This commit is contained in:
parent
47d23e9972
commit
c7f7c08ead
711 changed files with 82154 additions and 2 deletions
49
common/strmatcher/benchmark_test.go
Normal file
49
common/strmatcher/benchmark_test.go
Normal file
|
@ -0,0 +1,49 @@
|
|||
package strmatcher_test
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
. "github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
)
|
||||
|
||||
func BenchmarkDomainMatcherGroup(b *testing.B) {
|
||||
g := new(DomainMatcherGroup)
|
||||
|
||||
for i := 1; i <= 1024; i++ {
|
||||
g.Add(strconv.Itoa(i)+".example.com", uint32(i))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = g.Match("0.example.com")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkFullMatcherGroup(b *testing.B) {
|
||||
g := new(FullMatcherGroup)
|
||||
|
||||
for i := 1; i <= 1024; i++ {
|
||||
g.Add(strconv.Itoa(i)+".example.com", uint32(i))
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = g.Match("0.example.com")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMarchGroup(b *testing.B) {
|
||||
g := new(MatcherGroup)
|
||||
for i := 1; i <= 1024; i++ {
|
||||
m, err := Domain.New(strconv.Itoa(i) + ".example.com")
|
||||
common.Must(err)
|
||||
g.Add(m)
|
||||
}
|
||||
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = g.Match("0.example.com")
|
||||
}
|
||||
}
|
98
common/strmatcher/domain_matcher.go
Normal file
98
common/strmatcher/domain_matcher.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package strmatcher
|
||||
|
||||
import "strings"
|
||||
|
||||
func breakDomain(domain string) []string {
|
||||
return strings.Split(domain, ".")
|
||||
}
|
||||
|
||||
type node struct {
|
||||
values []uint32
|
||||
sub map[string]*node
|
||||
}
|
||||
|
||||
// DomainMatcherGroup is a IndexMatcher for a large set of Domain matchers.
|
||||
// Visible for testing only.
|
||||
type DomainMatcherGroup struct {
|
||||
root *node
|
||||
}
|
||||
|
||||
func (g *DomainMatcherGroup) Add(domain string, value uint32) {
|
||||
if g.root == nil {
|
||||
g.root = new(node)
|
||||
}
|
||||
|
||||
current := g.root
|
||||
parts := breakDomain(domain)
|
||||
for i := len(parts) - 1; i >= 0; i-- {
|
||||
part := parts[i]
|
||||
if current.sub == nil {
|
||||
current.sub = make(map[string]*node)
|
||||
}
|
||||
next := current.sub[part]
|
||||
if next == nil {
|
||||
next = new(node)
|
||||
current.sub[part] = next
|
||||
}
|
||||
current = next
|
||||
}
|
||||
|
||||
current.values = append(current.values, value)
|
||||
}
|
||||
|
||||
func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) {
|
||||
g.Add(string(m), value)
|
||||
}
|
||||
|
||||
func (g *DomainMatcherGroup) Match(domain string) []uint32 {
|
||||
if domain == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
current := g.root
|
||||
if current == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
nextPart := func(idx int) int {
|
||||
for i := idx - 1; i >= 0; i-- {
|
||||
if domain[i] == '.' {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
matches := [][]uint32{}
|
||||
idx := len(domain)
|
||||
for {
|
||||
if idx == -1 || current.sub == nil {
|
||||
break
|
||||
}
|
||||
|
||||
nidx := nextPart(idx)
|
||||
part := domain[nidx+1 : idx]
|
||||
next := current.sub[part]
|
||||
if next == nil {
|
||||
break
|
||||
}
|
||||
current = next
|
||||
idx = nidx
|
||||
if len(current.values) > 0 {
|
||||
matches = append(matches, current.values)
|
||||
}
|
||||
}
|
||||
switch len(matches) {
|
||||
case 0:
|
||||
return nil
|
||||
case 1:
|
||||
return matches[0]
|
||||
default:
|
||||
result := []uint32{}
|
||||
for idx := range matches {
|
||||
// Insert reversely, the subdomain that matches further ranks higher
|
||||
result = append(result, matches[len(matches)-1-idx]...)
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
76
common/strmatcher/domain_matcher_test.go
Normal file
76
common/strmatcher/domain_matcher_test.go
Normal file
|
@ -0,0 +1,76 @@
|
|||
package strmatcher_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
)
|
||||
|
||||
func TestDomainMatcherGroup(t *testing.T) {
|
||||
g := new(DomainMatcherGroup)
|
||||
g.Add("example.com", 1)
|
||||
g.Add("google.com", 2)
|
||||
g.Add("x.a.com", 3)
|
||||
g.Add("a.b.com", 4)
|
||||
g.Add("c.a.b.com", 5)
|
||||
g.Add("x.y.com", 4)
|
||||
g.Add("x.y.com", 6)
|
||||
|
||||
testCases := []struct {
|
||||
Domain string
|
||||
Result []uint32
|
||||
}{
|
||||
{
|
||||
Domain: "x.example.com",
|
||||
Result: []uint32{1},
|
||||
},
|
||||
{
|
||||
Domain: "y.com",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: "a.b.com",
|
||||
Result: []uint32{4},
|
||||
},
|
||||
{ // Matches [c.a.b.com, a.b.com]
|
||||
Domain: "c.a.b.com",
|
||||
Result: []uint32{5, 4},
|
||||
},
|
||||
{
|
||||
Domain: "c.a..b.com",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: ".com",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: "com",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: "",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: "x.y.com",
|
||||
Result: []uint32{4, 6},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
r := g.Match(testCase.Domain)
|
||||
if !reflect.DeepEqual(r, testCase.Result) {
|
||||
t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyDomainMatcherGroup(t *testing.T) {
|
||||
g := new(DomainMatcherGroup)
|
||||
r := g.Match("example.com")
|
||||
if len(r) != 0 {
|
||||
t.Error("Expect [], but ", r)
|
||||
}
|
||||
}
|
25
common/strmatcher/full_matcher.go
Normal file
25
common/strmatcher/full_matcher.go
Normal file
|
@ -0,0 +1,25 @@
|
|||
package strmatcher
|
||||
|
||||
type FullMatcherGroup struct {
|
||||
matchers map[string][]uint32
|
||||
}
|
||||
|
||||
func (g *FullMatcherGroup) Add(domain string, value uint32) {
|
||||
if g.matchers == nil {
|
||||
g.matchers = make(map[string][]uint32)
|
||||
}
|
||||
|
||||
g.matchers[domain] = append(g.matchers[domain], value)
|
||||
}
|
||||
|
||||
func (g *FullMatcherGroup) addMatcher(m fullMatcher, value uint32) {
|
||||
g.Add(string(m), value)
|
||||
}
|
||||
|
||||
func (g *FullMatcherGroup) Match(str string) []uint32 {
|
||||
if g.matchers == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return g.matchers[str]
|
||||
}
|
50
common/strmatcher/full_matcher_test.go
Normal file
50
common/strmatcher/full_matcher_test.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
package strmatcher_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
. "github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
)
|
||||
|
||||
func TestFullMatcherGroup(t *testing.T) {
|
||||
g := new(FullMatcherGroup)
|
||||
g.Add("example.com", 1)
|
||||
g.Add("google.com", 2)
|
||||
g.Add("x.a.com", 3)
|
||||
g.Add("x.y.com", 4)
|
||||
g.Add("x.y.com", 6)
|
||||
|
||||
testCases := []struct {
|
||||
Domain string
|
||||
Result []uint32
|
||||
}{
|
||||
{
|
||||
Domain: "example.com",
|
||||
Result: []uint32{1},
|
||||
},
|
||||
{
|
||||
Domain: "y.com",
|
||||
Result: nil,
|
||||
},
|
||||
{
|
||||
Domain: "x.y.com",
|
||||
Result: []uint32{4, 6},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
r := g.Match(testCase.Domain)
|
||||
if !reflect.DeepEqual(r, testCase.Result) {
|
||||
t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyFullMatcherGroup(t *testing.T) {
|
||||
g := new(FullMatcherGroup)
|
||||
r := g.Match("example.com")
|
||||
if len(r) != 0 {
|
||||
t.Error("Expect [], but ", r)
|
||||
}
|
||||
}
|
52
common/strmatcher/matchers.go
Normal file
52
common/strmatcher/matchers.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
package strmatcher
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type fullMatcher string
|
||||
|
||||
func (m fullMatcher) Match(s string) bool {
|
||||
return string(m) == s
|
||||
}
|
||||
|
||||
func (m fullMatcher) String() string {
|
||||
return "full:" + string(m)
|
||||
}
|
||||
|
||||
type substrMatcher string
|
||||
|
||||
func (m substrMatcher) Match(s string) bool {
|
||||
return strings.Contains(s, string(m))
|
||||
}
|
||||
|
||||
func (m substrMatcher) String() string {
|
||||
return "keyword:" + string(m)
|
||||
}
|
||||
|
||||
type domainMatcher string
|
||||
|
||||
func (m domainMatcher) Match(s string) bool {
|
||||
pattern := string(m)
|
||||
if !strings.HasSuffix(s, pattern) {
|
||||
return false
|
||||
}
|
||||
return len(s) == len(pattern) || s[len(s)-len(pattern)-1] == '.'
|
||||
}
|
||||
|
||||
func (m domainMatcher) String() string {
|
||||
return "domain:" + string(m)
|
||||
}
|
||||
|
||||
type regexMatcher struct {
|
||||
pattern *regexp.Regexp
|
||||
}
|
||||
|
||||
func (m *regexMatcher) Match(s string) bool {
|
||||
return m.pattern.MatchString(s)
|
||||
}
|
||||
|
||||
func (m *regexMatcher) String() string {
|
||||
return "regexp:" + m.pattern.String()
|
||||
}
|
73
common/strmatcher/matchers_test.go
Normal file
73
common/strmatcher/matchers_test.go
Normal file
|
@ -0,0 +1,73 @@
|
|||
package strmatcher_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
. "github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
)
|
||||
|
||||
func TestMatcher(t *testing.T) {
|
||||
cases := []struct {
|
||||
pattern string
|
||||
mType Type
|
||||
input string
|
||||
output bool
|
||||
}{
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Domain,
|
||||
input: "www.example.com",
|
||||
output: true,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Domain,
|
||||
input: "example.com",
|
||||
output: true,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Domain,
|
||||
input: "www.fxample.com",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Domain,
|
||||
input: "xample.com",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Domain,
|
||||
input: "xexample.com",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Full,
|
||||
input: "example.com",
|
||||
output: true,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Full,
|
||||
input: "xexample.com",
|
||||
output: false,
|
||||
},
|
||||
{
|
||||
pattern: "example.com",
|
||||
mType: Regex,
|
||||
input: "examplexcom",
|
||||
output: true,
|
||||
},
|
||||
}
|
||||
for _, test := range cases {
|
||||
matcher, err := test.mType.New(test.pattern)
|
||||
common.Must(err)
|
||||
if m := matcher.Match(test.input); m != test.output {
|
||||
t.Error("unexpected output: ", m, " for test case ", test)
|
||||
}
|
||||
}
|
||||
}
|
106
common/strmatcher/strmatcher.go
Normal file
106
common/strmatcher/strmatcher.go
Normal file
|
@ -0,0 +1,106 @@
|
|||
package strmatcher
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// Matcher is the interface to determine a string matches a pattern.
|
||||
type Matcher interface {
|
||||
// Match returns true if the given string matches a predefined pattern.
|
||||
Match(string) bool
|
||||
String() string
|
||||
}
|
||||
|
||||
// Type is the type of the matcher.
|
||||
type Type byte
|
||||
|
||||
const (
|
||||
// Full is the type of matcher that the input string must exactly equal to the pattern.
|
||||
Full Type = iota
|
||||
// Substr is the type of matcher that the input string must contain the pattern as a sub-string.
|
||||
Substr
|
||||
// Domain is the type of matcher that the input string must be a sub-domain or itself of the pattern.
|
||||
Domain
|
||||
// Regex is the type of matcher that the input string must matches the regular-expression pattern.
|
||||
Regex
|
||||
)
|
||||
|
||||
// New creates a new Matcher based on the given pattern.
|
||||
func (t Type) New(pattern string) (Matcher, error) {
|
||||
switch t {
|
||||
case Full:
|
||||
return fullMatcher(pattern), nil
|
||||
case Substr:
|
||||
return substrMatcher(pattern), nil
|
||||
case Domain:
|
||||
return domainMatcher(pattern), nil
|
||||
case Regex:
|
||||
r, err := regexp.Compile(pattern)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return ®exMatcher{
|
||||
pattern: r,
|
||||
}, nil
|
||||
default:
|
||||
panic("Unknown type")
|
||||
}
|
||||
}
|
||||
|
||||
// IndexMatcher is the interface for matching with a group of matchers.
|
||||
type IndexMatcher interface {
|
||||
// Match returns the index of a matcher that matches the input. It returns empty array if no such matcher exists.
|
||||
Match(input string) []uint32
|
||||
}
|
||||
|
||||
type matcherEntry struct {
|
||||
m Matcher
|
||||
id uint32
|
||||
}
|
||||
|
||||
// MatcherGroup is an implementation of IndexMatcher.
|
||||
// Empty initialization works.
|
||||
type MatcherGroup struct {
|
||||
count uint32
|
||||
fullMatcher FullMatcherGroup
|
||||
domainMatcher DomainMatcherGroup
|
||||
otherMatchers []matcherEntry
|
||||
}
|
||||
|
||||
// Add adds a new Matcher into the MatcherGroup, and returns its index. The index will never be 0.
|
||||
func (g *MatcherGroup) Add(m Matcher) uint32 {
|
||||
g.count++
|
||||
c := g.count
|
||||
|
||||
switch tm := m.(type) {
|
||||
case fullMatcher:
|
||||
g.fullMatcher.addMatcher(tm, c)
|
||||
case domainMatcher:
|
||||
g.domainMatcher.addMatcher(tm, c)
|
||||
default:
|
||||
g.otherMatchers = append(g.otherMatchers, matcherEntry{
|
||||
m: m,
|
||||
id: c,
|
||||
})
|
||||
}
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
// Match implements IndexMatcher.Match.
|
||||
func (g *MatcherGroup) Match(pattern string) []uint32 {
|
||||
result := []uint32{}
|
||||
result = append(result, g.fullMatcher.Match(pattern)...)
|
||||
result = append(result, g.domainMatcher.Match(pattern)...)
|
||||
for _, e := range g.otherMatchers {
|
||||
if e.m.Match(pattern) {
|
||||
result = append(result, e.id)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Size returns the number of matchers in the MatcherGroup.
|
||||
func (g *MatcherGroup) Size() uint32 {
|
||||
return g.count
|
||||
}
|
93
common/strmatcher/strmatcher_test.go
Normal file
93
common/strmatcher/strmatcher_test.go
Normal file
|
@ -0,0 +1,93 @@
|
|||
package strmatcher_test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/xtls/xray-core/v1/common"
|
||||
. "github.com/xtls/xray-core/v1/common/strmatcher"
|
||||
)
|
||||
|
||||
func TestMatcherGroup(t *testing.T) {
|
||||
rules := []struct {
|
||||
Type Type
|
||||
Domain string
|
||||
}{
|
||||
{
|
||||
Type: Regex,
|
||||
Domain: "apis\\.us$",
|
||||
},
|
||||
{
|
||||
Type: Substr,
|
||||
Domain: "apis",
|
||||
},
|
||||
{
|
||||
Type: Domain,
|
||||
Domain: "googleapis.com",
|
||||
},
|
||||
{
|
||||
Type: Domain,
|
||||
Domain: "com",
|
||||
},
|
||||
{
|
||||
Type: Full,
|
||||
Domain: "www.baidu.com",
|
||||
},
|
||||
{
|
||||
Type: Substr,
|
||||
Domain: "apis",
|
||||
},
|
||||
{
|
||||
Type: Domain,
|
||||
Domain: "googleapis.com",
|
||||
},
|
||||
{
|
||||
Type: Full,
|
||||
Domain: "fonts.googleapis.com",
|
||||
},
|
||||
{
|
||||
Type: Full,
|
||||
Domain: "www.baidu.com",
|
||||
},
|
||||
{
|
||||
Type: Domain,
|
||||
Domain: "example.com",
|
||||
},
|
||||
}
|
||||
cases := []struct {
|
||||
Input string
|
||||
Output []uint32
|
||||
}{
|
||||
{
|
||||
Input: "www.baidu.com",
|
||||
Output: []uint32{5, 9, 4},
|
||||
},
|
||||
{
|
||||
Input: "fonts.googleapis.com",
|
||||
Output: []uint32{8, 3, 7, 4, 2, 6},
|
||||
},
|
||||
{
|
||||
Input: "example.googleapis.com",
|
||||
Output: []uint32{3, 7, 4, 2, 6},
|
||||
},
|
||||
{
|
||||
Input: "testapis.us",
|
||||
Output: []uint32{1, 2, 6},
|
||||
},
|
||||
{
|
||||
Input: "example.com",
|
||||
Output: []uint32{10, 4},
|
||||
},
|
||||
}
|
||||
matcherGroup := &MatcherGroup{}
|
||||
for _, rule := range rules {
|
||||
matcher, err := rule.Type.New(rule.Domain)
|
||||
common.Must(err)
|
||||
matcherGroup.Add(matcher)
|
||||
}
|
||||
for _, test := range cases {
|
||||
if m := matcherGroup.Match(test.Input); !reflect.DeepEqual(m, test.Output) {
|
||||
t.Error("unexpected output: ", m, " for test case ", test)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue