mirror of https://github.com/v2ray/v2ray-core
move sniffing request to session
parent
95ed5a5ff8
commit
3828a463ea
|
@ -11,7 +11,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"v2ray.com/core"
|
"v2ray.com/core"
|
||||||
"v2ray.com/core/app/proxyman"
|
|
||||||
"v2ray.com/core/common"
|
"v2ray.com/core/common"
|
||||||
"v2ray.com/core/common/buf"
|
"v2ray.com/core/common/buf"
|
||||||
"v2ray.com/core/common/net"
|
"v2ray.com/core/common/net"
|
||||||
|
@ -196,8 +195,13 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
|
||||||
ctx = session.ContextWithOutbound(ctx, ob)
|
ctx = session.ContextWithOutbound(ctx, ob)
|
||||||
|
|
||||||
inbound, outbound := d.getLink(ctx)
|
inbound, outbound := d.getLink(ctx)
|
||||||
sniffingConfig := proxyman.SniffingConfigFromContext(ctx)
|
content := session.ContentFromContext(ctx)
|
||||||
if destination.Network != net.Network_TCP || sniffingConfig == nil || !sniffingConfig.Enabled {
|
if content == nil {
|
||||||
|
content = new(session.Content)
|
||||||
|
ctx = session.ContextWithContent(ctx, content)
|
||||||
|
}
|
||||||
|
sniffingRequest := content.SniffingRequest
|
||||||
|
if destination.Network != net.Network_TCP || !sniffingRequest.Enabled {
|
||||||
go d.routedDispatch(ctx, outbound, destination)
|
go d.routedDispatch(ctx, outbound, destination)
|
||||||
} else {
|
} else {
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -207,14 +211,9 @@ func (d *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
|
||||||
outbound.Reader = cReader
|
outbound.Reader = cReader
|
||||||
result, err := sniffer(ctx, cReader)
|
result, err := sniffer(ctx, cReader)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
content := session.ContentFromContext(ctx)
|
|
||||||
if content == nil {
|
|
||||||
content = new(session.Content)
|
|
||||||
}
|
|
||||||
content.Protocol = result.Protocol()
|
content.Protocol = result.Protocol()
|
||||||
ctx = session.ContextWithContent(ctx, content)
|
|
||||||
}
|
}
|
||||||
if err == nil && shouldOverride(result, sniffingConfig.DestinationOverride) {
|
if err == nil && shouldOverride(result, sniffingRequest.OverrideDestinationForProtocol) {
|
||||||
domain := result.Domain()
|
domain := result.Domain()
|
||||||
newError("sniffed domain: ", domain).WriteToLog(session.ExportIDToError(ctx))
|
newError("sniffed domain: ", domain).WriteToLog(session.ExportIDToError(ctx))
|
||||||
destination.Address = net.ParseAddress(domain)
|
destination.Address = net.ParseAddress(domain)
|
||||||
|
|
|
@ -81,9 +81,12 @@ func (w *tcpWorker) callback(conn internet.Connection) {
|
||||||
Gateway: net.TCPDestination(w.address, w.port),
|
Gateway: net.TCPDestination(w.address, w.port),
|
||||||
Tag: w.tag,
|
Tag: w.tag,
|
||||||
})
|
})
|
||||||
|
content := new(session.Content)
|
||||||
if w.sniffingConfig != nil {
|
if w.sniffingConfig != nil {
|
||||||
ctx = proxyman.ContextWithSniffingConfig(ctx, w.sniffingConfig)
|
content.SniffingRequest.Enabled = w.sniffingConfig.Enabled
|
||||||
|
content.SniffingRequest.OverrideDestinationForProtocol = w.sniffingConfig.DestinationOverride
|
||||||
}
|
}
|
||||||
|
ctx = session.ContextWithContent(ctx, content)
|
||||||
if w.uplinkCounter != nil || w.downlinkCounter != nil {
|
if w.uplinkCounter != nil || w.downlinkCounter != nil {
|
||||||
conn = &internet.StatCouterConnection{
|
conn = &internet.StatCouterConnection{
|
||||||
Connection: conn,
|
Connection: conn,
|
||||||
|
|
|
@ -3,21 +3,19 @@ package proxyman
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"v2ray.com/core/common/session"
|
||||||
)
|
)
|
||||||
|
|
||||||
type key int
|
// ContextWithSniffingConfig is a wrapper of session.ContextWithContent.
|
||||||
|
// Deprecated. Use session.ContextWithContent directly.
|
||||||
const (
|
|
||||||
sniffing key = iota
|
|
||||||
)
|
|
||||||
|
|
||||||
func ContextWithSniffingConfig(ctx context.Context, c *SniffingConfig) context.Context {
|
func ContextWithSniffingConfig(ctx context.Context, c *SniffingConfig) context.Context {
|
||||||
return context.WithValue(ctx, sniffing, c)
|
content := session.ContentFromContext(ctx)
|
||||||
}
|
if content == nil {
|
||||||
|
content = new(session.Content)
|
||||||
func SniffingConfigFromContext(ctx context.Context) *SniffingConfig {
|
ctx = session.ContextWithContent(ctx, content)
|
||||||
if c, ok := ctx.Value(sniffing).(*SniffingConfig); ok {
|
|
||||||
return c
|
|
||||||
}
|
}
|
||||||
return nil
|
content.SniffingRequest.Enabled = c.Enabled
|
||||||
|
content.SniffingRequest.OverrideDestinationForProtocol = c.DestinationOverride
|
||||||
|
return ctx
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,8 +55,15 @@ type Outbound struct {
|
||||||
ResolvedIPs []net.IP
|
ResolvedIPs []net.IP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SniffingRequest struct {
|
||||||
|
OverrideDestinationForProtocol []string
|
||||||
|
Enabled bool
|
||||||
|
}
|
||||||
|
|
||||||
// Content is the metadata of the connection content.
|
// Content is the metadata of the connection content.
|
||||||
type Content struct {
|
type Content struct {
|
||||||
// Protocol of current content.
|
// Protocol of current content.
|
||||||
Protocol string
|
Protocol string
|
||||||
|
|
||||||
|
SniffingRequest SniffingRequest
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue