Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions api/src/main/java/brainwine/api/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static brainwine.shared.LogMarkers.SERVER_MARKER;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import brainwine.api.config.BetaEntry;
Expand All @@ -11,13 +13,16 @@

import brainwine.api.config.ApiConfig;
import brainwine.api.config.NewsEntry;
import brainwine.api.config.SslConfig;
import brainwine.shared.JsonHelper;
import io.javalin.core.LoomUtil;

public class Api {

private static final Logger logger = LogManager.getLogger();
private final ApiConfig config;
private final List<NewsEntry> news;
private final BetaEntry beta;
private final DataFetcher dataFetcher;
private final GatewayService gatewayService;
private final PortalService portalService;
Expand All @@ -33,6 +38,10 @@ public Api(DataFetcher dataFetcher) {
logger.info(SERVER_MARKER, "Using data fetcher {}", dataFetcher.getClass().getName());
logger.info(SERVER_MARKER, "Loading configuration ...");
config = loadConfig();
logger.info(SERVER_MARKER, "Is SSL enabled? {}", config.getSslConfig().isSslEnabled() ? "Yes" : "No");
news = new ArrayList<>(config.getNews()); // Explicit copy
beta = config.getBeta();
Collections.reverse(news);
LoomUtil.useLoomThreadPool = false;
gatewayService = new GatewayService(this, config.getGatewayPort());
portalService = new PortalService(this, config.getPortalPort());
Expand All @@ -55,7 +64,9 @@ private ApiConfig loadConfig() {
return ApiConfig.DEFAULT_CONFIG;
}

return JsonHelper.readValue(file, ApiConfig.class);
ApiConfig config = JsonHelper.readValue(file, ApiConfig.class);
JsonHelper.writeValue(file, config);
return config;
} catch (Exception e) {
logger.fatal(SERVER_MARKER, "Failed to load configuration", e);
System.exit(-1);
Expand All @@ -69,17 +80,21 @@ public void broadcast(String type, Object data) {
}

public List<NewsEntry> getNews() {
return config.getNews();
return news;
}

public BetaEntry getBeta() {
return config.getBeta();
return beta;
}

public String getGameServerHost() {
return config.getGameServerIp() + ":" + config.getGameServerPort();
}

public SslConfig getSslConfig() {
return config.getSslConfig();
}

public DataFetcher getDataFetcher() {
return dataFetcher;
}
Expand Down
13 changes: 11 additions & 2 deletions api/src/main/java/brainwine/api/GatewayService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import brainwine.api.config.SslConfig;
import brainwine.api.models.PlayersRequest;
import brainwine.api.models.ServerConnectInfo;
import brainwine.api.models.SessionsRequest;
import brainwine.api.util.JettyUtils;
import brainwine.shared.JsonHelper;
import io.javalin.Javalin;
import io.javalin.http.Context;
Expand All @@ -35,8 +37,15 @@ public class GatewayService {
public GatewayService(Api api, int port) {
this.api = api;
this.dataFetcher = api.getDataFetcher();
logger.info(SERVER_MARKER, "Starting GatewayService @ port {} ...", port);
gateway = Javalin.create(config -> config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER)))
logger.info(SERVER_MARKER, "Starting GatewayService @ port {} ...", port);
SslConfig ssl = api.getSslConfig();
gateway = Javalin.create(config -> {
config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER));

if(ssl.isSslEnabled()) {
config.server(() -> JettyUtils.createJettyServerWithSsl(port, ssl.getKeyStorePath(), ssl.getKeyStorePassword()));
}
})
.exception(Exception.class, this::handleException)
.get("/clients", this::handleNewsRequest)
.get("/players", this::handlePlayerSearch)
Expand Down
11 changes: 10 additions & 1 deletion api/src/main/java/brainwine/api/PortalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import brainwine.api.config.SslConfig;
import brainwine.api.models.ZoneInfo;
import brainwine.api.util.ImageUtils;
import brainwine.api.util.JettyUtils;
import brainwine.shared.JsonHelper;
import io.javalin.Javalin;
import io.javalin.http.ContentType;
Expand All @@ -45,7 +47,14 @@ public class PortalService {
public PortalService(Api api, int port) {
this.dataFetcher = api.getDataFetcher();
logger.info(SERVER_MARKER, "Starting PortalService @ port {} ...", port);
portal = Javalin.create(config -> config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER)))
SslConfig ssl = api.getSslConfig();
portal = Javalin.create(config -> {
config.jsonMapper(new JavalinJackson(JsonHelper.MAPPER));

if(ssl.isSslEnabled()) {
config.server(() -> JettyUtils.createJettyServerWithSsl(port, ssl.getKeyStorePath(), ssl.getKeyStorePassword()));
}
})
.exception(Exception.class, this::handleException)
.get("/v1/map/{zone}", this::handleMapRequest)
.get("/v1/worlds", this::handleZoneSearch)
Expand Down
17 changes: 13 additions & 4 deletions api/src/main/java/brainwine/api/config/ApiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@
import java.util.Collections;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class ApiConfig {

public static final ApiConfig DEFAULT_CONFIG = new ApiConfig("127.0.0.1", 5002, 5001, 5003, Arrays.asList(NewsEntry.DEFAULT_NEWS), new BetaEntry());
public static final ApiConfig DEFAULT_CONFIG = new ApiConfig("127.0.0.1", 5002, 5001, 5003, SslConfig.DEFAULT_CONFIG, Arrays.asList(NewsEntry.DEFAULT_NEWS), new BetaEntry());
private final String gameServerIp;
private final int gameServerPort;
private final int gatewayPort;
private final int portalPort;
private final SslConfig sslConfig;
private final List<NewsEntry> news;
private final BetaEntry beta;
@ConstructorProperties({"game_server_ip", "game_server_port", "gateway_port", "portal_port", "news", "beta"})
public ApiConfig(String gameServerIp, int gameServerPort, int gatewayPort, int portalPort, List<NewsEntry> news, BetaEntry beta) {

@ConstructorProperties({"game_server_ip", "game_server_port", "gateway_port", "portal_port", "ssl", "news", "beta"})
public ApiConfig(String gameServerIp, int gameServerPort, int gatewayPort, int portalPort, SslConfig sslConfig, List<NewsEntry> news, BetaEntry beta) {
this.gameServerIp = gameServerIp;
this.gameServerPort = gameServerPort;
this.gatewayPort = gatewayPort;
this.portalPort = portalPort;
this.sslConfig = sslConfig == null ? SslConfig.DEFAULT_CONFIG : sslConfig;
System.out.println("ssl config: " + sslConfig);
this.news = news;
this.beta = beta;
Collections.reverse(this.news);
Expand All @@ -45,6 +49,11 @@ public int getPortalPort() {
return portalPort;
}

@JsonGetter("ssl")
public SslConfig getSslConfig() {
return sslConfig;
}

public List<NewsEntry> getNews() {
return news;
}
Expand Down
37 changes: 37 additions & 0 deletions api/src/main/java/brainwine/api/config/SslConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package brainwine.api.config;

import java.beans.ConstructorProperties;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class SslConfig {

public static final SslConfig DEFAULT_CONFIG = new SslConfig(false, "./keystore", "password");
private final boolean enableSsl;
private final String keyStorePath;
private final String keyStorePassword;

@ConstructorProperties({"enable_ssl", "keystore_path", "keystore_password"})
public SslConfig(boolean enableSsl, String keyStorePath, String keyStorePassword) {
this.enableSsl = enableSsl;
this.keyStorePath = keyStorePath;
this.keyStorePassword = keyStorePassword;
}

@JsonGetter("enable_ssl")
public boolean isSslEnabled() {
return enableSsl;
}

@JsonGetter("keystore_path")
public String getKeyStorePath() {
return keyStorePath;
}

@JsonGetter("keystore_password")
public String getKeyStorePassword() {
return keyStorePassword;
}
}
23 changes: 23 additions & 0 deletions api/src/main/java/brainwine/api/util/JettyUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package brainwine.api.util;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.util.ssl.SslContextFactory;

public class JettyUtils {

public static Server createJettyServerWithSsl(int port, String keyStorePath, String keyStorePassword) {
Server server = new Server();
ServerConnector connector = new ServerConnector(server, createSslContextFactory(keyStorePath, keyStorePassword));
connector.setPort(port);
server.addConnector(connector);
return server;
}

public static SslContextFactory.Server createSslContextFactory(String keyStorePath, String keyStorePassword) {
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keyStorePath);
sslContextFactory.setKeyStorePassword(keyStorePassword);
return sslContextFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@
import com.fasterxml.jackson.annotation.JsonProperty;

public class AnticheatConfig {
@JsonProperty("exploder_farm")
private ExploderFarm exploderFarm = new ExploderFarm();

@JsonProperty("afk_entity_spawn")
private AfkEntitySpawn afkEntitySpawn = new AfkEntitySpawn();

@JsonProperty("exoskeleton")
private Exoskeleton exoskeleton = new Exoskeleton();

@JsonProperty("exploder_farm")
private ExploderFarm exploderFarm = new ExploderFarm();

@JsonProperty("exploration")
private Exploration exploration = new Exploration();

public ExploderFarm getExploderFarm() {
return exploderFarm;
}

public AfkEntitySpawn getAfkEntitySpawn() {
return afkEntitySpawn;
}

public Exoskeleton getExoskeleton() {
return exoskeleton;
}

public ExploderFarm getExploderFarm() {
return exploderFarm;
}

public Exploration getExploration() {
return exploration;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package brainwine.gameserver.anticheat;

import brainwine.gameserver.item.InventoryType;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Exoskeleton {
@JsonProperty
public InventoryType inventoryType = InventoryType.ACCESSORY;

public InventoryType getInventoryType() {
return inventoryType;
}
}
Loading