0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-29 09:55:32 +00:00
This commit is contained in:
Meo597 2025-06-08 19:30:25 +08:00
parent 82f8367c40
commit 39d54e1d67

35
tls.go
View File

@ -43,7 +43,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math"
"net" "net"
"os" "os"
"runtime" "runtime"
@ -120,18 +119,20 @@ func (c *RatelimitedConn) Read(b []byte) (int, error) {
return n, err return n, err
} }
func NewBucketWithRate(bytesPerSec uint64, burstBytesPerSec uint64) *ratelimit.Bucket { func NewRatelimitedConn(con net.Conn, config *Config) *RatelimitedConn {
bytesPerSec := config.LimitFallbackUpload.BytesPerSec
burstBytesPerSec := config.LimitFallbackUpload.BurstBytesPerSec
afterBytes := config.LimitFallbackUpload.AfterBytes
if burstBytesPerSec < bytesPerSec { if burstBytesPerSec < bytesPerSec {
burstBytesPerSec = bytesPerSec burstBytesPerSec = bytesPerSec
} }
return ratelimit.NewBucketWithRate(float64(bytesPerSec), ToInt64(burstBytesPerSec))
}
func ToInt64(u uint64) int64 { return &RatelimitedConn{
if u > math.MaxInt64 { Conn: con,
return math.MaxInt64 Bucket: ratelimit.NewBucketWithRate(float64(bytesPerSec), int64(burstBytesPerSec)),
LimitAfter: int64(afterBytes) - int64(burstBytesPerSec),
} }
return int64(u)
} }
var ( var (
@ -266,11 +267,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(target, underlying) io.Copy(target, underlying)
} else { } else {
// Limit upload speed for fallback connection // Limit upload speed for fallback connection
io.Copy(target, &RatelimitedConn{ io.Copy(target, NewRatelimitedConn(underlying, config))
Conn: underlying,
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackUpload.AfterBytes) - ToInt64(config.LimitFallbackUpload.BurstBytesPerSec),
})
} }
} }
waitGroup.Done() waitGroup.Done()
@ -406,11 +403,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(target, underlying) io.Copy(target, underlying)
} else { } else {
// Limit upload speed for fallback connection (handshake ok but hello failed) // Limit upload speed for fallback connection (handshake ok but hello failed)
io.Copy(target, &RatelimitedConn{ io.Copy(target, NewRatelimitedConn(underlying, config))
Conn: underlying,
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackUpload.AfterBytes) - ToInt64(config.LimitFallbackUpload.BurstBytesPerSec),
})
} }
waitGroup.Done() waitGroup.Done()
}() }()
@ -420,11 +413,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(underlying, target) io.Copy(underlying, target)
} else { } else {
// Limit download speed for fallback connection // Limit download speed for fallback connection
io.Copy(underlying, &RatelimitedConn{ io.Copy(underlying, NewRatelimitedConn(target, config))
Conn: target,
Bucket: NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackDownload.AfterBytes) - ToInt64(config.LimitFallbackDownload.BurstBytesPerSec),
})
} }
// Here is bidirectional direct forwarding: // Here is bidirectional direct forwarding:
// client ---underlying--- server ---target--- dest // client ---underlying--- server ---target--- dest