Skip to content

Commit

Permalink
move sniffing result to session
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Feb 22, 2019
1 parent 6178d72 commit 7e5e080
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 26 deletions.
7 changes: 6 additions & 1 deletion app/dispatcher/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
outbound.Reader = cReader
result, err := sniffer(ctx, cReader)
if err == nil {
ctx = ContextWithSniffingResult(ctx, result)
content := session.ContentFromContext(ctx)
if content == nil {
content = new(session.Content)
}
content.Protocol = result.Protocol()
ctx = session.ContextWithContent(ctx, content)
}
if err == nil && shouldOverride(result, sniffingConfig.DestinationOverride) {
domain := result.Domain()
Expand Down
19 changes: 0 additions & 19 deletions app/dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,4 @@

package dispatcher

import "context"

//go:generate errorgen

type key int

const (
sniffing key = iota
)

func ContextWithSniffingResult(ctx context.Context, r SniffResult) context.Context {
return context.WithValue(ctx, sniffing, r)
}

func SniffingResultFromContext(ctx context.Context) SniffResult {
if c, ok := ctx.Value(sniffing).(SniffResult); ok {
return c
}
return nil
}
3 changes: 3 additions & 0 deletions app/dns/udpns.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@ func (s *ClassicNameServer) sendQuery(ctx context.Context, domain string, option
if inbound := session.InboundFromContext(ctx); inbound != nil {
udpCtx = session.ContextWithInbound(udpCtx, inbound)
}
udpCtx = session.ContextWithContent(udpCtx, &session.Content{
Protocol: "dns",
})
s.udpServer.Dispatch(udpCtx, s.address, b)
}
}
Expand Down
7 changes: 3 additions & 4 deletions app/router/condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"context"
"strings"

"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/net"
"v2ray.com/core/common/session"
"v2ray.com/core/common/strmatcher"
Expand Down Expand Up @@ -298,13 +297,13 @@ func NewProtocolMatcher(protocols []string) *ProtocolMatcher {
}

func (m *ProtocolMatcher) Apply(ctx context.Context) bool {
result := dispatcher.SniffingResultFromContext(ctx)
content := session.ContentFromContext(ctx)

if result == nil {
if content == nil {
return false
}

protocol := result.Protocol()
protocol := content.Protocol
for _, p := range m.protocols {
if strings.HasPrefix(protocol, p) {
return true
Expand Down
3 changes: 1 addition & 2 deletions app/router/condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

proto "github.com/golang/protobuf/proto"

"v2ray.com/core/app/dispatcher"
. "v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
Expand Down Expand Up @@ -218,7 +217,7 @@ func TestRoutingRule(t *testing.T) {
},
test: []ruleTest{
{
input: dispatcher.ContextWithSniffingResult(context.Background(), &http.SniffHeader{}),
input: session.ContextWithContent(context.Background(), &session.Content{Protocol: (&http.SniffHeader{}).Protocol()}),
output: true,
},
},
Expand Down
12 changes: 12 additions & 0 deletions common/session/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
idSessionKey sessionKey = iota
inboundSessionKey
outboundSessionKey
contentSessionKey
)

// ContextWithID returns a new context with the given ID.
Expand Down Expand Up @@ -44,3 +45,14 @@ func OutboundFromContext(ctx context.Context) *Outbound {
}
return nil
}

func ContextWithContent(ctx context.Context, content *Content) context.Context {
return context.WithValue(ctx, contentSessionKey, content)
}

func ContentFromContext(ctx context.Context) *Content {
if content, ok := ctx.Value(contentSessionKey).(*Content); ok {
return content
}
return nil
}
6 changes: 6 additions & 0 deletions common/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,9 @@ type Outbound struct {
// ResolvedIPs is the resolved IP addresses, if the Targe is a domain address.
ResolvedIPs []net.IP
}

// Content is the metadata of the connection content.
type Content struct {
// Protocol of current content.
Protocol string
}

0 comments on commit 7e5e080

Please sign in to comment.