2020-04-25 02:36:33 +00:00
|
|
|
package udp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-11 19:50:32 +00:00
|
|
|
"context"
|
2020-04-25 02:36:33 +00:00
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"net"
|
2021-07-02 09:42:00 +00:00
|
|
|
"sync"
|
2020-04-25 02:36:33 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/crypto/nacl/secretbox"
|
|
|
|
)
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// ErrDecryptionFailed is returned from ReadPacket if the received packet fails
|
|
|
|
// to decrypt.
|
|
|
|
var ErrDecryptionFailed = errors.New("decryption failed")
|
2020-11-30 01:32:18 +00:00
|
|
|
|
2020-07-11 19:50:32 +00:00
|
|
|
// Dialer is the default dialer that this package uses for all its dialing.
|
2021-07-02 09:42:00 +00:00
|
|
|
var Dialer = net.Dialer{
|
|
|
|
Timeout: 10 * time.Second,
|
2020-07-11 19:50:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 19:34:22 +00:00
|
|
|
// Connection represents a voice connection. It is not thread-safe.
|
2020-04-25 02:36:33 +00:00
|
|
|
type Connection struct {
|
|
|
|
GatewayIP string
|
|
|
|
GatewayPort uint16
|
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
conn net.Conn
|
|
|
|
ssrc uint32
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2020-11-17 22:43:00 +00:00
|
|
|
// frequency rate.Limiter
|
|
|
|
frequency *time.Ticker
|
|
|
|
timeIncr uint32
|
2021-09-28 20:19:04 +00:00
|
|
|
stopFreq chan struct{}
|
2020-11-17 22:43:00 +00:00
|
|
|
|
|
|
|
packet [12]byte
|
|
|
|
secret [32]byte
|
2020-04-25 02:36:33 +00:00
|
|
|
|
|
|
|
sequence uint16
|
|
|
|
timestamp uint32
|
|
|
|
nonce [24]byte
|
2020-11-30 01:32:18 +00:00
|
|
|
|
|
|
|
// recv fields
|
|
|
|
recvNonce [24]byte
|
|
|
|
recvBuf []byte // len 1400
|
|
|
|
recvOpus []byte // len 1400
|
|
|
|
recvPacket *Packet // uses recvOpus' backing array
|
2021-09-28 20:19:04 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
closed sync.Once
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// DialConnection dials the UDP connection using the given address and SSRC
|
|
|
|
// number.
|
2021-09-28 20:19:04 +00:00
|
|
|
func DialConnection(ctx context.Context, addr string, ssrc uint32) (*Connection, error) {
|
2021-07-02 09:42:00 +00:00
|
|
|
return DialConnectionCustom(ctx, &Dialer, addr, ssrc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialConnectionCustom dials the UDP connection with a custom dialer.
|
|
|
|
func DialConnectionCustom(
|
|
|
|
ctx context.Context, dialer *net.Dialer, addr string, ssrc uint32) (*Connection, error) {
|
|
|
|
|
2020-04-25 02:36:33 +00:00
|
|
|
// Create a new UDP connection.
|
2021-07-02 09:42:00 +00:00
|
|
|
conn, err := dialer.DialContext(ctx, "udp", addr)
|
2020-04-25 02:36:33 +00:00
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to dial host")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:16:42 +00:00
|
|
|
// https://discord.com/developers/docs/topics/voice-connections#ip-discovery
|
2020-04-25 02:36:33 +00:00
|
|
|
ssrcBuffer := [70]byte{
|
|
|
|
0x1, 0x2,
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint16(ssrcBuffer[2:4], 70)
|
|
|
|
binary.BigEndian.PutUint32(ssrcBuffer[4:8], ssrc)
|
|
|
|
|
|
|
|
_, err = conn.Write(ssrcBuffer[:])
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to write SSRC buffer")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var ipBuffer [70]byte
|
|
|
|
|
|
|
|
// ReadFull makes sure to read all 70 bytes.
|
|
|
|
_, err = io.ReadFull(conn, ipBuffer[:])
|
|
|
|
if err != nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to read IP buffer")
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ipbody := ipBuffer[4:68]
|
|
|
|
|
|
|
|
nullPos := bytes.Index(ipbody, []byte{'\x00'})
|
|
|
|
if nullPos < 0 {
|
|
|
|
return nil, errors.New("UDP IP discovery did not contain a null terminator")
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := ipbody[:nullPos]
|
|
|
|
port := binary.LittleEndian.Uint16(ipBuffer[68:70])
|
|
|
|
|
2020-11-03 17:16:42 +00:00
|
|
|
// https://discord.com/developers/docs/topics/voice-connections#encrypting-and-sending-voice
|
2020-04-25 03:14:06 +00:00
|
|
|
packet := [12]byte{
|
2020-04-25 02:36:33 +00:00
|
|
|
0: 0x80, // Version + Flags
|
|
|
|
1: 0x78, // Payload Type
|
|
|
|
// [2:4] // Sequence
|
|
|
|
// [4:8] // Timestamp
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write SSRC to the header.
|
2020-10-22 17:47:27 +00:00
|
|
|
binary.BigEndian.PutUint32(packet[8:12], ssrc) // SSRC
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
return &Connection{
|
|
|
|
GatewayIP: string(ip),
|
|
|
|
GatewayPort: port,
|
2020-11-17 22:43:00 +00:00
|
|
|
frequency: time.NewTicker(20 * time.Millisecond),
|
|
|
|
timeIncr: 960,
|
2021-09-28 20:19:04 +00:00
|
|
|
stopFreq: make(chan struct{}),
|
2020-11-17 22:43:00 +00:00
|
|
|
packet: packet,
|
|
|
|
ssrc: ssrc,
|
|
|
|
conn: conn,
|
2020-11-30 01:32:18 +00:00
|
|
|
recvBuf: make([]byte, 1400),
|
|
|
|
recvOpus: make([]byte, 1400),
|
|
|
|
recvPacket: &Packet{},
|
2020-10-22 17:47:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2020-11-17 22:43:00 +00:00
|
|
|
// ResetFrequency resets the internal frequency ticker as well as the timestamp
|
|
|
|
// incremental number. For more information, refer to
|
|
|
|
// https://tools.ietf.org/html/rfc7587#section-4.2.
|
|
|
|
//
|
|
|
|
// frameDuration controls the Opus frame duration used by the UDP connection to
|
|
|
|
// control the frequency of packets sent over. 20ms is the default by libopus.
|
|
|
|
//
|
|
|
|
// timestampIncr is the timestamp to increment for each Opus packet. This should
|
|
|
|
// be consistent with th given frameDuration. For the right combination, refer
|
|
|
|
// to the Valid Parameters section below.
|
|
|
|
//
|
|
|
|
// Valid Parameters
|
|
|
|
//
|
|
|
|
// The following table lists the recommended parameters for these variables.
|
|
|
|
//
|
|
|
|
// +---------+-----+-----+------+------+
|
|
|
|
// | Mode | 10 | 20 | 40 | 60 |
|
|
|
|
// +---------+-----+-----+------+------+
|
|
|
|
// | ts incr | 480 | 960 | 1920 | 2880 |
|
|
|
|
// +---------+-----+-----+------+------+
|
|
|
|
//
|
|
|
|
// Note that audio mode is omitted, as it is not recommended. For the full
|
|
|
|
// table, refer to the IETF RFC7587 section 4.2 link above.
|
|
|
|
func (c *Connection) ResetFrequency(frameDuration time.Duration, timeIncr uint32) {
|
|
|
|
c.frequency.Stop()
|
|
|
|
c.frequency = time.NewTicker(frameDuration)
|
|
|
|
c.timeIncr = timeIncr
|
|
|
|
}
|
|
|
|
|
2020-10-22 17:47:27 +00:00
|
|
|
// UseSecret uses the given secret. This method is not thread-safe, so it should
|
|
|
|
// only be used right after initialization.
|
|
|
|
func (c *Connection) UseSecret(secret [32]byte) {
|
|
|
|
c.secret = secret
|
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// SetWriteDeadline sets the UDP connection's write deadline.
|
|
|
|
func (c *Connection) SetWriteDeadline(deadline time.Time) {
|
|
|
|
c.conn.SetWriteDeadline(deadline)
|
2020-10-22 17:47:27 +00:00
|
|
|
}
|
2020-04-25 02:36:33 +00:00
|
|
|
|
2021-09-28 20:19:04 +00:00
|
|
|
// SetReadDeadline sets the UDP connection's read deadline.
|
|
|
|
func (c *Connection) SetReadDeadline(deadline time.Time) {
|
|
|
|
c.conn.SetReadDeadline(deadline)
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Close closes the connection.
|
2020-04-25 02:36:33 +00:00
|
|
|
func (c *Connection) Close() error {
|
2021-07-02 09:42:00 +00:00
|
|
|
c.closed.Do(func() {
|
2021-09-28 20:19:04 +00:00
|
|
|
// Be sure to only run this ONCE.
|
|
|
|
c.frequency.Stop()
|
|
|
|
close(c.stopFreq)
|
2021-07-02 09:42:00 +00:00
|
|
|
})
|
2021-09-28 20:19:04 +00:00
|
|
|
|
2020-11-17 19:34:22 +00:00
|
|
|
return c.conn.Close()
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Write sends a packet of audio into the voice UDP connection. It is made to be
|
|
|
|
// stream-compatible: the internal frequency clock will slow Write down to match
|
|
|
|
// the real playback time.
|
2020-04-25 02:36:33 +00:00
|
|
|
func (c *Connection) Write(b []byte) (int, error) {
|
2020-10-22 17:47:27 +00:00
|
|
|
// Write a new sequence.
|
|
|
|
binary.BigEndian.PutUint16(c.packet[2:4], c.sequence)
|
|
|
|
c.sequence++
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(c.packet[4:8], c.timestamp)
|
2020-11-17 22:43:00 +00:00
|
|
|
c.timestamp += c.timeIncr
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Copy the first 12 bytes from the packet into the nonce.
|
|
|
|
copy(c.nonce[:12], c.packet[:])
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Seal the message, but reuse the packet buffer. We pass in the first 12
|
|
|
|
// bytes of the packet, but allow it to reuse the whole packet buffer
|
|
|
|
toSend := secretbox.Seal(c.packet[:12], b, &c.nonce, &c.secret)
|
2020-10-22 17:47:27 +00:00
|
|
|
|
2020-11-17 22:43:00 +00:00
|
|
|
select {
|
|
|
|
case <-c.frequency.C:
|
2021-09-28 20:19:04 +00:00
|
|
|
// ok
|
|
|
|
case <-c.stopFreq:
|
2021-07-02 09:42:00 +00:00
|
|
|
return 0, errors.Wrap(net.ErrClosed, "frequency ticker stopped")
|
2020-11-17 22:43:00 +00:00
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
_, err := c.conn.Write(toSend)
|
2020-10-22 17:47:27 +00:00
|
|
|
if err != nil {
|
2021-07-02 09:42:00 +00:00
|
|
|
return 0, err
|
2020-10-22 17:47:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len(b), nil
|
2020-04-25 02:36:33 +00:00
|
|
|
}
|
2020-11-30 01:32:18 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Packet represents a voice packet.
|
|
|
|
type Packet struct {
|
|
|
|
header []byte
|
|
|
|
Opus []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// VersionFlags returns the version flags of the current packet.
|
|
|
|
func (p *Packet) VersionFlags() byte { return p.header[0] }
|
|
|
|
|
|
|
|
// Type returns the packet type.
|
|
|
|
func (p *Packet) Type() byte { return p.header[1] }
|
|
|
|
|
|
|
|
// Sequence returns the packet sequence.
|
|
|
|
func (p *Packet) Sequence() uint16 { return binary.BigEndian.Uint16(p.header[2:4]) }
|
|
|
|
|
|
|
|
// Timestamp returns the packet's timestamp.
|
|
|
|
func (p *Packet) Timestamp() uint32 { return binary.BigEndian.Uint32(p.header[4:8]) }
|
|
|
|
|
|
|
|
// SSRC returns the packet's SSRC number.
|
|
|
|
func (p *Packet) SSRC() uint32 { return binary.BigEndian.Uint32(p.header[8:12]) }
|
|
|
|
|
|
|
|
// Copy copies the current packet into the given packet.
|
|
|
|
func (p *Packet) Copy(dst *Packet) {
|
|
|
|
dst.header = append(dst.header[:0], p.header...)
|
|
|
|
dst.Opus = append(dst.Opus[:0], p.Opus...)
|
|
|
|
}
|
|
|
|
|
|
|
|
const packetHeaderSize = 12
|
|
|
|
|
|
|
|
// ReadPacket reads the UDP connection and returns a packet if successful. The
|
|
|
|
// returned packet is invalidated once ReadPacket is called again. To avoid
|
|
|
|
// this, manually Copy the packet.
|
2020-11-30 01:32:18 +00:00
|
|
|
func (c *Connection) ReadPacket() (*Packet, error) {
|
2021-07-02 09:42:00 +00:00
|
|
|
if c.recvPacket.header == nil {
|
|
|
|
// Initialize the recvPacket's header.
|
|
|
|
c.recvPacket.header = c.recvBuf[:12]
|
|
|
|
}
|
2020-11-30 01:32:18 +00:00
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
for {
|
|
|
|
i, err := c.conn.Read(c.recvBuf)
|
2020-11-30 01:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
if i < packetHeaderSize || (c.recvBuf[0] != 0x80 && c.recvBuf[0] != 0x90) {
|
2020-11-30 01:32:18 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Copy the nonce to be read.
|
|
|
|
// TODO: once Go 1.17 is released, we can remove recvNonce and directly
|
|
|
|
// cast it as (*[packetHeaderSize]byte)(c.recvBuf).
|
2020-11-30 01:32:18 +00:00
|
|
|
copy(c.recvNonce[:], c.recvBuf[0:packetHeaderSize])
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
|
2021-07-02 09:42:00 +00:00
|
|
|
// Open (decrypt) the rest of the received bytes.
|
2021-02-22 20:32:55 +00:00
|
|
|
c.recvPacket.Opus, ok = secretbox.Open(
|
2021-07-02 09:42:00 +00:00
|
|
|
c.recvOpus[:0], c.recvBuf[packetHeaderSize:i], &c.recvNonce, &c.secret)
|
2020-11-30 01:32:18 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, ErrDecryptionFailed
|
|
|
|
}
|
|
|
|
|
2021-02-22 20:32:55 +00:00
|
|
|
// Partial structure of the RTP header for reference
|
|
|
|
//
|
|
|
|
// 0 1 2 3
|
|
|
|
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
// |V=2|P|X| CC |M| PT | sequence number |
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
// | timestamp |
|
|
|
|
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
//
|
|
|
|
// References
|
|
|
|
//
|
|
|
|
// https://tools.ietf.org/html/rfc3550#section-5.1
|
|
|
|
//
|
|
|
|
|
|
|
|
// We first check VersionFlags (8-bit) for whether or not the 4th bit
|
|
|
|
// (extension) is set. The value of 0x10 is 0b00010000. RFC3550 section
|
|
|
|
// 5.1 explains the extension bit as:
|
|
|
|
//
|
|
|
|
// If the extension bit is set, the fixed header MUST be followed by
|
|
|
|
// exactly one header extension, with a format defined in Section
|
|
|
|
// 5.3.1.
|
|
|
|
//
|
2021-07-02 09:42:00 +00:00
|
|
|
isExtension := c.recvPacket.VersionFlags()&0x10 == 0x10
|
2021-02-22 20:32:55 +00:00
|
|
|
|
|
|
|
// We then check for whether or not the marker bit (9th bit) is set. The
|
|
|
|
// 9th bit is carried over to the second byte (Type), so we check its
|
|
|
|
// presence with 0x80, or 0b10000000. RFC3550 section 5.1 explains the
|
|
|
|
// marker bit as:
|
|
|
|
//
|
|
|
|
// The interpretation of the marker is defined by a profile. It is
|
|
|
|
// intended to allow significant events such as frame boundaries to
|
|
|
|
// be marked in the packet stream. A profile MAY define additional
|
|
|
|
// marker bits or specify that there is no marker bit by changing
|
|
|
|
// the number of bits in the payload type field (see Section 5.3).
|
|
|
|
//
|
|
|
|
// RFC3350 section 12.1 also writes:
|
|
|
|
//
|
|
|
|
// When the RTCP packet type field is compared to the corresponding
|
|
|
|
// octet of the RTP header, this range corresponds to the marker bit
|
|
|
|
// being 1 (which it usually is not in data packets) and to the high
|
|
|
|
// bit of the standard payload type field being 1 (since the static
|
|
|
|
// payload types are typically defined in the low half).
|
|
|
|
//
|
|
|
|
// This implies that, when the marker bit is 1, the received packet is
|
|
|
|
// an RTCP packet and NOT an RTP packet; therefore, we must ignore the
|
|
|
|
// unknown sections, so we do a (NOT isMarker) check below.
|
2021-07-02 09:42:00 +00:00
|
|
|
isMarker := c.recvPacket.Type()&0x80 != 0x0
|
2021-02-22 20:32:55 +00:00
|
|
|
|
|
|
|
if isExtension && !isMarker {
|
|
|
|
extLen := binary.BigEndian.Uint16(c.recvPacket.Opus[2:4])
|
|
|
|
shift := 4 + 4*int(extLen)
|
|
|
|
|
|
|
|
if len(c.recvPacket.Opus) > shift {
|
|
|
|
c.recvPacket.Opus = c.recvPacket.Opus[shift:]
|
|
|
|
}
|
2020-11-30 01:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.recvPacket, nil
|
|
|
|
}
|
|
|
|
}
|