mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
Add sniff domain
This commit is contained in:
parent
4a66ad7093
commit
05fca81c09
@ -55,6 +55,7 @@ type InboundContext struct {
|
|||||||
|
|
||||||
Protocol string
|
Protocol string
|
||||||
Domain string
|
Domain string
|
||||||
|
SniffDomain string
|
||||||
Client string
|
Client string
|
||||||
SniffContext any
|
SniffContext any
|
||||||
PacketSniffError error
|
PacketSniffError error
|
||||||
|
@ -23,6 +23,6 @@ func HTTPHost(_ context.Context, metadata *adapter.InboundContext, reader io.Rea
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
metadata.Protocol = C.ProtocolHTTP
|
metadata.Protocol = C.ProtocolHTTP
|
||||||
metadata.Domain = M.ParseSocksaddr(request.Host).AddrString()
|
metadata.SniffDomain = M.ParseSocksaddr(request.Host).AddrString()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,7 @@ func TestSniffHTTP1(t *testing.T) {
|
|||||||
var metadata adapter.InboundContext
|
var metadata adapter.InboundContext
|
||||||
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Domain, "www.google.com")
|
require.Equal(t, metadata.SniffDomain, "www.google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSniffHTTP1WithPort(t *testing.T) {
|
func TestSniffHTTP1WithPort(t *testing.T) {
|
||||||
@ -26,5 +26,5 @@ func TestSniffHTTP1WithPort(t *testing.T) {
|
|||||||
var metadata adapter.InboundContext
|
var metadata adapter.InboundContext
|
||||||
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
err := sniff.HTTPHost(context.Background(), &metadata, strings.NewReader(pkt))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Domain, "www.gov.cn")
|
require.Equal(t, metadata.SniffDomain, "www.gov.cn")
|
||||||
}
|
}
|
||||||
|
@ -308,7 +308,7 @@ find:
|
|||||||
metadata.SniffContext = fragments
|
metadata.SniffContext = fragments
|
||||||
return E.Cause1(ErrNeedMoreData, err)
|
return E.Cause1(ErrNeedMoreData, err)
|
||||||
}
|
}
|
||||||
metadata.Domain = fingerprint.ServerName
|
metadata.SniffDomain = fingerprint.ServerName
|
||||||
for metadata.Client == "" {
|
for metadata.Client == "" {
|
||||||
if len(frameTypeList) == 1 {
|
if len(frameTypeList) == 1 {
|
||||||
metadata.Client = C.ClientFirefox
|
metadata.Client = C.ClientFirefox
|
||||||
|
@ -29,7 +29,7 @@ func TestSniffQUICChromeNew(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = sniff.QUICClientHello(context.Background(), &metadata, pkt)
|
err = sniff.QUICClientHello(context.Background(), &metadata, pkt)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, "www.google.com", metadata.Domain)
|
require.Equal(t, "www.google.com", metadata.SniffDomain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSniffQUICChromium(t *testing.T) {
|
func TestSniffQUICChromium(t *testing.T) {
|
||||||
@ -45,7 +45,7 @@ func TestSniffQUICChromium(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
err = sniff.QUICClientHello(context.Background(), &metadata, pkt)
|
err = sniff.QUICClientHello(context.Background(), &metadata, pkt)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Domain, "google.com")
|
require.Equal(t, metadata.SniffDomain, "google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSniffUQUICChrome115(t *testing.T) {
|
func TestSniffUQUICChrome115(t *testing.T) {
|
||||||
@ -57,7 +57,7 @@ func TestSniffUQUICChrome115(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
||||||
require.Equal(t, metadata.Client, C.ClientQUICGo)
|
require.Equal(t, metadata.Client, C.ClientQUICGo)
|
||||||
require.Equal(t, metadata.Domain, "www.google.com")
|
require.Equal(t, metadata.SniffDomain, "www.google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSniffQUICFirefox(t *testing.T) {
|
func TestSniffQUICFirefox(t *testing.T) {
|
||||||
@ -69,7 +69,7 @@ func TestSniffQUICFirefox(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
||||||
require.Equal(t, metadata.Client, C.ClientFirefox)
|
require.Equal(t, metadata.Client, C.ClientFirefox)
|
||||||
require.Equal(t, metadata.Domain, "www.google.com")
|
require.Equal(t, metadata.SniffDomain, "www.google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSniffQUICSafari(t *testing.T) {
|
func TestSniffQUICSafari(t *testing.T) {
|
||||||
@ -81,7 +81,7 @@ func TestSniffQUICSafari(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
require.Equal(t, metadata.Protocol, C.ProtocolQUIC)
|
||||||
require.Equal(t, metadata.Client, C.ClientSafari)
|
require.Equal(t, metadata.Client, C.ClientSafari)
|
||||||
require.Equal(t, metadata.Domain, "www.google.com")
|
require.Equal(t, metadata.SniffDomain, "www.google.com")
|
||||||
}
|
}
|
||||||
|
|
||||||
func FuzzSniffQUIC(f *testing.F) {
|
func FuzzSniffQUIC(f *testing.F) {
|
||||||
|
@ -22,7 +22,7 @@ func TLSClientHello(ctx context.Context, metadata *adapter.InboundContext, reade
|
|||||||
}).HandshakeContext(ctx)
|
}).HandshakeContext(ctx)
|
||||||
if clientHello != nil {
|
if clientHello != nil {
|
||||||
metadata.Protocol = C.ProtocolTLS
|
metadata.Protocol = C.ProtocolTLS
|
||||||
metadata.Domain = clientHello.ServerName
|
metadata.SniffDomain = clientHello.ServerName
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if errors.Is(err, io.ErrUnexpectedEOF) {
|
if errors.Is(err, io.ErrUnexpectedEOF) {
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
C "github.com/sagernet/sing-box/constant"
|
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/atomic"
|
"github.com/sagernet/sing/common/atomic"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
@ -48,11 +47,8 @@ func (t TrackerMetadata) MarshalJSON() ([]byte, error) {
|
|||||||
if t.Metadata.Client != "" {
|
if t.Metadata.Client != "" {
|
||||||
sniffURL += ":" + t.Metadata.Client
|
sniffURL += ":" + t.Metadata.Client
|
||||||
}
|
}
|
||||||
if t.Metadata.Domain != "" {
|
if t.Metadata.SniffDomain != "" {
|
||||||
switch t.Metadata.Protocol {
|
sniffURL += "://" + t.Metadata.SniffDomain
|
||||||
case C.ProtocolHTTP, C.ProtocolQUIC, C.ProtocolTLS:
|
|
||||||
sniffURL += "://" + t.Metadata.Domain
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var processPath string
|
var processPath string
|
||||||
|
@ -526,6 +526,9 @@ func (r *Router) actionSniff(
|
|||||||
streamSniffers...,
|
streamSniffers...,
|
||||||
)
|
)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
if metadata.SniffDomain != "" {
|
||||||
|
metadata.Domain = metadata.SniffDomain
|
||||||
|
}
|
||||||
//goland:noinspection GoDeprecation
|
//goland:noinspection GoDeprecation
|
||||||
if action.OverrideDestination && M.IsDomainName(metadata.Domain) {
|
if action.OverrideDestination && M.IsDomainName(metadata.Domain) {
|
||||||
metadata.Destination = M.Socksaddr{
|
metadata.Destination = M.Socksaddr{
|
||||||
@ -533,10 +536,10 @@ func (r *Router) actionSniff(
|
|||||||
Port: metadata.Destination.Port,
|
Port: metadata.Destination.Port,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if metadata.Domain != "" && metadata.Client != "" {
|
if metadata.SniffDomain != "" && metadata.Client != "" {
|
||||||
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.Domain, ", client: ", metadata.Client)
|
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.SniffDomain, ", client: ", metadata.Client)
|
||||||
} else if metadata.Domain != "" {
|
} else if metadata.SniffDomain != "" {
|
||||||
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.SniffDomain)
|
||||||
} else {
|
} else {
|
||||||
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol)
|
r.logger.DebugContext(ctx, "sniffed protocol: ", metadata.Protocol)
|
||||||
}
|
}
|
||||||
@ -623,6 +626,9 @@ func (r *Router) actionSniff(
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if metadata.Protocol != "" {
|
if metadata.Protocol != "" {
|
||||||
|
if metadata.SniffDomain != "" {
|
||||||
|
metadata.Domain = metadata.SniffDomain
|
||||||
|
}
|
||||||
//goland:noinspection GoDeprecation
|
//goland:noinspection GoDeprecation
|
||||||
if action.OverrideDestination && M.IsDomainName(metadata.Domain) {
|
if action.OverrideDestination && M.IsDomainName(metadata.Domain) {
|
||||||
metadata.Destination = M.Socksaddr{
|
metadata.Destination = M.Socksaddr{
|
||||||
@ -630,10 +636,10 @@ func (r *Router) actionSniff(
|
|||||||
Port: metadata.Destination.Port,
|
Port: metadata.Destination.Port,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if metadata.Domain != "" && metadata.Client != "" {
|
if metadata.SniffDomain != "" && metadata.Client != "" {
|
||||||
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.Domain, ", client: ", metadata.Client)
|
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.SniffDomain, ", client: ", metadata.Client)
|
||||||
} else if metadata.Domain != "" {
|
} else if metadata.SniffDomain != "" {
|
||||||
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.SniffDomain)
|
||||||
} else if metadata.Client != "" {
|
} else if metadata.Client != "" {
|
||||||
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", client: ", metadata.Client)
|
r.logger.DebugContext(ctx, "sniffed packet protocol: ", metadata.Protocol, ", client: ", metadata.Client)
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user