Skip to content

Commit

Permalink
Merge pull request #147 from Frederikam/feature/close-events
Browse files Browse the repository at this point in the history
Release v3.1
  • Loading branch information
freyacodes authored Oct 14, 2018
2 parents 0b3ca3e + 1b1eb72 commit 2a9285a
Show file tree
Hide file tree
Showing 21 changed files with 333 additions and 465 deletions.
20 changes: 18 additions & 2 deletions IMPLEMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Server emitted an event. See the client implementation below.
```json
{
"op": "event",
...
"type": "..."
}
```

Expand All @@ -185,7 +185,7 @@ Server emitted an event. See the client implementation below.
* 2. TrackExceptionEvent
* 3. TrackStuckEvent
* <p>
* The remaining are caused by the client
* The remaining lavaplayer events are caused by client actions, and are therefore not forwarded via WS.
*/
private void handleEvent(JSONObject json) throws IOException {
LavalinkPlayer player = (LavalinkPlayer) lavalink.getPlayer(json.getString("guildId"));
Expand Down Expand Up @@ -221,6 +221,22 @@ private void handleEvent(JSONObject json) throws IOException {

See also: [AudioTrackEndReason.java](https://github.com/sedmelluq/lavaplayer/blob/master/main/src/main/java/com/sedmelluq/discord/lavaplayer/track/AudioTrackEndReason.java)

Additionally there is also the `WebSocketClosedEvent`, which signals when an audio web socket (to Discord) is closed.
This can happen for various reasons (normal and abnormal), e.g when using an expired voice server update.
4xxx codes are usually bad.
See the [Discord docs](https://discordapp.com/developers/docs/topics/opcodes-and-status-codes#voice-voice-close-event-codes).

```json
{
"op": "event",
"type": "WebSocketClosedEvent",
"guildId": "...",
"code": 4006,
"reason": "Your session is no longer valid.",
"byRemote": true
}
```

### REST API
The REST api is used to resolve audio tracks for use with the `play` op.
```
Expand Down
5 changes: 1 addition & 4 deletions LavalinkServer/application.yml.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
server: # REST server
server: # REST and WS server
port: 2333
address: 0.0.0.0
spring:
Expand All @@ -7,9 +7,6 @@ spring:
lavalink:
server:
password: "youshallnotpass"
ws:
port: 80
host: 0.0.0.0
sources:
youtube: true
bandcamp: true
Expand Down
14 changes: 10 additions & 4 deletions LavalinkServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,22 @@ test {
}

dependencies {
compile group: 'space.npstr', name: 'Magma', version: magmaVersion
compile group: 'com.sedmelluq', name: 'lavaplayer', version: lavaplayerVersion
compile group: 'com.github.DV8FromTheWorld', name: 'JDA-Audio', version: jdaAudioVersion
compile group: 'com.github.FredBoat.jda-nas', name: 'jda-nas', version: jdaNasVersion
compile group: 'com.sedmelluq', name: 'jda-nas', version: jdaNasVersion

compile group: 'com.github.shredder121', name: 'jda-async-packetprovider', version: jappVersion
compile group: 'org.java-websocket', name: 'Java-WebSocket', version: javaWebSocketVersion
//required by japp
compile group: 'org.apache.commons', name: 'commons-lang3', version: commonsLangVersion
compile group: 'org.springframework', name: 'spring-websocket', version: springWebSocketVersion
compile group: 'ch.qos.logback', name: 'logback-classic', version: logbackVersion
compile group: 'io.sentry', name: 'sentry-logback', version: sentryLogbackVersion
compile group: 'com.github.oshi', name: 'oshi-core', version: oshiVersion
compile group: 'org.json', name: 'json', version: jsonOrgVersion
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion
compile(group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: springBootVersion) {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
compile group: 'org.springframework.boot', name: 'spring-boot-starter-undertow', version: springBootVersion
compileOnly group: 'com.github.spotbugs', name: 'spotbugs-annotations', version: spotbugsAnnotationsVersion

compile group: 'io.prometheus', name: 'simpleclient', version: prometheusVersion
Expand Down
19 changes: 0 additions & 19 deletions LavalinkServer/src/main/java/lavalink/server/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,19 @@
import com.sedmelluq.discord.lavaplayer.tools.PlayerLibrary;
import lavalink.server.info.AppInfo;
import lavalink.server.info.GitRepoState;
import lavalink.server.io.SocketServer;
import lavalink.server.util.SimpleLogToSLF4JAdapter;
import net.dv8tion.jda.utils.SimpleLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.context.annotation.ComponentScan;

import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@SpringBootApplication
@ComponentScan
public class Launcher {

private static final Logger log = LoggerFactory.getLogger(Launcher.class);
Expand Down Expand Up @@ -76,20 +71,6 @@ public static void main(String[] args) {
sa.run(args);
}

public Launcher(SocketServer socketServer) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
log.info("Shutdown hook triggered");
try {
socketServer.stop(30);
} catch (InterruptedException e) {
log.warn("Interrupted while stopping socket server", e);
}
}, "shutdown hook"));

SimpleLog.LEVEL = SimpleLog.Level.OFF;
SimpleLog.addListener(new SimpleLogToSLF4JAdapter());
}

private static String getVersionInfo() {
AppInfo appInfo = new AppInfo();
GitRepoState gitRepoState = new GitRepoState();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package lavalink.server.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import lavalink.server.io.HandshakeInterceptorImpl;
import lavalink.server.io.SocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
* Created by napster on 05.03.18.
*/
@ConfigurationProperties(prefix = "lavalink.server.ws")
@Component
public class WebsocketConfig {
@Configuration
@EnableWebSocket
public class WebsocketConfig implements WebSocketConfigurer {

private int port = 80;
private String host = "0.0.0.0";
private final SocketServer server;
private final HandshakeInterceptorImpl handshakeInterceptor;

public int getPort() {
return port;
@Autowired
public WebsocketConfig(SocketServer server, HandshakeInterceptorImpl handshakeInterceptor) {
this.server = server;
this.handshakeInterceptor = handshakeInterceptor;
}

public void setPort(int port) {
this.port = port;
}

public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(server, "/")
.addInterceptors(handshakeInterceptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public AppInfo() {
this.groupId = prop.getProperty("groupId");
this.artifactId = prop.getProperty("artifactId");
this.buildNumber = prop.getProperty("buildNumber");
this.buildTime = Long.parseLong(prop.getProperty("buildTime"));
long bTime = -1L;
try {
bTime = Long.parseLong(prop.getProperty("buildTime"));
} catch (NumberFormatException ignored) { }
this.buildTime = bTime;
}

public String getVersion() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public GitRepoState() {
this.commitMessageFull = String.valueOf(properties.getOrDefault("git.commit.message.full", ""));
this.commitMessageShort = String.valueOf(properties.getOrDefault("git.commit.message.short", ""));
final String time = String.valueOf(properties.get("git.commit.time"));
if (time == null) {
if (time == null || time.equals("null")) {
this.commitTime = 0;
} else {
// https://github.com/n0mer/gradle-git-properties/issues/71
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package lavalink.server.io;

import lavalink.server.config.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;
import java.util.Objects;

@Controller
public class HandshakeInterceptorImpl implements HandshakeInterceptor {

private static final Logger log = LoggerFactory.getLogger(HandshakeInterceptorImpl.class);
private final ServerConfig serverConfig;

@Autowired
public HandshakeInterceptorImpl(ServerConfig serverConfig) {
this.serverConfig = serverConfig;
}

/**
* Checks credentials and sets the Lavalink version header
*
* @return true if authenticated
*/
@Override
public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) {
response.getHeaders().add("Lavalink-Major-Version", "3");

String password = request.getHeaders().getFirst("Authorization");
boolean matches = Objects.equals(password, serverConfig.getPassword());

if (matches) {
log.info("Incoming connection from " + request.getRemoteAddress());
} else {
log.error("Authentication failed from " + request.getRemoteAddress());
}

return matches;
}

// No action required
@Override
public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
Exception exception) {}
}
Loading

0 comments on commit 2a9285a

Please sign in to comment.