* fix: prevent duplicate call link detection (#23)

Co-authored-by: Maksim <maksim@MacBook-Pro-Maksim.local>

* bot improvements (#25)

Co-authored-by: Maksim <maksim@MBP-Maksim.lan>

* headless vk video creator

* add scroll to logs

* allow peer reconnection

* add todo about topology changes, kick stale peer to prevent SFU topology

* unified relay for headless

* remove relay binaries, extend build-go.sh to build them

* refactor constants

* limit headless resource usage

* nuke everyone and yourself at sfu topology changes

* add headless for creator

* save bot enabled state

* increment release version

---------

Co-authored-by: Nevenuga <80585376+Nevenuga@users.noreply.github.com>
Co-authored-by: Maksim <maksim@MacBook-Pro-Maksim.local>
Co-authored-by: Maksim <maksim@MBP-Maksim.lan>
Co-authored-by: kulikov <user@Mac.lan>
This commit is contained in:
authored and GitHub committed 2026-04-02 22:52:56 +02:00
1 parent d6a98e84ee
commit c8e1ba444d
37 files changed
+2589 -133

No files matched your search

-2
View File
@@ -49,5 +49,3 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
gvisor.dev/gvisor v0.0.0-20250523182742-eede7a881b20 // indirect
)
+30 -27
View File
@@ -12,6 +12,7 @@ import (
"time"
"github.com/gorilla/websocket"
"whitelist-bypass/relay/socks"
)
const (
@@ -247,18 +248,18 @@ type udpClient struct {
func (j *joinerRelay) handleUDPAssociate(tcpConn net.Conn) {
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
if err != nil {
tcpConn.Write([]byte{0x05, 0x01, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
tcpConn.Write(socks.GenFail)
tcpConn.Close()
return
}
udpConn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
tcpConn.Write([]byte{0x05, 0x01, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
tcpConn.Write(socks.GenFail)
tcpConn.Close()
return
}
localAddr := udpConn.LocalAddr().(*net.UDPAddr)
reply := []byte{0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0, 0}
reply := []byte{socks.Ver, 0x00, 0x00, socks.AtypIPv4, 127, 0, 0, 1, 0, 0}
binary.BigEndian.PutUint16(reply[8:10], uint16(localAddr.Port))
tcpConn.Write(reply)
logMsg("dc-joiner: UDP ASSOCIATE on port %d", localAddr.Port)
@@ -291,14 +292,14 @@ func (j *joinerRelay) handleUDPAssociate(tcpConn net.Conn) {
var dstAddr string
var headerLen int
switch atyp {
case 0x01:
case socks.AtypIPv4:
if n < 10 {
continue
}
dstAddr = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
binary.BigEndian.Uint16(buf[8:10]))
headerLen = 10
case 0x03:
case socks.AtypDomain:
dlen := int(buf[4])
if n < 5+dlen+2 {
continue
@@ -306,7 +307,7 @@ func (j *joinerRelay) handleUDPAssociate(tcpConn net.Conn) {
dstAddr = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
headerLen = 5 + dlen + 2
case 0x04:
case socks.AtypIPv6:
if n < 22 {
continue
}
@@ -404,39 +405,39 @@ func (j *joinerRelay) listenSOCKS(ln net.Listener) error {
func (j *joinerRelay) handleSOCKS(conn net.Conn) {
<-j.ready
buf := make([]byte, 258)
buf := make([]byte, socks.HandshakeBuf)
n, err := conn.Read(buf)
if err != nil || n < 2 || buf[0] != 0x05 {
if err != nil || n < 2 || buf[0] != socks.Ver {
conn.Close()
return
}
conn.Write([]byte{0x05, 0x00})
conn.Write(socks.NoAuth)
n, err = conn.Read(buf)
if err != nil || n < 7 || buf[0] != 0x05 {
conn.Write([]byte{0x05, 0x01, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
if err != nil || n < 7 || buf[0] != socks.Ver {
conn.Write(socks.GenFail)
conn.Close()
return
}
cmd := buf[1]
if cmd == 0x03 {
if cmd == socks.CmdUDP {
j.handleUDPAssociate(conn)
return
}
if cmd != 0x01 {
conn.Write([]byte{0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
if cmd != socks.CmdTCP {
conn.Write(socks.CmdErr)
conn.Close()
return
}
var host string
switch buf[3] {
case 0x01:
case socks.AtypIPv4:
if n < 10 {
conn.Close()
return
}
host = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
binary.BigEndian.Uint16(buf[8:10]))
case 0x03:
case socks.AtypDomain:
dlen := int(buf[4])
if n < 5+dlen+2 {
conn.Close()
@@ -444,7 +445,7 @@ func (j *joinerRelay) handleSOCKS(conn net.Conn) {
}
host = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
case 0x04:
case socks.AtypIPv6:
if n < 22 {
conn.Close()
return
@@ -453,7 +454,7 @@ func (j *joinerRelay) handleSOCKS(conn net.Conn) {
host = fmt.Sprintf("[%s]:%d", ip.String(),
binary.BigEndian.Uint16(buf[20:22]))
default:
conn.Write([]byte{0x05, 0x08, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.AddrErr)
conn.Close()
return
}
@@ -464,12 +465,12 @@ func (j *joinerRelay) handleSOCKS(conn net.Conn) {
j.send(id, msgConnect, []byte(host))
if err := <-sc.rdy; err != nil {
logMsg("dc-joiner: CONNECT %d failed: %v", id, err)
conn.Write([]byte{0x05, 0x05, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.ConnFail)
conn.Close()
j.conns.Delete(id)
return
}
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.OK)
logMsg("dc-joiner: CONNECTED %d -> %s", id, maskAddr(host))
go func() {
buf := make([]byte, readBufSize)
@@ -518,14 +519,16 @@ func (c *creatorRelay) handleMessage(connID uint32, msgType byte, payload []byte
case msgUDP:
go c.handleUDP(connID, payload)
case msgData:
val, ok := c.conns.Load(connID)
if ok {
val.(net.Conn).Write(payload)
if val, ok := c.conns.Load(connID); ok {
if conn, ok := val.(net.Conn); ok {
conn.Write(payload)
}
}
case msgClose:
val, ok := c.conns.LoadAndDelete(connID)
if ok {
val.(net.Conn).Close()
if val, ok := c.conns.LoadAndDelete(connID); ok {
if conn, ok := val.(net.Conn); ok {
conn.Close()
}
}
}
}
@@ -569,7 +572,7 @@ func (c *creatorRelay) handleUDP(connID uint32, payload []byte) {
if err != nil {
return
}
buf := make([]byte, 4096)
buf := make([]byte, socks.UDPBufSize)
n, err := conn.Read(buf)
if err != nil {
return
+38 -29
View File
@@ -9,6 +9,8 @@ import (
"sync"
"sync/atomic"
"time"
"whitelist-bypass/relay/socks"
)
const (
@@ -148,14 +150,16 @@ func (rb *RelayBridge) handleCreatorMessage(connID uint32, msgType byte, payload
case msgUDP:
go rb.handleUDP(connID, payload)
case msgData:
val, ok := rb.conns.Load(connID)
if ok {
val.(net.Conn).Write(payload)
if val, ok := rb.conns.Load(connID); ok {
if c, ok := val.(net.Conn); ok {
c.Write(payload)
}
}
case msgClose:
val, ok := rb.conns.LoadAndDelete(connID)
if ok {
val.(net.Conn).Close()
if val, ok := rb.conns.LoadAndDelete(connID); ok {
if c, ok := val.(net.Conn); ok {
c.Close()
}
}
}
}
@@ -184,7 +188,7 @@ func (rb *RelayBridge) handleUDP(connID uint32, payload []byte) {
defer conn.Close()
conn.SetDeadline(time.Now().Add(5 * time.Second))
conn.Write(data)
buf := make([]byte, 4096)
buf := make([]byte, socks.UDPBufSize)
n, err := conn.Read(buf)
if err != nil {
return
@@ -204,7 +208,7 @@ func (rb *RelayBridge) connectTCP(connID uint32, addr string) {
rb.send(connID, msgConnectOK, nil)
rb.logFn("relay: CONNECTED %d -> %s", connID, maskAddr(addr))
buf := make([]byte, 900)
buf := make([]byte, socks.VP8BufSize)
for {
n, err := conn.Read(buf)
if n > 0 {
@@ -247,38 +251,38 @@ func (rb *RelayBridge) ListenSOCKS(addr string) error {
func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
<-rb.ready
buf := make([]byte, 258)
buf := make([]byte, socks.HandshakeBuf)
n, err := conn.Read(buf)
if err != nil || n < 2 || buf[0] != 0x05 {
if err != nil || n < 2 || buf[0] != socks.Ver {
conn.Close()
return
}
conn.Write([]byte{0x05, 0x00})
conn.Write(socks.NoAuth)
n, err = conn.Read(buf)
if err != nil || n < 7 || buf[0] != 0x05 {
if err != nil || n < 7 || buf[0] != socks.Ver {
conn.Close()
return
}
cmd := buf[1]
if cmd == 0x03 {
if cmd == socks.CmdUDP {
rb.handleUDPAssociate(conn)
return
}
if cmd != 0x01 {
conn.Write([]byte{0x05, 0x07, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
if cmd != socks.CmdTCP {
conn.Write(socks.CmdErr)
conn.Close()
return
}
var host string
switch buf[3] {
case 0x01:
case socks.AtypIPv4:
if n < 10 {
conn.Close()
return
}
host = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
binary.BigEndian.Uint16(buf[8:10]))
case 0x03:
case socks.AtypDomain:
dlen := int(buf[4])
if n < 5+dlen+2 {
conn.Close()
@@ -286,7 +290,7 @@ func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
}
host = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
case 0x04:
case socks.AtypIPv6:
if n < 22 {
conn.Close()
return
@@ -295,7 +299,7 @@ func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
host = fmt.Sprintf("[%s]:%d", ip.String(),
binary.BigEndian.Uint16(buf[20:22]))
default:
conn.Write([]byte{0x05, 0x08, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.AddrErr)
conn.Close()
return
}
@@ -308,16 +312,16 @@ func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
if err := <-sc.rdy; err != nil {
rb.logFn("relay: SOCKS CONNECT %d failed: %v", id, err)
conn.Write([]byte{0x05, 0x05, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.ConnFail)
conn.Close()
rb.conns.Delete(id)
return
}
conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
conn.Write(socks.OK)
rb.logFn("relay: SOCKS CONNECTED %d -> %s", id, maskAddr(host))
go func() {
readBuf := make([]byte, 900)
readBuf := make([]byte, socks.VP8BufSize)
for {
rn, rerr := conn.Read(readBuf)
if rn > 0 {
@@ -333,15 +337,20 @@ func (rb *RelayBridge) handleSOCKS(conn net.Conn) {
}
func (rb *RelayBridge) handleUDPAssociate(tcpConn net.Conn) {
udpAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:0")
udpAddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:0")
if err != nil {
tcpConn.Write(socks.GenFail)
tcpConn.Close()
return
}
udpConn, err := net.ListenUDP("udp", udpAddr)
if err != nil {
tcpConn.Write([]byte{0x05, 0x01, 0x00, 0x01, 0, 0, 0, 0, 0, 0})
tcpConn.Write(socks.GenFail)
tcpConn.Close()
return
}
localAddr := udpConn.LocalAddr().(*net.UDPAddr)
reply := []byte{0x05, 0x00, 0x00, 0x01, 127, 0, 0, 1, 0, 0}
reply := []byte{socks.Ver, 0x00, 0x00, socks.AtypIPv4, 127, 0, 0, 1, 0, 0}
binary.BigEndian.PutUint16(reply[8:10], uint16(localAddr.Port))
tcpConn.Write(reply)
@@ -354,7 +363,7 @@ func (rb *RelayBridge) handleUDPAssociate(tcpConn net.Conn) {
go func() {
defer udpConn.Close()
defer tcpConn.Close()
buf := make([]byte, 4096)
buf := make([]byte, socks.UDPBufSize)
for {
n, addr, err := udpConn.ReadFromUDP(buf)
if err != nil {
@@ -371,14 +380,14 @@ func (rb *RelayBridge) handleUDPAssociate(tcpConn net.Conn) {
var dstAddr string
var headerLen int
switch atyp {
case 0x01:
case socks.AtypIPv4:
if n < 10 {
continue
}
dstAddr = fmt.Sprintf("%d.%d.%d.%d:%d", buf[4], buf[5], buf[6], buf[7],
binary.BigEndian.Uint16(buf[8:10]))
headerLen = 10
case 0x03:
case socks.AtypDomain:
dlen := int(buf[4])
if n < 5+dlen+2 {
continue
@@ -386,7 +395,7 @@ func (rb *RelayBridge) handleUDPAssociate(tcpConn net.Conn) {
dstAddr = fmt.Sprintf("%s:%d", string(buf[5:5+dlen]),
binary.BigEndian.Uint16(buf[5+dlen:7+dlen]))
headerLen = 5 + dlen + 2
case 0x04:
case socks.AtypIPv6:
if n < 22 {
continue
}
+3 -2
View File
@@ -10,6 +10,7 @@ import (
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v4"
"whitelist-bypass/relay/socks"
)
type tmPCState struct {
@@ -283,7 +284,7 @@ func (c *TelemostClient) handleICECandidate(data json.RawMessage, role string) {
func (c *TelemostClient) readTrack(track *webrtc.TrackRemote) {
if track.Codec().MimeType != webrtc.MimeTypeVP8 {
buf := make([]byte, 4096)
buf := make([]byte, socks.UDPBufSize)
for {
if _, _, err := track.Read(buf); err != nil {
c.logFn("telemost: readTrack (%s) error: %v", track.Codec().MimeType, err)
@@ -296,7 +297,7 @@ func (c *TelemostClient) readTrack(track *webrtc.TrackRemote) {
var frameBuf []byte
dataCount := 0
recvCount := 0
buf := make([]byte, 65536)
buf := make([]byte, socks.RTPBufSize)
for {
n, _, err := track.Read(buf)
if err != nil {
+173 -30
View File
@@ -8,17 +8,20 @@ import (
"github.com/pion/rtp"
"github.com/pion/rtp/codecs"
"github.com/pion/webrtc/v4"
"whitelist-bypass/relay/socks"
)
type VKClient struct {
WSHelper
pc *webrtc.PeerConnection
sampleTrack *webrtc.TrackLocalStaticSample
tunnel *VP8DataTunnel
logFn func(string, ...any)
remoteSet bool
pending []webrtc.ICECandidateInit
OnConnected func(*VP8DataTunnel)
pc *webrtc.PeerConnection
sampleTrack *webrtc.TrackLocalStaticSample
tunnel *VP8DataTunnel
logFn func(string, ...any)
remoteSet bool
pending []webrtc.ICECandidateInit
OnConnected func(*VP8DataTunnel)
dcProducerNotif *webrtc.DataChannel
dcProducerCmd *webrtc.DataChannel
}
func NewVKClient(logFn func(string, ...any)) *VKClient {
@@ -44,6 +47,7 @@ func (c *VKClient) handleMessage(raw []byte) {
if err := json.Unmarshal(raw, &msg); err != nil {
return
}
c.logFn("vk: >> msg type=%s id=%d", msg.Type, msg.ID)
switch msg.Type {
case "ice-servers":
@@ -58,26 +62,15 @@ func (c *VKClient) handleMessage(raw []byte) {
case "add-ice-candidate", "remote-ice-candidate":
c.handleICECandidate(msg.Data)
case "add-track", "create-data-channel":
case "reset":
c.handleReset(msg.ID)
case "close":
c.cleanup()
}
c.logFn("vk: << msg type=%s id=%d done", msg.Type, msg.ID)
}
func (c *VKClient) handleICEServers(data json.RawMessage) {
if c.pc != nil {
return
}
iceLogFn = c.logFn
iceServers, err := ParseICEServers(data)
if err != nil {
return
}
config := webrtc.Configuration{
ICEServers: iceServers,
ICETransportPolicy: webrtc.ICETransportPolicyRelay,
}
func (c *VKClient) createPC(config webrtc.Configuration) error {
se := webrtc.SettingEngine{}
se.SetNet(&AndroidNet{})
se.SetInterfaceFilter(func(iface string) bool { return false })
@@ -85,7 +78,7 @@ func (c *VKClient) handleICEServers(data json.RawMessage) {
pc, err := api.NewPeerConnection(config)
if err != nil {
c.logFn("vk: failed to create PC: %v", err)
return
return err
}
c.pc = pc
@@ -106,6 +99,46 @@ func (c *VKClient) handleICEServers(data json.RawMessage) {
c.logFn("vk: AddTrack video: sender=%v err=%v", videoSender != nil, videoErr)
c.logFn("vk: senders count: %d", len(pc.GetSenders()))
// Create DataChannels required by VK SFU.
// The SFU sends producer-updated (SDP offer) via producerNotification DC.
ordered := true
dcNotif, err := pc.CreateDataChannel("producerNotification", &webrtc.DataChannelInit{Ordered: &ordered})
if err != nil {
c.logFn("vk: failed to create producerNotification DC: %v", err)
} else {
c.dcProducerNotif = dcNotif
dcNotif.OnOpen(func() {
c.logFn("vk: producerNotification DC opened")
})
dcNotif.OnMessage(func(msg webrtc.DataChannelMessage) {
c.logFn("vk: producerNotification msg len=%d isString=%v", len(msg.Data), msg.IsString)
// Forward to Node bridge as sfu-dc-message
c.SendToHook("sfu-dc-message", map[string]interface{}{
"channel": "producerNotification",
"data": string(msg.Data),
})
})
}
dcCmd, err := pc.CreateDataChannel("producerCommand", &webrtc.DataChannelInit{Ordered: &ordered})
if err != nil {
c.logFn("vk: failed to create producerCommand DC: %v", err)
} else {
c.dcProducerCmd = dcCmd
dcCmd.OnOpen(func() {
c.logFn("vk: producerCommand DC opened")
})
dcCmd.OnMessage(func(msg webrtc.DataChannelMessage) {
c.logFn("vk: producerCommand msg len=%d", len(msg.Data))
c.SendToHook("sfu-dc-message", map[string]interface{}{
"channel": "producerCommand",
"data": string(msg.Data),
})
})
}
// SFU also expects screen share DCs
pc.CreateDataChannel("producerScreenShare", &webrtc.DataChannelInit{Ordered: &ordered})
pc.CreateDataChannel("consumerScreenShare", &webrtc.DataChannelInit{Ordered: &ordered})
pc.OnICECandidate(func(cand *webrtc.ICECandidate) {
if cand == nil {
c.logFn("vk: ICE gathering complete (nil candidate)")
@@ -136,7 +169,26 @@ func (c *VKClient) handleICEServers(data json.RawMessage) {
go c.readTrack(track)
})
c.logFn("vk: PC created (%d ICE servers)", len(iceServers))
c.logFn("vk: PC created (%d ICE servers)", len(config.ICEServers))
return nil
}
func (c *VKClient) handleICEServers(data json.RawMessage) {
if c.pc != nil {
return
}
iceLogFn = c.logFn
iceServers, err := ParseICEServers(data)
if err != nil {
c.logFn("vk: failed to parse ICE servers: %v", err)
return
}
if err := c.createPC(webrtc.Configuration{
ICEServers: iceServers,
ICETransportPolicy: webrtc.ICETransportPolicyRelay,
}); err != nil {
c.logFn("vk: handleICEServers createPC failed: %v", err)
}
}
func (c *VKClient) handleCreateOffer(id int) {
@@ -145,30 +197,53 @@ func (c *VKClient) handleCreateOffer(id int) {
}
offer, err := c.pc.CreateOffer(nil)
if err != nil {
c.logFn("vk: createOffer error: %v", err)
return
}
c.pc.SetLocalDescription(offer)
c.logFn("vk: created offer, senders=%d signalingState=%s", len(c.pc.GetSenders()), c.pc.SignalingState().String())
// Log offer media lines
for _, line := range splitLines(offer.SDP) {
if len(line) > 2 && line[:2] == "m=" {
c.logFn("vk: offer>> %s", line)
}
}
c.SendResponse(id, SDPMessage{Type: offer.Type.String(), SDP: offer.SDP})
}
func (c *VKClient) handleCreateAnswer(id int) {
if c.pc == nil {
c.logFn("vk: createAnswer error: no PC")
return
}
c.logFn("vk: createAnswer: signalingState=%s", c.pc.SignalingState().String())
answer, err := c.pc.CreateAnswer(nil)
if err != nil {
c.logFn("vk: createAnswer error: %v", err)
return
}
c.logFn("vk: createAnswer: setting local description...")
c.pc.SetLocalDescription(answer)
c.logFn("vk: created answer, senders=%d signalingState=%s", len(c.pc.GetSenders()), c.pc.SignalingState().String())
c.SendResponse(id, SDPMessage{Type: answer.Type.String(), SDP: answer.SDP})
// Wait for ICE gathering so the answer SDP includes candidates.
// In SFU mode the SDK reads localDescription after setLocalDescription
// and sends it via acceptProducer - it must have candidates already.
gatherDone := webrtc.GatheringCompletePromise(c.pc)
go func() {
<-gatherDone
localDesc := c.pc.LocalDescription()
c.logFn("vk: created answer with ICE, senders=%d signalingState=%s", len(c.pc.GetSenders()), c.pc.SignalingState().String())
c.SendResponse(id, SDPMessage{Type: localDesc.Type.String(), SDP: localDesc.SDP})
}()
}
func (c *VKClient) handleSetRemoteDescription(data json.RawMessage, id int) {
var sdpMsg SDPMessage
if err := json.Unmarshal(data, &sdpMsg); err != nil || c.pc == nil {
c.logFn("vk: setRemoteDescription: no pc or bad data")
if id > 0 {
c.SendResponse(id, "error: no pc or bad data")
}
return
}
var sdpType webrtc.SDPType
@@ -177,11 +252,34 @@ func (c *VKClient) handleSetRemoteDescription(data json.RawMessage, id int) {
} else {
sdpType = webrtc.SDPTypeAnswer
}
connState := c.pc.ConnectionState().String()
iceState := c.pc.ICEConnectionState().String()
c.logFn("vk: setRemoteDescription: type=%s signalingState=%s connectionState=%s iceState=%s senders=%d receivers=%d",
sdpMsg.Type, c.pc.SignalingState().String(), connState, iceState,
len(c.pc.GetSenders()), len(c.pc.GetReceivers()))
// Log SDP media lines for debugging codec issues
lines := 0
for _, line := range splitLines(sdpMsg.SDP) {
if len(line) > 2 && (line[:2] == "m=" || line[:2] == "a=") {
if line[:2] == "m=" || (len(line) > 9 && line[:9] == "a=rtpmap:") || (len(line) > 12 && line[:12] == "a=candidate:") {
c.logFn("vk: SDP>> %s", line)
lines++
if lines > 20 {
break
}
}
}
}
if err := c.pc.SetRemoteDescription(webrtc.SessionDescription{Type: sdpType, SDP: sdpMsg.SDP}); err != nil {
c.logFn("vk: setRemoteDescription error: %v", err)
c.logFn("vk: setRemoteDescription error (%s, state=%s): %v", sdpMsg.Type, c.pc.SignalingState().String(), err)
if id > 0 {
c.SendResponse(id, "ok")
}
return
}
c.logFn("vk: set remote description: %s, signalingState=%s, senders=%d", sdpMsg.Type, c.pc.SignalingState().String(), len(c.pc.GetSenders()))
c.logFn("vk: set remote description OK: %s, signalingState=%s, senders=%d", sdpMsg.Type, c.pc.SignalingState().String(), len(c.pc.GetSenders()))
for i, s := range c.pc.GetSenders() {
if s.Track() != nil {
c.logFn("vk: sender[%d]: kind=%s id=%s", i, s.Track().Kind().String(), s.Track().ID())
@@ -216,7 +314,7 @@ func (c *VKClient) handleICECandidate(data json.RawMessage) {
func (c *VKClient) readTrack(track *webrtc.TrackRemote) {
if track.Codec().MimeType != webrtc.MimeTypeVP8 {
buf := make([]byte, 4096)
buf := make([]byte, socks.UDPBufSize)
for {
if _, _, err := track.Read(buf); err != nil {
return
@@ -228,7 +326,7 @@ func (c *VKClient) readTrack(track *webrtc.TrackRemote) {
var frameBuf []byte
dataCount := 0
recvCount := 0
buf := make([]byte, 65536)
buf := make([]byte, socks.RTPBufSize)
for {
n, _, err := track.Read(buf)
if err != nil {
@@ -267,6 +365,51 @@ func (c *VKClient) readTrack(track *webrtc.TrackRemote) {
}
}
func (c *VKClient) handleReset(id int) {
c.logFn("vk: reset - closing PC for reconnection")
if c.tunnel != nil {
c.tunnel.Stop()
c.tunnel = nil
}
if c.pc != nil {
// Remove callbacks before Close() to prevent stale state events
c.pc.OnConnectionStateChange(nil)
c.pc.OnICECandidate(nil)
c.pc.OnTrack(nil)
oldPC := c.pc
c.pc = nil
// Close asynchronously - pc.Close() blocks on DTLS shutdown
go oldPC.Close()
}
c.remoteSet = false
c.pending = nil
c.dcProducerNotif = nil
c.dcProducerCmd = nil
c.sampleTrack = nil
if id > 0 {
c.SendResponse(id, "ok")
}
}
func splitLines(s string) []string {
var lines []string
start := 0
for i := 0; i < len(s); i++ {
if s[i] == '\n' {
line := s[start:i]
if len(line) > 0 && line[len(line)-1] == '\r' {
line = line[:len(line)-1]
}
lines = append(lines, line)
start = i + 1
}
}
if start < len(s) {
lines = append(lines, s[start:])
}
return lines
}
func (c *VKClient) cleanup() {
if c.tunnel != nil {
c.tunnel.Stop()
+5 -4
View File
@@ -3,6 +3,7 @@ package pion
import (
"encoding/binary"
"sync"
"sync/atomic"
"time"
"github.com/pion/webrtc/v4"
@@ -65,12 +66,12 @@ func (t *VP8DataTunnel) buildFrame(data []byte) []byte {
}
// SendData queues tunnel data to be sent in the next VP8 frame
var sendCount uint64
var sendCount atomic.Uint64
func (t *VP8DataTunnel) SendData(data []byte) {
sendCount++
if sendCount <= 5 || sendCount%100 == 0 {
t.logFn("vp8tunnel: SendData #%d len=%d queueLen=%d", sendCount, len(data), len(t.sendQueue))
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
}
BIN
View File
Binary file not shown.
+24
View File
@@ -0,0 +1,24 @@
package socks
const (
Ver = 0x05
CmdTCP = 0x01
CmdUDP = 0x03
AtypIPv4 = 0x01
AtypDomain = 0x03
AtypIPv6 = 0x04
HandshakeBuf = 258
UDPBufSize = 4096
RTPBufSize = 65536
VP8BufSize = 900
)
var (
NoAuth = []byte{Ver, 0x00}
OK = []byte{Ver, 0x00, 0x00, AtypIPv4, 0, 0, 0, 0, 0, 0}
ConnFail = []byte{Ver, 0x05, 0x00, AtypIPv4, 0, 0, 0, 0, 0, 0}
CmdErr = []byte{Ver, 0x07, 0x00, AtypIPv4, 0, 0, 0, 0, 0, 0}
AddrErr = []byte{Ver, 0x08, 0x00, AtypIPv4, 0, 0, 0, 0, 0, 0}
GenFail = []byte{Ver, 0x01, 0x00, AtypIPv4, 0, 0, 0, 0, 0, 0}
)