diff --git a/rtmp/socket.go b/rtmp/socket.go index 0e53b255..2a685110 100644 --- a/rtmp/socket.go +++ b/rtmp/socket.go @@ -35,6 +35,7 @@ package rtmp import ( "fmt" + "log" "net" ) @@ -59,12 +60,43 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { if err != nil { return false } + if r.Link.socksport != 0 { + if !C_SocksNegotiate(r, addr) { + return false + } + } r.m_bSendCounter = true return C_RTMP_Connect1(r, cp) } +// int SocksNegotiate(RTMP* r); +// rtmp.c +1062 +func C_SocksNegotiate(r *C_RTMP, addr *net.TCPAddr) (ok bool) { + ip := addr.IP.To4() + packet := []byte{ + 0x4, // SOCKS version + 0x1, // Establish a TCP/IP stream connection + byte(r.Link.port >> 8), byte(r.Link.port), + ip[0], ip[1], ip[2], ip[3], + 0x0, // Empty user ID string + } + + C_WriteN(r, packet) + + if C_ReadN(r, packet[:8]) != 8 { + return false + } + + if packet[0] == 0x0 && packet[1] == 0x5a { + return true + } + // TODO: use new logger here + log.Println("C_SocksNegotitate: SOCKS returned error code!") + return false +} + // int RTMPSockBuf_Fill(RTMPSockBuf* sb); // rtmp.c +4253 func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int {