0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-23 06:58:39 +00:00
This commit is contained in:
Meo597 2025-06-08 18:09:22 +08:00
parent 1eecf21a00
commit 766b7c6936

14
tls.go
View File

@ -43,6 +43,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math"
"net" "net"
"os" "os"
"runtime" "runtime"
@ -119,6 +120,13 @@ func (c *RatelimitedConn) Write(b []byte) (int, error) {
return n, err return n, err
} }
func NewBucketWithRate(bytesPerSec float64, burstBytesPerSec int64) *ratelimit.Bucket {
if burstBytesPerSec < int64(math.Ceil(bytesPerSec)) {
burstBytesPerSec = int64(math.Ceil(bytesPerSec))
}
return ratelimit.NewBucketWithRate(bytesPerSec, burstBytesPerSec)
}
var ( var (
size = 8192 size = 8192
empty = make([]byte, size) empty = make([]byte, size)
@ -253,7 +261,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
// Limit upload speed for fallback connection // Limit upload speed for fallback connection
io.Copy(&RatelimitedConn{ io.Copy(&RatelimitedConn{
Conn: target, Conn: target,
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec), Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec, LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec,
}, underlying) }, underlying)
} }
@ -393,7 +401,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
// Limit upload speed for fallback connection (handshake ok but hello failed) // Limit upload speed for fallback connection (handshake ok but hello failed)
io.Copy(&RatelimitedConn{ io.Copy(&RatelimitedConn{
Conn: target, Conn: target,
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec), Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec, LimitAfter: config.LimitFallbackUpload.AfterBytes - config.LimitFallbackUpload.BurstBytesPerSec,
}, underlying) }, underlying)
} }
@ -407,7 +415,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
// Limit download speed for fallback connection // Limit download speed for fallback connection
io.Copy(&RatelimitedConn{ io.Copy(&RatelimitedConn{
Conn: underlying, Conn: underlying,
Bucket: ratelimit.NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec), Bucket: NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec),
LimitAfter: config.LimitFallbackDownload.AfterBytes - config.LimitFallbackDownload.BurstBytesPerSec, LimitAfter: config.LimitFallbackDownload.AfterBytes - config.LimitFallbackDownload.BurstBytesPerSec,
}, target) }, target)
} }