Merge branch 'main' of github.com:kulikov0/whitelist-bypass

This commit is contained in:
kulikov0 committed 2026-08-31 23:05:10 +02:00
commit da3bf64efc
2 files changed
+136 -50

No files matched your search

+39 -2
View File
@@ -12,8 +12,10 @@ import (
)
const (
kcpConvBase = 0x77627374
kcpUpdateInterval = 10 * time.Millisecond
kcpConvBase = 0x77627374
kcpUpdateInterval = 10 * time.Millisecond
kcpIdleUpdateInterval = 500 * time.Millisecond
kcpIdleAfterTicks = 50
// 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
// would discard whole. 1200 RTP budget - 1 VP8 descriptor - interframe
@@ -146,6 +148,7 @@ type MultiTrackKCPTunnel struct {
stopCh chan struct{}
stopOnce sync.Once
nudge chan struct{}
currentWindow atomic.Int32
@@ -165,6 +168,7 @@ func NewMultiTrackKCPTunnel(mt *MultiTrackTunnel, logFn func(string, ...any)) *M
convMap: make(map[uint32]*trackKCPSession),
connPin: make(map[uint32]int),
stopCh: make(chan struct{}),
nudge: make(chan struct{}, 1),
}
subs := mt.SubTunnels()
window := kcpWindowFloor
@@ -198,6 +202,7 @@ func (t *MultiTrackKCPTunnel) SendData(frame []byte) {
t.sendRaw(connID, frame)
return
}
t.wake()
t.mu.Lock()
if len(t.sessions) == 0 {
@@ -285,6 +290,7 @@ func (t *MultiTrackKCPTunnel) handleDecodedSegment(payload []byte) {
return
}
t.inputSegments.Add(1)
t.wake()
messages := session.input(body)
if callback == nil {
return
@@ -368,21 +374,52 @@ func (t *MultiTrackKCPTunnel) handleInnerClose() {
}
}
func (t *MultiTrackKCPTunnel) wake() {
select {
case t.nudge <- struct{}{}:
default:
}
}
func (t *MultiTrackKCPTunnel) updateLoop() {
ticker := time.NewTicker(kcpUpdateInterval)
defer ticker.Stop()
ticks := 0
idleTicks := 0
fast := true
for {
select {
case <-t.stopCh:
return
case <-t.nudge:
if !fast {
fast = true
idleTicks = 0
ticker.Reset(kcpUpdateInterval)
}
case <-ticker.C:
t.mu.Lock()
sessions := make([]*trackKCPSession, len(t.sessions))
copy(sessions, t.sessions)
t.mu.Unlock()
pending := 0
for _, session := range sessions {
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++
if common.Debug && ticks%kcpStatsEvery == 0 && t.logFn != nil {
+97 -48
View File
@@ -23,6 +23,8 @@ const (
paceBatchFloorPercent = 80
paceDriftMin = 5 * time.Second
paceDriftMax = 20 * time.Second
idleSpinTicks = 40
)
type VP8DataTunnel struct {
@@ -238,71 +240,118 @@ func (t *VP8DataTunnel) writerLoop() {
ticker := time.NewTicker(sampleInterval)
drift := time.NewTimer(common.DurationInRange(paceDriftMin, paceDriftMax))
idle := time.NewTimer(time.Hour)
if !idle.Stop() {
<-idle.C
}
idleTicks := 0
spinning := true
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 {
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 {
case <-t.stopCh:
ticker.Stop()
drift.Stop()
idle.Stop()
return
case <-t.cfgChan:
reconfigure = true
case <-drift.C:
pacedBatch = pacedBatchFor(batch)
sampleInterval = sampleIntervalFor(fps, pacedBatch)
repace()
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)
case <-idle.C:
idleTicks = 0
emit(t.obf.EncodeKeepalive(keepalivePad), true)
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)
}
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)
}
idle.Reset(time.Duration(keepaliveEvery) * sampleInterval)
}
}
ticker.Stop()
drift.Stop()
idle.Stop()
}
}