mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-09-06 03:38:06 +02:00
WebServer initialized by Dagger
This commit is contained in:
parent
81a6ed7365
commit
20b651f909
@ -22,7 +22,10 @@ package com.djrapitops.plan;
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.command.PlanCommand;
|
||||
import com.djrapitops.plan.modules.APFModule;
|
||||
import com.djrapitops.plan.modules.common.*;
|
||||
import com.djrapitops.plan.modules.common.ExportModule;
|
||||
import com.djrapitops.plan.modules.common.LocaleModule;
|
||||
import com.djrapitops.plan.modules.common.PluginHookModule;
|
||||
import com.djrapitops.plan.modules.common.VersionCheckModule;
|
||||
import com.djrapitops.plan.modules.server.ServerAPIModule;
|
||||
import com.djrapitops.plan.modules.server.ServerDataCacheModule;
|
||||
import com.djrapitops.plan.modules.server.ServerInfoSystemModule;
|
||||
@ -64,7 +67,6 @@ import java.util.logging.Logger;
|
||||
BukkitInfoModule.class,
|
||||
BukkitDatabaseModule.class,
|
||||
ServerDataCacheModule.class,
|
||||
WebServerSystemModule.class,
|
||||
ServerInfoSystemModule.class,
|
||||
PluginHookModule.class,
|
||||
ServerAPIModule.class
|
||||
|
@ -10,7 +10,11 @@ import com.djrapitops.plan.modules.APFModule;
|
||||
import com.djrapitops.plan.modules.bungee.BungeeAPIModule;
|
||||
import com.djrapitops.plan.modules.bungee.BungeeConfigModule;
|
||||
import com.djrapitops.plan.modules.bungee.BungeeDatabaseModule;
|
||||
import com.djrapitops.plan.modules.common.*;
|
||||
import com.djrapitops.plan.modules.bungee.BungeeInfoModule;
|
||||
import com.djrapitops.plan.modules.common.ExportModule;
|
||||
import com.djrapitops.plan.modules.common.LocaleModule;
|
||||
import com.djrapitops.plan.modules.common.PluginHookModule;
|
||||
import com.djrapitops.plan.modules.common.VersionCheckModule;
|
||||
import com.djrapitops.plan.modules.server.ServerInfoSystemModule;
|
||||
import com.djrapitops.plan.system.BungeeSystem;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
@ -41,8 +45,8 @@ import java.io.InputStream;
|
||||
VersionCheckModule.class,
|
||||
BungeeConfigModule.class,
|
||||
LocaleModule.class,
|
||||
BungeeInfoModule.class,
|
||||
BungeeDatabaseModule.class,
|
||||
WebServerSystemModule.class,
|
||||
ServerInfoSystemModule.class,
|
||||
PluginHookModule.class,
|
||||
BungeeAPIModule.class
|
||||
|
@ -3,7 +3,10 @@ package com.djrapitops.plan;
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.command.PlanCommand;
|
||||
import com.djrapitops.plan.modules.APFModule;
|
||||
import com.djrapitops.plan.modules.common.*;
|
||||
import com.djrapitops.plan.modules.common.ExportModule;
|
||||
import com.djrapitops.plan.modules.common.LocaleModule;
|
||||
import com.djrapitops.plan.modules.common.PluginHookModule;
|
||||
import com.djrapitops.plan.modules.common.VersionCheckModule;
|
||||
import com.djrapitops.plan.modules.server.ServerAPIModule;
|
||||
import com.djrapitops.plan.modules.server.ServerDataCacheModule;
|
||||
import com.djrapitops.plan.modules.server.ServerInfoSystemModule;
|
||||
@ -51,7 +54,6 @@ import java.io.InputStream;
|
||||
LocaleModule.class,
|
||||
SpongeDatabaseModule.class,
|
||||
ServerDataCacheModule.class,
|
||||
WebServerSystemModule.class,
|
||||
ServerInfoSystemModule.class,
|
||||
SpongeInfoModule.class,
|
||||
PluginHookModule.class,
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.djrapitops.plan.modules.bungee;
|
||||
|
||||
import com.djrapitops.plan.PlanBungee;
|
||||
import com.djrapitops.plan.system.info.server.properties.BungeeServerProperties;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
/**
|
||||
* Dagger module for Bukkit ServerInfo.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Module
|
||||
public class BungeeInfoModule {
|
||||
|
||||
@Provides
|
||||
ServerProperties provideServerProperties(PlanBungee plugin) {
|
||||
return new BungeeServerProperties(plugin.getProxy());
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.djrapitops.plan.modules.common;
|
||||
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.webserver.WebServer;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
import dagger.Module;
|
||||
import dagger.Provides;
|
||||
|
||||
/**
|
||||
* Dagger module for WebServerSystem.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Module
|
||||
public class WebServerSystemModule {
|
||||
|
||||
@Provides
|
||||
WebServerSystem provideWebServerSystem(Locale locale) {
|
||||
return new WebServerSystem(() -> locale);
|
||||
}
|
||||
|
||||
@Provides
|
||||
WebServer provideWebServer(WebServerSystem webServerSystem) {
|
||||
return webServerSystem.getWebServer();
|
||||
}
|
||||
|
||||
}
|
@ -19,6 +19,7 @@ import com.djrapitops.plan.system.settings.config.ConfigSystem;
|
||||
import com.djrapitops.plan.system.settings.network.NetworkSettings;
|
||||
import com.djrapitops.plan.system.tasks.BukkitTaskSystem;
|
||||
import com.djrapitops.plan.system.update.VersionCheckSystem;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ -36,6 +37,7 @@ public class BukkitSystem extends PlanSystem implements ServerSystem {
|
||||
InfoSystem serverInfoSystem,
|
||||
BukkitServerInfo serverInfo,
|
||||
DBSystem databaseSystem,
|
||||
WebServerSystem webServerSystem,
|
||||
BukkitListenerSystem bukkitListenerSystem,
|
||||
BukkitTaskSystem bukkitTaskSystem,
|
||||
ExportSystem exportSystem,
|
||||
@ -43,6 +45,7 @@ public class BukkitSystem extends PlanSystem implements ServerSystem {
|
||||
PlanAPI planAPI,
|
||||
ShutdownHook shutdownHook
|
||||
) {
|
||||
super(webServerSystem);
|
||||
this.versionCheckSystem = versionCheckSystem;
|
||||
this.fileSystem = fileSystem;
|
||||
this.configSystem = serverConfigSystem;
|
||||
|
@ -19,6 +19,7 @@ import com.djrapitops.plan.system.settings.config.BungeeConfigSystem;
|
||||
import com.djrapitops.plan.system.settings.network.NetworkSettings;
|
||||
import com.djrapitops.plan.system.tasks.BungeeTaskSystem;
|
||||
import com.djrapitops.plan.system.update.VersionCheckSystem;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ -34,12 +35,15 @@ public class BungeeSystem extends PlanSystem {
|
||||
VersionCheckSystem versionCheckSystem,
|
||||
FileSystem fileSystem,
|
||||
BungeeConfigSystem bungeeConfigSystem,
|
||||
BungeeServerInfo bungeeServerInfo,
|
||||
BungeeCacheSystem bungeeCacheSystem,
|
||||
DBSystem databaseSystem,
|
||||
WebServerSystem webServerSystem,
|
||||
HookHandler hookHandler,
|
||||
ExportSystem exportSystem,
|
||||
PlanAPI planAPI
|
||||
) {
|
||||
super(webServerSystem);
|
||||
setTestSystem(this);
|
||||
|
||||
this.versionCheckSystem = versionCheckSystem;
|
||||
@ -52,7 +56,7 @@ public class BungeeSystem extends PlanSystem {
|
||||
taskSystem = new BungeeTaskSystem(plugin.getRunnableFactory());
|
||||
|
||||
infoSystem = new BungeeInfoSystem();
|
||||
serverInfo = new BungeeServerInfo(plugin);
|
||||
serverInfo = bungeeServerInfo;
|
||||
|
||||
this.hookHandler = hookHandler;
|
||||
this.planAPI = planAPI;
|
||||
|
@ -61,11 +61,11 @@ public abstract class PlanSystem implements SubSystem {
|
||||
// Not a SubSystem.
|
||||
protected PlanAPI planAPI;
|
||||
|
||||
public PlanSystem() {
|
||||
public PlanSystem(WebServerSystem webServerSystem) {
|
||||
this.webServerSystem = webServerSystem;
|
||||
Supplier<Locale> localeSupplier = () -> getLocaleSystem().getLocale();
|
||||
|
||||
processing = new Processing(localeSupplier);
|
||||
webServerSystem = new WebServerSystem(localeSupplier);
|
||||
localeSystem = new LocaleSystem();
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@ import com.djrapitops.plan.system.settings.config.ConfigSystem;
|
||||
import com.djrapitops.plan.system.settings.network.NetworkSettings;
|
||||
import com.djrapitops.plan.system.tasks.SpongeTaskSystem;
|
||||
import com.djrapitops.plan.system.update.VersionCheckSystem;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
@ -36,14 +37,16 @@ public class SpongeSystem extends PlanSystem implements ServerSystem {
|
||||
ConfigSystem serverConfigSystem,
|
||||
InfoSystem serverInfoSystem,
|
||||
SpongeServerInfo serverInfo,
|
||||
DBSystem databaseSystem,
|
||||
WebServerSystem webServerSystem,
|
||||
SpongeListenerSystem spongeListenerSystem,
|
||||
SpongeTaskSystem spongeTaskSystem,
|
||||
DBSystem databaseSystem,
|
||||
ExportSystem exportSystem,
|
||||
HookHandler hookHandler,
|
||||
PlanAPI planAPI,
|
||||
ShutdownHook shutdownHook
|
||||
) {
|
||||
super(webServerSystem);
|
||||
setTestSystem(this);
|
||||
|
||||
this.versionCheckSystem = versionCheckSystem;
|
||||
|
@ -10,7 +10,7 @@ import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
import com.djrapitops.plan.system.webserver.WebServer;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
@ -26,15 +26,23 @@ import java.util.UUID;
|
||||
*/
|
||||
public class BukkitServerInfo extends ServerInfo {
|
||||
|
||||
private final WebServer webServer;
|
||||
private final PlanConfig config;
|
||||
private ServerInfoFile serverInfoFile;
|
||||
private Database database;
|
||||
|
||||
@Inject
|
||||
public BukkitServerInfo(ServerProperties serverProperties, ServerInfoFile serverInfoFile, Database database, PlanConfig config) {
|
||||
public BukkitServerInfo(
|
||||
ServerProperties serverProperties,
|
||||
ServerInfoFile serverInfoFile,
|
||||
Database database,
|
||||
WebServer webServer,
|
||||
PlanConfig config
|
||||
) {
|
||||
super(serverProperties);
|
||||
this.serverInfoFile = serverInfoFile;
|
||||
this.database = database;
|
||||
this.webServer = webServer;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@ -67,7 +75,7 @@ public class BukkitServerInfo extends ServerInfo {
|
||||
return registerServer(serverUUID);
|
||||
}
|
||||
String name = config.getString(Settings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
|
||||
String webAddress = WebServerSystem.getInstance().getWebServer().getAccessAddress();
|
||||
String webAddress = webServer.getAccessAddress();
|
||||
if ("plan".equalsIgnoreCase(name)) {
|
||||
name = "Server " + serverID.get();
|
||||
}
|
||||
@ -83,7 +91,7 @@ public class BukkitServerInfo extends ServerInfo {
|
||||
}
|
||||
|
||||
private Server registerServer(UUID serverUUID) throws IOException {
|
||||
String webAddress = WebServerSystem.getInstance().getWebServer().getAccessAddress();
|
||||
String webAddress = webServer.getAccessAddress();
|
||||
String name = config.getString(Settings.SERVER_NAME).replaceAll("[^a-zA-Z0-9_\\s]", "_");
|
||||
int maxPlayers = serverProperties.getMaxPlayers();
|
||||
|
||||
@ -103,7 +111,12 @@ public class BukkitServerInfo extends ServerInfo {
|
||||
}
|
||||
|
||||
private UUID generateNewUUID() {
|
||||
String seed = serverProperties.getServerId() + serverProperties.getName() + serverProperties.getIp() + serverProperties.getPort() + serverProperties.getVersion() + serverProperties.getImplVersion();
|
||||
String seed = serverProperties.getServerId() +
|
||||
serverProperties.getName() +
|
||||
serverProperties.getIp() +
|
||||
serverProperties.getPort() +
|
||||
serverProperties.getVersion() +
|
||||
serverProperties.getImplVersion();
|
||||
return UUID.nameUUIDFromBytes(seed.getBytes());
|
||||
}
|
||||
}
|
@ -4,15 +4,14 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.info.server;
|
||||
|
||||
import com.djrapitops.plan.PlanBungee;
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.api.exceptions.database.DBOpException;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.server.properties.BungeeServerProperties;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.system.webserver.WebServerSystem;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plan.system.webserver.WebServer;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ -23,8 +22,21 @@ import java.util.UUID;
|
||||
*/
|
||||
public class BungeeServerInfo extends ServerInfo {
|
||||
|
||||
public BungeeServerInfo(PlanBungee plugin) {
|
||||
super(new BungeeServerProperties(plugin.getProxy()));
|
||||
private final Database database;
|
||||
private final WebServer webServer;
|
||||
private final PluginLogger logger;
|
||||
|
||||
@Inject
|
||||
public BungeeServerInfo(
|
||||
ServerProperties serverProperties,
|
||||
Database database,
|
||||
WebServer webServer,
|
||||
PluginLogger logger
|
||||
) {
|
||||
super(serverProperties);
|
||||
this.database = database;
|
||||
this.webServer = webServer;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -32,13 +44,12 @@ public class BungeeServerInfo extends ServerInfo {
|
||||
checkIfDefaultIP();
|
||||
|
||||
try {
|
||||
Database db = Database.getActive();
|
||||
Optional<Server> bungeeInfo = db.fetch().getBungeeInformation();
|
||||
Optional<Server> bungeeInfo = database.fetch().getBungeeInformation();
|
||||
if (bungeeInfo.isPresent()) {
|
||||
server = bungeeInfo.get();
|
||||
updateServerInfo(db);
|
||||
updateServerInfo(database);
|
||||
} else {
|
||||
server = registerBungeeInfo(db);
|
||||
server = registerBungeeInfo(database);
|
||||
}
|
||||
} catch (DBOpException e) {
|
||||
throw new EnableException("Failed to read Server information from Database.");
|
||||
@ -47,7 +58,7 @@ public class BungeeServerInfo extends ServerInfo {
|
||||
}
|
||||
|
||||
private void updateServerInfo(Database db) {
|
||||
String accessAddress = WebServerSystem.getInstance().getWebServer().getAccessAddress();
|
||||
String accessAddress = webServer.getAccessAddress();
|
||||
if (!accessAddress.equals(server.getWebAddress())) {
|
||||
server.setWebAddress(accessAddress);
|
||||
db.save().serverInfoForThisServer(server);
|
||||
@ -55,20 +66,19 @@ public class BungeeServerInfo extends ServerInfo {
|
||||
}
|
||||
|
||||
private void checkIfDefaultIP() throws EnableException {
|
||||
String ip = ServerInfo.getServerProperties_Old().getIp();
|
||||
String ip = serverProperties.getIp();
|
||||
if ("0.0.0.0".equals(ip)) {
|
||||
Log.error("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server.");
|
||||
Log.info("Player Analytics partially enabled (Use /planbungee to reload config)");
|
||||
logger.error("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server.");
|
||||
logger.info("Player Analytics partially enabled (Use /planbungee to reload config)");
|
||||
throw new EnableException("IP setting still 0.0.0.0 - Configure AlternativeIP/IP that connects to the Proxy server.");
|
||||
}
|
||||
}
|
||||
|
||||
private Server registerBungeeInfo(Database db) throws EnableException {
|
||||
ServerProperties properties = ServerInfo.getServerProperties_Old();
|
||||
UUID serverUUID = generateNewUUID(properties);
|
||||
String accessAddress = WebServerSystem.getInstance().getWebServer().getAccessAddress();
|
||||
UUID serverUUID = generateNewUUID();
|
||||
String accessAddress = webServer.getAccessAddress();
|
||||
|
||||
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, properties.getMaxPlayers());
|
||||
Server bungeeCord = new Server(-1, serverUUID, "BungeeCord", accessAddress, serverProperties.getMaxPlayers());
|
||||
db.save().serverInfoForThisServer(bungeeCord);
|
||||
|
||||
Optional<Server> bungeeInfo = db.fetch().getBungeeInformation();
|
||||
@ -78,8 +88,12 @@ public class BungeeServerInfo extends ServerInfo {
|
||||
throw new EnableException("BungeeCord registration failed (DB)");
|
||||
}
|
||||
|
||||
private UUID generateNewUUID(ServerProperties properties) {
|
||||
String seed = properties.getName() + properties.getIp() + properties.getPort() + properties.getVersion() + properties.getImplVersion();
|
||||
private UUID generateNewUUID() {
|
||||
String seed = serverProperties.getName() +
|
||||
serverProperties.getIp() +
|
||||
serverProperties.getPort() +
|
||||
serverProperties.getVersion() +
|
||||
serverProperties.getImplVersion();
|
||||
return UUID.nameUUIDFromBytes(seed.getBytes());
|
||||
}
|
||||
}
|
||||
|
@ -3,13 +3,20 @@ package com.djrapitops.plan.system.info.server;
|
||||
import com.djrapitops.plan.system.database.databases.Database;
|
||||
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.system.webserver.WebServer;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class SpongeServerInfo extends BukkitServerInfo {
|
||||
|
||||
@Inject
|
||||
public SpongeServerInfo(ServerProperties serverProperties, ServerInfoFile serverInfoFile, Database database, PlanConfig config) {
|
||||
super(serverProperties, serverInfoFile, database, config);
|
||||
public SpongeServerInfo(
|
||||
ServerProperties serverProperties,
|
||||
ServerInfoFile serverInfoFile,
|
||||
Database database,
|
||||
WebServer webServer,
|
||||
PlanConfig config
|
||||
) {
|
||||
super(serverProperties, serverInfoFile, database, webServer, config);
|
||||
}
|
||||
}
|
||||
|
@ -17,29 +17,32 @@ import com.sun.net.httpserver.Headers;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* HttpHandler for WebServer request management.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class RequestHandler implements HttpHandler {
|
||||
|
||||
private final Supplier<Locale> locale;
|
||||
private final Locale locale;
|
||||
private final ResponseHandler responseHandler;
|
||||
|
||||
RequestHandler(WebServer webServer) {
|
||||
responseHandler = new ResponseHandler(webServer);
|
||||
locale = webServer.getLocaleSupplier();
|
||||
@Inject
|
||||
RequestHandler(Locale locale, ResponseHandler responseHandler) {
|
||||
this.locale = locale;
|
||||
this.responseHandler = responseHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) {
|
||||
Headers requestHeaders = exchange.getRequestHeaders();
|
||||
Headers responseHeaders = exchange.getResponseHeaders();
|
||||
Request request = new Request(exchange, locale.get());
|
||||
Request request = new Request(exchange, locale);
|
||||
request.setAuth(getAuthorization(requestHeaders));
|
||||
|
||||
String requestString = request.toString();
|
||||
@ -53,7 +56,7 @@ public class RequestHandler implements HttpHandler {
|
||||
}
|
||||
|
||||
response.setResponseHeaders(responseHeaders);
|
||||
response.send(exchange, locale.get());
|
||||
response.send(exchange, locale);
|
||||
} catch (Exception e) {
|
||||
if (Settings.DEV_MODE.isTrue()) {
|
||||
Log.warn("THIS ERROR IS ONLY LOGGED IN DEV MODE:");
|
||||
|
@ -14,8 +14,11 @@ import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plan.system.webserver.pages.*;
|
||||
import com.djrapitops.plan.system.webserver.response.*;
|
||||
import com.djrapitops.plan.system.webserver.response.errors.*;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -26,15 +29,22 @@ import java.util.Optional;
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class ResponseHandler extends TreePageHandler {
|
||||
|
||||
private final WebServer webServer;
|
||||
private final ErrorHandler errorHandler;
|
||||
|
||||
public ResponseHandler(WebServer webServer) {
|
||||
this.webServer = webServer;
|
||||
private WebServer webServer;
|
||||
|
||||
@Inject
|
||||
public ResponseHandler(ErrorHandler errorHandler) {
|
||||
this.errorHandler = errorHandler;
|
||||
|
||||
registerWebAPIPages();
|
||||
registerDefaultPages();
|
||||
}
|
||||
|
||||
public void registerDefaultPages() {
|
||||
private void registerDefaultPages() {
|
||||
registerPage("favicon.ico", new RedirectResponse("https://puu.sh/tK0KL/6aa2ba141b.ico"), 5);
|
||||
registerPage("debug", new DebugPageHandler());
|
||||
registerPage("players", new PlayersPageHandler());
|
||||
@ -44,7 +54,7 @@ public class ResponseHandler extends TreePageHandler {
|
||||
registerPage("network", serverPageHandler);
|
||||
registerPage("server", serverPageHandler);
|
||||
registerPage("", webServer.isAuthRequired()
|
||||
? new RootPageHandler(this)
|
||||
? new RootPageHandler()
|
||||
: new PageHandler() {
|
||||
@Override
|
||||
public Response getResponse(Request request, List<String> target) {
|
||||
@ -53,7 +63,7 @@ public class ResponseHandler extends TreePageHandler {
|
||||
});
|
||||
}
|
||||
|
||||
public void registerWebAPIPages() {
|
||||
private void registerWebAPIPages() {
|
||||
registerPage("info", new InfoRequestPageHandler());
|
||||
}
|
||||
|
||||
@ -84,7 +94,7 @@ public class ResponseHandler extends TreePageHandler {
|
||||
return new InternalErrorResponse(request.getTarget(), e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.toLog(this.getClass(), e);
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
return new InternalErrorResponse(request.getTarget(), e);
|
||||
}
|
||||
}
|
||||
@ -106,7 +116,7 @@ public class ResponseHandler extends TreePageHandler {
|
||||
if (webServer.isUsingHTTPS()) {
|
||||
return DefaultResponses.BASIC_AUTH.get();
|
||||
} else {
|
||||
return forbiddenResponse();
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -118,13 +128,11 @@ public class ResponseHandler extends TreePageHandler {
|
||||
if (!isAuthRequired || isAuthorized) {
|
||||
return pageHandler.getResponse(request, target);
|
||||
}
|
||||
return forbiddenResponse();
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
}
|
||||
}
|
||||
|
||||
public Response forbiddenResponse() {
|
||||
return ResponseCache.loadResponse(PageId.FORBIDDEN.id(), () ->
|
||||
new ForbiddenResponse("Your user is not authorized to view this page.<br>"
|
||||
+ "If you believe this is an error contact staff to change your access level."));
|
||||
public void setWebServer(WebServer webServer) {
|
||||
this.webServer = webServer;
|
||||
}
|
||||
}
|
||||
|
@ -1,22 +1,25 @@
|
||||
package com.djrapitops.plan.system.webserver;
|
||||
|
||||
import com.djrapitops.plan.PlanPlugin;
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.system.SubSystem;
|
||||
import com.djrapitops.plan.system.file.FileSystem;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.locale.lang.PluginLang;
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plan.system.settings.config.PlanConfig;
|
||||
import com.djrapitops.plan.utilities.html.HtmlUtils;
|
||||
import com.djrapitops.plugin.StaticHolder;
|
||||
import com.djrapitops.plugin.api.Check;
|
||||
import com.djrapitops.plugin.api.utility.log.Log;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
import com.djrapitops.plugin.logging.console.PluginLogger;
|
||||
import com.djrapitops.plugin.logging.error.ErrorHandler;
|
||||
import com.djrapitops.plugin.utilities.Verify;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import com.sun.net.httpserver.HttpsConfigurator;
|
||||
import com.sun.net.httpserver.HttpsParameters;
|
||||
import com.sun.net.httpserver.HttpsServer;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import javax.net.ssl.*;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -30,14 +33,21 @@ import java.security.cert.CertificateException;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class WebServer implements SubSystem {
|
||||
|
||||
private final Supplier<Locale> locale;
|
||||
private final Locale locale;
|
||||
private final FileSystem fileSystem;
|
||||
private final PlanConfig config;
|
||||
|
||||
private final RequestHandler requestHandler;
|
||||
|
||||
private final PluginLogger logger;
|
||||
private final ErrorHandler errorHandler;
|
||||
|
||||
private int port;
|
||||
private boolean enabled = false;
|
||||
@ -45,13 +55,27 @@ public class WebServer implements SubSystem {
|
||||
|
||||
private boolean usingHttps = false;
|
||||
|
||||
private RequestHandler requestHandler;
|
||||
private ResponseHandler responseHandler;
|
||||
|
||||
public WebServer(Supplier<Locale> locale) {
|
||||
@Inject
|
||||
public WebServer(
|
||||
Locale locale,
|
||||
FileSystem fileSystem,
|
||||
PlanConfig config,
|
||||
PluginLogger logger,
|
||||
ErrorHandler errorHandler,
|
||||
RequestHandler requestHandler
|
||||
) {
|
||||
this.locale = locale;
|
||||
this.fileSystem = fileSystem;
|
||||
this.config = config;
|
||||
|
||||
this.requestHandler = requestHandler;
|
||||
requestHandler.getResponseHandler().setWebServer(this);
|
||||
|
||||
this.logger = logger;
|
||||
this.errorHandler = errorHandler;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static WebServer getInstance() {
|
||||
WebServer webServer = WebServerSystem.getInstance().getWebServer();
|
||||
Verify.nullCheck(webServer, () -> new IllegalStateException("WebServer was not initialized."));
|
||||
@ -60,25 +84,18 @@ public class WebServer implements SubSystem {
|
||||
|
||||
@Override
|
||||
public void enable() throws EnableException {
|
||||
this.port = Settings.WEBSERVER_PORT.getNumber();
|
||||
|
||||
requestHandler = new RequestHandler(this);
|
||||
responseHandler = requestHandler.getResponseHandler();
|
||||
|
||||
PlanPlugin plugin = PlanPlugin.getInstance();
|
||||
StaticHolder.saveInstance(RequestHandler.class, plugin.getClass());
|
||||
StaticHolder.saveInstance(ResponseHandler.class, plugin.getClass());
|
||||
this.port = config.getNumber(Settings.WEBSERVER_PORT);
|
||||
|
||||
initServer();
|
||||
|
||||
if (!isEnabled()) {
|
||||
if (Check.isBungeeAvailable()) {
|
||||
throw new EnableException(locale.get().getString(PluginLang.ENABLE_FAIL_NO_WEB_SERVER_BUNGEE));
|
||||
throw new EnableException(locale.getString(PluginLang.ENABLE_FAIL_NO_WEB_SERVER_BUNGEE));
|
||||
}
|
||||
if (Settings.WEBSERVER_DISABLED.isTrue()) {
|
||||
Log.warn(locale.get().getString(PluginLang.ENABLE_NOTIFY_WEB_SERVER_DISABLED));
|
||||
if (config.isTrue(Settings.WEBSERVER_DISABLED)) {
|
||||
logger.warn(locale.getString(PluginLang.ENABLE_NOTIFY_WEB_SERVER_DISABLED));
|
||||
} else {
|
||||
Log.error(locale.get().getString(PluginLang.WEB_SERVER_FAIL_PORT_BIND, port));
|
||||
logger.error(locale.getString(PluginLang.WEB_SERVER_FAIL_PORT_BIND, port));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -87,24 +104,24 @@ public class WebServer implements SubSystem {
|
||||
* Starts up the WebServer in a new Thread Pool.
|
||||
*/
|
||||
private void initServer() {
|
||||
// Check if Bukkit WebServer has been disabled.
|
||||
if (!Check.isBungeeAvailable() && Settings.WEBSERVER_DISABLED.isTrue()) {
|
||||
if (!Check.isBungeeAvailable() && config.isTrue(Settings.WEBSERVER_DISABLED)) {
|
||||
// Bukkit WebServer has been disabled.
|
||||
return;
|
||||
}
|
||||
|
||||
// Server is already enabled stop code
|
||||
if (enabled) {
|
||||
// Server is already enabled stop code
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
usingHttps = startHttpsServer();
|
||||
|
||||
Log.debug(usingHttps ? "Https Start Successful." : "Https Start Failed.");
|
||||
logger.debug(usingHttps ? "Https Start Successful." : "Https Start Failed.");
|
||||
|
||||
if (!usingHttps) {
|
||||
Log.infoColor("§e" + locale.get().getString(PluginLang.WEB_SERVER_NOTIFY_HTTP_USER_AUTH));
|
||||
server = HttpServer.create(new InetSocketAddress(Settings.WEBSERVER_IP.toString(), port), 10);
|
||||
logger.log(L.INFO_COLOR, "§e" + locale.getString(PluginLang.WEB_SERVER_NOTIFY_HTTP_USER_AUTH));
|
||||
server = HttpServer.create(new InetSocketAddress(config.getString(Settings.WEBSERVER_IP), port), 10);
|
||||
}
|
||||
server.createContext("/", requestHandler);
|
||||
|
||||
@ -113,22 +130,22 @@ public class WebServer implements SubSystem {
|
||||
|
||||
enabled = true;
|
||||
|
||||
Log.info(locale.get().getString(PluginLang.ENABLED_WEB_SERVER, server.getAddress().getPort(), getAccessAddress()));
|
||||
logger.info(locale.getString(PluginLang.ENABLED_WEB_SERVER, server.getAddress().getPort(), getAccessAddress()));
|
||||
} catch (IllegalArgumentException | IllegalStateException | IOException e) {
|
||||
Log.toLog(this.getClass(), e);
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean startHttpsServer() {
|
||||
String keyStorePath = Settings.WEBSERVER_CERTIFICATE_PATH.toString();
|
||||
String keyStorePath = config.getString(Settings.WEBSERVER_CERTIFICATE_PATH);
|
||||
if (!Paths.get(keyStorePath).isAbsolute()) {
|
||||
keyStorePath = FileSystem.getDataFolder_Old() + File.separator + keyStorePath;
|
||||
keyStorePath = fileSystem.getDataFolder() + File.separator + keyStorePath;
|
||||
}
|
||||
|
||||
char[] storepass = Settings.WEBSERVER_CERTIFICATE_STOREPASS.toString().toCharArray();
|
||||
char[] keypass = Settings.WEBSERVER_CERTIFICATE_KEYPASS.toString().toCharArray();
|
||||
String alias = Settings.WEBSERVER_CERTIFICATE_ALIAS.toString();
|
||||
char[] storepass = config.getString(Settings.WEBSERVER_CERTIFICATE_STOREPASS).toCharArray();
|
||||
char[] keypass = config.getString(Settings.WEBSERVER_CERTIFICATE_KEYPASS).toCharArray();
|
||||
String alias = config.getString(Settings.WEBSERVER_CERTIFICATE_ALIAS);
|
||||
|
||||
boolean startSuccessful = false;
|
||||
try (FileInputStream fIn = new FileInputStream(keyStorePath)) {
|
||||
@ -141,7 +158,7 @@ public class WebServer implements SubSystem {
|
||||
throw new IllegalStateException("Certificate with Alias: " + alias + " was not found in the Keystore.");
|
||||
}
|
||||
|
||||
Log.info("Certificate: " + cert.getType());
|
||||
logger.info("Certificate: " + cert.getType());
|
||||
|
||||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
|
||||
keyManagerFactory.init(keystore, keypass);
|
||||
@ -149,7 +166,7 @@ public class WebServer implements SubSystem {
|
||||
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
|
||||
trustManagerFactory.init(keystore);
|
||||
|
||||
server = HttpsServer.create(new InetSocketAddress(Settings.WEBSERVER_IP.toString(), port), 10);
|
||||
server = HttpsServer.create(new InetSocketAddress(config.getString(Settings.WEBSERVER_IP), port), 10);
|
||||
SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
|
||||
sslContext.init(keyManagerFactory.getKeyManagers(), null/*trustManagerFactory.getTrustManagers()*/, null);
|
||||
|
||||
@ -168,20 +185,20 @@ public class WebServer implements SubSystem {
|
||||
});
|
||||
startSuccessful = true;
|
||||
} catch (IllegalStateException e) {
|
||||
Log.error(e.getMessage());
|
||||
Log.toLog(this.getClass(), e);
|
||||
logger.error(e.getMessage());
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
} catch (KeyManagementException | NoSuchAlgorithmException e) {
|
||||
Log.error(locale.get().getString(PluginLang.WEB_SERVER_FAIL_SSL_CONTEXT));
|
||||
Log.toLog(this.getClass(), e);
|
||||
logger.error(locale.getString(PluginLang.WEB_SERVER_FAIL_SSL_CONTEXT));
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
} catch (FileNotFoundException e) {
|
||||
Log.infoColor("§e" + locale.get().getString(PluginLang.WEB_SERVER_NOTIFY_NO_CERT_FILE, keyStorePath));
|
||||
Log.info(locale.get().getString(PluginLang.WEB_SERVER_NOTIFY_HTTP));
|
||||
logger.log(L.INFO_COLOR, "§e" + locale.getString(PluginLang.WEB_SERVER_NOTIFY_NO_CERT_FILE, keyStorePath));
|
||||
logger.info(locale.getString(PluginLang.WEB_SERVER_NOTIFY_HTTP));
|
||||
} catch (IOException e) {
|
||||
Log.error("WebServer: " + e);
|
||||
Log.toLog(this.getClass(), e);
|
||||
logger.error("WebServer: " + e);
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
} catch (KeyStoreException | CertificateException | UnrecoverableKeyException e) {
|
||||
Log.error(locale.get().getString(PluginLang.WEB_SERVER_FAIL_STORE_LOAD));
|
||||
Log.toLog(this.getClass(), e);
|
||||
logger.error(locale.getString(PluginLang.WEB_SERVER_FAIL_STORE_LOAD));
|
||||
errorHandler.log(L.ERROR, this.getClass(), e);
|
||||
}
|
||||
return startSuccessful;
|
||||
}
|
||||
@ -199,7 +216,7 @@ public class WebServer implements SubSystem {
|
||||
@Override
|
||||
public void disable() {
|
||||
if (server != null) {
|
||||
Log.info(locale.get().getString(PluginLang.DISABLED_WEB_SERVER));
|
||||
logger.info(locale.getString(PluginLang.DISABLED_WEB_SERVER));
|
||||
server.stop(0);
|
||||
}
|
||||
enabled = false;
|
||||
@ -218,18 +235,6 @@ public class WebServer implements SubSystem {
|
||||
}
|
||||
|
||||
public String getAccessAddress() {
|
||||
return isEnabled() ? getProtocol() + "://" + HtmlUtils.getIP() : Settings.EXTERNAL_WEBSERVER_LINK.toString();
|
||||
}
|
||||
|
||||
public RequestHandler getRequestHandler() {
|
||||
return requestHandler;
|
||||
}
|
||||
|
||||
public ResponseHandler getResponseHandler() {
|
||||
return responseHandler;
|
||||
}
|
||||
|
||||
Supplier<Locale> getLocaleSupplier() {
|
||||
return locale;
|
||||
return isEnabled() ? getProtocol() + "://" + HtmlUtils.getIP() : config.getString(Settings.EXTERNAL_WEBSERVER_LINK);
|
||||
}
|
||||
}
|
||||
|
@ -7,11 +7,10 @@ package com.djrapitops.plan.system.webserver;
|
||||
import com.djrapitops.plan.api.exceptions.EnableException;
|
||||
import com.djrapitops.plan.system.PlanSystem;
|
||||
import com.djrapitops.plan.system.SubSystem;
|
||||
import com.djrapitops.plan.system.locale.Locale;
|
||||
import com.djrapitops.plan.system.webserver.cache.ResponseCache;
|
||||
import com.djrapitops.plugin.api.Benchmark;
|
||||
import com.djrapitops.plugin.benchmarking.Timings;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* WebServer subsystem for managing WebServer initialization.
|
||||
@ -20,16 +19,21 @@ import java.util.function.Supplier;
|
||||
*/
|
||||
public class WebServerSystem implements SubSystem {
|
||||
|
||||
private WebServer webServer;
|
||||
private final WebServer webServer;
|
||||
private Timings timings;
|
||||
|
||||
public WebServerSystem(Supplier<Locale> locale) {
|
||||
webServer = new WebServer(locale);
|
||||
@Inject
|
||||
public WebServerSystem(WebServer webServer, Timings timings) {
|
||||
this.webServer = webServer;
|
||||
this.timings = timings;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static WebServerSystem getInstance() {
|
||||
return PlanSystem.getInstance().getWebServerSystem();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static boolean isWebServerEnabled() {
|
||||
WebServer webServer = getInstance().webServer;
|
||||
return webServer != null && webServer.isEnabled();
|
||||
@ -37,12 +41,9 @@ public class WebServerSystem implements SubSystem {
|
||||
|
||||
@Override
|
||||
public void enable() throws EnableException {
|
||||
Benchmark.start("WebServer Initialization");
|
||||
timings.start("WebServer Initialization");
|
||||
webServer.enable();
|
||||
ResponseHandler responseHandler = webServer.getResponseHandler();
|
||||
responseHandler.registerWebAPIPages();
|
||||
responseHandler.registerDefaultPages();
|
||||
Benchmark.stop("Enable", "WebServer Initialization");
|
||||
timings.end("WebServer Initialization");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -7,12 +7,11 @@ package com.djrapitops.plan.system.webserver.pages;
|
||||
import com.djrapitops.plan.api.exceptions.connection.WebException;
|
||||
import com.djrapitops.plan.data.WebUser;
|
||||
import com.djrapitops.plan.system.webserver.Request;
|
||||
import com.djrapitops.plan.system.webserver.ResponseHandler;
|
||||
import com.djrapitops.plan.system.webserver.auth.Authentication;
|
||||
import com.djrapitops.plan.system.webserver.response.DefaultResponses;
|
||||
import com.djrapitops.plan.system.webserver.response.RedirectResponse;
|
||||
import com.djrapitops.plan.system.webserver.response.Response;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -25,12 +24,6 @@ import java.util.Optional;
|
||||
*/
|
||||
public class RootPageHandler extends PageHandler {
|
||||
|
||||
private final ResponseHandler responseHandler;
|
||||
|
||||
public RootPageHandler(ResponseHandler responseHandler) {
|
||||
this.responseHandler = responseHandler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response getResponse(Request request, List<String> target) throws WebException {
|
||||
Optional<Authentication> auth = request.getAuth();
|
||||
@ -43,13 +36,13 @@ public class RootPageHandler extends PageHandler {
|
||||
int permLevel = webUser.getPermLevel();
|
||||
switch (permLevel) {
|
||||
case 0:
|
||||
return responseHandler.getPageHandler("server").getResponse(request, Collections.emptyList());
|
||||
return new RedirectResponse("/server");
|
||||
case 1:
|
||||
return responseHandler.getPageHandler("players").getResponse(request, Collections.emptyList());
|
||||
return new RedirectResponse("/players");
|
||||
case 2:
|
||||
return responseHandler.getPageHandler("player").getResponse(request, Collections.singletonList(webUser.getName()));
|
||||
return new RedirectResponse("/player/" + webUser.getName());
|
||||
default:
|
||||
return responseHandler.forbiddenResponse();
|
||||
return DefaultResponses.FORBIDDEN.get();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,8 @@
|
||||
*/
|
||||
package com.djrapitops.plan.system.webserver.response;
|
||||
|
||||
import com.djrapitops.plan.system.webserver.response.errors.ForbiddenResponse;
|
||||
|
||||
/**
|
||||
* Enum containing default responses that don't need to be cached because they're always the same.
|
||||
*
|
||||
@ -11,7 +13,9 @@ package com.djrapitops.plan.system.webserver.response;
|
||||
*/
|
||||
public enum DefaultResponses {
|
||||
BASIC_AUTH(PromptAuthorizationResponse.getBasicAuthResponse()),
|
||||
SUCCESS(new TextResponse("Success"));
|
||||
SUCCESS(new TextResponse("Success")),
|
||||
FORBIDDEN(new ForbiddenResponse("Your user is not authorized to view this page.<br>"
|
||||
+ "If you believe this is an error contact staff to change your access level."));
|
||||
|
||||
private final Response response;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user