Merge branch 'main' of github.com:kulikov0/whitelist-bypass
This commit is contained in:
commit
da3bf64efc
2 files changed
+136
-50
No files matched your search
@@ -12,8 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
kcpConvBase = 0x77627374
|
kcpConvBase = 0x77627374
|
||||||
kcpUpdateInterval = 10 * time.Millisecond
|
kcpUpdateInterval = 10 * time.Millisecond
|
||||||
|
kcpIdleUpdateInterval = 500 * time.Millisecond
|
||||||
|
kcpIdleAfterTicks = 50
|
||||||
// One KCP segment must ride in a single RTP packet so a dropped packet
|
// One KCP segment must ride in a single RTP packet so a dropped packet
|
||||||
// loses only its own frame, not a two-packet frame that readVP8Track
|
// loses only its own frame, not a two-packet frame that readVP8Track
|
||||||
// would discard whole. 1200 RTP budget - 1 VP8 descriptor - interframe
|
// would discard whole. 1200 RTP budget - 1 VP8 descriptor - interframe
|
||||||
@@ -146,6 +148,7 @@ type MultiTrackKCPTunnel struct {
|
|||||||
|
|
||||||
stopCh chan struct{}
|
stopCh chan struct{}
|
||||||
stopOnce sync.Once
|
stopOnce sync.Once
|
||||||
|
nudge chan struct{}
|
||||||
|
|
||||||
currentWindow atomic.Int32
|
currentWindow atomic.Int32
|
||||||
|
|
||||||
@@ -165,6 +168,7 @@ func NewMultiTrackKCPTunnel(mt *MultiTrackTunnel, logFn func(string, ...any)) *M
|
|||||||
convMap: make(map[uint32]*trackKCPSession),
|
convMap: make(map[uint32]*trackKCPSession),
|
||||||
connPin: make(map[uint32]int),
|
connPin: make(map[uint32]int),
|
||||||
stopCh: make(chan struct{}),
|
stopCh: make(chan struct{}),
|
||||||
|
nudge: make(chan struct{}, 1),
|
||||||
}
|
}
|
||||||
subs := mt.SubTunnels()
|
subs := mt.SubTunnels()
|
||||||
window := kcpWindowFloor
|
window := kcpWindowFloor
|
||||||
@@ -198,6 +202,7 @@ func (t *MultiTrackKCPTunnel) SendData(frame []byte) {
|
|||||||
t.sendRaw(connID, frame)
|
t.sendRaw(connID, frame)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
t.wake()
|
||||||
|
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
if len(t.sessions) == 0 {
|
if len(t.sessions) == 0 {
|
||||||
@@ -285,6 +290,7 @@ func (t *MultiTrackKCPTunnel) handleDecodedSegment(payload []byte) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
t.inputSegments.Add(1)
|
t.inputSegments.Add(1)
|
||||||
|
t.wake()
|
||||||
messages := session.input(body)
|
messages := session.input(body)
|
||||||
if callback == nil {
|
if callback == nil {
|
||||||
return
|
return
|
||||||
@@ -368,21 +374,52 @@ func (t *MultiTrackKCPTunnel) handleInnerClose() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *MultiTrackKCPTunnel) wake() {
|
||||||
|
select {
|
||||||
|
case t.nudge <- struct{}{}:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (t *MultiTrackKCPTunnel) updateLoop() {
|
func (t *MultiTrackKCPTunnel) updateLoop() {
|
||||||
ticker := time.NewTicker(kcpUpdateInterval)
|
ticker := time.NewTicker(kcpUpdateInterval)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
ticks := 0
|
ticks := 0
|
||||||
|
idleTicks := 0
|
||||||
|
fast := true
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-t.stopCh:
|
case <-t.stopCh:
|
||||||
return
|
return
|
||||||
|
case <-t.nudge:
|
||||||
|
if !fast {
|
||||||
|
fast = true
|
||||||
|
idleTicks = 0
|
||||||
|
ticker.Reset(kcpUpdateInterval)
|
||||||
|
}
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
t.mu.Lock()
|
t.mu.Lock()
|
||||||
sessions := make([]*trackKCPSession, len(t.sessions))
|
sessions := make([]*trackKCPSession, len(t.sessions))
|
||||||
copy(sessions, t.sessions)
|
copy(sessions, t.sessions)
|
||||||
t.mu.Unlock()
|
t.mu.Unlock()
|
||||||
|
pending := 0
|
||||||
for _, session := range sessions {
|
for _, session := range sessions {
|
||||||
session.update()
|
session.update()
|
||||||
|
pending += session.waitSnd()
|
||||||
|
}
|
||||||
|
if pending > 0 {
|
||||||
|
idleTicks = 0
|
||||||
|
if !fast {
|
||||||
|
fast = true
|
||||||
|
ticker.Reset(kcpUpdateInterval)
|
||||||
|
}
|
||||||
|
} else if fast {
|
||||||
|
idleTicks++
|
||||||
|
if idleTicks >= kcpIdleAfterTicks {
|
||||||
|
fast = false
|
||||||
|
idleTicks = 0
|
||||||
|
ticker.Reset(kcpIdleUpdateInterval)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ticks++
|
ticks++
|
||||||
if common.Debug && ticks%kcpStatsEvery == 0 && t.logFn != nil {
|
if common.Debug && ticks%kcpStatsEvery == 0 && t.logFn != nil {
|
||||||
|
|||||||
+97
-48
@@ -23,6 +23,8 @@ const (
|
|||||||
paceBatchFloorPercent = 80
|
paceBatchFloorPercent = 80
|
||||||
paceDriftMin = 5 * time.Second
|
paceDriftMin = 5 * time.Second
|
||||||
paceDriftMax = 20 * time.Second
|
paceDriftMax = 20 * time.Second
|
||||||
|
|
||||||
|
idleSpinTicks = 40
|
||||||
)
|
)
|
||||||
|
|
||||||
type VP8DataTunnel struct {
|
type VP8DataTunnel struct {
|
||||||
@@ -238,71 +240,118 @@ func (t *VP8DataTunnel) writerLoop() {
|
|||||||
|
|
||||||
ticker := time.NewTicker(sampleInterval)
|
ticker := time.NewTicker(sampleInterval)
|
||||||
drift := time.NewTimer(common.DurationInRange(paceDriftMin, paceDriftMax))
|
drift := time.NewTimer(common.DurationInRange(paceDriftMin, paceDriftMax))
|
||||||
|
idle := time.NewTimer(time.Hour)
|
||||||
|
if !idle.Stop() {
|
||||||
|
<-idle.C
|
||||||
|
}
|
||||||
idleTicks := 0
|
idleTicks := 0
|
||||||
|
spinning := true
|
||||||
reconfigure := false
|
reconfigure := false
|
||||||
|
|
||||||
|
emit := func(sample []byte, isKeepalive bool) {
|
||||||
|
if sample == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if t.WriteFrame != nil {
|
||||||
|
if err := t.WriteFrame(sample); err != nil {
|
||||||
|
if common.Debug {
|
||||||
|
t.logFn("vp8tunnel: WriteFrame error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else if err := t.track.WriteSample(media.Sample{Data: sample, Duration: sampleInterval}); err != nil {
|
||||||
|
if common.Debug {
|
||||||
|
t.logFn("vp8tunnel: WriteSample error: %v", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n := t.sentFrames.Add(1)
|
||||||
|
if isKeepalive {
|
||||||
|
t.keepaliveFrames.Add(1)
|
||||||
|
}
|
||||||
|
if common.Debug && (n <= 5 || n%500 == 0) {
|
||||||
|
keepalives := t.keepaliveFrames.Load()
|
||||||
|
t.logFn("vp8tunnel: sent frame #%d size=%d data=%d keepalive=%d", n, len(sample), n-keepalives, keepalives)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repace := func() {
|
||||||
|
pacedBatch = pacedBatchFor(batch)
|
||||||
|
sampleInterval = sampleIntervalFor(fps, pacedBatch)
|
||||||
|
if spinning {
|
||||||
|
ticker.Reset(sampleInterval)
|
||||||
|
}
|
||||||
|
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
|
||||||
|
drift.Reset(common.DurationInRange(paceDriftMin, paceDriftMax))
|
||||||
|
if common.Debug {
|
||||||
|
t.logFn("vp8tunnel: pace drift pacedBatch=%d/%d sampleInterval=%s", pacedBatch, batch, sampleInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for !reconfigure {
|
for !reconfigure {
|
||||||
|
if spinning {
|
||||||
|
select {
|
||||||
|
case <-t.stopCh:
|
||||||
|
ticker.Stop()
|
||||||
|
drift.Stop()
|
||||||
|
idle.Stop()
|
||||||
|
return
|
||||||
|
case <-t.cfgChan:
|
||||||
|
reconfigure = true
|
||||||
|
case <-drift.C:
|
||||||
|
repace()
|
||||||
|
case <-ticker.C:
|
||||||
|
select {
|
||||||
|
case data := <-t.sendQueue:
|
||||||
|
emit(t.obf.EncodeData(data), false)
|
||||||
|
idleTicks = 0
|
||||||
|
default:
|
||||||
|
idleTicks++
|
||||||
|
switch {
|
||||||
|
case idleTicks >= keepaliveEvery:
|
||||||
|
idleTicks = 0
|
||||||
|
emit(t.obf.EncodeKeepalive(keepalivePad), true)
|
||||||
|
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
|
||||||
|
case idleTicks >= idleSpinTicks:
|
||||||
|
ticker.Stop()
|
||||||
|
spinning = false
|
||||||
|
idle.Reset(time.Duration(keepaliveEvery-idleTicks) * sampleInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-t.stopCh:
|
case <-t.stopCh:
|
||||||
ticker.Stop()
|
|
||||||
drift.Stop()
|
drift.Stop()
|
||||||
|
idle.Stop()
|
||||||
return
|
return
|
||||||
case <-t.cfgChan:
|
case <-t.cfgChan:
|
||||||
reconfigure = true
|
reconfigure = true
|
||||||
case <-drift.C:
|
case <-drift.C:
|
||||||
pacedBatch = pacedBatchFor(batch)
|
repace()
|
||||||
sampleInterval = sampleIntervalFor(fps, pacedBatch)
|
case data := <-t.sendQueue:
|
||||||
|
if !idle.Stop() {
|
||||||
|
select {
|
||||||
|
case <-idle.C:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
emit(t.obf.EncodeData(data), false)
|
||||||
|
idleTicks = 0
|
||||||
|
spinning = true
|
||||||
ticker.Reset(sampleInterval)
|
ticker.Reset(sampleInterval)
|
||||||
|
case <-idle.C:
|
||||||
|
idleTicks = 0
|
||||||
|
emit(t.obf.EncodeKeepalive(keepalivePad), true)
|
||||||
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
|
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
|
||||||
drift.Reset(common.DurationInRange(paceDriftMin, paceDriftMax))
|
idle.Reset(time.Duration(keepaliveEvery) * sampleInterval)
|
||||||
if common.Debug {
|
|
||||||
t.logFn("vp8tunnel: pace drift pacedBatch=%d/%d sampleInterval=%s", pacedBatch, batch, sampleInterval)
|
|
||||||
}
|
|
||||||
case <-ticker.C:
|
|
||||||
var sample []byte
|
|
||||||
isKeepalive := false
|
|
||||||
select {
|
|
||||||
case data := <-t.sendQueue:
|
|
||||||
sample = t.obf.EncodeData(data)
|
|
||||||
idleTicks = 0
|
|
||||||
default:
|
|
||||||
idleTicks++
|
|
||||||
if idleTicks < keepaliveEvery {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
idleTicks = 0
|
|
||||||
sample = t.obf.EncodeKeepalive(keepalivePad)
|
|
||||||
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
|
|
||||||
isKeepalive = true
|
|
||||||
}
|
|
||||||
if sample == nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if t.WriteFrame != nil {
|
|
||||||
if err := t.WriteFrame(sample); err != nil {
|
|
||||||
if common.Debug {
|
|
||||||
t.logFn("vp8tunnel: WriteFrame error: %v", err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else if err := t.track.WriteSample(media.Sample{Data: sample, Duration: sampleInterval}); err != nil {
|
|
||||||
if common.Debug {
|
|
||||||
t.logFn("vp8tunnel: WriteSample error: %v", err)
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
n := t.sentFrames.Add(1)
|
|
||||||
if isKeepalive {
|
|
||||||
t.keepaliveFrames.Add(1)
|
|
||||||
}
|
|
||||||
if common.Debug && (n <= 5 || n%500 == 0) {
|
|
||||||
keepalives := t.keepaliveFrames.Load()
|
|
||||||
t.logFn("vp8tunnel: sent frame #%d size=%d data=%d keepalive=%d", n, len(sample), n-keepalives, keepalives)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
drift.Stop()
|
drift.Stop()
|
||||||
|
idle.Stop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user