0
0
mirror of https://github.com/XTLS/REALITY.git synced 2025-08-22 14:38:35 +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"
"fmt"
"io"
"math"
"net"
"os"
"runtime"
@ -119,6 +120,13 @@ func (c *RatelimitedConn) Write(b []byte) (int, error) {
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 (
size = 8192
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
io.Copy(&RatelimitedConn{
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,
}, 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)
io.Copy(&RatelimitedConn{
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,
}, underlying)
}
@ -407,7 +415,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
// Limit download speed for fallback connection
io.Copy(&RatelimitedConn{
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,
}, target)
}