Skip to content
Snippets Groups Projects
Commit a8f8ebc5 authored by Martin Hustoles's avatar Martin Hustoles
Browse files

added a simple key auth

parent 88656ef0
No related branches found
No related tags found
No related merge requests found
Showing
with 39 additions and 9 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -5,13 +5,45 @@ import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.server.RegisteredServer;
import io.javalin.Javalin;
import io.javalin.http.UnauthorizedResponse;
import org.fsi.pixelcampusproxy.PixelcampusProxy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.ExecutionException;
public class PixelcampusProxyApiService {
public static String getOnlinePlayers(){
private PixelcampusProxyApiService() {
}
private static Javalin app;
private static final Logger logger = LoggerFactory.getLogger(PixelcampusProxy.class);
public static void init(String AUTH_KEY){
app = Javalin.create();
//checks if the api key is correct. If not, dont register handlers!
app.beforeMatched(ctx -> {
String authHeader = ctx.header("key");
if (authHeader == null || !authHeader.equals(AUTH_KEY)) {
ctx.status(401).result("Unauthorized");
logger.warn(ctx.host() + " tried to access " + ctx.endpointHandlerPath() + " with invalid AUTH key.");
throw new UnauthorizedResponse();
}
});
app.get("/players", ctx -> ctx.json(getOnlinePlayers()));
app.get("/servers", ctx -> ctx.json(getNetworkServers()));
app.start(7070);
}
private static String getOnlinePlayers(){
JsonArray onlinePlayers = new JsonArray();
JsonObject player;
......@@ -33,7 +65,7 @@ public class PixelcampusProxyApiService {
return onlinePlayers.toString();
}
public static String getNetworkServers(){
private static String getNetworkServers(){
JsonArray networkServers = new JsonArray();
JsonObject server;
......
......@@ -10,7 +10,6 @@ import com.velocitypowered.api.event.Subscribe;
import com.velocitypowered.api.event.proxy.ProxyInitializeEvent;
import com.velocitypowered.api.plugin.Plugin;
import com.velocitypowered.api.proxy.ProxyServer;
import io.javalin.Javalin;
import org.slf4j.Logger;
@Plugin(
......@@ -34,15 +33,14 @@ public class PixelcampusProxy {
@Inject
private Logger logger;
private static String AUTH_KEY = "=dZq%!i,V05-}QS{}{~wQ5<=+h9:_>8)G$t&W[9(";
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
//Proxy API
Javalin.create(/*config*/)
.get("/players", ctx -> ctx.json(PixelcampusProxyApiService.getOnlinePlayers()))
.get("/servers", ctx -> ctx.json(PixelcampusProxyApiService.getNetworkServers()))
.start(7070);
//API to fetch server and Player Information
PixelcampusProxyApiService.init(AUTH_KEY);
//hub command
CommandManager commandManager = proxy.getCommandManager();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment