2022-04-17 20:43:07 +08:00
|
|
|
package emu.grasscutter.server.dispatch;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import com.google.protobuf.ByteString;
|
|
|
|
import com.sun.net.httpserver.HttpExchange;
|
2022-04-21 22:06:25 +08:00
|
|
|
import com.sun.net.httpserver.HttpServer;
|
2022-04-17 20:43:07 +08:00
|
|
|
import com.sun.net.httpserver.HttpsConfigurator;
|
|
|
|
import com.sun.net.httpserver.HttpsServer;
|
|
|
|
|
2022-04-21 18:04:00 +08:00
|
|
|
import emu.grasscutter.Config;
|
2022-04-17 20:43:07 +08:00
|
|
|
import emu.grasscutter.Grasscutter;
|
|
|
|
import emu.grasscutter.database.DatabaseHelper;
|
|
|
|
import emu.grasscutter.game.Account;
|
|
|
|
import emu.grasscutter.net.proto.QueryCurrRegionHttpRspOuterClass.QueryCurrRegionHttpRsp;
|
|
|
|
import emu.grasscutter.net.proto.QueryRegionListHttpRspOuterClass.QueryRegionListHttpRsp;
|
|
|
|
import emu.grasscutter.net.proto.RegionInfoOuterClass.RegionInfo;
|
|
|
|
import emu.grasscutter.net.proto.RegionSimpleInfoOuterClass.RegionSimpleInfo;
|
2022-04-21 22:06:25 +08:00
|
|
|
import emu.grasscutter.server.dispatch.json.*;
|
2022-04-17 20:43:07 +08:00
|
|
|
import emu.grasscutter.server.dispatch.json.ComboTokenReqJson.LoginTokenData;
|
|
|
|
import emu.grasscutter.utils.FileUtils;
|
|
|
|
import emu.grasscutter.utils.Utils;
|
|
|
|
|
2022-04-21 22:06:25 +08:00
|
|
|
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;
|
2022-04-22 01:04:03 +08:00
|
|
|
import java.util.*;
|
2022-04-17 20:43:07 +08:00
|
|
|
|
2022-04-18 13:11:27 +08:00
|
|
|
public final class DispatchServer {
|
2022-04-21 22:06:25 +08:00
|
|
|
public static String query_region_list = "";
|
|
|
|
public static String query_cur_region = "";
|
2022-04-22 01:04:03 +08:00
|
|
|
|
2022-04-17 20:43:07 +08:00
|
|
|
private final InetSocketAddress address;
|
|
|
|
private final Gson gson;
|
2022-04-21 19:20:49 +08:00
|
|
|
private final String defaultServerName = "os_usa";
|
2022-04-17 20:43:07 +08:00
|
|
|
|
|
|
|
public String regionListBase64;
|
2022-04-21 18:04:00 +08:00
|
|
|
public HashMap<String, RegionData> regions;
|
|
|
|
|
2022-04-17 20:43:07 +08:00
|
|
|
public DispatchServer() {
|
2022-04-21 18:04:00 +08:00
|
|
|
this.regions = new HashMap<String, RegionData>();
|
2022-04-21 13:53:50 +08:00
|
|
|
this.address = new InetSocketAddress(Grasscutter.getConfig().getDispatchOptions().Ip, Grasscutter.getConfig().getDispatchOptions().Port);
|
2022-04-17 20:43:07 +08:00
|
|
|
this.gson = new GsonBuilder().create();
|
|
|
|
|
|
|
|
this.loadQueries();
|
|
|
|
this.initRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
public InetSocketAddress getAddress() {
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Gson getGsonFactory() {
|
|
|
|
return gson;
|
|
|
|
}
|
|
|
|
|
2022-04-21 19:20:49 +08:00
|
|
|
public QueryCurrRegionHttpRsp getCurrRegion() {
|
|
|
|
// Needs to be fixed by having the game servers connect to the dispatch server.
|
|
|
|
if(Grasscutter.getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
|
|
|
|
return regions.get(defaultServerName).parsedRegionQuery;
|
2022-04-21 18:04:00 +08:00
|
|
|
}
|
|
|
|
|
2022-04-22 01:04:03 +08:00
|
|
|
Grasscutter.getLogger().warn("[Dispatch] Unsupported run mode for getCurrRegion()");
|
2022-04-21 18:04:00 +08:00
|
|
|
return null;
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void loadQueries() {
|
|
|
|
File file;
|
|
|
|
|
|
|
|
file = new File(Grasscutter.getConfig().DATA_FOLDER + "query_region_list.txt");
|
|
|
|
if (file.exists()) {
|
|
|
|
query_region_list = new String(FileUtils.read(file));
|
|
|
|
} else {
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().warn("[Dispatch] query_region_list not found! Using default region list.");
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
file = new File(Grasscutter.getConfig().DATA_FOLDER + "query_cur_region.txt");
|
|
|
|
if (file.exists()) {
|
|
|
|
query_cur_region = new String(FileUtils.read(file));
|
|
|
|
} else {
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().warn("[Dispatch] query_cur_region not found! Using default current region.");
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void initRegion() {
|
|
|
|
try {
|
|
|
|
byte[] decoded = Base64.getDecoder().decode(query_region_list);
|
|
|
|
QueryRegionListHttpRsp rl = QueryRegionListHttpRsp.parseFrom(decoded);
|
|
|
|
|
|
|
|
byte[] decoded2 = Base64.getDecoder().decode(query_cur_region);
|
|
|
|
QueryCurrRegionHttpRsp regionQuery = QueryCurrRegionHttpRsp.parseFrom(decoded2);
|
2022-04-21 13:53:50 +08:00
|
|
|
|
2022-04-21 18:04:00 +08:00
|
|
|
List<RegionSimpleInfo> servers = new ArrayList<RegionSimpleInfo>();
|
|
|
|
List<String> usedNames = new ArrayList<String>(); // List to check for potential naming conflicts
|
|
|
|
if(Grasscutter.getConfig().RunMode.equalsIgnoreCase("HYBRID")) { // Automatically add the game server if in hybrid mode
|
|
|
|
RegionSimpleInfo server = RegionSimpleInfo.newBuilder()
|
|
|
|
.setName("os_usa")
|
|
|
|
.setTitle(Grasscutter.getConfig().getGameServerOptions().Name)
|
|
|
|
.setType("DEV_PUBLIC")
|
2022-04-23 01:16:55 +08:00
|
|
|
.setDispatchUrl("http" + (Grasscutter.getConfig().getDispatchOptions().FrontHTTPS ? "s" : "") + "://" + (Grasscutter.getConfig().getDispatchOptions().PublicIp.isEmpty() ? Grasscutter.getConfig().getDispatchOptions().Ip : Grasscutter.getConfig().getDispatchOptions().PublicIp) + ":" + (Grasscutter.getConfig().getDispatchOptions().PublicPort != 0 ? Grasscutter.getConfig().getDispatchOptions().PublicPort : Grasscutter.getConfig().getDispatchOptions().Port) + "/query_cur_region_" + defaultServerName)
|
2022-04-21 18:04:00 +08:00
|
|
|
.build();
|
|
|
|
usedNames.add(defaultServerName);
|
|
|
|
servers.add(server);
|
|
|
|
|
|
|
|
RegionInfo serverRegion = regionQuery.getRegionInfo().toBuilder()
|
|
|
|
.setIp((Grasscutter.getConfig().getGameServerOptions().PublicIp.isEmpty() ? Grasscutter.getConfig().getGameServerOptions().Ip : Grasscutter.getConfig().getGameServerOptions().PublicIp))
|
2022-04-23 01:42:04 +08:00
|
|
|
.setPort(Grasscutter.getConfig().getGameServerOptions().PublicPort != 0 ? Grasscutter.getConfig().getGameServerOptions().PublicPort : Grasscutter.getConfig().getGameServerOptions().Port)
|
2022-04-21 18:04:00 +08:00
|
|
|
.setSecretKey(ByteString.copyFrom(FileUtils.read(Grasscutter.getConfig().KEY_FOLDER + "dispatchSeed.bin")))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
QueryCurrRegionHttpRsp parsedRegionQuery = regionQuery.toBuilder().setRegionInfo(serverRegion).build();
|
|
|
|
regions.put(defaultServerName, new RegionData(parsedRegionQuery, Base64.getEncoder().encodeToString(parsedRegionQuery.toByteString().toByteArray())));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(Grasscutter.getConfig().getDispatchOptions().getGameServers().length == 0) {
|
2022-04-22 01:04:03 +08:00
|
|
|
Grasscutter.getLogger().error("[Dispatch] There are no game servers available. Exiting due to unplayable state.");
|
2022-04-21 18:04:00 +08:00
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Config.DispatchServerOptions.RegionInfo regionInfo : Grasscutter.getConfig().getDispatchOptions().getGameServers()) {
|
|
|
|
if(usedNames.contains(regionInfo.Name)) {
|
|
|
|
Grasscutter.getLogger().error("Region name already in use.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
RegionSimpleInfo server = RegionSimpleInfo.newBuilder()
|
|
|
|
.setName(regionInfo.Name)
|
|
|
|
.setTitle(regionInfo.Title)
|
|
|
|
.setType("DEV_PUBLIC")
|
2022-04-22 21:25:58 +08:00
|
|
|
.setDispatchUrl("http" + (Grasscutter.getConfig().getDispatchOptions().FrontHTTPS ? "s" : "") + "://" + (Grasscutter.getConfig().getDispatchOptions().PublicIp.isEmpty() ? Grasscutter.getConfig().getDispatchOptions().Ip : Grasscutter.getConfig().getDispatchOptions().PublicIp) + ":" + getAddress().getPort() + "/query_cur_region_" + regionInfo.Name)
|
2022-04-21 18:04:00 +08:00
|
|
|
.build();
|
|
|
|
usedNames.add(regionInfo.Name);
|
|
|
|
servers.add(server);
|
|
|
|
|
|
|
|
RegionInfo serverRegion = regionQuery.getRegionInfo().toBuilder()
|
|
|
|
.setIp(regionInfo.Ip)
|
|
|
|
.setPort(regionInfo.Port)
|
|
|
|
.setSecretKey(ByteString.copyFrom(FileUtils.read(Grasscutter.getConfig().KEY_FOLDER + "dispatchSeed.bin")))
|
|
|
|
.build();
|
|
|
|
|
|
|
|
QueryCurrRegionHttpRsp parsedRegionQuery = regionQuery.toBuilder().setRegionInfo(serverRegion).build();
|
|
|
|
regions.put(regionInfo.Name, new RegionData(parsedRegionQuery, Base64.getEncoder().encodeToString(parsedRegionQuery.toByteString().toByteArray())));
|
|
|
|
}
|
|
|
|
|
2022-04-17 20:43:07 +08:00
|
|
|
QueryRegionListHttpRsp regionList = QueryRegionListHttpRsp.newBuilder()
|
2022-04-21 18:04:00 +08:00
|
|
|
.addAllServers(servers)
|
2022-04-17 20:43:07 +08:00
|
|
|
.setClientSecretKey(rl.getClientSecretKey())
|
|
|
|
.setClientCustomConfigEncrypted(rl.getClientCustomConfigEncrypted())
|
|
|
|
.setEnableLoginPc(true)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
this.regionListBase64 = Base64.getEncoder().encodeToString(regionList.toByteString().toByteArray());
|
|
|
|
} catch (Exception e) {
|
2022-04-22 01:15:05 +08:00
|
|
|
Grasscutter.getLogger().error("[Dispatch] Error while initializing region info!", e);
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void start() throws Exception {
|
2022-04-19 12:49:08 +08:00
|
|
|
HttpServer server;
|
2022-04-22 01:04:03 +08:00
|
|
|
if (Grasscutter.getConfig().getDispatchOptions().UseSSL) {
|
2022-04-19 12:49:08 +08:00
|
|
|
HttpsServer httpsServer;
|
|
|
|
httpsServer = HttpsServer.create(getAddress(), 0);
|
|
|
|
SSLContext sslContext = SSLContext.getInstance("TLS");
|
2022-04-21 13:53:50 +08:00
|
|
|
try (FileInputStream fis = new FileInputStream(Grasscutter.getConfig().getDispatchOptions().KeystorePath)) {
|
|
|
|
char[] keystorePassword = Grasscutter.getConfig().getDispatchOptions().KeystorePassword.toCharArray();
|
2022-04-19 12:49:08 +08:00
|
|
|
KeyStore ks = KeyStore.getInstance("PKCS12");
|
|
|
|
ks.load(fis, keystorePassword);
|
|
|
|
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
|
|
|
|
kmf.init(ks, keystorePassword);
|
|
|
|
|
|
|
|
sslContext.init(kmf.getKeyManagers(), null, null);
|
|
|
|
|
|
|
|
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
|
|
server = httpsServer;
|
|
|
|
} catch (Exception e) {
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().warn("[Dispatch] No SSL cert found! Falling back to HTTP server.");
|
2022-04-22 01:04:03 +08:00
|
|
|
Grasscutter.getConfig().getDispatchOptions().UseSSL = false;
|
2022-04-21 22:21:28 +08:00
|
|
|
server = HttpServer.create(getAddress(), 0);
|
2022-04-19 12:49:08 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
server = HttpServer.create(getAddress(), 0);
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
server.createContext("/", t -> responseHTML(t, "Hello"));
|
2022-04-17 20:43:07 +08:00
|
|
|
|
|
|
|
// Dispatch
|
2022-04-18 13:11:27 +08:00
|
|
|
server.createContext("/query_region_list", t -> {
|
|
|
|
// Log
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s request: query_region_list", t.getRemoteAddress()));
|
2022-04-21 22:22:40 +08:00
|
|
|
|
|
|
|
responseHTML(t, regionListBase64);
|
2022-04-17 20:43:07 +08:00
|
|
|
});
|
2022-04-21 18:04:00 +08:00
|
|
|
|
|
|
|
for (String regionName : regions.keySet()) {
|
|
|
|
server.createContext("/query_cur_region_" + regionName, t -> {
|
|
|
|
String regionCurrentBase64 = regions.get(regionName).Base64;
|
|
|
|
// Log
|
2022-04-22 01:04:03 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("Client %s request: query_cur_region_%s", t.getRemoteAddress(), regionName));
|
2022-04-21 18:04:00 +08:00
|
|
|
// Create a response form the request query parameters
|
|
|
|
URI uri = t.getRequestURI();
|
|
|
|
String response = "CAESGE5vdCBGb3VuZCB2ZXJzaW9uIGNvbmZpZw==";
|
|
|
|
if (uri.getQuery() != null && uri.getQuery().length() > 0) {
|
|
|
|
response = regionCurrentBase64;
|
|
|
|
}
|
2022-04-22 01:04:03 +08:00
|
|
|
responseHTML(t, response);
|
2022-04-21 18:04:00 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-17 20:43:07 +08:00
|
|
|
// Login via account
|
2022-04-18 13:11:27 +08:00
|
|
|
server.createContext("/hk4e_global/mdk/shield/api/login", t -> {
|
|
|
|
// Get post data
|
|
|
|
LoginAccountRequestJson requestData = null;
|
|
|
|
try {
|
|
|
|
String body = Utils.toString(t.getRequestBody());
|
|
|
|
requestData = getGsonFactory().fromJson(body, LoginAccountRequestJson.class);
|
2022-04-22 01:04:03 +08:00
|
|
|
} catch (Exception ignored) { }
|
|
|
|
|
2022-04-18 13:11:27 +08:00
|
|
|
// Create response json
|
|
|
|
if (requestData == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LoginResultJson responseData = new LoginResultJson();
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s is trying to log in", t.getRemoteAddress()));
|
2022-04-18 13:11:27 +08:00
|
|
|
|
|
|
|
// Login
|
|
|
|
Account account = DatabaseHelper.getAccountByName(requestData.account);
|
|
|
|
|
2022-04-20 12:03:01 +08:00
|
|
|
// Check if account exists, else create a new one.
|
2022-04-20 18:28:54 +08:00
|
|
|
if (account == null) {
|
|
|
|
// Account doesnt exist, so we can either auto create it if the config value is set
|
2022-04-21 13:53:50 +08:00
|
|
|
if (Grasscutter.getConfig().getDispatchOptions().AutomaticallyCreateAccounts) {
|
2022-04-20 18:28:54 +08:00
|
|
|
// This account has been created AUTOMATICALLY. There will be no permissions added.
|
|
|
|
account = DatabaseHelper.createAccountWithId(requestData.account, 0);
|
2022-04-21 22:06:25 +08:00
|
|
|
|
|
|
|
if (account != null) {
|
|
|
|
responseData.message = "OK";
|
|
|
|
responseData.data.account.uid = account.getId();
|
|
|
|
responseData.data.account.token = account.generateSessionKey();
|
|
|
|
responseData.data.account.email = account.getEmail();
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s failed to log in: Account %s created", t.getRemoteAddress(), responseData.data.account.uid));
|
2022-04-21 22:06:25 +08:00
|
|
|
} else {
|
|
|
|
responseData.retcode = -201;
|
|
|
|
responseData.message = "Username not found, create failed.";
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s failed to log in: Account create failed", t.getRemoteAddress()));
|
2022-04-21 22:06:25 +08:00
|
|
|
}
|
2022-04-20 18:28:54 +08:00
|
|
|
} else {
|
|
|
|
responseData.retcode = -201;
|
|
|
|
responseData.message = "Username not found.";
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s failed to log in: Account no found", t.getRemoteAddress()));
|
2022-04-20 18:28:54 +08:00
|
|
|
}
|
2022-04-20 12:10:32 +08:00
|
|
|
} else {
|
2022-04-20 18:28:54 +08:00
|
|
|
// Account was found, log the player in
|
2022-04-20 12:10:32 +08:00
|
|
|
responseData.message = "OK";
|
|
|
|
responseData.data.account.uid = account.getId();
|
|
|
|
responseData.data.account.token = account.generateSessionKey();
|
|
|
|
responseData.data.account.email = account.getEmail();
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s logged in as %s", t.getRemoteAddress(), responseData.data.account.uid));
|
2022-04-18 13:11:27 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
responseJSON(t, responseData);
|
2022-04-17 20:43:07 +08:00
|
|
|
});
|
|
|
|
// Login via token
|
2022-04-18 13:11:27 +08:00
|
|
|
server.createContext("/hk4e_global/mdk/shield/api/verify", t -> {
|
|
|
|
// Get post data
|
|
|
|
LoginTokenRequestJson requestData = null;
|
|
|
|
try {
|
|
|
|
String body = Utils.toString(t.getRequestBody());
|
|
|
|
requestData = getGsonFactory().fromJson(body, LoginTokenRequestJson.class);
|
2022-04-22 01:04:03 +08:00
|
|
|
} catch (Exception ignored) { }
|
|
|
|
|
2022-04-18 13:11:27 +08:00
|
|
|
// Create response json
|
|
|
|
if (requestData == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LoginResultJson responseData = new LoginResultJson();
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s is trying to log in via token", t.getRemoteAddress()));
|
2022-04-18 13:11:27 +08:00
|
|
|
|
|
|
|
// Login
|
|
|
|
Account account = DatabaseHelper.getAccountById(requestData.uid);
|
|
|
|
|
|
|
|
// Test
|
|
|
|
if (account == null || !account.getSessionKey().equals(requestData.token)) {
|
|
|
|
responseData.retcode = -111;
|
|
|
|
responseData.message = "Game account cache information error";
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s failed to log in via token", t.getRemoteAddress()));
|
2022-04-18 13:11:27 +08:00
|
|
|
} else {
|
|
|
|
responseData.message = "OK";
|
|
|
|
responseData.data.account.uid = requestData.uid;
|
|
|
|
responseData.data.account.token = requestData.token;
|
|
|
|
responseData.data.account.email = account.getEmail();
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s logged in via token as %s", t.getRemoteAddress(), responseData.data.account.uid));
|
2022-04-18 13:11:27 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
responseJSON(t, responseData);
|
2022-04-17 20:43:07 +08:00
|
|
|
});
|
|
|
|
// Exchange for combo token
|
2022-04-18 13:11:27 +08:00
|
|
|
server.createContext("/hk4e_global/combo/granter/login/v2/login", t -> {
|
|
|
|
// Get post data
|
|
|
|
ComboTokenReqJson requestData = null;
|
|
|
|
try {
|
|
|
|
String body = Utils.toString(t.getRequestBody());
|
|
|
|
requestData = getGsonFactory().fromJson(body, ComboTokenReqJson.class);
|
2022-04-22 01:04:03 +08:00
|
|
|
} catch (Exception ignored) { }
|
|
|
|
|
2022-04-18 13:11:27 +08:00
|
|
|
// Create response json
|
|
|
|
if (requestData == null || requestData.data == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LoginTokenData loginData = getGsonFactory().fromJson(requestData.data, LoginTokenData.class); // Get login data
|
|
|
|
ComboTokenResJson responseData = new ComboTokenResJson();
|
|
|
|
|
|
|
|
// Login
|
|
|
|
Account account = DatabaseHelper.getAccountById(loginData.uid);
|
|
|
|
|
|
|
|
// Test
|
|
|
|
if (account == null || !account.getSessionKey().equals(loginData.token)) {
|
|
|
|
responseData.retcode = -201;
|
|
|
|
responseData.message = "Wrong session key.";
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s failed to exchange combo token", t.getRemoteAddress()));
|
2022-04-18 13:11:27 +08:00
|
|
|
} else {
|
|
|
|
responseData.message = "OK";
|
|
|
|
responseData.data.open_id = loginData.uid;
|
|
|
|
responseData.data.combo_id = "157795300";
|
|
|
|
responseData.data.combo_token = account.generateLoginToken();
|
2022-04-21 22:22:40 +08:00
|
|
|
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info(String.format("[Dispatch] Client %s succeed to exchange combo token", t.getRemoteAddress()));
|
2022-04-18 13:11:27 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
responseJSON(t, responseData);
|
2022-04-17 20:43:07 +08:00
|
|
|
});
|
|
|
|
// Agreement and Protocol
|
|
|
|
server.createContext( // hk4e-sdk-os.hoyoverse.com
|
|
|
|
"/hk4e_global/mdk/agreement/api/getAgreementInfos",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"marketing_agreements\":[]}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-sdk-os.hoyoverse.com
|
|
|
|
"/hk4e_global/combo/granter/api/compareProtocolVersion",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"modified\":true,\"protocol\":{\"id\":0,\"app_id\":4,\"language\":\"en\",\"user_proto\":\"\",\"priv_proto\":\"\",\"major\":7,\"minimum\":0,\"create_time\":\"0\",\"teenager_proto\":\"\",\"third_proto\":\"\"}}}")
|
|
|
|
);
|
|
|
|
// Game data
|
|
|
|
server.createContext( // hk4e-api-os.hoyoverse.com
|
|
|
|
"/common/hk4e_global/announcement/api/getAlertPic",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"total\":0,\"list\":[]}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-api-os.hoyoverse.com
|
|
|
|
"/common/hk4e_global/announcement/api/getAlertAnn",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"alert\":false,\"alert_id\":0,\"remind\":true}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-api-os.hoyoverse.com
|
|
|
|
"/common/hk4e_global/announcement/api/getAnnList",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"list\":[],\"total\":0,\"type_list\":[],\"alert\":false,\"alert_id\":0,\"timezone\":0,\"t\":\"" + System.currentTimeMillis() + "\"}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-api-os-static.hoyoverse.com
|
|
|
|
"/common/hk4e_global/announcement/api/getAnnContent",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"list\":[],\"total\":0}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-sdk-os.hoyoverse.com
|
|
|
|
"/hk4e_global/mdk/shopwindow/shopwindow/listPriceTier",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"suggest_currency\":\"USD\",\"tiers\":[]}}")
|
|
|
|
);
|
|
|
|
// Captcha
|
|
|
|
server.createContext( // api-account-os.hoyoverse.com
|
|
|
|
"/account/risky/api/check",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"id\":\"c8820f246a5241ab9973f71df3ddd791\",\"action\":\"\",\"geetest\":{\"challenge\":\"\",\"gt\":\"\",\"new_captcha\":0,\"success\":1}}}")
|
|
|
|
);
|
|
|
|
// Config
|
|
|
|
server.createContext( // sdk-os-static.hoyoverse.com
|
|
|
|
"/combo/box/api/config/sdk/combo",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"vals\":{\"disable_email_bind_skip\":\"false\",\"email_bind_remind_interval\":\"7\",\"email_bind_remind\":\"true\"}}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-sdk-os-static.hoyoverse.com
|
|
|
|
"/hk4e_global/combo/granter/api/getConfig",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"protocol\":true,\"qr_enabled\":false,\"log_level\":\"INFO\",\"announce_url\":\"https://webstatic-sea.hoyoverse.com/hk4e/announcement/index.html?sdk_presentation_style=fullscreen\\u0026sdk_screen_transparent=true\\u0026game_biz=hk4e_global\\u0026auth_appid=announcement\\u0026game=hk4e#/\",\"push_alias_type\":2,\"disable_ysdk_guard\":false,\"enable_announce_pic_popup\":true}}")
|
|
|
|
);
|
|
|
|
server.createContext( // hk4e-sdk-os-static.hoyoverse.com
|
|
|
|
"/hk4e_global/mdk/shield/api/loadConfig",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"message\":\"OK\",\"data\":{\"id\":6,\"game_key\":\"hk4e_global\",\"client\":\"PC\",\"identity\":\"I_IDENTITY\",\"guest\":false,\"ignore_versions\":\"\",\"scene\":\"S_NORMAL\",\"name\":\"原神海外\",\"disable_regist\":false,\"enable_email_captcha\":false,\"thirdparty\":[\"fb\",\"tw\"],\"disable_mmt\":false,\"server_guest\":false,\"thirdparty_ignore\":{\"tw\":\"\",\"fb\":\"\"},\"enable_ps_bind_account\":false,\"thirdparty_login_configs\":{\"tw\":{\"token_type\":\"TK_GAME_TOKEN\",\"game_token_expires_in\":604800},\"fb\":{\"token_type\":\"TK_GAME_TOKEN\",\"game_token_expires_in\":604800}}}}")
|
|
|
|
);
|
|
|
|
// Test api?
|
|
|
|
server.createContext( // abtest-api-data-sg.hoyoverse.com
|
|
|
|
"/data_abtest_api/config/experiment/list",
|
|
|
|
new DispatchHttpJsonHandler("{\"retcode\":0,\"success\":true,\"message\":\"\",\"data\":[{\"code\":1000,\"type\":2,\"config_id\":\"14\",\"period_id\":\"6036_99\",\"version\":\"1\",\"configs\":{\"cardType\":\"old\"}}]}")
|
|
|
|
);
|
|
|
|
// Log Server
|
|
|
|
server.createContext( // log-upload-os.mihoyo.com
|
|
|
|
"/log/sdk/upload",
|
|
|
|
new DispatchHttpJsonHandler("{\"code\":0}")
|
|
|
|
);
|
|
|
|
server.createContext( // log-upload-os.mihoyo.com
|
|
|
|
"/sdk/upload",
|
|
|
|
new DispatchHttpJsonHandler("{\"code\":0}")
|
|
|
|
);
|
2022-04-19 20:48:03 +08:00
|
|
|
server.createContext( // /perf/config/verify?device_id=xxx&platform=x&name=xxx
|
|
|
|
"/perf/config/verify",
|
|
|
|
new DispatchHttpJsonHandler("{\"code\":0}")
|
|
|
|
);
|
2022-04-17 20:43:07 +08:00
|
|
|
|
|
|
|
// Logging servers
|
2022-04-21 21:41:30 +08:00
|
|
|
server.createContext( // overseauspider.yuanshen.com
|
2022-04-21 22:06:25 +08:00
|
|
|
"/log",
|
2022-04-17 20:43:07 +08:00
|
|
|
new DispatchHttpJsonHandler("{\"code\":0}")
|
|
|
|
);
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 21:41:30 +08:00
|
|
|
server.createContext( // log-upload-os.mihoyo.com
|
2022-04-21 22:06:25 +08:00
|
|
|
"/crash/dataUpload",
|
2022-04-17 20:43:07 +08:00
|
|
|
new DispatchHttpJsonHandler("{\"code\":0}")
|
|
|
|
);
|
2022-04-21 22:22:40 +08:00
|
|
|
server.createContext("/gacha", t -> responseHTML(t, "<!doctype html><html lang=\"en\"><head><title>Gacha</title></head><body></body></html>"));
|
2022-04-22 01:04:03 +08:00
|
|
|
|
2022-04-21 21:41:30 +08:00
|
|
|
// Start server
|
|
|
|
server.start();
|
2022-04-21 22:26:28 +08:00
|
|
|
Grasscutter.getLogger().info("[Dispatch] Dispatch server started on port " + getAddress().getPort());
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
private void responseJSON(HttpExchange t, Object data) throws IOException {
|
2022-04-21 22:06:25 +08:00
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
2022-04-21 22:22:40 +08:00
|
|
|
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();
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private Map<String, String> parseQueryString(String qs) {
|
2022-04-21 22:06:25 +08:00
|
|
|
Map<String, String> result = new HashMap<>();
|
2022-04-21 22:22:40 +08:00
|
|
|
if (qs == null) {
|
2022-04-21 22:06:25 +08:00
|
|
|
return result;
|
2022-04-21 22:22:40 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
|
|
|
int last = 0, next, l = qs.length();
|
|
|
|
while (last < l) {
|
|
|
|
next = qs.indexOf('&', last);
|
2022-04-21 22:22:40 +08:00
|
|
|
if (next == -1) {
|
2022-04-21 22:06:25 +08:00
|
|
|
next = l;
|
2022-04-21 22:22:40 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
|
|
|
|
if (next > last) {
|
|
|
|
int eqPos = qs.indexOf('=', last);
|
|
|
|
try {
|
2022-04-21 22:22:40 +08:00
|
|
|
if (eqPos < 0 || eqPos > next) {
|
2022-04-21 22:06:25 +08:00
|
|
|
result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), "");
|
2022-04-21 22:22:40 +08:00
|
|
|
} else {
|
2022-04-21 22:06:25 +08:00
|
|
|
result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8"));
|
2022-04-21 22:22:40 +08:00
|
|
|
}
|
2022-04-21 22:06:25 +08:00
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last = next + 1;
|
|
|
|
}
|
|
|
|
return result;
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
2022-04-21 18:04:00 +08:00
|
|
|
|
|
|
|
public static class RegionData {
|
|
|
|
|
|
|
|
QueryCurrRegionHttpRsp parsedRegionQuery;
|
|
|
|
String Base64;
|
|
|
|
|
|
|
|
public RegionData(QueryCurrRegionHttpRsp prq, String b64) {
|
|
|
|
this.parsedRegionQuery = prq;
|
|
|
|
this.Base64 = b64;
|
|
|
|
}
|
|
|
|
}
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|