mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-07-14 13:10:13 -04:00
add server side first write delay
This commit is contained in:
committed by
Xiaokang Wang (Shelikhoo)
parent
5d08dc5a13
commit
29f16325d2
@@ -32,6 +32,9 @@ type connState struct {
|
||||
|
||||
protocolVersion [2]byte
|
||||
|
||||
firstWrite bool
|
||||
firstWriteDelay time.Duration
|
||||
|
||||
transportLayerPadding *TransportLayerPadding
|
||||
}
|
||||
|
||||
@@ -66,6 +69,16 @@ func (s *connState) Read(b []byte) (n int, err error) {
|
||||
}
|
||||
|
||||
func (s *connState) Write(b []byte) (n int, err error) {
|
||||
if s.firstWrite {
|
||||
firstWriteDelayTimer := time.NewTimer(s.firstWriteDelay)
|
||||
defer firstWriteDelayTimer.Stop()
|
||||
select {
|
||||
case <-s.ctx.Done():
|
||||
return 0, s.ctx.Err()
|
||||
case <-firstWriteDelayTimer.C:
|
||||
s.firstWrite = false
|
||||
}
|
||||
}
|
||||
if s.transportLayerPadding != nil && s.transportLayerPadding.Enabled {
|
||||
b = Pack(b, 0)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
cryptoRand "crypto/rand"
|
||||
"math/big"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -77,14 +79,29 @@ func (s *Server) Addr() net.Addr {
|
||||
|
||||
func (s *Server) accept(clientConn net.Conn, serverConn net.Conn) {
|
||||
ctx, cancel := context.WithCancel(s.ctx)
|
||||
|
||||
firstWriteDelay := time.Duration(0)
|
||||
if s.config.DeferFirstPayloadWriteTime != nil {
|
||||
uniformRandomAdd := big.NewInt(int64(s.config.DeferFirstPayloadWriteTime.UniformRandomMultiplierNanoseconds))
|
||||
uniformRandomAddBigInt, err := cryptoRand.Int(cryptoRand.Reader, uniformRandomAdd)
|
||||
if err != nil {
|
||||
newError("failed to generate random delay").Base(err).AtWarning().WriteToLog()
|
||||
return
|
||||
}
|
||||
uniformRandomAddU64 := uint64(uniformRandomAddBigInt.Int64())
|
||||
firstWriteDelay = time.Duration(s.config.DeferFirstPayloadWriteTime.BaseNanoseconds + uniformRandomAddU64)
|
||||
}
|
||||
|
||||
conn := &connState{
|
||||
ctx: ctx,
|
||||
done: cancel,
|
||||
localAddr: clientConn.LocalAddr(),
|
||||
remoteAddr: clientConn.RemoteAddr(),
|
||||
primaryKey: s.config.PrimaryKey,
|
||||
handler: s.onIncomingReadyConnection,
|
||||
readPipe: make(chan []byte, 1),
|
||||
ctx: ctx,
|
||||
done: cancel,
|
||||
localAddr: clientConn.LocalAddr(),
|
||||
remoteAddr: clientConn.RemoteAddr(),
|
||||
primaryKey: s.config.PrimaryKey,
|
||||
handler: s.onIncomingReadyConnection,
|
||||
readPipe: make(chan []byte, 1),
|
||||
firstWrite: true,
|
||||
firstWriteDelay: firstWriteDelay,
|
||||
}
|
||||
|
||||
conn.mirrorConn = mirrorbase.NewMirroredTLSConn(ctx, clientConn, serverConn, conn.onC2SMessage, nil, conn,
|
||||
|
||||
Reference in New Issue
Block a user