74 byte handshake (#1343)

* feat(voice): 74 byte udp handshake

Update the format of UDP handshake to use 74 byte packets.

* docs: use new positions in comments

* feat: document type and length fields

Add descriptions of type and length fields to the request packet documentation.
This commit is contained in:
Fedor Lapshin 2023-03-10 00:33:26 +03:00 committed by GitHub
parent 916f693c00
commit ba97131805
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -599,44 +599,46 @@ func (v *VoiceConnection) udpOpen() (err error) {
return return
} }
// Create a 70 byte array and put the SSRC code from the Op 2 VoiceConnection event // Create a 74 byte array to store the packet data
// into it. Then send that over the UDP connection to Discord sb := make([]byte, 74)
sb := make([]byte, 70) binary.BigEndian.PutUint16(sb, 1) // Packet type (0x1 is request, 0x2 is response)
binary.BigEndian.PutUint32(sb, v.op2.SSRC) binary.BigEndian.PutUint16(sb[2:], 70) // Packet length (excluding type and length fields)
binary.BigEndian.PutUint32(sb[4:], v.op2.SSRC) // The SSRC code from the Op 2 VoiceConnection event
// And send that data over the UDP connection to Discord.
_, err = v.udpConn.Write(sb) _, err = v.udpConn.Write(sb)
if err != nil { if err != nil {
v.log(LogWarning, "udp write error to %s, %s", addr.String(), err) v.log(LogWarning, "udp write error to %s, %s", addr.String(), err)
return return
} }
// Create a 70 byte array and listen for the initial handshake response // Create a 74 byte array and listen for the initial handshake response
// from Discord. Once we get it parse the IP and PORT information out // from Discord. Once we get it parse the IP and PORT information out
// of the response. This should be our public IP and PORT as Discord // of the response. This should be our public IP and PORT as Discord
// saw us. // saw us.
rb := make([]byte, 70) rb := make([]byte, 74)
rlen, _, err := v.udpConn.ReadFromUDP(rb) rlen, _, err := v.udpConn.ReadFromUDP(rb)
if err != nil { if err != nil {
v.log(LogWarning, "udp read error, %s, %s", addr.String(), err) v.log(LogWarning, "udp read error, %s, %s", addr.String(), err)
return return
} }
if rlen < 70 { if rlen < 74 {
v.log(LogWarning, "received udp packet too small") v.log(LogWarning, "received udp packet too small")
return fmt.Errorf("received udp packet too small") return fmt.Errorf("received udp packet too small")
} }
// Loop over position 4 through 20 to grab the IP address // Loop over position 8 through 71 to grab the IP address.
// Should never be beyond position 20.
var ip string var ip string
for i := 4; i < 20; i++ { for i := 8; i < len(rb)-2; i++ {
if rb[i] == 0 { if rb[i] == 0 {
break break
} }
ip += string(rb[i]) ip += string(rb[i])
} }
// Grab port from position 68 and 69 // Grab port from position 72 and 73
port := binary.BigEndian.Uint16(rb[68:70]) port := binary.BigEndian.Uint16(rb[len(rb)-2:])
// Take the data from above and send it back to Discord to finalize // Take the data from above and send it back to Discord to finalize
// the UDP connection handshake. // the UDP connection handshake.