Merge pull request #16 from pedox/feat-dispatch-server

Added Config for using http server with non SSL
This commit is contained in:
Melledy 2022-04-18 22:05:51 -07:00 committed by GitHub
commit 53e7f01a15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 18 deletions

View File

@ -6,12 +6,15 @@ public final class Config {
public int DispatchServerPort = 443; public int DispatchServerPort = 443;
public String DispatchServerKeystorePath = "./keystore.p12"; public String DispatchServerKeystorePath = "./keystore.p12";
public String DispatchServerKeystorePassword = ""; public String DispatchServerKeystorePassword = "";
public Boolean UseSSL = true;
public String GameServerName = "Test"; public String GameServerName = "Test";
public String GameServerIp = "127.0.0.1"; public String GameServerIp = "127.0.0.1";
public String GameServerPublicIp = ""; public String GameServerPublicIp = "";
public int GameServerPort = 22102; public int GameServerPort = 22102;
public int UploadLogPort = 80;
public String DatabaseUrl = "mongodb://localhost:27017"; public String DatabaseUrl = "mongodb://localhost:27017";
public String DatabaseCollection = "grasscutter"; public String DatabaseCollection = "grasscutter";

View File

@ -139,9 +139,11 @@ public final class DispatchServer {
} }
public void start() throws Exception { public void start() throws Exception {
HttpsServer server = HttpsServer.create(getAddress(), 0); HttpServer server;
if(Grasscutter.getConfig().UseSSL) {
HttpsServer httpsServer;
httpsServer = HttpsServer.create(getAddress(), 0);
SSLContext sslContext = SSLContext.getInstance("TLS"); SSLContext sslContext = SSLContext.getInstance("TLS");
try (FileInputStream fis = new FileInputStream(Grasscutter.getConfig().DispatchServerKeystorePath)) { try (FileInputStream fis = new FileInputStream(Grasscutter.getConfig().DispatchServerKeystorePath)) {
char[] keystorePassword = Grasscutter.getConfig().DispatchServerKeystorePassword.toCharArray(); char[] keystorePassword = Grasscutter.getConfig().DispatchServerKeystorePassword.toCharArray();
KeyStore ks = KeyStore.getInstance("PKCS12"); KeyStore ks = KeyStore.getInstance("PKCS12");
@ -151,11 +153,15 @@ public final class DispatchServer {
sslContext.init(kmf.getKeyManagers(), null, null); sslContext.init(kmf.getKeyManagers(), null, null);
server.setHttpsConfigurator(new HttpsConfigurator(sslContext)); httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
server = httpsServer;
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error("No SSL cert found!"); Grasscutter.getLogger().error("No SSL cert found!");
return; return;
} }
} else {
server = HttpServer.create(getAddress(), 0);
}
server.createContext("/", t -> { server.createContext("/", t -> {
//Create a response form the request query parameters //Create a response form the request query parameters
@ -396,7 +402,7 @@ public final class DispatchServer {
overseaLogServer.start(); overseaLogServer.start();
Grasscutter.getLogger().info("Log server (overseauspider) started on port " + 8888); Grasscutter.getLogger().info("Log server (overseauspider) started on port " + 8888);
HttpServer uploadLogServer = HttpServer.create(new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, 80), 0); HttpServer uploadLogServer = HttpServer.create(new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, Grasscutter.getConfig().UploadLogPort), 0);
uploadLogServer.createContext( // log-upload-os.mihoyo.com uploadLogServer.createContext( // log-upload-os.mihoyo.com
"/crash/dataUpload", "/crash/dataUpload",
new DispatchHttpJsonHandler("{\"code\":0}") new DispatchHttpJsonHandler("{\"code\":0}")
@ -413,7 +419,7 @@ public final class DispatchServer {
os.close(); os.close();
}); });
uploadLogServer.start(); uploadLogServer.start();
Grasscutter.getLogger().info("Log server (log-upload-os) started on port " + 80); Grasscutter.getLogger().info("Log server (log-upload-os) started on port " + Grasscutter.getConfig().UploadLogPort);
} }
private Map<String, String> parseQueryString(String qs) { private Map<String, String> parseQueryString(String qs) {