Browse Source

Routing Context: Fix GetUser() & Use string for Attributes Value

Vigilans 5 years ago
parent
commit
5a497890e6

+ 3 - 9
app/router/condition.go

@@ -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
-		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)
 	predefined := make(starlark.StringDict)

+ 1 - 1
app/router/condition_test.go

@@ -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,
 				},
 				},
 			},
 			},

+ 8 - 5
common/session/session.go

@@ -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]
 }
 }

+ 2 - 2
features/routing/context.go

@@ -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
 }
 }

+ 2 - 2
features/routing/session/context.go

@@ -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
 	}
 	}