fight with topology changes at vk dual track

This commit is contained in:
kulikov0 committed 2026-06-09 16:27:17 +02:00
1 parent 0d787cb0f7
commit f1ce863405
3 files changed
+84 -22

No files matched your search

+41 -14
View File
@@ -22,6 +22,7 @@ import (
)
const TopologyDirect = "DIRECT"
const maxServerBounces = 5
type CallInfo struct {
CallID string
@@ -85,6 +86,10 @@ type Bridge struct {
newRelay func() Relay
p2p *P2PHandler
screenSharing bool
serverBounces int
suppressScreenshare bool
bouncing bool
}
func httpPost(endpoint string, form url.Values, extraHeaders map[string]string) ([]byte, error) {
@@ -355,6 +360,11 @@ func (b *Bridge) setScreenSharing(enabled bool) {
b.mu.Unlock()
return
}
if enabled && b.suppressScreenshare {
b.mu.Unlock()
log.Println("[vk-ws] screenshare suppressed after SERVER flap, staying single-track DIRECT")
return
}
b.screenSharing = enabled
b.mu.Unlock()
log.Printf("[vk-ws] peer track count change, screenshare=%v", enabled)
@@ -394,13 +404,8 @@ func (b *Bridge) handleVKMessage(raw []byte) {
log.Printf("[vk-ws] Topology changed to %s", topo)
b.topology = topo
if topo != TopologyDirect {
log.Printf("[vk-ws] SFU not supported, kicking %d peers", len(b.peers))
for pid := range b.peers {
b.vkSend("remove-participant", map[string]interface{}{
"participantId": pid,
"ban": false,
})
}
b.bounceForServerTopology("SERVER topology")
return
}
case "participant-joined", "participant-added":
@@ -408,13 +413,8 @@ func (b *Bridge) handleVKMessage(raw []byte) {
b.peers[int64(pid)] = struct{}{}
log.Printf("[vk-ws] Participant %d joined (total: %d)", int64(pid), len(b.peers))
if b.topology != TopologyDirect {
log.Printf("[vk-ws] Kicking peer %d (SFU topology)", int64(pid))
b.vkSend("remove-participant", map[string]interface{}{
"participantId": int64(pid),
"ban": false,
})
log.Println("[FATAL] SFU topology is not supported, exiting")
os.Exit(1)
b.bounceForServerTopology("participant joined under SERVER")
return
}
}
@@ -506,6 +506,32 @@ func (b *Bridge) initRelay() {
b.p2p.Init()
}
func (b *Bridge) bounceForServerTopology(reason string) {
b.mu.Lock()
if b.bouncing {
b.mu.Unlock()
return
}
b.bouncing = true
b.serverBounces++
count := b.serverBounces
if count > maxServerBounces {
b.suppressScreenshare = true
}
suppress := b.suppressScreenshare
ws := b.vkWs
b.mu.Unlock()
if suppress {
log.Printf("[vk-ws] %s -> reconnect #%d, suppressing screenshare to settle single-track DIRECT", reason, count)
} else {
log.Printf("[vk-ws] %s -> manual reconnect #%d to recover DIRECT", reason, count)
}
if ws != nil {
ws.Close()
}
}
func (b *Bridge) readLoop() error {
for {
_, msg, err := b.vkWs.ReadMessage()
@@ -568,6 +594,7 @@ func (b *Bridge) run(callInfo *CallInfo, cookieStr string, cfg VKConfig) {
b.mu.Lock()
b.screenSharing = false
b.bouncing = false
b.mu.Unlock()
b.sendMediaSettings(false)
+18 -2
View File
@@ -36,6 +36,7 @@ import (
type statusEmitter struct{}
var tunnelLostCh = make(chan struct{}, 1)
var selfHealReconnect bool
func (statusEmitter) EmitStatus(status string) {
log.Printf("[status] %s", status)
@@ -45,7 +46,7 @@ func (statusEmitter) EmitStatus(status string) {
if strings.HasPrefix(status, "CAPTCHA:") {
fmt.Printf("STATUS:%s\n", status)
}
if status == common.StatusTunnelLost {
if status == common.StatusTunnelLost && !selfHealReconnect {
select {
case tunnelLostCh <- struct{}{}:
default:
@@ -236,12 +237,26 @@ func main() {
}
}
var (
bridge *tunnel.RelayBridge
bridgeMu sync.Mutex
)
onConnected := func(t tunnel.DataTunnel) {
readBuf := common.VP8BufSize
if _, ok := t.(*tunnel.DCTunnel); ok {
readBuf = common.DCBufSize
}
bridge := tunnel.NewRelayBridgeWithAuth(t, "joiner", readBuf, log.Printf, *socksUser, *socksPass)
bridgeMu.Lock()
defer bridgeMu.Unlock()
// Reconnect: swap the new tunnel behind the persistent SOCKS
// listener instead of binding a second one
if bridge != nil {
bridge.SwapTunnel(t)
log.Printf("[socks] tunnel swapped after reconnect")
return
}
bridge = tunnel.NewRelayBridgeWithAuth(t, "joiner", readBuf, log.Printf, *socksUser, *socksPass)
bridge.SetPersistentListener(true)
bridge.MarkReady()
addr := fmt.Sprintf("%s:%d", *socksHost, *socksPort)
go func() {
@@ -265,6 +280,7 @@ func main() {
runTelemost(*link, *displayName, *vp8FPS, *vp8Batch,
onConnected, addCandidate)
case "vk":
selfHealReconnect = true
runVK(*link, *displayName, *tunnelMode, *vp8FPS, *vp8Batch, *dualTrack,
onConnected, addCandidate)
case "dion", "dn":
+25 -6
View File
@@ -24,8 +24,11 @@ import (
const (
vkReconnectInitialDelay = time.Second
vkReconnectMaxDelay = 16 * time.Second
vkMaxReconnectAttempts = 10
)
const vkTopologyDirect = "DIRECT"
type vkAuthRottenError struct {
Code string
Msg string
@@ -155,11 +158,16 @@ func (h *VKHeadlessJoiner) RunWithParams(jsonParams string) {
if !h.waitBeforeRetry(int(h.reconnectAttempt.Load())) {
return
}
h.reconnectAttempt.Add(1)
attempt := h.reconnectAttempt.Add(1)
if h.isClosed() {
return
}
h.logFn("headless: reconnect attempt #%d", h.reconnectAttempt.Load())
if int(attempt) > vkMaxReconnectAttempts {
h.logFn("headless: gave up after %d consecutive reconnect attempts", vkMaxReconnectAttempts)
h.Status.EmitStatusError("reconnect attempts exhausted")
return
}
h.logFn("headless: reconnect attempt #%d", attempt)
h.Status.EmitStatus(common.StatusReconnecting)
if err := h.runOnce(); err != nil {
var authRotten *vkAuthRottenError
@@ -517,16 +525,27 @@ func (h *VKHeadlessJoiner) handleVKMessage(raw []byte) {
case "topology-changed":
topo, _ := msg["topology"].(string)
h.logFn("headless: topology: %s", topo)
if topo == "server" {
h.logFn("headless: ERROR: server topology not supported")
if topo != "" && topo != vkTopologyDirect {
h.logFn("headless: %s topology -> closing WS to reconnect and recover DIRECT", topo)
h.vkMu.Lock()
ws := h.vkWs
h.vkMu.Unlock()
if ws != nil {
ws.Close()
}
}
case "participant-joined", "participant-added":
h.logFn("headless: <- %s", notif)
case "participant-left":
h.logFn("headless: <- %s", notif)
case "hungup":
h.logFn("headless: ERROR: call ended (hungup)")
h.Status.EmitStatusError("call ended")
h.logFn("headless: peer hungup -> closing WS to reconnect")
h.vkMu.Lock()
ws := h.vkWs
h.vkMu.Unlock()
if ws != nil {
ws.Close()
}
}
case "response":