1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-29 23:36:25 -04:00

Merge pull request #188 from Vigilans/vigilans/routing-context-fix

Routing Context: Fix GetUser() & Use string for Attributes Value
This commit is contained in:
Kslr 2020-09-13 14:31:16 +08:00 committed by GitHub
commit a8972cdf06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16 additions and 19 deletions

View File

@ -288,17 +288,11 @@ func NewAttributeMatcher(code string) (*AttributeMatcher, error) {
}, nil }, 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) attrsDict := new(starlark.Dict)
for key, value := range attrs { for key, value := range attrs {
var starValue starlark.Value attrsDict.SetKey(starlark.String(key), starlark.String(value))
switch value := value.(type) {
case string:
starValue = starlark.String(value)
}
if starValue != nil {
attrsDict.SetKey(starlark.String(key), starValue)
}
} }
predefined := make(starlark.StringDict) predefined := make(starlark.StringDict)

View File

@ -313,7 +313,7 @@ func TestRoutingRule(t *testing.T) {
}, },
test: []ruleTest{ 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, output: true,
}, },
}, },

View File

@ -53,6 +53,7 @@ type Outbound struct {
Gateway net.Address Gateway net.Address
} }
// SniffingRequest controls the behavior of content sniffing.
type SniffingRequest struct { type SniffingRequest struct {
OverrideDestinationForProtocol []string OverrideDestinationForProtocol []string
Enabled bool Enabled bool
@ -65,7 +66,7 @@ type Content struct {
SniffingRequest SniffingRequest SniffingRequest SniffingRequest
Attributes map[string]interface{} Attributes map[string]string
SkipRoutePick bool SkipRoutePick bool
} }
@ -76,16 +77,18 @@ type Sockopt struct {
Mark int32 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 { if c.Attributes == nil {
c.Attributes = make(map[string]interface{}) c.Attributes = make(map[string]string)
} }
c.Attributes[name] = value 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 { if c.Attributes == nil {
return nil return ""
} }
return c.Attributes[name] return c.Attributes[name]
} }

View File

@ -6,7 +6,7 @@ import (
// Context is a feature to store connection information for routing. // Context is a feature to store connection information for routing.
// //
// v2ray:api:beta // v2ray:api:stable
type Context interface { type Context interface {
// GetInboundTag returns the tag of the inbound the connection was from. // GetInboundTag returns the tag of the inbound the connection was from.
GetInboundTag() string GetInboundTag() string
@ -36,5 +36,5 @@ type Context interface {
GetUser() string GetUser() string
// GetAttributes returns extra attributes from the conneciont content. // GetAttributes returns extra attributes from the conneciont content.
GetAttributes() map[string]interface{} GetAttributes() map[string]string
} }

View File

@ -95,14 +95,14 @@ func (ctx *Context) GetProtocol() string {
// GetUser implements routing.Context. // GetUser implements routing.Context.
func (ctx *Context) GetUser() string { func (ctx *Context) GetUser() string {
if ctx.Inbound == nil { if ctx.Inbound == nil || ctx.Inbound.User == nil {
return "" return ""
} }
return ctx.Inbound.User.Email return ctx.Inbound.User.Email
} }
// GetAttributes implements routing.Context. // GetAttributes implements routing.Context.
func (ctx *Context) GetAttributes() map[string]interface{} { func (ctx *Context) GetAttributes() map[string]string {
if ctx.Content == nil { if ctx.Content == nil {
return nil return nil
} }