Commit 23226ad4 authored by Jaida Wu's avatar Jaida Wu
Browse files

Refact code


Signed-off-by: default avatarJaida Wu <mlgmxyysd@meowcat.org>
parent e242e72b
package emu.grasscutter.server.dispatch;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.security.KeyStore;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.protobuf.ByteString;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.Account;
......@@ -32,27 +14,31 @@ import emu.grasscutter.net.proto.QueryCurrRegionHttpRspOuterClass.QueryCurrRegio
import emu.grasscutter.net.proto.QueryRegionListHttpRspOuterClass.QueryRegionListHttpRsp;
import emu.grasscutter.net.proto.RegionInfoOuterClass.RegionInfo;
import emu.grasscutter.net.proto.RegionSimpleInfoOuterClass.RegionSimpleInfo;
import emu.grasscutter.server.dispatch.json.ComboTokenReqJson;
import emu.grasscutter.server.dispatch.json.ComboTokenResJson;
import emu.grasscutter.server.dispatch.json.LoginAccountRequestJson;
import emu.grasscutter.server.dispatch.json.LoginResultJson;
import emu.grasscutter.server.dispatch.json.LoginTokenRequestJson;
import emu.grasscutter.server.dispatch.json.*;
import emu.grasscutter.server.dispatch.json.ComboTokenReqJson.LoginTokenData;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;
import com.sun.net.httpserver.HttpServer;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URLDecoder;
import java.security.KeyStore;
import java.util.Base64;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public final class DispatchServer {
public static String query_region_list = "";
public static String query_cur_region = "";
private final InetSocketAddress address;
private final Gson gson;
private QueryCurrRegionHttpRsp currRegion;
public String regionListBase64;
public String regionCurrentBase64;
public static String query_region_list = "";
public static String query_cur_region = "";
private QueryCurrRegionHttpRsp currRegion;
public DispatchServer() {
this.address = new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, Grasscutter.getConfig().DispatchServerPort);
......@@ -140,7 +126,7 @@ public final class DispatchServer {
public void start() throws Exception {
HttpServer server;
if(Grasscutter.getConfig().UseSSL) {
if (Grasscutter.getConfig().UseSSL) {
HttpsServer httpsServer;
httpsServer = HttpsServer.create(getAddress(), 0);
SSLContext sslContext = SSLContext.getInstance("TLS");
......@@ -213,7 +199,7 @@ public final class DispatchServer {
try {
String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, LoginAccountRequestJson.class);
} catch (Exception e) {
} catch (Exception ignored) {
}
// Create response json
......@@ -232,10 +218,15 @@ public final class DispatchServer {
// This account has been created AUTOMATICALLY. There will be no permissions added.
account = DatabaseHelper.createAccountWithId(requestData.account, 0);
if (account != null) {
responseData.message = "OK";
responseData.data.account.uid = account.getId();
responseData.data.account.token = account.generateSessionKey();
responseData.data.account.email = account.getEmail();
} else {
responseData.retcode = -201;
responseData.message = "Username not found, create failed.";
}
} else {
responseData.retcode = -201;
responseData.message = "Username not found.";
......@@ -248,15 +239,7 @@ public final class DispatchServer {
responseData.data.account.email = account.getEmail();
}
// Create a response
String response = getGsonFactory().toJson(responseData);
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
responseJson(t, responseData);
});
// Login via token
server.createContext("/hk4e_global/mdk/shield/api/verify", t -> {
......@@ -265,7 +248,7 @@ public final class DispatchServer {
try {
String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, LoginTokenRequestJson.class);
} catch (Exception e) {
} catch (Exception ignored) {
}
// Create response json
......@@ -288,15 +271,7 @@ public final class DispatchServer {
responseData.data.account.email = account.getEmail();
}
// Create a response
String response = getGsonFactory().toJson(responseData);
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
responseJson(t, responseData);
});
// Exchange for combo token
server.createContext("/hk4e_global/combo/granter/login/v2/login", t -> {
......@@ -305,7 +280,7 @@ public final class DispatchServer {
try {
String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, ComboTokenReqJson.class);
} catch (Exception e) {
} catch (Exception ignored) {
}
// Create response json
......@@ -329,15 +304,7 @@ public final class DispatchServer {
responseData.data.combo_token = account.generateLoginToken();
}
// Create a response
String response = getGsonFactory().toJson(responseData);
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
responseJson(t, responseData);
});
// Agreement and Protocol
server.createContext( // hk4e-sdk-os.hoyoverse.com
......@@ -432,6 +399,18 @@ public final class DispatchServer {
Grasscutter.getLogger().info("Dispatch server started on port " + getAddress().getPort());
}
private void responseJson(HttpExchange t, Object data) throws IOException {
// Create a response
String response = getGsonFactory().toJson(data);
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("application/json"));
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}
private Map<String, String> parseQueryString(String qs) {
Map<String, String> result = new HashMap<>();
if (qs == null)
......
......@@ -7,7 +7,7 @@ public class ComboTokenReqJson {
public String device;
public String sign;
public class LoginTokenData {
public static class LoginTokenData {
public String uid;
public String token;
public boolean guest;
......
......@@ -5,7 +5,7 @@ public class ComboTokenResJson {
public int retcode;
public LoginData data = new LoginData();
public class LoginData {
public static class LoginData {
public int account_type = 1;
public boolean heartbeat;
public String combo_id;
......
......@@ -5,7 +5,7 @@ public class LoginResultJson {
public int retcode;
public VerifyData data = new VerifyData();
public class VerifyData {
public static class VerifyData {
public VerifyAccountData account = new VerifyAccountData();
public boolean device_grant_required = false;
public String realname_operation = "NONE";
......@@ -13,7 +13,7 @@ public class LoginResultJson {
public boolean safe_mobile_required = false;
}
public class VerifyAccountData {
public static class VerifyAccountData {
public String uid;
public String name = "";
public String email;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment