Commit 1143886b authored by Jaida Wu's avatar Jaida Wu
Browse files

Add some log to dispatch server


Signed-off-by: default avatarJaida Wu <mlgmxyysd@meowcat.org>
parent 12674f6c
...@@ -150,48 +150,26 @@ public final class DispatchServer { ...@@ -150,48 +150,26 @@ public final class DispatchServer {
server = HttpServer.create(getAddress(), 0); server = HttpServer.create(getAddress(), 0);
} }
server.createContext("/", t -> { server.createContext("/", t -> responseHTML(t, "Hello"));
//Create a response form the request query parameters
String response = "Hello";
//Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("text/html; charset=UTF-8"));
t.sendResponseHeaders(200, response.getBytes().length);
//Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
});
// Dispatch // Dispatch
server.createContext("/query_region_list", t -> { server.createContext("/query_region_list", t -> {
// Log // Log
Grasscutter.getLogger().info("Client request: query_region_list"); Grasscutter.getLogger().info(String.format("Client %s request: query_region_list", t.getRemoteAddress()));
// Create a response form the request query parameters
String response = regionListBase64; responseHTML(t, regionListBase64);
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("text/html; charset=UTF-8"));
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}); });
server.createContext("/query_cur_region", t -> { server.createContext("/query_cur_region", t -> {
// Log // Log
Grasscutter.getLogger().info("Client request: query_cur_region"); Grasscutter.getLogger().info(String.format("Client %s request: query_cur_region", t.getRemoteAddress()));
// Create a response form the request query parameters // Create a response form the request query parameters
URI uri = t.getRequestURI(); URI uri = t.getRequestURI();
String response = "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw=="; String response = "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw==";
if (uri.getQuery() != null && uri.getQuery().length() > 0) { if (uri.getQuery() != null && uri.getQuery().length() > 0) {
response = regionCurrentBase64; response = regionCurrentBase64;
} }
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("text/html; charset=UTF-8")); responseHTML(t, response);
t.sendResponseHeaders(200, response.getBytes().length);
// Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
}); });
// Login via account // Login via account
server.createContext("/hk4e_global/mdk/shield/api/login", t -> { server.createContext("/hk4e_global/mdk/shield/api/login", t -> {
...@@ -201,7 +179,6 @@ public final class DispatchServer { ...@@ -201,7 +179,6 @@ public final class DispatchServer {
String body = Utils.toString(t.getRequestBody()); String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, LoginAccountRequestJson.class); requestData = getGsonFactory().fromJson(body, LoginAccountRequestJson.class);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
// Create response json // Create response json
if (requestData == null) { if (requestData == null) {
...@@ -209,6 +186,8 @@ public final class DispatchServer { ...@@ -209,6 +186,8 @@ public final class DispatchServer {
} }
LoginResultJson responseData = new LoginResultJson(); LoginResultJson responseData = new LoginResultJson();
Grasscutter.getLogger().info(String.format("Client %s is trying to log in", t.getRemoteAddress()));
// Login // Login
Account account = DatabaseHelper.getAccountByName(requestData.account); Account account = DatabaseHelper.getAccountByName(requestData.account);
...@@ -224,13 +203,19 @@ public final class DispatchServer { ...@@ -224,13 +203,19 @@ public final class DispatchServer {
responseData.data.account.uid = account.getId(); responseData.data.account.uid = account.getId();
responseData.data.account.token = account.generateSessionKey(); responseData.data.account.token = account.generateSessionKey();
responseData.data.account.email = account.getEmail(); responseData.data.account.email = account.getEmail();
Grasscutter.getLogger().info(String.format("Client %s failed to log in: Account %s created", t.getRemoteAddress(), responseData.data.account.uid));
} else { } else {
responseData.retcode = -201; responseData.retcode = -201;
responseData.message = "Username not found, create failed."; responseData.message = "Username not found, create failed.";
Grasscutter.getLogger().info(String.format("Client %s failed to log in: Account create failed", t.getRemoteAddress()));
} }
} else { } else {
responseData.retcode = -201; responseData.retcode = -201;
responseData.message = "Username not found."; responseData.message = "Username not found.";
Grasscutter.getLogger().info(String.format("Client %s failed to log in: Account no found", t.getRemoteAddress()));
} }
} else { } else {
// Account was found, log the player in // Account was found, log the player in
...@@ -238,10 +223,13 @@ public final class DispatchServer { ...@@ -238,10 +223,13 @@ public final class DispatchServer {
responseData.data.account.uid = account.getId(); responseData.data.account.uid = account.getId();
responseData.data.account.token = account.generateSessionKey(); responseData.data.account.token = account.generateSessionKey();
responseData.data.account.email = account.getEmail(); responseData.data.account.email = account.getEmail();
Grasscutter.getLogger().info(String.format("Client %s logged in as %s", t.getRemoteAddress(), responseData.data.account.uid));
} }
responseJson(t, responseData); responseJSON(t, responseData);
}); });
// Login via token // Login via token
server.createContext("/hk4e_global/mdk/shield/api/verify", t -> { server.createContext("/hk4e_global/mdk/shield/api/verify", t -> {
// Get post data // Get post data
...@@ -250,13 +238,13 @@ public final class DispatchServer { ...@@ -250,13 +238,13 @@ public final class DispatchServer {
String body = Utils.toString(t.getRequestBody()); String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, LoginTokenRequestJson.class); requestData = getGsonFactory().fromJson(body, LoginTokenRequestJson.class);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
// Create response json // Create response json
if (requestData == null) { if (requestData == null) {
return; return;
} }
LoginResultJson responseData = new LoginResultJson(); LoginResultJson responseData = new LoginResultJson();
Grasscutter.getLogger().info(String.format("Client %s is trying to log in via token", t.getRemoteAddress()));
// Login // Login
Account account = DatabaseHelper.getAccountById(requestData.uid); Account account = DatabaseHelper.getAccountById(requestData.uid);
...@@ -265,14 +253,18 @@ public final class DispatchServer { ...@@ -265,14 +253,18 @@ public final class DispatchServer {
if (account == null || !account.getSessionKey().equals(requestData.token)) { if (account == null || !account.getSessionKey().equals(requestData.token)) {
responseData.retcode = -111; responseData.retcode = -111;
responseData.message = "Game account cache information error"; responseData.message = "Game account cache information error";
Grasscutter.getLogger().info(String.format("Client %s failed to log in via token", t.getRemoteAddress()));
} else { } else {
responseData.message = "OK"; responseData.message = "OK";
responseData.data.account.uid = requestData.uid; responseData.data.account.uid = requestData.uid;
responseData.data.account.token = requestData.token; responseData.data.account.token = requestData.token;
responseData.data.account.email = account.getEmail(); responseData.data.account.email = account.getEmail();
Grasscutter.getLogger().info(String.format("Client %s logged in via token as %s", t.getRemoteAddress(), responseData.data.account.uid));
} }
responseJson(t, responseData); responseJSON(t, responseData);
}); });
// Exchange for combo token // Exchange for combo token
server.createContext("/hk4e_global/combo/granter/login/v2/login", t -> { server.createContext("/hk4e_global/combo/granter/login/v2/login", t -> {
...@@ -282,7 +274,6 @@ public final class DispatchServer { ...@@ -282,7 +274,6 @@ public final class DispatchServer {
String body = Utils.toString(t.getRequestBody()); String body = Utils.toString(t.getRequestBody());
requestData = getGsonFactory().fromJson(body, ComboTokenReqJson.class); requestData = getGsonFactory().fromJson(body, ComboTokenReqJson.class);
} catch (Exception ignored) { } catch (Exception ignored) {
} }
// Create response json // Create response json
if (requestData == null || requestData.data == null) { if (requestData == null || requestData.data == null) {
...@@ -298,14 +289,18 @@ public final class DispatchServer { ...@@ -298,14 +289,18 @@ public final class DispatchServer {
if (account == null || !account.getSessionKey().equals(loginData.token)) { if (account == null || !account.getSessionKey().equals(loginData.token)) {
responseData.retcode = -201; responseData.retcode = -201;
responseData.message = "Wrong session key."; responseData.message = "Wrong session key.";
Grasscutter.getLogger().info(String.format("Client %s failed to exchange combo token", t.getRemoteAddress()));
} else { } else {
responseData.message = "OK"; responseData.message = "OK";
responseData.data.open_id = loginData.uid; responseData.data.open_id = loginData.uid;
responseData.data.combo_id = "157795300"; responseData.data.combo_id = "157795300";
responseData.data.combo_token = account.generateLoginToken(); responseData.data.combo_token = account.generateLoginToken();
Grasscutter.getLogger().info(String.format("Client %s succeed to exchange combo token", t.getRemoteAddress()));
} }
responseJson(t, responseData); responseJSON(t, responseData);
}); });
// Agreement and Protocol // Agreement and Protocol
server.createContext( // hk4e-sdk-os.hoyoverse.com server.createContext( // hk4e-sdk-os.hoyoverse.com
...@@ -384,23 +379,13 @@ public final class DispatchServer { ...@@ -384,23 +379,13 @@ public final class DispatchServer {
"/crash/dataUpload", "/crash/dataUpload",
new DispatchHttpJsonHandler("{\"code\":0}") new DispatchHttpJsonHandler("{\"code\":0}")
); );
server.createContext("/gacha", t -> { server.createContext("/gacha", t -> responseHTML(t, "<!doctype html><html lang=\"en\"><head><title>Gacha</title></head><body></body></html>"));
//Create a response form the request query parameters
String response = "<!doctype html><html lang=\"en\"><head><title>Gacha</title></head><body></body></html>";
//Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("text/html; charset=UTF-8"));
t.sendResponseHeaders(200, response.getBytes().length);
//Write the response string
OutputStream os = t.getResponseBody();
os.write(response.getBytes());
os.close();
});
// Start server // Start server
server.start(); server.start();
Grasscutter.getLogger().info("Dispatch server started on port " + getAddress().getPort()); Grasscutter.getLogger().info("Dispatch server started on port " + getAddress().getPort());
} }
private void responseJson(HttpExchange t, Object data) throws IOException { private void responseJSON(HttpExchange t, Object data) throws IOException {
// Create a response // Create a response
String response = getGsonFactory().toJson(data); String response = getGsonFactory().toJson(data);
// Set the response header status and length // Set the response header status and length
...@@ -412,24 +397,37 @@ public final class DispatchServer { ...@@ -412,24 +397,37 @@ public final class DispatchServer {
os.close(); os.close();
} }
private void responseHTML(HttpExchange t, String response) throws IOException {
// Set the response header status and length
t.getResponseHeaders().put("Content-Type", Collections.singletonList("text/html; charset=UTF-8"));
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) { private Map<String, String> parseQueryString(String qs) {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
if (qs == null) if (qs == null) {
return result; return result;
}
int last = 0, next, l = qs.length(); int last = 0, next, l = qs.length();
while (last < l) { while (last < l) {
next = qs.indexOf('&', last); next = qs.indexOf('&', last);
if (next == -1) if (next == -1) {
next = l; next = l;
}
if (next > last) { if (next > last) {
int eqPos = qs.indexOf('=', last); int eqPos = qs.indexOf('=', last);
try { try {
if (eqPos < 0 || eqPos > next) if (eqPos < 0 || eqPos > next) {
result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), ""); result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), "");
else } else {
result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8")); result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8"));
}
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java
} }
......
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