2018-08-24 01:24:20 +00:00
|
|
|
package gavalink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2019-01-13 23:37:38 +00:00
|
|
|
"github.com/valyala/fastjson"
|
2018-08-24 01:24:20 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-10-13 18:07:59 +00:00
|
|
|
"strings"
|
2018-08-24 01:24:20 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeConfig configures a Lavalink Node
|
|
|
|
type NodeConfig struct {
|
|
|
|
// REST is the host where Lavalink's REST server runs
|
|
|
|
//
|
|
|
|
// This value is expected without a trailing slash, e.g. like
|
|
|
|
// `http://localhost:2333`
|
|
|
|
REST string
|
|
|
|
// WebSocket is the host where Lavalink's WebSocket server runs
|
|
|
|
//
|
|
|
|
// This value is expected without a trailing slash, e.g. like
|
|
|
|
// `http://localhost:8012`
|
|
|
|
WebSocket string
|
|
|
|
// Password is the expected Authorization header for the Node
|
|
|
|
Password string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Node wraps a Lavalink Node
|
|
|
|
type Node struct {
|
|
|
|
config NodeConfig
|
2019-10-13 08:39:18 +00:00
|
|
|
stats *RemoteStats
|
2018-08-24 01:24:20 +00:00
|
|
|
manager *Lavalink
|
|
|
|
wsConn *websocket.Conn
|
2019-10-13 01:13:23 +00:00
|
|
|
client *http.Client
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 08:39:18 +00:00
|
|
|
type RemoteStats struct {
|
|
|
|
Op string `json:"op"`
|
|
|
|
Players int `json:"players"`
|
|
|
|
ActivePlayers int `json:"playingPlayers"`
|
|
|
|
Uptime int64 `json:"uptime"`
|
|
|
|
Memory *MemoryStats `json:"memory"`
|
|
|
|
Cpu *CpuStats `json:"cpu"`
|
2019-10-13 08:44:51 +00:00
|
|
|
Frames *FrameStats `json:"frameStats"`
|
2019-10-13 08:39:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MemoryStats struct {
|
|
|
|
Free uint64 `json:"free"`
|
|
|
|
Used uint64 `json:"used"`
|
|
|
|
Allocated uint64 `json:"allocated"`
|
|
|
|
Reserveable uint64 `json:"reserveable"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type CpuStats struct {
|
|
|
|
Cores int `json:"cores"`
|
|
|
|
SystemLoad float64 `json:"systemLoad"`
|
|
|
|
LavalinkLoad float64 `json:"lavalinkLoad"`
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:44:51 +00:00
|
|
|
type FrameStats struct {
|
|
|
|
Sent int `json:"sent"`
|
|
|
|
Nulled int `json:"nulled"`
|
|
|
|
Deficit int `json:"deficit"`
|
|
|
|
}
|
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
func (node *Node) open() error {
|
|
|
|
header := http.Header{}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
header.Set("Authorization", node.config.Password)
|
|
|
|
header.Set("Num-Shards", node.manager.shards)
|
|
|
|
header.Set("User-Id", node.manager.userID)
|
|
|
|
|
2019-10-13 18:07:59 +00:00
|
|
|
if node.manager.capabilities != nil {
|
|
|
|
v := make([]string, 0)
|
|
|
|
|
|
|
|
for k, vals := range node.manager.capabilities {
|
|
|
|
b, err := json.Marshal(vals)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v = append(v, k+"="+string(b))
|
|
|
|
}
|
|
|
|
|
|
|
|
header.Set("Capabilities", strings.Join(v, ";"))
|
|
|
|
}
|
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
ws, resp, err := websocket.DefaultDialer.Dial(node.config.WebSocket, header)
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
vstr := resp.Header.Get("Lavalink-Major-Version")
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
v, err := strconv.Atoi(vstr)
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if v < 3 {
|
|
|
|
return errInvalidVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
node.wsConn = ws
|
|
|
|
go node.listen()
|
|
|
|
|
|
|
|
Log.Println("node", node.config.WebSocket, "opened")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) stop() {
|
|
|
|
// someone already stopped this
|
|
|
|
if node.wsConn == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
_ = node.wsConn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (node *Node) listen() {
|
2019-01-13 23:37:38 +00:00
|
|
|
var p fastjson.Parser
|
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
for {
|
|
|
|
msgType, msg, err := node.wsConn.ReadMessage()
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
Log.Println(err)
|
|
|
|
// try to reconnect
|
|
|
|
oerr := node.open()
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if oerr != nil {
|
|
|
|
Log.Println("node", node.config.WebSocket, "failed and could not reconnect, destroying.", err, oerr)
|
|
|
|
node.manager.removeNode(node)
|
|
|
|
return
|
|
|
|
}
|
2019-10-13 01:13:23 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
Log.Println("node", node.config.WebSocket, "reconnected")
|
|
|
|
return
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
|
|
|
if msgType != websocket.TextMessage {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := p.ParseBytes(msg)
|
2018-08-24 01:24:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2019-01-13 23:37:38 +00:00
|
|
|
continue
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 08:39:18 +00:00
|
|
|
node.onEvent(v, msg)
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
}
|
2018-08-24 01:24:20 +00:00
|
|
|
|
2019-10-13 08:39:18 +00:00
|
|
|
func (node *Node) onEvent(v *fastjson.Value, msg []byte) error {
|
2019-01-13 23:37:38 +00:00
|
|
|
op := jsonStringValue(v, "op")
|
2018-08-24 01:24:20 +00:00
|
|
|
|
2019-01-13 23:37:38 +00:00
|
|
|
switch op {
|
2019-10-13 08:39:18 +00:00
|
|
|
case opStats:
|
|
|
|
node.stats = &RemoteStats{}
|
|
|
|
|
|
|
|
err := json.Unmarshal(msg, &node.stats)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-08-24 01:24:20 +00:00
|
|
|
case opPlayerUpdate:
|
2019-01-13 23:37:38 +00:00
|
|
|
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
|
2019-10-13 08:39:18 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
|
|
|
player.time = v.GetInt("state", "time")
|
|
|
|
player.position = v.GetInt("state", "position")
|
2018-08-24 01:24:20 +00:00
|
|
|
case opEvent:
|
2019-01-13 23:37:38 +00:00
|
|
|
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-01-13 23:37:38 +00:00
|
|
|
track := jsonStringValue(v, "track")
|
|
|
|
|
|
|
|
switch jsonStringValue(v, "type") {
|
2018-08-24 01:24:20 +00:00
|
|
|
case eventTrackEnd:
|
2018-08-26 02:58:41 +00:00
|
|
|
player.track = ""
|
2019-01-13 23:37:38 +00:00
|
|
|
err = player.handler.OnTrackEnd(player, track, jsonStringValue(v, "reason"))
|
2018-08-24 01:24:20 +00:00
|
|
|
case eventTrackException:
|
2019-01-13 23:37:38 +00:00
|
|
|
err = player.handler.OnTrackException(player, track, jsonStringValue(v, "reason"))
|
2018-08-24 01:24:20 +00:00
|
|
|
case eventTrackStuck:
|
2019-01-13 23:37:38 +00:00
|
|
|
err = player.handler.OnTrackStuck(player, track, v.GetInt("thresholdMs"))
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2019-02-04 00:31:53 +00:00
|
|
|
case opVoiceProcessed:
|
|
|
|
player, err := node.manager.GetPlayer(jsonStringValue(v, "guildId"))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
track := jsonStringValue(v, "track")
|
|
|
|
|
|
|
|
data := &VoiceProcessingData{
|
2019-10-14 23:18:25 +00:00
|
|
|
node: node,
|
2019-10-14 02:58:25 +00:00
|
|
|
UserID: jsonStringValue(v, "userId"),
|
|
|
|
URL: fmt.Sprintf("%s/audio/%s", node.config.REST, track),
|
|
|
|
File: track,
|
2019-02-04 00:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return player.handler.OnVoiceProcessed(player, data, v.GetBool("hotword"), v.GetBool("override"))
|
2018-08-24 01:24:20 +00:00
|
|
|
default:
|
|
|
|
return errUnknownPayload
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatePlayer creates an audio player on this node
|
|
|
|
func (node *Node) CreatePlayer(guildID string, sessionID string, event VoiceServerUpdate, handler EventHandler) (*Player, error) {
|
2019-01-13 23:37:38 +00:00
|
|
|
msg := voiceUpdateMessage{
|
2018-08-24 01:24:20 +00:00
|
|
|
Op: opVoiceUpdate,
|
|
|
|
GuildID: guildID,
|
|
|
|
SessionID: sessionID,
|
|
|
|
Event: &event,
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
|
|
|
err := node.writeMessage(msg)
|
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
player := &Player{
|
2019-10-13 03:57:59 +00:00
|
|
|
guildID: guildID,
|
2019-10-13 04:04:01 +00:00
|
|
|
sessionID: sessionID,
|
2019-10-13 03:57:59 +00:00
|
|
|
manager: node.manager,
|
|
|
|
node: node,
|
|
|
|
handler: handler,
|
|
|
|
vol: 100,
|
|
|
|
lastVoiceServerUpdate: event,
|
2018-08-24 01:24:20 +00:00
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
node.manager.players[guildID] = player
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
return player, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadTracks queries lavalink to return a Tracks object
|
|
|
|
//
|
|
|
|
// query should be a valid Lavaplayer query, including but not limited to:
|
|
|
|
// - A direct media URI
|
|
|
|
// - A direct Youtube /watch URI
|
|
|
|
// - A search query, prefixed with ytsearch: or scsearch:
|
|
|
|
//
|
|
|
|
// See the Lavaplayer Source Code for all valid options.
|
|
|
|
func (node *Node) LoadTracks(query string) (*Tracks, error) {
|
|
|
|
url := fmt.Sprintf("%s/loadtracks?identifier=%s", node.config.REST, query)
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
req.Header.Set("Authorization", node.config.Password)
|
|
|
|
|
2019-10-13 01:13:23 +00:00
|
|
|
resp, err := node.client.Do(req)
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
tracks := new(Tracks)
|
2019-01-13 23:37:38 +00:00
|
|
|
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(&tracks); err != nil {
|
2018-08-24 01:24:20 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
2018-08-24 01:24:20 +00:00
|
|
|
return tracks, nil
|
|
|
|
}
|
2019-01-13 23:37:38 +00:00
|
|
|
|
|
|
|
func (node *Node) writeMessage(v interface{}) error {
|
|
|
|
return node.wsConn.WriteJSON(v)
|
|
|
|
}
|