mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-12-01 14:18:36 +00:00
Wait for client payload before kicking early conn handshake
This commit is contained in:
@@ -54,7 +54,7 @@ require (
|
||||
github.com/sagernet/sing-shadowsocks v0.2.9-0.20260929204512-65740e0f0e3e
|
||||
github.com/sagernet/sing-shadowsocks2 v0.2.2-0.20261110092058-52ade8ab7e95
|
||||
github.com/sagernet/sing-shadowtls v0.2.2-0.20261125124310-5028b2558ebf
|
||||
github.com/sagernet/sing-snell v0.0.0-20261110094602-04667b0a321c
|
||||
github.com/sagernet/sing-snell v0.0.0-20261125120456-6dfe590430a8
|
||||
github.com/sagernet/sing-tun v0.9.7-0.20261124071700-111dc3d13db1
|
||||
github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca
|
||||
github.com/sagernet/sing-vmess v0.2.9-0.20261124075649-4acc92a6f6c6
|
||||
|
||||
@@ -336,8 +336,8 @@ github.com/sagernet/sing-shadowsocks2 v0.2.2-0.20261110092058-52ade8ab7e95 h1:nu
|
||||
github.com/sagernet/sing-shadowsocks2 v0.2.2-0.20261110092058-52ade8ab7e95/go.mod h1:RnXS0lExcDAovvDeniJ4IKa2IuChrdipolPYWBv9hWQ=
|
||||
github.com/sagernet/sing-shadowtls v0.2.2-0.20261125124310-5028b2558ebf h1:1qCQPI1qWXi7pKpta0nedF/53zqbsCdtQVuXUD8HD+s=
|
||||
github.com/sagernet/sing-shadowtls v0.2.2-0.20261125124310-5028b2558ebf/go.mod h1:WyztWlXzZUKSu8uXVRfXTo4vgK2Jvpe5X6+sWRmUZrI=
|
||||
github.com/sagernet/sing-snell v0.0.0-20261110094602-04667b0a321c h1:OODxu54K5ZvgnS97t8HlN3tily8Oj+s6ygOJ1ZzyfFQ=
|
||||
github.com/sagernet/sing-snell v0.0.0-20261110094602-04667b0a321c/go.mod h1:Na4U54WiSQxWD/KEFIIdr+rPE9Qnr/kk8EtKXa18pRk=
|
||||
github.com/sagernet/sing-snell v0.0.0-20261125120456-6dfe590430a8 h1:tS4oi/f36knm2Q/X2aaPunk/dPbeF0GG+7rFOmWBfSQ=
|
||||
github.com/sagernet/sing-snell v0.0.0-20261125120456-6dfe590430a8/go.mod h1:9O6b0qRI/QjuycrYQVnftrRjztzHUV2qYuX6rmpDtCY=
|
||||
github.com/sagernet/sing-tun v0.9.7-0.20261124071700-111dc3d13db1 h1:nv9FUH+EVTGZtdlH1+pxVx0vIqyd5ScMc6wCTrS6gaw=
|
||||
github.com/sagernet/sing-tun v0.9.7-0.20261124071700-111dc3d13db1/go.mod h1:NqRKOzmp+umzkKtiSRa+AIP6pmCtdCGTUZLb1zscf4I=
|
||||
github.com/sagernet/sing-usbip v0.0.0-20260817040617-28bd42667eca h1:5wA+IE0Fq1CGVLOgSpm0gKKZ03HzugcJ2JyhmDqeX6A=
|
||||
|
||||
+62
-41
@@ -2,6 +2,7 @@ package route
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/netip"
|
||||
@@ -153,15 +154,20 @@ func (m *ConnectionManager) NewConnection(ctx context.Context, this N.Dialer, co
|
||||
}
|
||||
remoteConn = spoofConn
|
||||
}
|
||||
serverFirst := sniff.Skip(&metadata)
|
||||
var done atomic.Bool
|
||||
if m.kickWriteHandshake(ctx, conn, remoteConn, serverFirst, false, &done, onClose) {
|
||||
if closed, _ := m.kickWriteHandshake(ctx, remoteConn, conn, false, true, &done, onClose); closed {
|
||||
return
|
||||
}
|
||||
if m.kickWriteHandshake(ctx, remoteConn, conn, serverFirst, true, &done, onClose) {
|
||||
waitPayload := !sniff.Skip(&metadata) && !E.IsTimeout(metadata.SniffError)
|
||||
closed, sourceEOF := m.kickWriteHandshake(ctx, conn, remoteConn, waitPayload, false, &done, onClose)
|
||||
if closed {
|
||||
return
|
||||
}
|
||||
go m.connectionCopy(ctx, conn, remoteConn, false, &done, onClose)
|
||||
if sourceEOF {
|
||||
go m.finishConnectionCopy(ctx, conn, remoteConn, nil, false, &done, onClose)
|
||||
} else {
|
||||
go m.connectionCopy(ctx, conn, remoteConn, false, &done, onClose)
|
||||
}
|
||||
go m.connectionCopy(ctx, remoteConn, conn, true, &done, onClose)
|
||||
}
|
||||
|
||||
@@ -300,6 +306,10 @@ func (m *ConnectionManager) deleteURLTestHistory(chain []adapter.Outbound, err e
|
||||
|
||||
func (m *ConnectionManager) connectionCopy(ctx context.Context, source net.Conn, destination net.Conn, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
|
||||
_, err := bufio.CopyWithIncreateBuffer(destination, source, bufio.DefaultIncreaseBufferAfter, bufio.DefaultBatchSize)
|
||||
m.finishConnectionCopy(ctx, source, destination, err, direction, done, onClose)
|
||||
}
|
||||
|
||||
func (m *ConnectionManager) finishConnectionCopy(ctx context.Context, source net.Conn, destination net.Conn, err error, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
|
||||
if err != nil {
|
||||
common.Close(source, destination)
|
||||
} else {
|
||||
@@ -339,49 +349,60 @@ func (m *ConnectionManager) connectionCopy(ctx context.Context, source net.Conn,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *ConnectionManager) kickWriteHandshake(ctx context.Context, source net.Conn, destination net.Conn, serverFirst bool, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) bool {
|
||||
func (m *ConnectionManager) kickWriteHandshake(ctx context.Context, source net.Conn, destination net.Conn, waitPayload bool, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) (bool, bool) {
|
||||
if !N.NeedHandshakeForWrite(destination) {
|
||||
return false
|
||||
return false, false
|
||||
}
|
||||
sourceReader, readCounters := N.UnwrapCountReader(source, nil)
|
||||
destinationWriter, writeCounters := N.UnwrapCountWriter(destination, nil)
|
||||
var (
|
||||
err error
|
||||
wrotePayload bool
|
||||
payload *buf.Buffer
|
||||
sourceEOF bool
|
||||
err error
|
||||
)
|
||||
if serverFirst {
|
||||
_ = destination.SetWriteDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
||||
_, err = destination.Write(nil)
|
||||
_ = destination.SetWriteDeadline(time.Time{})
|
||||
} else {
|
||||
var cachedBuffer *buf.Buffer
|
||||
sourceReader, readCounters := N.UnwrapCountReader(source, nil)
|
||||
destinationWriter, writeCounters := N.UnwrapCountWriter(destination, nil)
|
||||
if cachedReader, ok := sourceReader.(N.CachedReader); ok {
|
||||
cachedBuffer = cachedReader.ReadCached()
|
||||
if cachedReader, ok := sourceReader.(N.CachedReader); ok {
|
||||
payload = cachedReader.ReadCached()
|
||||
}
|
||||
if payload == nil && waitPayload && source.SetReadDeadline(time.Now().Add(C.ReadPayloadTimeout)) == nil {
|
||||
sourceReader, readCounters = N.UnwrapCountReader(source, nil)
|
||||
payload = buf.NewPacket()
|
||||
_, err = payload.ReadOnceFrom(sourceReader)
|
||||
_ = source.SetReadDeadline(time.Time{})
|
||||
if errors.Is(err, io.EOF) {
|
||||
sourceEOF = true
|
||||
err = nil
|
||||
} else if E.IsTimeout(err) {
|
||||
err = nil
|
||||
} else if err != nil {
|
||||
err = E.Cause(err, "read payload")
|
||||
}
|
||||
if cachedBuffer != nil {
|
||||
wrotePayload = true
|
||||
dataLen := cachedBuffer.Len()
|
||||
_, err = destinationWriter.Write(cachedBuffer.Bytes())
|
||||
cachedBuffer.Release()
|
||||
if err == nil {
|
||||
for _, counter := range readCounters {
|
||||
counter(int64(dataLen))
|
||||
}
|
||||
for _, counter := range writeCounters {
|
||||
counter(int64(dataLen))
|
||||
}
|
||||
if err != nil || payload.IsEmpty() {
|
||||
payload.Release()
|
||||
payload = nil
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case err != nil:
|
||||
case payload != nil:
|
||||
dataLen := payload.Len()
|
||||
_, err = destinationWriter.Write(payload.Bytes())
|
||||
payload.Release()
|
||||
if err == nil {
|
||||
for _, counter := range readCounters {
|
||||
counter(int64(dataLen))
|
||||
}
|
||||
} else {
|
||||
_ = destination.SetWriteDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
||||
_, err = destinationWriter.Write(nil)
|
||||
_ = destination.SetWriteDeadline(time.Time{})
|
||||
for _, counter := range writeCounters {
|
||||
counter(int64(dataLen))
|
||||
}
|
||||
return false, sourceEOF
|
||||
}
|
||||
default:
|
||||
_ = destination.SetWriteDeadline(time.Now().Add(C.ReadPayloadTimeout))
|
||||
_, err = destinationWriter.Write(nil)
|
||||
_ = destination.SetWriteDeadline(time.Time{})
|
||||
if err == nil || E.IsMulti(err, os.ErrInvalid, context.DeadlineExceeded, io.EOF) || E.IsTimeout(err) {
|
||||
return false, sourceEOF
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if !wrotePayload && (E.IsMulti(err, os.ErrInvalid, context.DeadlineExceeded, io.EOF) || E.IsTimeout(err)) {
|
||||
return false
|
||||
}
|
||||
if !done.Swap(true) {
|
||||
if onClose != nil {
|
||||
@@ -394,7 +415,7 @@ func (m *ConnectionManager) kickWriteHandshake(ctx context.Context, source net.C
|
||||
} else {
|
||||
m.logger.ErrorContext(ctx, "connection download handshake: ", err)
|
||||
}
|
||||
return true
|
||||
return true, false
|
||||
}
|
||||
|
||||
func (m *ConnectionManager) packetConnectionCopy(ctx context.Context, source N.PacketReader, destination N.PacketWriter, direction bool, done *atomic.Bool, onClose N.CloseHandlerFunc) {
|
||||
|
||||
Reference in New Issue
Block a user