mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-03-11 17:23:51 +00:00
Fix tcp sniffing
This commit is contained in:
parent
dc127e2994
commit
862e3c430c
2 changed files with 25 additions and 11 deletions
|
@ -1,11 +1,16 @@
|
||||||
package sniff
|
package sniff
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sagernet/sing-box/adapter"
|
"github.com/sagernet/sing-box/adapter"
|
||||||
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
@ -13,13 +18,23 @@ type (
|
||||||
PacketSniffer = func(ctx context.Context, packet []byte) (*adapter.InboundContext, error)
|
PacketSniffer = func(ctx context.Context, packet []byte) (*adapter.InboundContext, error)
|
||||||
)
|
)
|
||||||
|
|
||||||
func PeekStream(ctx context.Context, reader io.Reader, sniffers ...StreamSniffer) (*adapter.InboundContext, error) {
|
func PeekStream(ctx context.Context, conn net.Conn, buffer *buf.Buffer, sniffers ...StreamSniffer) (*adapter.InboundContext, error) {
|
||||||
|
err := conn.SetReadDeadline(time.Now().Add(300 * time.Millisecond))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
_, err = buffer.ReadFrom(conn)
|
||||||
|
err = E.Errors(err, conn.SetReadDeadline(time.Time{}))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var metadata *adapter.InboundContext
|
||||||
for _, sniffer := range sniffers {
|
for _, sniffer := range sniffers {
|
||||||
sniffMetadata, err := sniffer(ctx, reader)
|
metadata, err = sniffer(ctx, bytes.NewReader(buffer.Bytes()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return sniffMetadata, nil
|
return metadata, nil
|
||||||
}
|
}
|
||||||
return nil, os.ErrInvalid
|
return nil, os.ErrInvalid
|
||||||
}
|
}
|
||||||
|
|
|
@ -389,8 +389,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
|
||||||
defer common.KeepAlive(_buffer)
|
defer common.KeepAlive(_buffer)
|
||||||
buffer := common.Dup(_buffer)
|
buffer := common.Dup(_buffer)
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
reader := io.TeeReader(conn, buffer)
|
sniffMetadata, err := sniff.PeekStream(ctx, conn, buffer, sniff.TLSClientHello, sniff.HTTPHost)
|
||||||
sniffMetadata, err := sniff.PeekStream(ctx, reader, sniff.TLSClientHello, sniff.HTTPHost)
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
metadata.Protocol = sniffMetadata.Protocol
|
metadata.Protocol = sniffMetadata.Protocol
|
||||||
metadata.Domain = sniffMetadata.Domain
|
metadata.Domain = sniffMetadata.Domain
|
||||||
|
@ -398,9 +397,9 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
|
||||||
metadata.Destination.Fqdn = metadata.Domain
|
metadata.Destination.Fqdn = metadata.Domain
|
||||||
}
|
}
|
||||||
if metadata.Domain != "" {
|
if metadata.Domain != "" {
|
||||||
r.logger.WithContext(ctx).Info("sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
r.logger.WithContext(ctx).Debug("sniffed protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
||||||
} else {
|
} else {
|
||||||
r.logger.WithContext(ctx).Info("sniffed protocol: ", metadata.Protocol)
|
r.logger.WithContext(ctx).Debug("sniffed protocol: ", metadata.Protocol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !buffer.IsEmpty() {
|
if !buffer.IsEmpty() {
|
||||||
|
@ -413,7 +412,7 @@ func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata ad
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
metadata.DestinationAddresses = addresses
|
metadata.DestinationAddresses = addresses
|
||||||
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
|
r.dnsLogger.WithContext(ctx).Debug("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
|
||||||
}
|
}
|
||||||
detour := r.match(ctx, metadata, r.defaultOutboundForConnection)
|
detour := r.match(ctx, metadata, r.defaultOutboundForConnection)
|
||||||
if !common.Contains(detour.Network(), C.NetworkTCP) {
|
if !common.Contains(detour.Network(), C.NetworkTCP) {
|
||||||
|
@ -442,9 +441,9 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
|
||||||
metadata.Destination.Fqdn = metadata.Domain
|
metadata.Destination.Fqdn = metadata.Domain
|
||||||
}
|
}
|
||||||
if metadata.Domain != "" {
|
if metadata.Domain != "" {
|
||||||
r.logger.WithContext(ctx).Info("sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
r.logger.WithContext(ctx).Debug("sniffed packet protocol: ", metadata.Protocol, ", domain: ", metadata.Domain)
|
||||||
} else {
|
} else {
|
||||||
r.logger.WithContext(ctx).Info("sniffed packet protocol: ", metadata.Protocol)
|
r.logger.WithContext(ctx).Debug("sniffed packet protocol: ", metadata.Protocol)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
conn = bufio.NewCachedPacketConn(conn, buffer, originDestination)
|
conn = bufio.NewCachedPacketConn(conn, buffer, originDestination)
|
||||||
|
@ -455,7 +454,7 @@ func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, m
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
metadata.DestinationAddresses = addresses
|
metadata.DestinationAddresses = addresses
|
||||||
r.dnsLogger.WithContext(ctx).Info("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
|
r.dnsLogger.WithContext(ctx).Debug("resolved [", strings.Join(F.MapToString(metadata.DestinationAddresses), " "), "]")
|
||||||
}
|
}
|
||||||
detour := r.match(ctx, metadata, r.defaultOutboundForPacketConnection)
|
detour := r.match(ctx, metadata, r.defaultOutboundForPacketConnection)
|
||||||
if !common.Contains(detour.Network(), C.NetworkUDP) {
|
if !common.Contains(detour.Network(), C.NetworkUDP) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue