fix config ack, add randomness for keyframes and interframes

This commit is contained in:
kulikov0 committed 2026-07-26 20:30:48 +02:00
1 parent c36c561e35
commit 2bad03c542
17 files changed
+312 -109

No files matched your search

+14 -3
View File
@@ -111,8 +111,17 @@ func (o *TunnelObfuscator) dataHeader() []byte {
return hdr
}
func (o *TunnelObfuscator) EncodeKeepalive() []byte {
return o.keepaliveHeader()
func (o *TunnelObfuscator) EncodeKeepalive(padLen int) []byte {
hdr := o.keepaliveHeader()
if padLen <= 0 {
return hdr
}
out := make([]byte, keepaliveHdrLen+padLen)
copy(out, hdr)
if _, err := rand.Read(out[keepaliveHdrLen:]); err != nil {
return hdr
}
return out
}
func (o *TunnelObfuscator) EncodeData(payload []byte) []byte {
@@ -163,10 +172,12 @@ func (o *TunnelObfuscator) Decode(frame []byte) DecodeResult {
return DecodeResult{}
}
var hdrLen, epochOff int
isKeepaliveFrame := false
switch frame[0] {
case vp8Keepalive[0]:
hdrLen = keepaliveHdrLen
epochOff = vp8KeepaliveLen
isKeepaliveFrame = true
case vp8Interframe[0]:
hdrLen = interframeHdrLen
epochOff = vp8InterframeLen
@@ -192,7 +203,7 @@ func (o *TunnelObfuscator) Decode(frame []byte) DecodeResult {
}
o.mu.Unlock()
if len(frame) == hdrLen {
if isKeepaliveFrame || len(frame) == hdrLen {
res.Keepalive = true
return res
}
+18 -11
View File
@@ -10,10 +10,11 @@ import (
)
const (
screenWriterFPS = 24
screenWriterBatch = 30
screenWriterMaxBytes = 60000
screenWriterQueue = 256
screenWriterFPS = 24
screenWriterBatch = 30
screenWriterMaxBytes = 60000
screenWriterQueue = 256
screenKeepalivePadMax = 48
)
type ScreenWriter struct {
@@ -89,7 +90,7 @@ func (w *ScreenWriter) Reconfigure(fps, batch int) {
}
}
func (w *ScreenWriter) interval() (time.Duration, int) {
func (w *ScreenWriter) interval() time.Duration {
w.cfgMu.Lock()
fps, batch := w.fps, w.batch
w.cfgMu.Unlock()
@@ -101,11 +102,15 @@ func (w *ScreenWriter) interval() (time.Duration, int) {
if sample <= 0 {
sample = time.Millisecond
}
keepaliveEvery := int((100 * time.Millisecond) / sample)
if keepaliveEvery < 1 {
keepaliveEvery = 1
return sample
}
func (w *ScreenWriter) nextKeepalive(sample time.Duration) (ticks, padLen int) {
ticks = int(common.DurationInRange(keepaliveIdleMin, keepaliveIdleMax) / sample)
if ticks < 1 {
ticks = 1
}
return sample, keepaliveEvery
return ticks, common.IntInRange(0, screenKeepalivePadMax)
}
func (w *ScreenWriter) Start() {
@@ -143,7 +148,8 @@ func (w *ScreenWriter) emit(msg []byte) {
func (w *ScreenWriter) writerLoop() {
for {
sample, keepaliveEvery := w.interval()
sample := w.interval()
keepaliveEvery, keepalivePad := w.nextKeepalive(sample)
ticker := time.NewTicker(sample)
idle := 0
reconfigure := false
@@ -165,7 +171,8 @@ func (w *ScreenWriter) writerLoop() {
continue
}
idle = 0
w.emit(w.obf.EncodeKeepalive())
w.emit(w.obf.EncodeKeepalive(keepalivePad))
keepaliveEvery, keepalivePad = w.nextKeepalive(sample)
}
}
}
+93 -27
View File
@@ -15,7 +15,14 @@ const (
defaultVP8FPS = 24
defaultVP8Batch = 30
keepaliveIdlePeriod = 100 * time.Millisecond
keepaliveIdleMin = 60 * time.Millisecond
keepaliveIdleMax = 200 * time.Millisecond
keepalivePadMax = 176
sendQueueDepth = 128
paceBatchFloorPercent = 80
paceDriftMin = 5 * time.Second
paceDriftMax = 20 * time.Second
)
type VP8DataTunnel struct {
@@ -29,9 +36,12 @@ type VP8DataTunnel struct {
stopOnce sync.Once
running atomic.Bool
cfgMu sync.Mutex
fps int
batch int
cfgMu sync.Mutex
fps int
batch int
keepaliveMin time.Duration
keepaliveMax time.Duration
keepalivePadMax int
sentFrames atomic.Uint64
recvFrames atomic.Uint64
@@ -55,17 +65,47 @@ func NewVP8DataTunnelWithQueue(track *webrtc.TrackLocalStaticSample, obf *Tunnel
queueDepth = sendQueueDepth
}
return &VP8DataTunnel{
track: track,
obf: obf,
logFn: logFn,
stopCh: make(chan struct{}),
sendQueue: make(chan []byte, queueDepth),
cfgChan: make(chan struct{}, 1),
fps: defaultVP8FPS,
batch: defaultVP8Batch,
track: track,
obf: obf,
logFn: logFn,
stopCh: make(chan struct{}),
sendQueue: make(chan []byte, queueDepth),
cfgChan: make(chan struct{}, 1),
fps: defaultVP8FPS,
batch: defaultVP8Batch,
keepaliveMin: keepaliveIdleMin,
keepaliveMax: keepaliveIdleMax,
keepalivePadMax: keepalivePadMax,
}
}
func (t *VP8DataTunnel) SetKeepaliveShape(minPeriod, maxPeriod time.Duration, padMax int) {
t.cfgMu.Lock()
if minPeriod > 0 {
t.keepaliveMin = minPeriod
}
if maxPeriod >= t.keepaliveMin {
t.keepaliveMax = maxPeriod
}
if padMax >= 0 {
t.keepalivePadMax = padMax
}
newMin, newMax, newPad := t.keepaliveMin, t.keepaliveMax, t.keepalivePadMax
t.cfgMu.Unlock()
t.logFn("vp8tunnel: keepalive shape min=%s max=%s padMax=%d", newMin, newMax, newPad)
}
func (t *VP8DataTunnel) nextKeepalive(sampleInterval time.Duration) (ticks, padLen int) {
t.cfgMu.Lock()
minPeriod, maxPeriod, padMax := t.keepaliveMin, t.keepaliveMax, t.keepalivePadMax
t.cfgMu.Unlock()
ticks = int(common.DurationInRange(minPeriod, maxPeriod) / sampleInterval)
if ticks < 1 {
ticks = 1
}
return ticks, common.IntInRange(0, padMax)
}
func (t *VP8DataTunnel) Reconfigure(fps, batch int) {
if fps <= 0 && batch <= 0 {
return
@@ -153,35 +193,49 @@ func (t *VP8DataTunnel) Stop() {
}
}
func (t *VP8DataTunnel) currentIntervals() (sampleInterval time.Duration, keepaliveEvery, fps, batch int) {
func (t *VP8DataTunnel) currentRate() (fps, batch int) {
t.cfgMu.Lock()
fps = t.fps
batch = t.batch
t.cfgMu.Unlock()
defer t.cfgMu.Unlock()
return t.fps, t.batch
}
func sampleIntervalFor(fps, batch int) time.Duration {
if fps < 1 {
fps = 1
}
frameInterval := time.Second / time.Duration(fps)
sampleInterval = frameInterval
interval := frameInterval
if batch > 1 {
sampleInterval = frameInterval / time.Duration(batch)
interval = frameInterval / time.Duration(batch)
}
if sampleInterval <= 0 {
sampleInterval = time.Millisecond
if interval <= 0 {
interval = time.Millisecond
}
return interval
}
keepaliveEvery = int(keepaliveIdlePeriod / sampleInterval)
if keepaliveEvery < 1 {
keepaliveEvery = 1
func pacedBatchFor(batch int) int {
if batch <= 1 {
return batch
}
return
floor := batch * paceBatchFloorPercent / 100
if floor < 1 {
floor = 1
}
return common.IntInRange(floor, batch)
}
func (t *VP8DataTunnel) writerLoop() {
for {
sampleInterval, keepaliveEvery, fps, batch := t.currentIntervals()
t.logFn("vp8tunnel: writer (re)started fps=%d batch=%d sampleInterval=%s keepaliveEvery=%d",
fps, batch, sampleInterval, keepaliveEvery)
fps, batch := t.currentRate()
pacedBatch := pacedBatchFor(batch)
sampleInterval := sampleIntervalFor(fps, pacedBatch)
keepaliveEvery, keepalivePad := t.nextKeepalive(sampleInterval)
t.logFn("vp8tunnel: writer (re)started fps=%d batch=%d pacedBatch=%d sampleInterval=%s keepaliveEvery=%d",
fps, batch, pacedBatch, sampleInterval, keepaliveEvery)
ticker := time.NewTicker(sampleInterval)
drift := time.NewTimer(common.DurationInRange(paceDriftMin, paceDriftMax))
idleTicks := 0
reconfigure := false
@@ -189,9 +243,19 @@ func (t *VP8DataTunnel) writerLoop() {
select {
case <-t.stopCh:
ticker.Stop()
drift.Stop()
return
case <-t.cfgChan:
reconfigure = true
case <-drift.C:
pacedBatch = pacedBatchFor(batch)
sampleInterval = sampleIntervalFor(fps, pacedBatch)
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)
}
case <-ticker.C:
var sample []byte
isKeepalive := false
@@ -205,7 +269,8 @@ func (t *VP8DataTunnel) writerLoop() {
continue
}
idleTicks = 0
sample = t.obf.EncodeKeepalive()
sample = t.obf.EncodeKeepalive(keepalivePad)
keepaliveEvery, keepalivePad = t.nextKeepalive(sampleInterval)
isKeepalive = true
}
if sample == nil {
@@ -228,6 +293,7 @@ func (t *VP8DataTunnel) writerLoop() {
}
}
ticker.Stop()
drift.Stop()
}
}