Files
whitelist-bypass/relay/tunnel/vp8tunnel.go
T
kulikov0 1b2d6bc5d3 v0.2.1 (#53)
* ios proxy app, tune vp8 tunnel

* refactor go modules, move headless joiner android to a go captcha

* restore emojis in vk bot keyboard

* headless support in vk bot

* remove telemost dc hooks, update cretor-app ui and bot

* remove telemost support from go code, add fallback to video if mode == dc in joiners

* fix headless creators in windows

* update vk call creation autoclick

* force desktop useragent for creator app

* kick disconnected user in headless vk creator immediately when connection is lost

* update README.md

* update SETUP.md

* update release version

* add fallback for link creation in vk bot
2026-04-19 17:47:36 +02:00

141 lines
3.5 KiB
Go

package tunnel
import (
"encoding/binary"
"sync"
"sync/atomic"
"time"
"github.com/pion/webrtc/v4"
"github.com/pion/webrtc/v4/pkg/media"
)
var vp8Keyframe = []byte{
16, 2, 0, 157, 1, 42, 2, 0, 2, 0, 2, 7, 8, 133, 133, 136,
153, 132, 136, 11, 2, 0, 12, 13, 96, 0, 254, 252, 173, 16,
}
var vp8Interframe = []byte{
177, 1, 0, 8, 17, 24, 0, 24, 0, 24, 88, 47, 244, 0, 8, 0, 0,
}
const DataFrameMarker = 0xFF
type VP8DataTunnel struct {
track *webrtc.TrackLocalStaticSample
mu sync.Mutex
logFn func(string, ...any)
frameCount uint64
running bool
stopCh chan struct{}
sendQueue chan []byte
OnData func([]byte)
OnClose func()
}
func (t *VP8DataTunnel) SetOnData(fn func([]byte)) { t.OnData = fn }
func (t *VP8DataTunnel) SetOnClose(fn func()) { t.OnClose = fn }
func NewVP8DataTunnel(track *webrtc.TrackLocalStaticSample, logFn func(string, ...any)) *VP8DataTunnel {
return &VP8DataTunnel{
track: track,
logFn: logFn,
stopCh: make(chan struct{}),
sendQueue: make(chan []byte, 256),
}
}
func (t *VP8DataTunnel) buildFrame(data []byte) []byte {
t.frameCount++
if len(data) == 0 {
if t.frameCount%60 == 0 {
return vp8Keyframe
}
return vp8Interframe
}
frame := make([]byte, 1+4+len(data))
frame[0] = DataFrameMarker
binary.BigEndian.PutUint32(frame[1:5], uint32(len(data)))
copy(frame[5:], data)
return frame
}
var sendCount atomic.Uint64
func (t *VP8DataTunnel) SendData(data []byte) {
n := sendCount.Add(1)
if n <= 5 || n%100 == 0 {
t.logFn("vp8tunnel: SendData #%d len=%d queueLen=%d", n, len(data), len(t.sendQueue))
}
t.sendQueue <- data
}
func (t *VP8DataTunnel) Start(fps int) {
t.running = true
keepaliveInterval := time.Second / time.Duration(fps)
lastSend := time.Now()
go func() {
ticker := time.NewTicker(keepaliveInterval)
defer ticker.Stop()
for {
select {
case <-t.stopCh:
return
case data := <-t.sendQueue:
now := time.Now()
elapsed := now.Sub(lastSend)
// Most stable interval for burst mode, prevents congestion control
minInterval := 1250 * time.Microsecond
if elapsed < minInterval {
time.Sleep(minInterval - elapsed)
}
frame := t.buildFrame(data)
err := t.track.WriteSample(media.Sample{Data: frame, Duration: keepaliveInterval})
lastSend = time.Now()
if err != nil {
t.logFn("vp8tunnel: WriteSample DATA error: %v (frame %d, %d bytes)", err, t.frameCount-1, len(frame))
} else if t.frameCount <= 10 || t.frameCount%100 == 0 {
t.logFn("vp8tunnel: WriteSample DATA ok frame=%d size=%d dataLen=%d first=0x%02x", t.frameCount-1, len(frame), len(data), frame[0])
}
if t.frameCount%60 == 0 {
kf := t.buildFrame(nil)
t.track.WriteSample(media.Sample{Data: kf, Duration: keepaliveInterval})
}
ticker.Reset(keepaliveInterval)
case <-ticker.C:
lastSend = time.Now()
frame := t.buildFrame(nil)
err := t.track.WriteSample(media.Sample{Data: frame, Duration: keepaliveInterval})
if t.frameCount <= 3 || t.frameCount%500 == 0 {
t.logFn("vp8tunnel: KEEPALIVE frame=%d first=0x%02x err=%v", t.frameCount-1, frame[0], err)
}
}
}
}()
}
func (t *VP8DataTunnel) Stop() {
if t.running {
close(t.stopCh)
t.running = false
if t.OnClose != nil {
t.OnClose()
}
}
}
func ExtractDataFromPayload(payload []byte) []byte {
if len(payload) < 5 {
return nil
}
if payload[0] != DataFrameMarker {
return nil
}
dataLen := binary.BigEndian.Uint32(payload[1:5])
if dataLen == 0 || int(dataLen) > len(payload)-5 {
return nil
}
return payload[5 : 5+dataLen]
}