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:58:39 +08:00
parent 8a58ed1c1a
commit 82f8367c40

22
tls.go
View File

@ -108,8 +108,8 @@ type RatelimitedConn struct {
LimitAfter int64
}
func (c *RatelimitedConn) Write(b []byte) (int, error) {
n, err := c.Conn.Write(b)
func (c *RatelimitedConn) Read(b []byte) (int, error) {
n, err := c.Conn.Read(b)
if n != 0 {
if c.LimitAfter <= 0 {
c.Bucket.Wait(int64(n))
@ -266,11 +266,11 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(target, underlying)
} else {
// Limit upload speed for fallback connection
io.Copy(&RatelimitedConn{
Conn: target,
io.Copy(target, &RatelimitedConn{
Conn: underlying,
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackUpload.AfterBytes) - ToInt64(config.LimitFallbackUpload.BurstBytesPerSec),
}, underlying)
})
}
}
waitGroup.Done()
@ -406,11 +406,11 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(target, underlying)
} else {
// Limit upload speed for fallback connection (handshake ok but hello failed)
io.Copy(&RatelimitedConn{
Conn: target,
io.Copy(target, &RatelimitedConn{
Conn: underlying,
Bucket: NewBucketWithRate(config.LimitFallbackUpload.BytesPerSec, config.LimitFallbackUpload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackUpload.AfterBytes) - ToInt64(config.LimitFallbackUpload.BurstBytesPerSec),
}, underlying)
})
}
waitGroup.Done()
}()
@ -420,11 +420,11 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
io.Copy(underlying, target)
} else {
// Limit download speed for fallback connection
io.Copy(&RatelimitedConn{
Conn: underlying,
io.Copy(underlying, &RatelimitedConn{
Conn: target,
Bucket: NewBucketWithRate(config.LimitFallbackDownload.BytesPerSec, config.LimitFallbackDownload.BurstBytesPerSec),
LimitAfter: ToInt64(config.LimitFallbackDownload.AfterBytes) - ToInt64(config.LimitFallbackDownload.BurstBytesPerSec),
}, target)
})
}
// Here is bidirectional direct forwarding:
// client ---underlying--- server ---target--- dest