-
Notifications
You must be signed in to change notification settings - Fork 145
/
VoiceSocket.lua
213 lines (171 loc) · 4.68 KB
/
VoiceSocket.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
local uv = require('uv')
local class = require('class')
local timer = require('timer')
local enums = require('enums')
local sodium = require('voice/sodium') or {}
local WebSocket = require('client/WebSocket')
local logLevel = assert(enums.logLevel)
local format = string.format
local setInterval, clearInterval = timer.setInterval, timer.clearInterval
local wrap = coroutine.wrap
local time = os.time
local unpack, pack = string.unpack, string.pack -- luacheck: ignore
local SUPPORTED_ENCRYPTION_MODES = { 'aead_xchacha20_poly1305_rtpsize' }
if sodium.aead_aes256_gcm then -- AEAD AES256-GCM is only available if the hardware supports it
table.insert(SUPPORTED_ENCRYPTION_MODES, 1, 'aead_aes256_gcm_rtpsize')
end
local IDENTIFY = 0
local SELECT_PROTOCOL = 1
local READY = 2
local HEARTBEAT = 3
local DESCRIPTION = 4
local SPEAKING = 5
local HEARTBEAT_ACK = 6
local RESUME = 7
local HELLO = 8
local RESUMED = 9
local function checkMode(modes)
for _, ENCRYPTION_MODE in ipairs(SUPPORTED_ENCRYPTION_MODES) do
for _, mode in ipairs(modes) do
if mode == ENCRYPTION_MODE then
return mode
end
end
end
end
local VoiceSocket = class('VoiceSocket', WebSocket)
for name in pairs(logLevel) do
VoiceSocket[name] = function(self, fmt, ...)
local client = self._client
return client[name](client, format('Voice : %s', fmt), ...)
end
end
function VoiceSocket:__init(state, connection, manager)
WebSocket.__init(self, manager)
self._state = state
self._manager = manager
self._client = manager._client
self._connection = connection
self._session_id = state.session_id
self._seq_ack = -1
end
function VoiceSocket:handleDisconnect()
-- TODO: reconnecting and resuming
self._connection:_cleanup()
end
function VoiceSocket:handlePayload(payload)
local manager = self._manager
local d = payload.d
local op = payload.op
if payload.seq then
self._seq_ack = payload.seq
end
self:debug('WebSocket OP %s', op)
if op == HELLO then
self:info('Received HELLO')
self:startHeartbeat(d.heartbeat_interval)
self:identify()
elseif op == READY then
self:info('Received READY')
local mode = checkMode(d.modes)
if mode then
self:debug('Selected encryption mode %q', mode)
self._mode = mode
self._ssrc = d.ssrc
self:handshake(d.ip, d.port)
else
self:error('No supported encryption mode available')
self:disconnect()
end
elseif op == RESUMED then
self:info('Received RESUMED')
elseif op == DESCRIPTION then
if d.mode == self._mode then
self._connection:_prepare(d.secret_key, self)
else
self:error('%q encryption mode not available', self._mode)
self:disconnect()
end
elseif op == HEARTBEAT_ACK then
manager:emit('heartbeat', nil, self._sw.milliseconds) -- TODO: id
elseif op == SPEAKING then
return -- TODO
elseif op == 12 or op == 13 then
return -- ignore
elseif op then
self:warning('Unhandled WebSocket payload OP %i', op)
end
end
local function loop(self)
return wrap(self.heartbeat)(self)
end
function VoiceSocket:startHeartbeat(interval)
if self._heartbeat then
clearInterval(self._heartbeat)
end
self._heartbeat = setInterval(interval, loop, self)
end
function VoiceSocket:stopHeartbeat()
if self._heartbeat then
clearInterval(self._heartbeat)
end
self._heartbeat = nil
end
function VoiceSocket:heartbeat()
self._sw:reset()
return self:_send(HEARTBEAT, {
t = time(),
seq_ack = self._seq_ack,
})
end
function VoiceSocket:identify()
local state = self._state
return self:_send(IDENTIFY, {
server_id = state.guild_id,
user_id = state.user_id,
session_id = state.session_id,
token = state.token,
}, true)
end
function VoiceSocket:resume()
local state = self._state
return self:_send(RESUME, {
server_id = state.guild_id,
session_id = state.session_id,
token = state.token,
seq_ack = self._seq_ack,
})
end
function VoiceSocket:handshake(server_ip, server_port)
local udp = uv.new_udp()
self._udp = udp
self._ip = server_ip
self._port = server_port
udp:recv_start(function(err, data)
assert(not err, err)
udp:recv_stop()
local client_ip = unpack('xxxxxxxxz', data)
local client_port = unpack('<I2', data, -2)
return wrap(self.selectProtocol)(self, client_ip, client_port)
end)
local packet = pack('>I2I2I4c64H', 0x1, 70, self._ssrc, self._ip, self._port)
return udp:send(packet, server_ip, server_port)
end
function VoiceSocket:selectProtocol(address, port)
return self:_send(SELECT_PROTOCOL, {
protocol = 'udp',
data = {
address = address,
port = port,
mode = self._mode,
}
})
end
function VoiceSocket:setSpeaking(speaking)
return self:_send(SPEAKING, {
speaking = speaking,
delay = 0,
ssrc = self._ssrc,
})
end
return VoiceSocket