move sniffing result to session

This commit is contained in:
Darien Raymond 2019-02-22 16:58:16 +01:00
parent 6178d7281c
commit 7e5e080488
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
7 changed files with 31 additions and 26 deletions

View File

@ -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()

View File

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

View File

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

View File

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

View File

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

View File

@ -8,6 +8,7 @@ const (
idSessionKey sessionKey = iota
inboundSessionKey
outboundSessionKey
contentSessionKey
)
// ContextWithID returns a new context with the given ID.
@ -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
}

View File

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