Skip to content

Commit

Permalink
Merge pull request v2fly#188 from Vigilans/vigilans/routing-context-fix
Browse files Browse the repository at this point in the history
Routing Context: Fix GetUser() & Use string for Attributes Value
  • Loading branch information
kslr authored Sep 13, 2020
2 parents 3d85520 + 5a49789 commit a8972cd
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 19 deletions.
12 changes: 3 additions & 9 deletions app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/router/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down
13 changes: 8 additions & 5 deletions common/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Outbound struct {
Gateway net.Address
}

// SniffingRequest controls the behavior of content sniffing.
type SniffingRequest struct {
OverrideDestinationForProtocol []string
Enabled bool
Expand All @@ -65,7 +66,7 @@ type Content struct {

SniffingRequest SniffingRequest

Attributes map[string]interface{}
Attributes map[string]string

SkipRoutePick bool
}
Expand All @@ -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]
}
4 changes: 2 additions & 2 deletions features/routing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions features/routing/session/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit a8972cd

Please sign in to comment.