mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-06-14 20:39:36 +03:00
Simplify http attrabute matching
In the past, we use Starlark script, it is over engineered and barely used. By switching to simple key value string contains logic we can reduce core size about 0.7MB
This commit is contained in:
@ -6,8 +6,6 @@ import (
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/strmatcher"
|
||||
"github.com/xtls/xray-core/features/routing"
|
||||
"go.starlark.net/starlark"
|
||||
"go.starlark.net/syntax"
|
||||
)
|
||||
|
||||
type Condition interface {
|
||||
@ -284,44 +282,22 @@ func (m *ProtocolMatcher) Apply(ctx routing.Context) bool {
|
||||
}
|
||||
|
||||
type AttributeMatcher struct {
|
||||
program *starlark.Program
|
||||
}
|
||||
|
||||
func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
|
||||
starFile, err := syntax.Parse("attr.star", "satisfied=("+code+")", 0)
|
||||
if err != nil {
|
||||
return nil, newError("attr rule").Base(err)
|
||||
}
|
||||
p, err := starlark.FileProgram(starFile, func(name string) bool {
|
||||
return name == "attrs"
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &AttributeMatcher{
|
||||
program: p,
|
||||
}, nil
|
||||
configuredKeys map[string]string
|
||||
}
|
||||
|
||||
// Match implements attributes matching.
|
||||
func (m *AttributeMatcher) Match(attrs map[string]string) bool {
|
||||
attrsDict := new(starlark.Dict)
|
||||
// headers are insensitive most likely. So we do a convert
|
||||
httpHeaders := make(map[string]string)
|
||||
for key, value := range attrs {
|
||||
attrsDict.SetKey(starlark.String(key), starlark.String(value))
|
||||
httpHeaders[strings.ToLower(key)] = strings.ToLower(value)
|
||||
}
|
||||
|
||||
predefined := make(starlark.StringDict)
|
||||
predefined["attrs"] = attrsDict
|
||||
|
||||
thread := &starlark.Thread{
|
||||
Name: "matcher",
|
||||
for key, value := range m.configuredKeys {
|
||||
if a, ok := httpHeaders[key]; !ok || !strings.Contains(a, value) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
results, err := m.program.Init(thread, predefined)
|
||||
if err != nil {
|
||||
newError("attr matcher").Base(err).WriteToLog()
|
||||
}
|
||||
satisfied := results["satisfied"]
|
||||
return satisfied != nil && bool(satisfied.Truth())
|
||||
return true
|
||||
}
|
||||
|
||||
// Apply implements Condition.
|
||||
|
Reference in New Issue
Block a user