mirror of
https://github.com/Grasscutters/Grasscutter.git
synced 2026-02-07 10:36:41 +01:00
Fix whitespace [skip actions]
This commit is contained in:
@@ -30,22 +30,22 @@ public final class HttpServer {
|
||||
this.express = new Express(config -> {
|
||||
// Set the Express HTTP server.
|
||||
config.server(HttpServer::createServer);
|
||||
|
||||
|
||||
// Configure encryption/HTTPS/SSL.
|
||||
config.enforceSsl = HTTP_ENCRYPTION.useEncryption;
|
||||
|
||||
|
||||
// Configure HTTP policies.
|
||||
if(HTTP_POLICIES.cors.enabled) {
|
||||
if (HTTP_POLICIES.cors.enabled) {
|
||||
var allowedOrigins = HTTP_POLICIES.cors.allowedOrigins;
|
||||
if (allowedOrigins.length > 0)
|
||||
config.enableCorsForOrigin(allowedOrigins);
|
||||
else config.enableCorsForAllOrigins();
|
||||
}
|
||||
|
||||
|
||||
// Configure debug logging.
|
||||
if(DISPATCH_INFO.logRequests == ServerDebugMode.ALL)
|
||||
if (DISPATCH_INFO.logRequests == ServerDebugMode.ALL)
|
||||
config.enableDevLogging();
|
||||
|
||||
|
||||
// Disable compression on static files.
|
||||
config.precompressStaticFiles = false;
|
||||
});
|
||||
@@ -60,26 +60,26 @@ public final class HttpServer {
|
||||
Server server = new Server();
|
||||
ServerConnector serverConnector
|
||||
= new ServerConnector(server);
|
||||
|
||||
if(HTTP_ENCRYPTION.useEncryption) {
|
||||
|
||||
if (HTTP_ENCRYPTION.useEncryption) {
|
||||
var sslContextFactory = new SslContextFactory.Server();
|
||||
var keystoreFile = new File(HTTP_ENCRYPTION.keystore);
|
||||
|
||||
if(!keystoreFile.exists()) {
|
||||
|
||||
if (!keystoreFile.exists()) {
|
||||
HTTP_ENCRYPTION.useEncryption = false;
|
||||
HTTP_ENCRYPTION.useInRouting = false;
|
||||
|
||||
|
||||
Grasscutter.getLogger().warn(translate("messages.dispatch.keystore.no_keystore_error"));
|
||||
} else try {
|
||||
sslContextFactory.setKeyStorePath(keystoreFile.getPath());
|
||||
sslContextFactory.setKeyStorePassword(HTTP_ENCRYPTION.keystorePassword);
|
||||
} catch (Exception ignored) {
|
||||
Grasscutter.getLogger().warn(translate("messages.dispatch.keystore.password_error"));
|
||||
|
||||
|
||||
try {
|
||||
sslContextFactory.setKeyStorePath(keystoreFile.getPath());
|
||||
sslContextFactory.setKeyStorePassword("123456");
|
||||
|
||||
|
||||
Grasscutter.getLogger().warn(translate("messages.dispatch.keystore.default_password"));
|
||||
} catch (Exception exception) {
|
||||
Grasscutter.getLogger().warn(translate("messages.dispatch.keystore.general_error"), exception);
|
||||
@@ -88,10 +88,10 @@ public final class HttpServer {
|
||||
serverConnector = new ServerConnector(server, sslContextFactory);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
serverConnector.setPort(HTTP_INFO.bindPort);
|
||||
server.setConnectors(new ServerConnector[]{serverConnector});
|
||||
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -112,9 +112,9 @@ public final class HttpServer {
|
||||
public HttpServer addRouter(Class<? extends Router> router, Object... args) {
|
||||
// Get all constructor parameters.
|
||||
Class<?>[] types = new Class<?>[args.length];
|
||||
for(var argument : args)
|
||||
for (var argument : args)
|
||||
types[args.length - 1] = argument.getClass();
|
||||
|
||||
|
||||
try { // Create a router instance & apply routes.
|
||||
var constructor = router.getDeclaredConstructor(types); // Get the constructor.
|
||||
var routerInstance = constructor.newInstance(args); // Create instance.
|
||||
@@ -130,9 +130,9 @@ public final class HttpServer {
|
||||
*/
|
||||
public void start() throws UnsupportedEncodingException {
|
||||
// Attempt to start the HTTP server.
|
||||
if(HTTP_INFO.bindAddress.equals("")){
|
||||
if (HTTP_INFO.bindAddress.equals("")) {
|
||||
this.express.listen(HTTP_INFO.bindPort);
|
||||
}else{
|
||||
}else {
|
||||
this.express.listen(HTTP_INFO.bindAddress, HTTP_INFO.bindPort);
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public final class HttpServer {
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
express.get("/", (request, response) -> {
|
||||
File file = new File(HTTP_STATIC_FILES.indexFile);
|
||||
if(!file.exists())
|
||||
if (!file.exists())
|
||||
response.send("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
@@ -173,19 +173,19 @@ public final class HttpServer {
|
||||
public static class UnhandledRequestRouter implements Router {
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
handle.error(404, context -> {
|
||||
if(DISPATCH_INFO.logRequests == ServerDebugMode.MISSING)
|
||||
if (DISPATCH_INFO.logRequests == ServerDebugMode.MISSING)
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.unhandled_request_error", context.method(), context.url()));
|
||||
context.contentType("text/html");
|
||||
|
||||
|
||||
File file = new File(HTTP_STATIC_FILES.errorFile);
|
||||
if(!file.exists())
|
||||
if (!file.exists())
|
||||
context.result("""
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf8">
|
||||
</head>
|
||||
|
||||
|
||||
<body>
|
||||
<img src="https://http.cat/404" />
|
||||
</body>
|
||||
|
||||
@@ -60,7 +60,7 @@ public final class RegionHandler implements Router {
|
||||
List<String> usedNames = new ArrayList<>(); // List to check for potential naming conflicts.
|
||||
|
||||
var configuredRegions = new ArrayList<>(List.of(DISPATCH_INFO.regions));
|
||||
if(SERVER.runMode != ServerRunMode.HYBRID && configuredRegions.size() == 0) {
|
||||
if (SERVER.runMode != ServerRunMode.HYBRID && configuredRegions.size() == 0) {
|
||||
Grasscutter.getLogger().error("[Dispatch] There are no game servers available. Exiting due to unplayable state.");
|
||||
System.exit(1);
|
||||
} else if (configuredRegions.size() == 0)
|
||||
@@ -136,11 +136,11 @@ public final class RegionHandler implements Router {
|
||||
// Get region data.
|
||||
String regionData = "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw==";
|
||||
if (request.query().values().size() > 0) {
|
||||
if(region != null)
|
||||
if (region != null)
|
||||
regionData = region.getBase64();
|
||||
}
|
||||
|
||||
if( versionName.contains("2.7.5") || versionName.contains("2.8.")) {
|
||||
if ( versionName.contains("2.7.5") || versionName.contains("2.8.")) {
|
||||
try {
|
||||
QueryCurrentRegionEvent event = new QueryCurrentRegionEvent(regionData); event.call();
|
||||
|
||||
@@ -227,4 +227,4 @@ public final class RegionHandler implements Router {
|
||||
public static QueryCurrRegionHttpRsp getCurrentRegion() {
|
||||
return SERVER.runMode == ServerRunMode.HYBRID ? regions.get("os_usa").getRegionQuery() : null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ public final class AnnouncementsHandler implements Router {
|
||||
|
||||
express.get("/hk4e/announcement/*", AnnouncementsHandler::getPageResources);
|
||||
}
|
||||
|
||||
|
||||
private static void getAnnouncement(Request request, Response response) {
|
||||
String data = "";
|
||||
if (Objects.equals(request.baseUrl(), "/common/hk4e_global/announcement/api/getAnnContent")) {
|
||||
try {
|
||||
data = FileUtils.readToString(DataLoader.load("GameAnnouncement.json"));
|
||||
} catch (Exception e) {
|
||||
if(e.getClass() == IOException.class) {
|
||||
if (e.getClass() == IOException.class) {
|
||||
Grasscutter.getLogger().info("Unable to read file 'GameAnnouncementList.json'. \n" + e);
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ public final class AnnouncementsHandler implements Router {
|
||||
try {
|
||||
data = FileUtils.readToString(DataLoader.load("GameAnnouncementList.json"));
|
||||
} catch (Exception e) {
|
||||
if(e.getClass() == IOException.class) {
|
||||
if (e.getClass() == IOException.class) {
|
||||
Grasscutter.getLogger().info("Unable to read file 'GameAnnouncementList.json'. \n" + e);
|
||||
}
|
||||
}
|
||||
@@ -76,9 +76,9 @@ public final class AnnouncementsHandler implements Router {
|
||||
.replace("{{SYSTEM_TIME}}", String.valueOf(System.currentTimeMillis()));
|
||||
response.send("{\"retcode\":0,\"message\":\"OK\",\"data\": " + data + "}");
|
||||
}
|
||||
|
||||
|
||||
private static void getPageResources(Request request, Response response) {
|
||||
try(InputStream filestream = DataLoader.load(request.path())) {
|
||||
try (InputStream filestream = DataLoader.load(request.path())) {
|
||||
String possibleFilename = Utils.toFilePath(DATA(request.path()));
|
||||
|
||||
MediaType fromExtension = MediaType.getByExtension(possibleFilename.substring(possibleFilename.lastIndexOf(".") + 1));
|
||||
|
||||
@@ -30,14 +30,14 @@ import static emu.grasscutter.utils.Language.translate;
|
||||
*/
|
||||
public final class GachaHandler implements Router {
|
||||
public static final String gachaMappings = DATA(Utils.toFilePath("gacha/mappings.js"));
|
||||
|
||||
|
||||
@Override public void applyRoutes(Express express, Javalin handle) {
|
||||
express.get("/gacha", GachaHandler::gachaRecords);
|
||||
express.get("/gacha/details", GachaHandler::gachaDetails);
|
||||
|
||||
|
||||
express.useStaticFallback("/gacha/mappings", gachaMappings, Location.EXTERNAL);
|
||||
}
|
||||
|
||||
|
||||
private static void gachaRecords(Request request, Response response) {
|
||||
File recordsTemplate = new File(Utils.toFilePath(DATA("gacha/records.html")));
|
||||
if (!recordsTemplate.exists()) {
|
||||
@@ -48,7 +48,7 @@ public final class GachaHandler implements Router {
|
||||
|
||||
String sessionKey = request.query("s");
|
||||
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
|
||||
if(account == null) {
|
||||
if (account == null) {
|
||||
response.status(403).send("Requested account was not found");
|
||||
return;
|
||||
}
|
||||
@@ -59,9 +59,9 @@ public final class GachaHandler implements Router {
|
||||
}
|
||||
|
||||
int page = 0, gachaType = 0;
|
||||
if(request.query("p") != null)
|
||||
if (request.query("p") != null)
|
||||
page = Integer.parseInt(request.query("p"));
|
||||
if(request.query("gachaType") != null)
|
||||
if (request.query("gachaType") != null)
|
||||
gachaType = Integer.parseInt(request.query("gachaType"));
|
||||
|
||||
String records = DatabaseHelper.getGachaRecords(player.getUid(), page, gachaType).toString();
|
||||
@@ -76,7 +76,7 @@ public final class GachaHandler implements Router {
|
||||
.replace("{{LANGUAGE}}", Utils.getLanguageCode(account.getLocale()));
|
||||
response.send(template);
|
||||
}
|
||||
|
||||
|
||||
private static void gachaDetails(Request request, Response response) {
|
||||
File detailsTemplate = new File(Utils.toFilePath(DATA("gacha/details.html")));
|
||||
if (!detailsTemplate.exists()) {
|
||||
@@ -87,7 +87,7 @@ public final class GachaHandler implements Router {
|
||||
|
||||
String sessionKey = request.query("s");
|
||||
Account account = DatabaseHelper.getAccountBySessionKey(sessionKey);
|
||||
if(account == null) {
|
||||
if (account == null) {
|
||||
response.status(403).send("Requested account was not found");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ public final class GenericHandler implements Router {
|
||||
|
||||
// api-account-os.hoyoverse.com
|
||||
express.post("/account/risky/api/check", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"id\":\"none\",\"action\":\"ACTION_NONE\",\"geetest\":null}}"));
|
||||
|
||||
|
||||
// sdk-os-static.hoyoverse.com
|
||||
express.get("/combo/box/api/config/sdk/combo", new HttpJsonResponse("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"vals\":{\"disable_email_bind_skip\":\"false\",\"email_bind_remind_interval\":\"7\",\"email_bind_remind\":\"true\"}}}"));
|
||||
// hk4e-sdk-os-static.hoyoverse.com
|
||||
@@ -35,7 +35,7 @@ public final class GenericHandler implements Router {
|
||||
// Test api?
|
||||
// abtest-api-data-sg.hoyoverse.com
|
||||
express.post("/data_abtest_api/config/experiment/list", new HttpJsonResponse("{\"retcode\":0,\"success\":true,\"message\":\"\",\"data\":[{\"code\":1000,\"type\":2,\"config_id\":\"14\",\"period_id\":\"6036_99\",\"version\":\"1\",\"configs\":{\"cardType\":\"old\"}}]}"));
|
||||
|
||||
|
||||
// log-upload-os.mihoyo.com
|
||||
express.all("/log/sdk/upload", new HttpJsonResponse("{\"code\":0}"));
|
||||
express.all("/sdk/upload", new HttpJsonResponse("{\"code\":0}"));
|
||||
@@ -45,10 +45,10 @@ public final class GenericHandler implements Router {
|
||||
|
||||
// webstatic-sea.hoyoverse.com
|
||||
express.get("/admin/mi18n/plat_oversea/*", new WebStaticVersionResponse());
|
||||
|
||||
|
||||
express.get("/status/server", GenericHandler::serverStatus);
|
||||
}
|
||||
|
||||
|
||||
private static void serverStatus(Request request, Response response) {
|
||||
int playerCount = Grasscutter.getGameServer().getPlayers().size();
|
||||
int maxPlayer = ACCOUNT.maxPlayer;
|
||||
|
||||
@@ -14,30 +14,30 @@ import static emu.grasscutter.config.Configuration.*;
|
||||
import static emu.grasscutter.utils.Language.translate;
|
||||
|
||||
public final class HttpJsonResponse implements HttpContextHandler {
|
||||
private final String response;
|
||||
private final String[] missingRoutes = { // TODO: When http requests for theses routes are found please remove it from this list and update the route request type in the DispatchServer
|
||||
"/common/hk4e_global/announcement/api/getAlertPic",
|
||||
"/common/hk4e_global/announcement/api/getAlertAnn",
|
||||
"/common/hk4e_global/announcement/api/getAnnList",
|
||||
"/common/hk4e_global/announcement/api/getAnnContent",
|
||||
"/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier",
|
||||
"/log/sdk/upload",
|
||||
"/sdk/upload",
|
||||
"/perf/config/verify",
|
||||
"/log",
|
||||
"/crash/dataUpload"
|
||||
};
|
||||
|
||||
public HttpJsonResponse(String response) {
|
||||
this.response = response;
|
||||
}
|
||||
private final String response;
|
||||
private final String[] missingRoutes = { // TODO: When http requests for theses routes are found please remove it from this list and update the route request type in the DispatchServer
|
||||
"/common/hk4e_global/announcement/api/getAlertPic",
|
||||
"/common/hk4e_global/announcement/api/getAlertAnn",
|
||||
"/common/hk4e_global/announcement/api/getAnnList",
|
||||
"/common/hk4e_global/announcement/api/getAnnContent",
|
||||
"/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier",
|
||||
"/log/sdk/upload",
|
||||
"/sdk/upload",
|
||||
"/perf/config/verify",
|
||||
"/log",
|
||||
"/crash/dataUpload"
|
||||
};
|
||||
|
||||
@Override
|
||||
public void handle(Request req, Response res) throws IOException {
|
||||
// Checking for ALL here isn't required as when ALL is enabled enableDevLogging() gets enabled
|
||||
if(DISPATCH_INFO.logRequests == ServerDebugMode.MISSING && Arrays.stream(missingRoutes).anyMatch(x -> Objects.equals(x, req.baseUrl()))) {
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.request", req.ip(), req.method(), req.baseUrl()) + (DISPATCH_INFO.logRequests == ServerDebugMode.MISSING ? "(MISSING)" : ""));
|
||||
}
|
||||
res.send(response);
|
||||
}
|
||||
public HttpJsonResponse(String response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(Request req, Response res) throws IOException {
|
||||
// Checking for ALL here isn't required as when ALL is enabled enableDevLogging() gets enabled
|
||||
if (DISPATCH_INFO.logRequests == ServerDebugMode.MISSING && Arrays.stream(missingRoutes).anyMatch(x -> Objects.equals(x, req.baseUrl()))) {
|
||||
Grasscutter.getLogger().info(translate("messages.dispatch.request", req.ip(), req.method(), req.baseUrl()) + (DISPATCH_INFO.logRequests == ServerDebugMode.MISSING ? "(MISSING)" : ""));
|
||||
}
|
||||
res.send(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,13 +27,13 @@ public class WebStaticVersionResponse implements HttpContextHandler {
|
||||
}
|
||||
|
||||
private static void getPageResources(String path, Response response) {
|
||||
try(InputStream filestream = FileUtils.readResourceAsStream(path)) {
|
||||
try (InputStream filestream = FileUtils.readResourceAsStream(path)) {
|
||||
|
||||
MediaType fromExtension = MediaType.getByExtension(path.substring(path.lastIndexOf(".") + 1));
|
||||
response.type((fromExtension != null) ? fromExtension.getMIME() : "application/octet-stream");
|
||||
response.send(filestream.readAllBytes());
|
||||
} catch (Exception e) {
|
||||
if(DISPATCH_INFO.logRequests == Grasscutter.ServerDebugMode.MISSING) {
|
||||
if (DISPATCH_INFO.logRequests == Grasscutter.ServerDebugMode.MISSING) {
|
||||
Grasscutter.getLogger().warn("Webstatic File Missing: " + path);
|
||||
}
|
||||
response.status(404);
|
||||
|
||||
Reference in New Issue
Block a user