forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
voice.go
343 lines (278 loc) · 8.66 KB
/
voice.go
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Discordgo - Discord bindings for Go
// Available at https://github.com/bwmarrin/discordgo
// Copyright 2015 Bruce Marriner <bruce@sqls.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains experimental functions for interacting with the Discord
// Voice websocket and UDP connections.
//
// EVERYTHING in this file is very experimental and will change.
package discordgo
import (
"encoding/binary"
"encoding/json"
"fmt"
"net"
"strings"
"time"
"github.com/gorilla/websocket"
)
// A VEvent is the initial structure for voice websocket events. I think
// I can reuse the data websocket structure here.
type VEvent struct {
Type string `json:"t"`
State int `json:"s"`
Operation int `json:"op"`
RawData json.RawMessage `json:"d"`
}
// A VoiceOP2 stores the data for voice operation 2 websocket events
// which is sort of like the voice READY packet
type VoiceOP2 struct {
SSRC uint32 `json:"ssrc"`
Port int `json:"port"`
Modes []string `json:"modes"`
HeartbeatInterval time.Duration `json:"heartbeat_interval"`
}
type voiceHandshakeData struct {
ServerID string `json:"server_id"`
UserID string `json:"user_id"`
SessionID string `json:"session_id"`
Token string `json:"token"`
}
type voiceHandshakeOp struct {
Op int `json:"op"` // Always 0
Data voiceHandshakeData `json:"d"`
}
// VoiceOpenWS opens a voice websocket connection. This should be called
// after VoiceChannelJoin is used and the data VOICE websocket events
// are captured.
func (s *Session) VoiceOpenWS() {
// Don't open a socket if one is already open
if s.VwsConn != nil {
return
}
var self User
var err error
self, err = s.User("@me") // TODO: Move to @ login and store in session
// Connect to Voice Websocket
vg := fmt.Sprintf("wss://%s", strings.TrimSuffix(s.VEndpoint, ":80"))
s.VwsConn, _, err = websocket.DefaultDialer.Dial(vg, nil)
if err != nil {
fmt.Println("VOICE cannot open websocket:", err)
}
json := voiceHandshakeOp{0, voiceHandshakeData{s.VGuildID, self.ID, s.VSessionID, s.VToken}}
err = s.VwsConn.WriteJSON(json)
if err != nil {
fmt.Println("VOICE ERROR sending init packet:", err)
}
// Start a listening for voice websocket events
go s.VoiceListen()
}
// Close closes the connection to the voice websocket.
func (s *Session) VoiceCloseWS() {
s.VwsConn.Close()
}
// VoiceListen listens on the voice websocket for messages and passes them
// to the voice event handler.
func (s *Session) VoiceListen() (err error) {
for {
messageType, message, err := s.VwsConn.ReadMessage()
if err != nil {
fmt.Println("Voice Listen Error:", err)
break
}
// Pass received message to voice event handler
go s.VoiceEvent(messageType, message)
}
return
}
// VoiceEvent handles any messages received on the voice websocket
func (s *Session) VoiceEvent(messageType int, message []byte) (err error) {
if s.Debug {
fmt.Println("VOICE EVENT:", messageType)
printJSON(message)
}
var e VEvent
if err := json.Unmarshal(message, &e); err != nil {
return err
}
switch e.Operation {
case 2: // READY packet
var st VoiceOP2
if err := json.Unmarshal(e.RawData, &st); err != nil {
fmt.Println(e.Type, err)
printJSON(e.RawData) // TODO: Better error logginEventg
return err
}
// Start the voice websocket heartbeat to keep the connection alive
go s.VoiceHeartbeat(st.HeartbeatInterval)
// Store all event data into the session
s.Vop2 = st
// We now have enough data to start the UDP connection
s.VoiceOpenUDP()
return
case 3: // HEARTBEAT response
// add code to use this to track latency?
return
case 4:
// TODO
default:
fmt.Println("UNKNOWN VOICE OP: ", e.Operation)
printJSON(e.RawData)
}
return
}
type voiceUDPData struct {
Address string `json:"address"` // Public IP of machine running this code
Port uint16 `json:"port"` // UDP Port of machine running this code
Mode string `json:"mode"` // plain or ? (plain or encrypted)
}
type voiceUDPD struct {
Protocol string `json:"protocol"` // Always "udp" ?
Data voiceUDPData `json:"data"`
}
type voiceUDPOp struct {
Op int `json:"op"` // Always 1
Data voiceUDPD `json:"d"`
}
// VoiceOpenUDP opens a UDP connect to the voice server and completes the
// initial required handshake. This connect is left open in the session
// and can be used to send or receive audio.
func (s *Session) VoiceOpenUDP() {
udpHost := fmt.Sprintf("%s:%d", strings.TrimSuffix(s.VEndpoint, ":80"), s.Vop2.Port)
serverAddr, err := net.ResolveUDPAddr("udp", udpHost)
if err != nil {
fmt.Println(err)
}
s.UDPConn, err = net.DialUDP("udp", nil, serverAddr)
if err != nil {
fmt.Println(err)
}
// Create a 70 byte array and put the SSRC code from the Op 2 Voice event
// into it. Then send that over the UDP connection to Discord
sb := make([]byte, 70)
binary.BigEndian.PutUint32(sb, s.Vop2.SSRC)
s.UDPConn.Write(sb)
// Create a 70 byte array and listen for the initial handshake response
// 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
// saw us.
rb := make([]byte, 70)
rlen, _, err := s.UDPConn.ReadFromUDP(rb)
if rlen < 70 {
fmt.Println("Voice RLEN should be 70 but isn't")
}
// Loop over position 4 though 20 to grab the IP address
// Should never be beyond position 20.
var ip string
for i := 4; i < 20; i++ {
if rb[i] == 0 {
break
}
ip += string(rb[i])
}
// Grab port from postion 68 and 69
port := binary.LittleEndian.Uint16(rb[68:70])
// Take the parsed data from above and send it back to Discord
// to finalize the UDP handshake.
jsondata := voiceUDPOp{1, voiceUDPD{"udp", voiceUDPData{ip, port, "plain"}}}
err = s.VwsConn.WriteJSON(jsondata)
if err != nil {
fmt.Println("error:", err)
return
}
s.UDPReady = true
}
// VoiceCloseUDP closes the voice UDP connection.
func (s *Session) VoiceCloseUDP() {
s.UDPConn.Close()
}
type voiceSpeakingData struct {
Speaking bool `json:"speaking"`
Delay int `json:"delay"`
}
type voiceSpeakingOp struct {
Op int `json:"op"` // Always 5
Data voiceSpeakingData `json:"d"`
}
func (s *Session) VoiceSpeaking() {
if s.VwsConn == nil {
// TODO return an error
fmt.Println("No Voice websocket.")
return
}
json := voiceSpeakingOp{5, voiceSpeakingData{true, 0}}
err := s.VwsConn.WriteJSON(json)
if err != nil {
fmt.Println("error:", err)
return
}
}
// VoiceListenUDP is test code to listen for UDP packets
func (s *Session) VoiceListenUDP() {
// start the udp keep alive too. Otherwise listening doesn't get much.
// THIS DOES NOT WORK YET
// go s.VoiceUDPKeepalive(s.Vop2.HeartbeatInterval) // lets try the ws timer
for {
b := make([]byte, 1024) //TODO DO NOT PUT MAKE INSIDE LOOP
rlen, _, err := s.UDPConn.ReadFromUDP(b)
if err != nil {
fmt.Println("Error reading from UDP:", err)
// return
}
if rlen < 1 {
fmt.Println("Empty UDP packet received")
continue
// empty packet?
}
fmt.Println("READ FROM UDP: ", b)
}
}
// VoiceUDPKeepalive sends a packet to keep the UDP connection forwarding
// alive for NATed clients. Without this no audio can be received
// after short periods of silence.
// Not sure how often this is supposed to be sent or even what payload
// I am suppose to be sending. So this is very.. unfinished :)
func (s *Session) VoiceUDPKeepalive(i time.Duration) {
// NONE OF THIS WORKS. SO DON'T USE IT.
//
// testing with the above 70 byte SSRC packet.
//
// Create a 70 byte array and put the SSRC code from the Op 2 Voice event
// into it. Then send that over the UDP connection to Discord
ticker := time.NewTicker(i * time.Millisecond)
for range ticker.C {
sb := make([]byte, 8)
sb[0] = 0x80
sb[1] = 0xc9
sb[2] = 0x00
sb[3] = 0x01
ssrcBE := make([]byte, 4)
binary.BigEndian.PutUint32(ssrcBE, s.Vop2.SSRC)
sb[4] = ssrcBE[0]
sb[5] = ssrcBE[1]
sb[6] = ssrcBE[2]
sb[7] = ssrcBE[3]
s.UDPConn.Write(ssrcBE)
}
}
type voiceHeartbeatOp struct {
Op int `json:"op"` // Always 3
Data int `json:"d"`
}
// VoiceHeartbeat sends regular heartbeats to voice Discord so it knows the client
// is still connected. If you do not send these heartbeats Discord will
// disconnect the websocket connection after a few seconds.
func (s *Session) VoiceHeartbeat(i time.Duration) {
ticker := time.NewTicker(i * time.Millisecond)
for {
err := s.VwsConn.WriteJSON(voiceHeartbeatOp{3, int(time.Now().Unix())})
if err != nil {
s.VoiceReady = false
fmt.Println(err)
return // TODO LOG ERROR
}
s.VoiceReady = true
<-ticker.C
}
}