5 Commits

Author SHA1 Message Date
1d03d3e476 Update version to v1.5.0 2022-10-06 13:27:17 +08:00
ef885af137 Impl token persistence (#19) 2022-10-06 13:27:04 +08:00
b70b73667d Add author @方块君 2022-09-02 20:20:47 +08:00
623856ca99 Update version to v1.4.0
Updated to be compatible with new http server
Add all code headers
2022-09-02 20:18:31 +08:00
方块君
b3bc0f051a Fix send code failed 2022-07-26 23:39:52 +08:00
25 changed files with 564 additions and 160 deletions

View File

@@ -4,7 +4,7 @@ plugins {
} }
group 'com.github.jie65535.opencommand' group 'com.github.jie65535.opencommand'
version 'dev-1.3.0' version 'dev-1.5.0'
sourceCompatibility = 17 sourceCompatibility = 17
targetCompatibility = 17 targetCompatibility = 17

View File

@@ -1,7 +1,23 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand; package com.github.jie65535.opencommand;
import com.github.jie65535.opencommand.socket.SocketClient; import com.github.jie65535.opencommand.socket.SocketClient;
import com.github.jie65535.opencommand.socket.SocketUtils;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList; import com.github.jie65535.opencommand.socket.packet.player.PlayerList;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;

View File

@@ -18,12 +18,43 @@
package com.github.jie65535.opencommand; package com.github.jie65535.opencommand;
public class OpenCommandConfig { public class OpenCommandConfig {
/**
* 控制台 Token
*/
public String consoleToken = ""; public String consoleToken = "";
/**
* 验证码过期时间(单位秒)
*/
public int codeExpirationTime_S = 60; public int codeExpirationTime_S = 60;
/**
* 临时Token过期时间单位秒
*/
public int tempTokenExpirationTime_S = 300; public int tempTokenExpirationTime_S = 300;
/**
* Token 最后使用过期时间(单位小时)
*/
public int tokenLastUseExpirationTime_H = 48; public int tokenLastUseExpirationTime_H = 48;
/**
* Socket 端口
*/
public int socketPort = 5746; public int socketPort = 5746;
/**
* Socket Token
*/
public String socketToken = ""; public String socketToken = "";
/**
* Socket 主机地址
*/
public String socketHost = "127.0.0.1"; public String socketHost = "127.0.0.1";
/**
* Socket 显示名称
*/
public String socketDisplayName = ""; public String socketDisplayName = "";
} }

View File

@@ -0,0 +1,42 @@
package com.github.jie65535.opencommand;
import com.github.jie65535.opencommand.model.Client;
import java.util.Date;
import java.util.Vector;
/**
* 插件持久化数据
*/
public class OpenCommandData {
/**
* 连接的客户端列表
*/
public Vector<Client> clients = new Vector<>();
/**
* 通过令牌获取客户端
* @param token 令牌
* @return 客户端对象若未找到返回null
*/
public Client getClientByToken(String token) {
for (var c : clients) {
if (c.token.equals(token))
return c;
}
return null;
}
/**
* 移除所有过期的客户端
*/
public void removeExpiredClients() {
var now = new Date();
clients.removeIf(client -> client.tokenExpireTime.before(now));
}
public void addClient(Client client) {
clients.add(client);
}
}

View File

@@ -19,16 +19,15 @@ package com.github.jie65535.opencommand;
import com.github.jie65535.opencommand.json.JsonRequest; import com.github.jie65535.opencommand.json.JsonRequest;
import com.github.jie65535.opencommand.json.JsonResponse; import com.github.jie65535.opencommand.json.JsonResponse;
import com.github.jie65535.opencommand.model.Client;
import com.github.jie65535.opencommand.socket.SocketData; import com.github.jie65535.opencommand.socket.SocketData;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.MessageHandler; import emu.grasscutter.utils.MessageHandler;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import express.Express;
import express.http.Request;
import express.http.Response;
import io.javalin.Javalin; import io.javalin.Javalin;
import io.javalin.http.Context;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@@ -40,34 +39,33 @@ import java.util.Map;
public final class OpenCommandHandler implements Router { public final class OpenCommandHandler implements Router {
@Override @Override
public void applyRoutes(Express express, Javalin javalin) { public void applyRoutes(Javalin javalin) {
express.post("/opencommand/api", OpenCommandHandler::handle); javalin.post("/opencommand/api", OpenCommandHandler::handle);
} }
private static final Map<String, Integer> clients = new HashMap<>();
private static final Map<String, Date> tokenExpireTime = new HashMap<>();
private static final Map<String, Integer> codes = new HashMap<>(); private static final Map<String, Integer> codes = new HashMap<>();
private static final Int2ObjectMap<Date> codeExpireTime = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap<Date> codeExpireTime = new Int2ObjectOpenHashMap<>();
public static void handle(Request request, Response response) { public static void handle(Context context) {
// Trigger cleanup action
cleanupExpiredData();
var plugin = OpenCommandPlugin.getInstance(); var plugin = OpenCommandPlugin.getInstance();
var config = plugin.getConfig(); var config = plugin.getConfig();
var data = plugin.getData();
var now = new Date(); var now = new Date();
// Trigger cleanup action
cleanupExpiredCodes();
data.removeExpiredClients();
var req = request.body(JsonRequest.class); var req = context.bodyAsClass(JsonRequest.class);
response.type("application/json");
if (req.action.equals("sendCode")) { if (req.action.equals("sendCode")) {
int playerId = (int) req.data; int playerId = (int) req.data;
var player = plugin.getServer().getPlayerByUid(playerId); var player = plugin.getServer().getPlayerByUid(playerId);
if (player == null) { if (player == null) {
response.json(new JsonResponse(404, "Player Not Found.")); context.json(new JsonResponse(404, "Player Not Found."));
} else { } else {
if (codeExpireTime.containsKey(playerId)) { if (codeExpireTime.containsKey(playerId)) {
var expireTime = codeExpireTime.get(playerId); var expireTime = codeExpireTime.get(playerId);
if (now.before(expireTime)) { if (now.before(expireTime)) {
response.json(new JsonResponse(403, "Requests are too frequent")); context.json(new JsonResponse(403, "Requests are too frequent"));
return; return;
} }
} }
@@ -77,56 +75,56 @@ public final class OpenCommandHandler implements Router {
token = Utils.bytesToHex(Crypto.createSessionKey(32)); token = Utils.bytesToHex(Crypto.createSessionKey(32));
int code = Utils.randomRange(1000, 9999); int code = Utils.randomRange(1000, 9999);
codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L)); codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L));
tokenExpireTime.put(token, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L));
codes.put(token, code); codes.put(token, code);
clients.put(token, playerId); data.addClient(new Client(token, playerId, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L)));
player.dropMessage("[Open Command] Verification code: " + code); player.dropMessage("[Open Command] Verification code: " + code);
response.json(new JsonResponse(token)); context.json(new JsonResponse(token));
} }
return; return;
} else if (req.action.equals("ping")) { } else if (req.action.equals("ping")) {
response.json(new JsonResponse()); context.json(new JsonResponse());
return; return;
} else if (req.action.equals("online")) { } else if (req.action.equals("online")) {
var p = new ArrayList<String>(); var p = new ArrayList<String>();
plugin.getServer().getPlayers().forEach((uid, player) -> p.add(player.getNickname())); plugin.getServer().getPlayers().forEach((uid, player) -> p.add(player.getNickname()));
response.json(new JsonResponse(200, "Success", new SocketData.OnlinePlayer(p))); context.json(new JsonResponse(200, "Success", new SocketData.OnlinePlayer(p)));
return; return;
} }
// token is required // token is required
if (req.token == null || req.token.isEmpty()) { if (req.token == null || req.token.isEmpty()) {
response.json(new JsonResponse(401, "Unauthorized")); context.json(new JsonResponse(401, "Unauthorized"));
return; return;
} }
var isConsole = req.token.equals(config.consoleToken); var isConsole = req.token.equals(config.consoleToken);
if (!isConsole && !clients.containsKey(req.token)) { var client = data.getClientByToken(req.token);
response.json(new JsonResponse(401, "Unauthorized")); if (!isConsole && client == null) {
context.json(new JsonResponse(401, "Unauthorized"));
return; return;
} }
if (isConsole) { if (isConsole) {
if (req.action.equals("verify")) { if (req.action.equals("verify")) {
response.json(new JsonResponse()); context.json(new JsonResponse());
return; return;
} else if (req.action.equals("command")) { } else if (req.action.equals("command")) {
//noinspection SynchronizationOnLocalVariableOrMethodParameter //noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (plugin) { synchronized (plugin) {
try { try {
plugin.getLogger().info(String.format("IP: %s run command in console > %s", request.ip(), req.data)); plugin.getLogger().info(String.format("IP: %s run command in console > %s", context.ip(), req.data));
var resultCollector = new MessageHandler(); var resultCollector = new MessageHandler();
EventListeners.setConsoleMessageHandler(resultCollector); EventListeners.setConsoleMessageHandler(resultCollector);
CommandMap.getInstance().invoke(null, null, req.data.toString()); CommandMap.getInstance().invoke(null, null, req.data.toString());
response.json(new JsonResponse(resultCollector.getMessage())); context.json(new JsonResponse(resultCollector.getMessage()));
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().warn("Run command failed.", e); plugin.getLogger().warn("Run command failed.", e);
EventListeners.setConsoleMessageHandler(null); EventListeners.setConsoleMessageHandler(null);
response.json(new JsonResponse(500, "error", e.getLocalizedMessage())); context.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
} }
} }
return; return;
} else if (req.action.equals("runmode")) { } else if (req.action.equals("runmode")) {
response.json(new JsonResponse(200, "Success", 0)); context.json(new JsonResponse(200, "Success", 0));
return; return;
} }
} else if (codes.containsKey(req.token)) { } else if (codes.containsKey(req.token)) {
@@ -134,23 +132,23 @@ public final class OpenCommandHandler implements Router {
if (codes.get(req.token).equals(req.data)) { if (codes.get(req.token).equals(req.data)) {
codes.remove(req.token); codes.remove(req.token);
// update token expire time // update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L)); client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
response.json(new JsonResponse()); context.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip())); plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", client.playerId, context.ip()));
plugin.saveData();
} else { } else {
response.json(new JsonResponse(400, "Verification failed")); context.json(new JsonResponse(400, "Verification failed"));
} }
return; return;
} }
} else { } else {
if (req.action.equals("command")) { if (req.action.equals("command")) {
// update token expire time // update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L)); client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
var playerId = clients.get(req.token); var player = plugin.getServer().getPlayerByUid(client.playerId);
var player = plugin.getServer().getPlayerByUid(playerId);
var command = req.data.toString(); var command = req.data.toString();
if (player == null) { if (player == null) {
response.json(new JsonResponse(404, "Player not found")); context.json(new JsonResponse(404, "Player not found"));
return; return;
} }
// Player MessageHandler do not support concurrency // Player MessageHandler do not support concurrency
@@ -160,10 +158,10 @@ public final class OpenCommandHandler implements Router {
var resultCollector = new MessageHandler(); var resultCollector = new MessageHandler();
player.setMessageHandler(resultCollector); player.setMessageHandler(resultCollector);
CommandMap.getInstance().invoke(player, player, command); CommandMap.getInstance().invoke(player, player, command);
response.json(new JsonResponse(resultCollector.getMessage())); context.json(new JsonResponse(resultCollector.getMessage()));
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().warn("Run command failed.", e); plugin.getLogger().warn("Run command failed.", e);
response.json(new JsonResponse(500, "error", e.getLocalizedMessage())); context.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
} finally { } finally {
player.setMessageHandler(null); player.setMessageHandler(null);
} }
@@ -171,21 +169,13 @@ public final class OpenCommandHandler implements Router {
return; return;
} }
} }
response.json(new JsonResponse(403, "forbidden")); context.json(new JsonResponse(403, "forbidden"));
} }
private static void cleanupExpiredData() { private static void cleanupExpiredCodes() {
var now = new Date(); var now = new Date();
codeExpireTime.int2ObjectEntrySet().removeIf(entry -> entry.getValue().before(now)); codeExpireTime.int2ObjectEntrySet().removeIf(entry -> entry.getValue().before(now));
if (codeExpireTime.isEmpty())
var it = tokenExpireTime.entrySet().iterator(); codes.clear();
while (it.hasNext()) {
var entry = it.next();
if (entry.getValue().before(now)) {
it.remove();
// remove expired token
clients.remove(entry.getKey());
}
}
} }
} }

View File

@@ -19,6 +19,7 @@ package com.github.jie65535.opencommand;
import com.github.jie65535.opencommand.json.JsonRequest; import com.github.jie65535.opencommand.json.JsonRequest;
import com.github.jie65535.opencommand.json.JsonResponse; import com.github.jie65535.opencommand.json.JsonResponse;
import com.github.jie65535.opencommand.model.Client;
import com.github.jie65535.opencommand.socket.SocketData; import com.github.jie65535.opencommand.socket.SocketData;
import com.github.jie65535.opencommand.socket.SocketDataWait; import com.github.jie65535.opencommand.socket.SocketDataWait;
import com.github.jie65535.opencommand.socket.SocketServer; import com.github.jie65535.opencommand.socket.SocketServer;
@@ -26,54 +27,48 @@ import com.github.jie65535.opencommand.socket.packet.HttpPacket;
import com.github.jie65535.opencommand.socket.packet.RunConsoleCommand; import com.github.jie65535.opencommand.socket.packet.RunConsoleCommand;
import com.github.jie65535.opencommand.socket.packet.player.Player; import com.github.jie65535.opencommand.socket.packet.player.Player;
import com.github.jie65535.opencommand.socket.packet.player.PlayerEnum; import com.github.jie65535.opencommand.socket.packet.player.PlayerEnum;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.server.http.Router; import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.MessageHandler;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
import express.Express;
import express.http.Request;
import express.http.Response;
import io.javalin.Javalin; import io.javalin.Javalin;
import io.javalin.http.Context;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.FutureTask;
public final class OpenCommandOnlyHttpHandler implements Router { public final class OpenCommandOnlyHttpHandler implements Router {
@Override @Override
public void applyRoutes(Express express, Javalin javalin) { public void applyRoutes(Javalin javalin) {
express.post("/opencommand/api", OpenCommandOnlyHttpHandler::handle); javalin.post("/opencommand/api", OpenCommandOnlyHttpHandler::handle);
} }
private static final Map<String, Integer> clients = new HashMap<>();
private static final Map<String, Date> tokenExpireTime = new HashMap<>();
private static final Map<String, Integer> codes = new HashMap<>(); private static final Map<String, Integer> codes = new HashMap<>();
private static final Int2ObjectMap<Date> codeExpireTime = new Int2ObjectOpenHashMap<>(); private static final Int2ObjectMap<Date> codeExpireTime = new Int2ObjectOpenHashMap<>();
public static void handle(Request request, Response response) { public static void handle(Context context) {
// Trigger cleanup action
cleanupExpiredData();
var plugin = OpenCommandPlugin.getInstance(); var plugin = OpenCommandPlugin.getInstance();
var config = plugin.getConfig(); var config = plugin.getConfig();
var data = plugin.getData();
var now = new Date(); var now = new Date();
// Trigger cleanup action
cleanupExpiredCodes();
data.removeExpiredClients();
var req = request.body(JsonRequest.class); var req = context.bodyAsClass(JsonRequest.class);
response.type("application/json");
if (req.action.equals("sendCode")) { if (req.action.equals("sendCode")) {
int playerId = (int) req.data; int playerId = (int) req.data;
var player = SocketData.getPlayer(playerId); var player = SocketData.getPlayer(playerId);
if (player == null) { if (player == null) {
response.json(new JsonResponse(404, "Player Not Found.")); context.json(new JsonResponse(404, "Player Not Found."));
} else { } else {
if (codeExpireTime.containsKey(playerId)) { if (codeExpireTime.containsKey(playerId)) {
var expireTime = codeExpireTime.get(playerId); var expireTime = codeExpireTime.get(playerId);
if (now.before(expireTime)) { if (now.before(expireTime)) {
response.json(new JsonResponse(403, "Requests are too frequent")); context.json(new JsonResponse(403, "Requests are too frequent"));
return; return;
} }
} }
@@ -83,43 +78,43 @@ public final class OpenCommandOnlyHttpHandler implements Router {
token = Utils.bytesToHex(Crypto.createSessionKey(32)); token = Utils.bytesToHex(Crypto.createSessionKey(32));
int code = Utils.randomRange(1000, 9999); int code = Utils.randomRange(1000, 9999);
codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L)); codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L));
tokenExpireTime.put(token, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L));
codes.put(token, code); codes.put(token, code);
clients.put(token, playerId); data.addClient(new Client(token, playerId, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L)));
Player.dropMessage(playerId, "[Open Command] Verification code: " + code); Player.dropMessage(playerId, "[Open Command] Verification code: " + code);
response.json(new JsonResponse(token)); context.json(new JsonResponse(token));
} }
return; return;
} else if (req.action.equals("ping")) { } else if (req.action.equals("ping")) {
response.json(new JsonResponse()); context.json(new JsonResponse());
return; return;
} else if (req.action.equals("online")) { } else if (req.action.equals("online")) {
response.json(new JsonResponse(200, "Success", SocketData.getOnlinePlayer())); context.json(new JsonResponse(200, "Success", SocketData.getOnlinePlayer()));
return; return;
} }
// token is required // token is required
if (req.token == null || req.token.isEmpty()) { if (req.token == null || req.token.isEmpty()) {
response.json(new JsonResponse(401, "Unauthorized")); context.json(new JsonResponse(401, "Unauthorized"));
return; return;
} }
var isConsole = req.token.equals(config.consoleToken); var isConsole = req.token.equals(config.consoleToken);
if (!isConsole && !clients.containsKey(req.token)) { var client = data.getClientByToken(req.token);
response.json(new JsonResponse(401, "Unauthorized")); if (!isConsole && client == null) {
context.json(new JsonResponse(401, "Unauthorized"));
return; return;
} }
if (isConsole) { if (isConsole) {
if (req.action.equals("verify")) { if (req.action.equals("verify")) {
response.json(new JsonResponse()); context.json(new JsonResponse());
return; return;
} else if (req.action.equals("command")) { } else if (req.action.equals("command")) {
var server = SocketServer.getClientInfoByUuid(req.server); var server = SocketServer.getClientInfoByUuid(req.server);
if (server == null) { if (server == null) {
response.json(new JsonResponse(404, "Server Not Found.")); context.json(new JsonResponse(404, "Server Not Found."));
return; return;
} }
plugin.getLogger().info(String.format("IP: %s run command in console > %s", request.ip(), req.data)); plugin.getLogger().info(String.format("IP: %s run command in console > %s", context.ip(), req.data));
var wait = new SocketDataWait<HttpPacket>(2000L) { var wait = new SocketDataWait<HttpPacket>(2000L) {
@Override @Override
public void run() { public void run() {
@@ -136,18 +131,18 @@ public final class OpenCommandOnlyHttpHandler implements Router {
}; };
SocketServer.sendPacketAndWait(server.ip, new RunConsoleCommand(req.data.toString()), wait); SocketServer.sendPacketAndWait(server.ip, new RunConsoleCommand(req.data.toString()), wait);
var data = wait.getData(); var packet = wait.getData();
if (data == null) { if (packet == null) {
response.json(new JsonResponse(408, "Timeout")); context.json(new JsonResponse(408, "Timeout"));
return; return;
} }
response.json(new JsonResponse(data.code, data.message, data.data)); context.json(new JsonResponse(packet.code, packet.message, packet.data));
return; return;
} else if (req.action.equals("server")) { } else if (req.action.equals("server")) {
response.json(new JsonResponse(200, "Success", SocketServer.getOnlineClient())); context.json(new JsonResponse(200, "Success", SocketServer.getOnlineClient()));
return; return;
} else if (req.action.equals("runmode")) { } else if (req.action.equals("runmode")) {
response.json(new JsonResponse(200, "Success", 1)); context.json(new JsonResponse(200, "Success", 1));
return; return;
} }
} else if (codes.containsKey(req.token)) { } else if (codes.containsKey(req.token)) {
@@ -155,11 +150,12 @@ public final class OpenCommandOnlyHttpHandler implements Router {
if (codes.get(req.token).equals(req.data)) { if (codes.get(req.token).equals(req.data)) {
codes.remove(req.token); codes.remove(req.token);
// update token expire time // update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L)); client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
response.json(new JsonResponse()); context.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip())); plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", client.playerId, context.ip()));
plugin.saveData();
} else { } else {
response.json(new JsonResponse(400, "Verification failed")); context.json(new JsonResponse(400, "Verification failed"));
} }
return; return;
} }
@@ -181,44 +177,35 @@ public final class OpenCommandOnlyHttpHandler implements Router {
}; };
// update token expire time // update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L)); client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
var playerId = clients.get(req.token);
var command = req.data.toString(); var command = req.data.toString();
var player = new Player(); var player = new Player();
player.uid = playerId; player.uid = client.playerId;
player.type = PlayerEnum.RunCommand; player.type = PlayerEnum.RunCommand;
player.data = command; player.data = command;
if (!SocketServer.sendUidPacket(playerId, player, socketDataWait)) { if (!SocketServer.sendUidPacketAndWait(client.playerId, player, socketDataWait)) {
response.json(new JsonResponse(404, "Player Not Found.")); context.json(new JsonResponse(404, "Player Not Found."));
return; return;
} }
HttpPacket httpPacket = socketDataWait.getData(); HttpPacket httpPacket = socketDataWait.getData();
if (httpPacket == null) { if (httpPacket == null) {
response.json(new JsonResponse(500, "error", "Wait timeout")); context.json(new JsonResponse(500, "error", "Wait timeout"));
return; return;
} }
response.json(new JsonResponse(httpPacket.code, httpPacket.message)); context.json(new JsonResponse(httpPacket.code, httpPacket.message));
return; return;
} }
} }
response.json(new JsonResponse(403, "forbidden")); context.json(new JsonResponse(403, "forbidden"));
} }
private static void cleanupExpiredData() { private static void cleanupExpiredCodes() {
var now = new Date(); var now = new Date();
codeExpireTime.int2ObjectEntrySet().removeIf(entry -> entry.getValue().before(now)); codeExpireTime.int2ObjectEntrySet().removeIf(entry -> entry.getValue().before(now));
if (codeExpireTime.isEmpty())
var it = tokenExpireTime.entrySet().iterator(); codes.clear();
while (it.hasNext()) {
var entry = it.next();
if (entry.getValue().before(now)) {
it.remove();
// remove expired token
clients.remove(entry.getKey());
}
}
} }
} }

View File

@@ -26,11 +26,9 @@ import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent; import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
import emu.grasscutter.server.event.player.PlayerJoinEvent; import emu.grasscutter.server.event.player.PlayerJoinEvent;
import emu.grasscutter.server.event.player.PlayerQuitEvent; import emu.grasscutter.server.event.player.PlayerQuitEvent;
import emu.grasscutter.utils.JsonUtils;
import java.io.File; import java.io.*;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public final class OpenCommandPlugin extends Plugin { public final class OpenCommandPlugin extends Plugin {
@@ -42,10 +40,19 @@ public final class OpenCommandPlugin extends Plugin {
private OpenCommandConfig config; private OpenCommandConfig config;
private OpenCommandData data;
private Grasscutter.ServerRunMode runMode = Grasscutter.ServerRunMode.HYBRID;
@Override @Override
public void onLoad() { public void onLoad() {
instance = this; instance = this;
// 加载配置
loadConfig(); loadConfig();
// 加载数据
loadData();
// 启动Socket
startSocket();
} }
@Override @Override
@@ -54,7 +61,7 @@ public final class OpenCommandPlugin extends Plugin {
.priority(HandlerPriority.HIGH) .priority(HandlerPriority.HIGH)
.listener(EventListeners::onCommandResponse) .listener(EventListeners::onCommandResponse)
.register(this); .register(this);
if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) { if (runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
// 仅运行游戏服务器时注册玩家加入和离开事件 // 仅运行游戏服务器时注册玩家加入和离开事件
new EventHandler<>(PlayerJoinEvent.class) new EventHandler<>(PlayerJoinEvent.class)
.priority(HandlerPriority.HIGH) .priority(HandlerPriority.HIGH)
@@ -64,7 +71,7 @@ public final class OpenCommandPlugin extends Plugin {
.priority(HandlerPriority.HIGH) .priority(HandlerPriority.HIGH)
.listener(EventListeners::onPlayerQuit) .listener(EventListeners::onPlayerQuit)
.register(this); .register(this);
} else if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) { } else if (runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) {
getHandle().addRouter(OpenCommandOnlyHttpHandler.class); getHandle().addRouter(OpenCommandOnlyHttpHandler.class);
} else { } else {
getHandle().addRouter(OpenCommandHandler.class); getHandle().addRouter(OpenCommandHandler.class);
@@ -74,6 +81,7 @@ public final class OpenCommandPlugin extends Plugin {
@Override @Override
public void onDisable() { public void onDisable() {
saveData();
getLogger().info("[OpenCommand] Disabled"); getLogger().info("[OpenCommand] Disabled");
} }
@@ -81,34 +89,66 @@ public final class OpenCommandPlugin extends Plugin {
return config; return config;
} }
public OpenCommandData getData() {
return data;
}
private void loadConfig() { private void loadConfig() {
var configFile = new File(getDataFolder(), "config.json"); var configFile = new File(getDataFolder(), "config.json");
if (!configFile.exists()) { if (!configFile.exists()) {
config = new OpenCommandConfig(); config = new OpenCommandConfig();
try (var file = new FileWriter(configFile)) { try (var file = new FileWriter(configFile)) {
file.write(Grasscutter.getGsonFactory().toJson(config)); file.write(JsonUtils.encode(config));
} catch (IOException e) { } catch (IOException e) {
getLogger().error("[OpenCommand] Unable to write to config file."); getLogger().error("[OpenCommand] Unable to write to config file.");
} catch (Exception e) { } catch (Exception e) {
getLogger().error("[OpenCommand] Unable to save config file."); getLogger().error("[OpenCommand] Unable to save config file.");
} }
} else { } else {
try (var file = new FileReader(configFile)) { try {
config = Grasscutter.getGsonFactory().fromJson(file, OpenCommandConfig.class); config = JsonUtils.loadToClass(configFile.getAbsolutePath(), OpenCommandConfig.class);
} catch (Exception exception) { } catch (Exception exception) {
config = new OpenCommandConfig(); config = new OpenCommandConfig();
getLogger().error("[OpenCommand] There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json."); getLogger().error("[OpenCommand] There was an error while trying to load the configuration from config.json. Please make sure that there are no syntax errors. If you want to start with a default configuration, delete your existing config.json.");
} }
} }
// 启动Socket try {
startSocket(); runMode = Grasscutter.getConfig().server.runMode;
} catch (Exception ex) {
getLogger().warn("[OpenCommand] Failed to load server configuration, default HYBRID mode is being used.");
}
}
private void loadData() {
var dataFile = new File(getDataFolder(), "data.json");
if (!dataFile.exists()) {
data = new OpenCommandData();
saveData();
} else {
try {
data = JsonUtils.loadToClass(dataFile.getAbsolutePath(), OpenCommandData.class);
} catch (Exception exception) {
data = new OpenCommandData();
getLogger().error("[OpenCommand] There was an error while trying to load the data from data.json. Please make sure that there are no syntax errors. If you want to start with a default data, delete your existing data.json.");
}
}
}
public void saveData() {
try (var file = new FileWriter(new File(getDataFolder(), "data.json"))) {
file.write(JsonUtils.encode(data));
} catch (IOException e) {
getLogger().error("[OpenCommand] Unable to write to data file.");
} catch (Exception e) {
getLogger().error("[OpenCommand] Unable to save data file.");
}
} }
private void startSocket() { private void startSocket() {
if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) { if (runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
getLogger().info("[OpenCommand] Starting socket client..."); getLogger().info("[OpenCommand] Starting socket client...");
SocketClient.connectServer(); SocketClient.connectServer();
} else if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) { } else if (runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) {
getLogger().info("[OpenCommand] Starting socket server..."); getLogger().info("[OpenCommand] Starting socket server...");
try { try {
SocketServer.startServer(); SocketServer.startServer();

View File

@@ -0,0 +1,16 @@
package com.github.jie65535.opencommand.model;
import java.util.Date;
public final class Client {
public String token;
public Integer playerId;
public Date tokenExpireTime;
public Client(String token, Integer playerId, Date tokenExpireTime) {
this.token = token;
this.playerId = playerId;
this.tokenExpireTime = tokenExpireTime;
}
}

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
public class ClientInfo { public class ClientInfo {

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
import com.github.jie65535.opencommand.EventListeners; import com.github.jie65535.opencommand.EventListeners;
@@ -8,6 +25,7 @@ import com.github.jie65535.opencommand.socket.packet.player.Player;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList; import com.github.jie65535.opencommand.socket.packet.player.PlayerList;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.MessageHandler; import emu.grasscutter.utils.MessageHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -122,18 +140,17 @@ public class SocketClient {
@Override @Override
public void run() { public void run() {
//noinspection InfiniteLoopStatement
while (true) { while (true) {
try { try {
if (exit) { if (exit) {
return; return;
} }
String data = SocketUtils.readString(is); String data = SocketUtils.readString(is);
Packet packet = Grasscutter.getGsonFactory().fromJson(data, Packet.class); Packet packet = JsonUtils.decode(data, Packet.class);
switch (packet.type) { switch (packet.type) {
// 玩家类 // 玩家类
case Player: case Player:
var player = Grasscutter.getGsonFactory().fromJson(packet.data, Player.class); var player = JsonUtils.decode(packet.data, Player.class);
switch (player.type) { switch (player.type) {
// 运行命令 // 运行命令
case RunCommand -> { case RunCommand -> {
@@ -170,7 +187,7 @@ public class SocketClient {
} }
break; break;
case RunConsoleCommand: case RunConsoleCommand:
var consoleCommand = Grasscutter.getGsonFactory().fromJson(packet.data, RunConsoleCommand.class); var consoleCommand = JsonUtils.decode(packet.data, RunConsoleCommand.class);
var plugin = OpenCommandPlugin.getInstance(); var plugin = OpenCommandPlugin.getInstance();
//noinspection SynchronizationOnLocalVariableOrMethodParameter //noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (plugin) { synchronized (plugin) {

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList; import com.github.jie65535.opencommand.socket.packet.player.PlayerList;

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
// 异步等待数据返回 // 异步等待数据返回

View File

@@ -1,9 +1,26 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
import com.github.jie65535.opencommand.OpenCommandPlugin; import com.github.jie65535.opencommand.OpenCommandPlugin;
import com.github.jie65535.opencommand.socket.packet.*; import com.github.jie65535.opencommand.socket.packet.*;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList; import com.github.jie65535.opencommand.socket.packet.player.PlayerList;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import java.io.IOException; import java.io.IOException;
@@ -52,9 +69,9 @@ public class SocketServer {
// 向全部客户端发送数据 // 向全部客户端发送数据
public static boolean sendAllPacket(BasePacket packet) { public static boolean sendAllPacket(BasePacket packet) {
var p = SocketUtils.getPacket(packet); var p = SocketUtils.getPacket(packet);
HashMap<String, ClientThread> old = (HashMap<String, ClientThread>) clientList.clone(); HashMap<String, ClientInfo> old = (HashMap<String, ClientInfo>) clientList.clone();
for (var client : old.entrySet()) { for (var client : old.entrySet()) {
if (!client.getValue().sendPacket(p)) { if (!client.getValue().clientThread.sendPacket(p)) {
mLogger.warn("[OpenCommand] Send packet to client {} failed", client.getKey()); mLogger.warn("[OpenCommand] Send packet to client {} failed", client.getKey());
clientList.remove(client.getKey()); clientList.remove(client.getKey());
} }
@@ -90,8 +107,24 @@ public class SocketServer {
return false; return false;
} }
public static boolean sendUidPacket(Integer playerId, BasePacket player) {
var p = SocketUtils.getPacket(player);
var clientID = SocketData.getPlayerInServer(playerId);
if (clientID == null) return false;
var client = clientList.get(clientID);
if (client != null) {
if (!client.clientThread.sendPacket(p)) {
mLogger.warn("[OpenCommand] Send packet to client {} failed", clientID);
clientList.remove(clientID);
return false;
}
return true;
}
return false;
}
// 根据Uid发送到相应的客户端异步返回数据 // 根据Uid发送到相应的客户端异步返回数据
public static boolean sendUidPacket(Integer playerId, BasePacket player, SocketDataWait<?> socketDataWait) { public static boolean sendUidPacketAndWait(Integer playerId, BasePacket player, SocketDataWait<?> socketDataWait) {
var p = SocketUtils.getPacketAndPackID(player); var p = SocketUtils.getPacketAndPackID(player);
var clientID = SocketData.getPlayerInServer(playerId); var clientID = SocketData.getPlayerInServer(playerId);
if (clientID == null) return false; if (clientID == null) return false;
@@ -174,13 +207,12 @@ public class SocketServer {
@Override @Override
public void run() { public void run() {
// noinspection InfiniteLoopStatement
while (true) { while (true) {
try { try {
String data = SocketUtils.readString(is); String data = SocketUtils.readString(is);
Packet packet = Grasscutter.getGsonFactory().fromJson(data, Packet.class); Packet packet = JsonUtils.decode(data, Packet.class);
if (packet.type == PacketEnum.AuthPacket) { if (packet.type == PacketEnum.AuthPacket) {
AuthPacket authPacket = Grasscutter.getGsonFactory().fromJson(packet.data, AuthPacket.class); AuthPacket authPacket = JsonUtils.decode(packet.data, AuthPacket.class);
if (authPacket.token.equals(token)) { if (authPacket.token.equals(token)) {
auth = true; auth = true;
displayName = authPacket.displayName; displayName = authPacket.displayName;
@@ -201,12 +233,12 @@ public class SocketServer {
switch (packet.type) { switch (packet.type) {
// 缓存玩家列表 // 缓存玩家列表
case PlayerList -> { case PlayerList -> {
PlayerList playerList = Grasscutter.getGsonFactory().fromJson(packet.data, PlayerList.class); PlayerList playerList = JsonUtils.decode(packet.data, PlayerList.class);
SocketData.playerList.put(address, playerList); SocketData.playerList.put(address, playerList);
} }
// Http信息返回 // Http信息返回
case HttpPacket -> { case HttpPacket -> {
HttpPacket httpPacket = Grasscutter.getGsonFactory().fromJson(packet.data, HttpPacket.class); HttpPacket httpPacket = JsonUtils.decode(packet.data, HttpPacket.class);
var socketWait = socketDataWaitList.get(packet.packetID); var socketWait = socketDataWaitList.get(packet.packetID);
if (socketWait == null) { if (socketWait == null) {
mLogger.error("[OpenCommand] HttpPacket: " + packet.packetID + " not found"); mLogger.error("[OpenCommand] HttpPacket: " + packet.packetID + " not found");
@@ -216,9 +248,7 @@ public class SocketServer {
socketDataWaitList.remove(packet.packetID); socketDataWaitList.remove(packet.packetID);
} }
// 心跳包 // 心跳包
case HeartBeat -> { case HeartBeat -> clientTimeout.put(address, 0);
clientTimeout.put(address, 0);
}
} }
} catch (Throwable e) { } catch (Throwable e) {
e.printStackTrace(); e.printStackTrace();

View File

@@ -1,9 +1,25 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket; package com.github.jie65535.opencommand.socket;
import com.github.jie65535.opencommand.OpenCommandPlugin;
import com.github.jie65535.opencommand.socket.packet.BasePacket; import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.Packet; import com.github.jie65535.opencommand.socket.packet.Packet;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@@ -26,7 +42,7 @@ public class SocketUtils {
packet.type = bPacket.getType(); packet.type = bPacket.getType();
packet.data = bPacket.getPacket(); packet.data = bPacket.getPacket();
packet.packetID = UUID.randomUUID().toString(); packet.packetID = UUID.randomUUID().toString();
return Grasscutter.getGsonFactory().toJson(packet); return JsonUtils.encode(packet);
} }
/** /**
@@ -43,7 +59,7 @@ public class SocketUtils {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
list.add(packet.packetID); list.add(packet.packetID);
list.add(Grasscutter.getGsonFactory().toJson(packet)); list.add(JsonUtils.encode(packet));
return list; return list;
} }
@@ -59,7 +75,7 @@ public class SocketUtils {
packet.type = bPacket.getType(); packet.type = bPacket.getType();
packet.data = bPacket.getPacket(); packet.data = bPacket.getPacket();
packet.packetID = packetID; packet.packetID = packetID;
return Grasscutter.getGsonFactory().toJson(packet); return JsonUtils.encode(packet);
} }
/** /**
@@ -117,8 +133,7 @@ public class SocketUtils {
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
String s = new String(sByte); return new String(sByte);
return s;
} }
/** /**

View File

@@ -1,6 +1,23 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
public class AuthPacket extends BasePacket { public class AuthPacket extends BasePacket {
public String token; public String token;
@@ -13,7 +30,7 @@ public class AuthPacket extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
// 基本数据包 // 基本数据包

View File

@@ -1,6 +1,23 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
// 心跳包 // 心跳包
public class HeartBeat extends BasePacket { public class HeartBeat extends BasePacket {
@@ -12,7 +29,7 @@ public class HeartBeat extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override

View File

@@ -1,6 +1,23 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
// http返回数据 // http返回数据
public class HttpPacket extends BasePacket { public class HttpPacket extends BasePacket {
@@ -24,7 +41,7 @@ public class HttpPacket extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
// 数据包结构 // 数据包结构

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
// 数据包类型列表 // 数据包类型列表

View File

@@ -1,6 +1,23 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet; package com.github.jie65535.opencommand.socket.packet;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
public class RunConsoleCommand extends BasePacket { public class RunConsoleCommand extends BasePacket {
public String command; public String command;
@@ -11,7 +28,7 @@ public class RunConsoleCommand extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override

View File

@@ -1,9 +1,26 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet.player; package com.github.jie65535.opencommand.socket.packet.player;
import com.github.jie65535.opencommand.socket.SocketServer; import com.github.jie65535.opencommand.socket.SocketServer;
import com.github.jie65535.opencommand.socket.packet.BasePacket; import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.PacketEnum; import com.github.jie65535.opencommand.socket.packet.PacketEnum;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
// 玩家操作类 // 玩家操作类
public class Player extends BasePacket { public class Player extends BasePacket {
@@ -13,7 +30,7 @@ public class Player extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override
@@ -26,6 +43,6 @@ public class Player extends BasePacket {
p.type = PlayerEnum.DropMessage; p.type = PlayerEnum.DropMessage;
p.uid = uid; p.uid = uid;
p.data = str; p.data = str;
SocketServer.sendAllPacket(p); SocketServer.sendUidPacket(uid, p);
} }
} }

View File

@@ -1,3 +1,20 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet.player; package com.github.jie65535.opencommand.socket.packet.player;
// 玩家操作列表 // 玩家操作列表

View File

@@ -1,9 +1,25 @@
/*
* gc-opencommand
* Copyright (C) 2022 jie65535
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.jie65535.opencommand.socket.packet.player; package com.github.jie65535.opencommand.socket.packet.player;
import com.github.jie65535.opencommand.socket.packet.BasePacket; import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.PacketEnum; import com.github.jie65535.opencommand.socket.packet.PacketEnum;
import emu.grasscutter.Grasscutter; import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.game.player.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@@ -18,7 +34,7 @@ public class PlayerList extends BasePacket {
@Override @Override
public String getPacket() { public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this); return JsonUtils.encode(this);
} }
@Override @Override

View File

@@ -1,7 +1,7 @@
{ {
"name": "opencommand-plugin", "name": "opencommand-plugin",
"description": "Open command interface for third-party clients", "description": "Open command interface for third-party clients",
"version": "dev-1.3.0", "version": "dev-1.5.0",
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin", "mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
"authors": ["jie65535"] "authors": ["jie65535", "方块君"]
} }