0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-24 15:38:36 +00:00
This commit is contained in:
Meo597 2025-06-08 19:57:06 +08:00
parent d405a94c95
commit 931974b633

28
tls.go
View File

@ -124,6 +124,10 @@ func NewRatelimitedConn(conn net.Conn, limit *LimitFallback) net.Conn {
burstBytesPerSec := limit.BurstBytesPerSec burstBytesPerSec := limit.BurstBytesPerSec
afterBytes := limit.AfterBytes afterBytes := limit.AfterBytes
if bytesPerSec == 0 {
return conn
}
if burstBytesPerSec < bytesPerSec { if burstBytesPerSec < bytesPerSec {
burstBytesPerSec = bytesPerSec burstBytesPerSec = bytesPerSec
} }
@ -263,12 +267,8 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
if config.Show && hs.clientHello != nil { if config.Show && hs.clientHello != nil {
fmt.Printf("REALITY remoteAddr: %v\tforwarded SNI: %v\n", remoteAddr, hs.clientHello.serverName) fmt.Printf("REALITY remoteAddr: %v\tforwarded SNI: %v\n", remoteAddr, hs.clientHello.serverName)
} }
if config.LimitFallbackUpload.BytesPerSec == 0 { // Limit upload speed for fallback connection
io.Copy(target, underlying) io.Copy(target, NewRatelimitedConn(underlying, &config.LimitFallbackUpload))
} else {
// Limit upload speed for fallback connection
io.Copy(target, NewRatelimitedConn(underlying, &config.LimitFallbackUpload))
}
} }
waitGroup.Done() waitGroup.Done()
}() }()
@ -399,22 +399,14 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
if hs.c.conn == conn { // if we processed the Client Hello successfully but the target did not if hs.c.conn == conn { // if we processed the Client Hello successfully but the target did not
waitGroup.Add(1) waitGroup.Add(1)
go func() { go func() {
if config.LimitFallbackUpload.BytesPerSec == 0 { // Limit upload speed for fallback connection (handshake ok but hello failed)
io.Copy(target, underlying) io.Copy(target, NewRatelimitedConn(underlying, &config.LimitFallbackUpload))
} else {
// Limit upload speed for fallback connection (handshake ok but hello failed)
io.Copy(target, NewRatelimitedConn(underlying, &config.LimitFallbackUpload))
}
waitGroup.Done() waitGroup.Done()
}() }()
} }
conn.Write(s2cSaved) conn.Write(s2cSaved)
if config.LimitFallbackDownload.BytesPerSec == 0 { // Limit download speed for fallback connection
io.Copy(underlying, target) io.Copy(underlying, NewRatelimitedConn(target, &config.LimitFallbackDownload))
} else {
// Limit download speed for fallback connection
io.Copy(underlying, NewRatelimitedConn(target, &config.LimitFallbackDownload))
}
// Here is bidirectional direct forwarding: // Here is bidirectional direct forwarding:
// client ---underlying--- server ---target--- dest // client ---underlying--- server ---target--- dest
// Call `underlying.CloseWrite()` once `io.Copy()` returned // Call `underlying.CloseWrite()` once `io.Copy()` returned