From 5a497890e60dfa7723a1f0714f7ef3bd9c993768 Mon Sep 17 00:00:00 2001 From: Vigilans Date: Sun, 13 Sep 2020 00:34:35 +0800 Subject: [PATCH] Routing Context: Fix GetUser() & Use string for Attributes Value --- app/router/condition.go | 12 +++--------- app/router/condition_test.go | 2 +- common/session/session.go | 13 ++++++++----- features/routing/context.go | 4 ++-- features/routing/session/context.go | 4 ++-- 5 files changed, 16 insertions(+), 19 deletions(-) diff --git a/app/router/condition.go b/app/router/condition.go index 5db1fa3c5..c77fc657e 100644 --- a/app/router/condition.go +++ b/app/router/condition.go @@ -288,17 +288,11 @@ func NewAttributeMatcher(code string) (*AttributeMatcher, error) { }, nil } -func (m *AttributeMatcher) Match(attrs map[string]interface{}) bool { +// Match implements attributes matching. +func (m *AttributeMatcher) Match(attrs map[string]string) bool { attrsDict := new(starlark.Dict) for key, value := range attrs { - var starValue starlark.Value - switch value := value.(type) { - case string: - starValue = starlark.String(value) - } - if starValue != nil { - attrsDict.SetKey(starlark.String(key), starValue) - } + attrsDict.SetKey(starlark.String(key), starlark.String(value)) } predefined := make(starlark.StringDict) diff --git a/app/router/condition_test.go b/app/router/condition_test.go index caef73679..af055bb4e 100644 --- a/app/router/condition_test.go +++ b/app/router/condition_test.go @@ -313,7 +313,7 @@ func TestRoutingRule(t *testing.T) { }, test: []ruleTest{ { - input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]interface{}{":path": "/test/1"}}), + input: withContent(&session.Content{Protocol: "http/1.1", Attributes: map[string]string{":path": "/test/1"}}), output: true, }, }, diff --git a/common/session/session.go b/common/session/session.go index 8d7b1ff6a..17b461d56 100644 --- a/common/session/session.go +++ b/common/session/session.go @@ -53,6 +53,7 @@ type Outbound struct { Gateway net.Address } +// SniffingRequest controls the behavior of content sniffing. type SniffingRequest struct { OverrideDestinationForProtocol []string Enabled bool @@ -65,7 +66,7 @@ type Content struct { SniffingRequest SniffingRequest - Attributes map[string]interface{} + Attributes map[string]string SkipRoutePick bool } @@ -76,16 +77,18 @@ type Sockopt struct { Mark int32 } -func (c *Content) SetAttribute(name string, value interface{}) { +// SetAttribute attachs additional string attributes to content. +func (c *Content) SetAttribute(name string, value string) { if c.Attributes == nil { - c.Attributes = make(map[string]interface{}) + c.Attributes = make(map[string]string) } c.Attributes[name] = value } -func (c *Content) Attribute(name string) interface{} { +// Attribute retrieves additional string attributes from content. +func (c *Content) Attribute(name string) string { if c.Attributes == nil { - return nil + return "" } return c.Attributes[name] } diff --git a/features/routing/context.go b/features/routing/context.go index b4adabafd..8fea19150 100644 --- a/features/routing/context.go +++ b/features/routing/context.go @@ -6,7 +6,7 @@ import ( // Context is a feature to store connection information for routing. // -// v2ray:api:beta +// v2ray:api:stable type Context interface { // GetInboundTag returns the tag of the inbound the connection was from. GetInboundTag() string @@ -36,5 +36,5 @@ type Context interface { GetUser() string // GetAttributes returns extra attributes from the conneciont content. - GetAttributes() map[string]interface{} + GetAttributes() map[string]string } diff --git a/features/routing/session/context.go b/features/routing/session/context.go index 6d61d4f94..bd5bb6337 100644 --- a/features/routing/session/context.go +++ b/features/routing/session/context.go @@ -95,14 +95,14 @@ func (ctx *Context) GetProtocol() string { // GetUser implements routing.Context. func (ctx *Context) GetUser() string { - if ctx.Inbound == nil { + if ctx.Inbound == nil || ctx.Inbound.User == nil { return "" } return ctx.Inbound.User.Email } // GetAttributes implements routing.Context. -func (ctx *Context) GetAttributes() map[string]interface{} { +func (ctx *Context) GetAttributes() map[string]string { if ctx.Content == nil { return nil }