DispatchServer.java 18.4 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;

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;
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.ComboTokenReqJson.LoginTokenData;
import emu.grasscutter.utils.FileUtils;
import emu.grasscutter.utils.Utils;

import com.sun.net.httpserver.HttpServer;

KingRainbow44's avatar
KingRainbow44 committed
46
public final class DispatchServer {
Melledy's avatar
Melledy committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
	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 = "";
	
	public DispatchServer() {
		this.address = new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, Grasscutter.getConfig().DispatchServerPort);
		this.gson = new GsonBuilder().create();
		
		this.loadQueries();
		this.initRegion();
	}
	
	public InetSocketAddress getAddress() {
		return address;
	}
	
	public Gson getGsonFactory() {
		return gson;
	}

	public QueryCurrRegionHttpRsp getCurrRegion() {
		return currRegion;
	}
	
	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 {
			Grasscutter.getLogger().warn("query_region_list not found! Using default region list.");
		}
	
		file = new File(Grasscutter.getConfig().DATA_FOLDER + "query_cur_region.txt");
		if (file.exists()) {
			query_cur_region = new String(FileUtils.read(file));
		} else {
			Grasscutter.getLogger().warn("query_cur_region not found! Using default current region.");
		}
	}

	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);
			
			RegionSimpleInfo server = RegionSimpleInfo.newBuilder()
					.setName("os_usa")
					.setTitle(Grasscutter.getConfig().GameServerName)
					.setType("DEV_PUBLIC")
					.setDispatchUrl("https://" + Grasscutter.getConfig().DispatchServerIp + ":" + getAddress().getPort() + "/query_cur_region")
					.build();
			
			RegionSimpleInfo serverTest2 = RegionSimpleInfo.newBuilder()
					.setName("os_euro")
					.setTitle("Grasscutter")
					.setType("DEV_PUBLIC")
					.setDispatchUrl("https://" + Grasscutter.getConfig().DispatchServerIp + ":" + getAddress().getPort() + "/query_cur_region")
					.build();
				
			QueryRegionListHttpRsp regionList = QueryRegionListHttpRsp.newBuilder()
				.addServers(server)
				.addServers(serverTest2)
				.setClientSecretKey(rl.getClientSecretKey())
			    .setClientCustomConfigEncrypted(rl.getClientCustomConfigEncrypted())
			    .setEnableLoginPc(true)
				.build();
						
			RegionInfo currentRegion = regionQuery.getRegionInfo().toBuilder()
					.setIp(Grasscutter.getConfig().GameServerIp)
					.setPort(Grasscutter.getConfig().GameServerPort)
					.setSecretKey(ByteString.copyFrom(FileUtils.read(Grasscutter.getConfig().KEY_FOLDER + "dispatchSeed.bin")))
					.build();
			
			QueryCurrRegionHttpRsp parsedRegionQuery = regionQuery.toBuilder().setRegionInfo(currentRegion).build();

			this.regionListBase64 = Base64.getEncoder().encodeToString(regionList.toByteString().toByteArray());
			this.regionCurrentBase64 = Base64.getEncoder().encodeToString(parsedRegionQuery.toByteString().toByteArray());
			this.currRegion = parsedRegionQuery;
		} catch (Exception e) {
KingRainbow44's avatar
KingRainbow44 committed
137
			Grasscutter.getLogger().error("Error while initializing region info!", e);
Melledy's avatar
Melledy committed
138
139
140
141
		}
	}

	public void start() throws Exception {
KingRainbow44's avatar
KingRainbow44 committed
142
		HttpsServer server = HttpsServer.create(getAddress(), 0);
Melledy's avatar
Melledy committed
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
		SSLContext sslContext = SSLContext.getInstance("TLS");
		
		try (FileInputStream fis = new FileInputStream(Grasscutter.getConfig().DispatchServerKeystorePath)) {
			char[] keystorePassword = Grasscutter.getConfig().DispatchServerKeystorePassword.toCharArray();
			KeyStore ks = KeyStore.getInstance("PKCS12");
			ks.load(fis, keystorePassword);
			KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
			kmf.init(ks, keystorePassword);
			
			sslContext.init(kmf.getKeyManagers(), null, null);
			
			server.setHttpsConfigurator(new HttpsConfigurator(sslContext));
		} catch (Exception e) {
			Grasscutter.getLogger().error("No SSL cert found!");
			return;
		}
		
KingRainbow44's avatar
KingRainbow44 committed
160
161
162
163
164
165
166
167
168
169
		server.createContext("/", t -> {
			//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();
Melledy's avatar
Melledy committed
170
171
172
		});
		
		// Dispatch
KingRainbow44's avatar
KingRainbow44 committed
173
174
175
176
177
178
179
180
181
182
183
184
		server.createContext("/query_region_list", t -> {
			// Log
			Grasscutter.getLogger().info("Client request: query_region_list");
			// Create a response form the request query parameters
			String response = 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();
Melledy's avatar
Melledy committed
185
		});
KingRainbow44's avatar
KingRainbow44 committed
186
187
188
189
190
191
192
193
		server.createContext("/query_cur_region", t -> {
			// Log
			Grasscutter.getLogger().info("Client request: query_cur_region");
			// 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;
Melledy's avatar
Melledy committed
194
			}
KingRainbow44's avatar
KingRainbow44 committed
195
196
197
198
199
200
201
			// 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();
Melledy's avatar
Melledy committed
202
203
		});
		// Login via account
KingRainbow44's avatar
KingRainbow44 committed
204
205
206
207
208
209
210
		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);
			} catch (Exception e) {
Melledy's avatar
Melledy committed
211
212
				
			}
KingRainbow44's avatar
KingRainbow44 committed
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
			// Create response json
			if (requestData == null) {
				return;
			}
			LoginResultJson responseData = new LoginResultJson();
			
			// Login
			Account account = DatabaseHelper.getAccountByName(requestData.account);
			
			// Test
			if (account == null) {
				responseData.retcode = -201;
				responseData.message = "Username not found.";
			} else {
				responseData.message = "OK";
				responseData.data.account.uid = account.getId();
				responseData.data.account.token = account.generateSessionKey();
				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();
Melledy's avatar
Melledy committed
242
243
		});
		// Login via token
KingRainbow44's avatar
KingRainbow44 committed
244
245
246
247
248
249
250
		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);
			} catch (Exception e) {
Melledy's avatar
Melledy committed
251
252
				
			}
KingRainbow44's avatar
KingRainbow44 committed
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
			// Create response json
			if (requestData == null) {
				return;
			}
			LoginResultJson responseData = new LoginResultJson();

			// 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";
			} else {
				responseData.message = "OK";
				responseData.data.account.uid = requestData.uid;
				responseData.data.account.token = requestData.token;
				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();
Melledy's avatar
Melledy committed
282
283
		});
		// Exchange for combo token
KingRainbow44's avatar
KingRainbow44 committed
284
285
286
287
288
289
290
		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);
			} catch (Exception e) {
Melledy's avatar
Melledy committed
291
292
				
			}
KingRainbow44's avatar
KingRainbow44 committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
			// 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.";
			} else {
				responseData.message = "OK";
				responseData.data.open_id = loginData.uid;
				responseData.data.combo_id = "157795300";
				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();
Melledy's avatar
Melledy committed
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
		});
		// 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}")
		);
		// Start server
		server.start();
		Grasscutter.getLogger().info("Dispatch server started on port " + getAddress().getPort());
		
		// Logging servers
		HttpServer overseaLogServer = HttpServer.create(new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, 8888), 0);
		overseaLogServer.createContext( // overseauspider.yuanshen.com
				"/log", 
				new DispatchHttpJsonHandler("{\"code\":0}")
		);
		overseaLogServer.start();
		Grasscutter.getLogger().info("Log server (overseauspider) started on port " + 8888);
		
		HttpServer uploadLogServer = HttpServer.create(new InetSocketAddress(Grasscutter.getConfig().DispatchServerIp, 80), 0);
		uploadLogServer.createContext( // log-upload-os.mihoyo.com
				"/crash/dataUpload", 
				new DispatchHttpJsonHandler("{\"code\":0}")
		);
KingRainbow44's avatar
KingRainbow44 committed
404
405
406
407
408
409
410
411
412
413
		uploadLogServer.createContext("/gacha", t -> {
			//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();
Melledy's avatar
Melledy committed
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
		});
		uploadLogServer.start();
		Grasscutter.getLogger().info("Log server (log-upload-os) started on port " + 80);
	}
	
	private Map<String, String> parseQueryString(String qs) {
	    Map<String, String> result = new HashMap<>();
	    if (qs == null)
	        return result;

	    int last = 0, next, l = qs.length();
	    while (last < l) {
	        next = qs.indexOf('&', last);
	        if (next == -1)
	            next = l;

	        if (next > last) {
	            int eqPos = qs.indexOf('=', last);
	            try {
	                if (eqPos < 0 || eqPos > next)
	                    result.put(URLDecoder.decode(qs.substring(last, next), "utf-8"), "");
	                else
	                    result.put(URLDecoder.decode(qs.substring(last, eqPos), "utf-8"), URLDecoder.decode(qs.substring(eqPos + 1, next), "utf-8"));
	            } catch (UnsupportedEncodingException e) {
	                throw new RuntimeException(e); // will never happen, utf-8 support is mandatory for java
	            }
	        }
	        last = next + 1;
	    }
	    return result;
	}
}