Update version to v1.4.0

Updated to be compatible with new http server
Add all code headers
This commit is contained in:
2022-09-02 20:18:31 +08:00
parent b3bc0f051a
commit 623856ca99
23 changed files with 418 additions and 105 deletions

View File

@ -4,7 +4,7 @@ plugins {
}
group 'com.github.jie65535.opencommand'
version 'dev-1.3.0'
version 'dev-1.4.0'
sourceCompatibility = 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;
import com.github.jie65535.opencommand.socket.SocketClient;
import com.github.jie65535.opencommand.socket.SocketUtils;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.player.Player;

View File

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

View File

@ -25,10 +25,8 @@ import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.MessageHandler;
import emu.grasscutter.utils.Utils;
import express.Express;
import express.http.Request;
import express.http.Response;
import io.javalin.Javalin;
import io.javalin.http.Context;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -40,8 +38,8 @@ import java.util.Map;
public final class OpenCommandHandler implements Router {
@Override
public void applyRoutes(Express express, Javalin javalin) {
express.post("/opencommand/api", OpenCommandHandler::handle);
public void applyRoutes(Javalin javalin) {
javalin.post("/opencommand/api", OpenCommandHandler::handle);
}
private static final Map<String, Integer> clients = new HashMap<>();
@ -49,25 +47,24 @@ public final class OpenCommandHandler implements Router {
private static final Map<String, Integer> codes = new HashMap<>();
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 config = plugin.getConfig();
var now = new Date();
var req = request.body(JsonRequest.class);
response.type("application/json");
var req = context.bodyAsClass(JsonRequest.class);
if (req.action.equals("sendCode")) {
int playerId = (int) req.data;
var player = plugin.getServer().getPlayerByUid(playerId);
if (player == null) {
response.json(new JsonResponse(404, "Player Not Found."));
context.json(new JsonResponse(404, "Player Not Found."));
} else {
if (codeExpireTime.containsKey(playerId)) {
var expireTime = codeExpireTime.get(playerId);
if (now.before(expireTime)) {
response.json(new JsonResponse(403, "Requests are too frequent"));
context.json(new JsonResponse(403, "Requests are too frequent"));
return;
}
}
@ -81,52 +78,52 @@ public final class OpenCommandHandler implements Router {
codes.put(token, code);
clients.put(token, playerId);
player.dropMessage("[Open Command] Verification code: " + code);
response.json(new JsonResponse(token));
context.json(new JsonResponse(token));
}
return;
} else if (req.action.equals("ping")) {
response.json(new JsonResponse());
context.json(new JsonResponse());
return;
} else if (req.action.equals("online")) {
var p = new ArrayList<String>();
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;
}
// token is required
if (req.token == null || req.token.isEmpty()) {
response.json(new JsonResponse(401, "Unauthorized"));
context.json(new JsonResponse(401, "Unauthorized"));
return;
}
var isConsole = req.token.equals(config.consoleToken);
if (!isConsole && !clients.containsKey(req.token)) {
response.json(new JsonResponse(401, "Unauthorized"));
context.json(new JsonResponse(401, "Unauthorized"));
return;
}
if (isConsole) {
if (req.action.equals("verify")) {
response.json(new JsonResponse());
context.json(new JsonResponse());
return;
} else if (req.action.equals("command")) {
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (plugin) {
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();
EventListeners.setConsoleMessageHandler(resultCollector);
CommandMap.getInstance().invoke(null, null, req.data.toString());
response.json(new JsonResponse(resultCollector.getMessage()));
context.json(new JsonResponse(resultCollector.getMessage()));
} catch (Exception e) {
plugin.getLogger().warn("Run command failed.", e);
EventListeners.setConsoleMessageHandler(null);
response.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
context.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
}
}
return;
} else if (req.action.equals("runmode")) {
response.json(new JsonResponse(200, "Success", 0));
context.json(new JsonResponse(200, "Success", 0));
return;
}
} else if (codes.containsKey(req.token)) {
@ -135,10 +132,10 @@ public final class OpenCommandHandler implements Router {
codes.remove(req.token);
// update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L));
response.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip()));
context.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), context.ip()));
} else {
response.json(new JsonResponse(400, "Verification failed"));
context.json(new JsonResponse(400, "Verification failed"));
}
return;
}
@ -150,7 +147,7 @@ public final class OpenCommandHandler implements Router {
var player = plugin.getServer().getPlayerByUid(playerId);
var command = req.data.toString();
if (player == null) {
response.json(new JsonResponse(404, "Player not found"));
context.json(new JsonResponse(404, "Player not found"));
return;
}
// Player MessageHandler do not support concurrency
@ -160,10 +157,10 @@ public final class OpenCommandHandler implements Router {
var resultCollector = new MessageHandler();
player.setMessageHandler(resultCollector);
CommandMap.getInstance().invoke(player, player, command);
response.json(new JsonResponse(resultCollector.getMessage()));
context.json(new JsonResponse(resultCollector.getMessage()));
} catch (Exception 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 {
player.setMessageHandler(null);
}
@ -171,7 +168,7 @@ public final class OpenCommandHandler implements Router {
return;
}
}
response.json(new JsonResponse(403, "forbidden"));
context.json(new JsonResponse(403, "forbidden"));
}
private static void cleanupExpiredData() {

View File

@ -29,10 +29,8 @@ import com.github.jie65535.opencommand.socket.packet.player.PlayerEnum;
import emu.grasscutter.server.http.Router;
import emu.grasscutter.utils.Crypto;
import emu.grasscutter.utils.Utils;
import express.Express;
import express.http.Request;
import express.http.Response;
import io.javalin.Javalin;
import io.javalin.http.Context;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
@ -43,8 +41,8 @@ import java.util.Map;
public final class OpenCommandOnlyHttpHandler implements Router {
@Override
public void applyRoutes(Express express, Javalin javalin) {
express.post("/opencommand/api", OpenCommandOnlyHttpHandler::handle);
public void applyRoutes(Javalin javalin) {
javalin.post("/opencommand/api", OpenCommandOnlyHttpHandler::handle);
}
private static final Map<String, Integer> clients = new HashMap<>();
@ -52,25 +50,24 @@ public final class OpenCommandOnlyHttpHandler implements Router {
private static final Map<String, Integer> codes = new HashMap<>();
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 config = plugin.getConfig();
var now = new Date();
var req = request.body(JsonRequest.class);
response.type("application/json");
var req = context.bodyAsClass(JsonRequest.class);
if (req.action.equals("sendCode")) {
int playerId = (int) req.data;
var player = SocketData.getPlayer(playerId);
if (player == null) {
response.json(new JsonResponse(404, "Player Not Found."));
context.json(new JsonResponse(404, "Player Not Found."));
} else {
if (codeExpireTime.containsKey(playerId)) {
var expireTime = codeExpireTime.get(playerId);
if (now.before(expireTime)) {
response.json(new JsonResponse(403, "Requests are too frequent"));
context.json(new JsonResponse(403, "Requests are too frequent"));
return;
}
}
@ -84,39 +81,39 @@ public final class OpenCommandOnlyHttpHandler implements Router {
codes.put(token, code);
clients.put(token, playerId);
Player.dropMessage(playerId, "[Open Command] Verification code: " + code);
response.json(new JsonResponse(token));
context.json(new JsonResponse(token));
}
return;
} else if (req.action.equals("ping")) {
response.json(new JsonResponse());
context.json(new JsonResponse());
return;
} else if (req.action.equals("online")) {
response.json(new JsonResponse(200, "Success", SocketData.getOnlinePlayer()));
context.json(new JsonResponse(200, "Success", SocketData.getOnlinePlayer()));
return;
}
// token is required
if (req.token == null || req.token.isEmpty()) {
response.json(new JsonResponse(401, "Unauthorized"));
context.json(new JsonResponse(401, "Unauthorized"));
return;
}
var isConsole = req.token.equals(config.consoleToken);
if (!isConsole && !clients.containsKey(req.token)) {
response.json(new JsonResponse(401, "Unauthorized"));
context.json(new JsonResponse(401, "Unauthorized"));
return;
}
if (isConsole) {
if (req.action.equals("verify")) {
response.json(new JsonResponse());
context.json(new JsonResponse());
return;
} else if (req.action.equals("command")) {
var server = SocketServer.getClientInfoByUuid(req.server);
if (server == null) {
response.json(new JsonResponse(404, "Server Not Found."));
context.json(new JsonResponse(404, "Server Not Found."));
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) {
@Override
public void run() {
@ -135,16 +132,16 @@ public final class OpenCommandOnlyHttpHandler implements Router {
SocketServer.sendPacketAndWait(server.ip, new RunConsoleCommand(req.data.toString()), wait);
var data = wait.getData();
if (data == null) {
response.json(new JsonResponse(408, "Timeout"));
context.json(new JsonResponse(408, "Timeout"));
return;
}
response.json(new JsonResponse(data.code, data.message, data.data));
context.json(new JsonResponse(data.code, data.message, data.data));
return;
} else if (req.action.equals("server")) {
response.json(new JsonResponse(200, "Success", SocketServer.getOnlineClient()));
context.json(new JsonResponse(200, "Success", SocketServer.getOnlineClient()));
return;
} else if (req.action.equals("runmode")) {
response.json(new JsonResponse(200, "Success", 1));
context.json(new JsonResponse(200, "Success", 1));
return;
}
} else if (codes.containsKey(req.token)) {
@ -153,10 +150,10 @@ public final class OpenCommandOnlyHttpHandler implements Router {
codes.remove(req.token);
// update token expire time
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L));
response.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip()));
context.json(new JsonResponse());
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), context.ip()));
} else {
response.json(new JsonResponse(400, "Verification failed"));
context.json(new JsonResponse(400, "Verification failed"));
}
return;
}
@ -187,21 +184,21 @@ public final class OpenCommandOnlyHttpHandler implements Router {
player.data = command;
if (!SocketServer.sendUidPacketAndWait(playerId, player, socketDataWait)) {
response.json(new JsonResponse(404, "Player Not Found."));
context.json(new JsonResponse(404, "Player Not Found."));
return;
}
HttpPacket httpPacket = socketDataWait.getData();
if (httpPacket == null) {
response.json(new JsonResponse(500, "error", "Wait timeout"));
context.json(new JsonResponse(500, "error", "Wait timeout"));
return;
}
response.json(new JsonResponse(httpPacket.code, httpPacket.message));
context.json(new JsonResponse(httpPacket.code, httpPacket.message));
return;
}
}
response.json(new JsonResponse(403, "forbidden"));
context.json(new JsonResponse(403, "forbidden"));
}
private static void cleanupExpiredData() {

View File

@ -26,11 +26,9 @@ import emu.grasscutter.server.event.HandlerPriority;
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
import emu.grasscutter.server.event.player.PlayerJoinEvent;
import emu.grasscutter.server.event.player.PlayerQuitEvent;
import emu.grasscutter.utils.JsonUtils;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
public final class OpenCommandPlugin extends Plugin {
@ -42,10 +40,15 @@ public final class OpenCommandPlugin extends Plugin {
private OpenCommandConfig config;
private Grasscutter.ServerRunMode runMode = Grasscutter.ServerRunMode.HYBRID;
@Override
public void onLoad() {
instance = this;
// 加载配置
loadConfig();
// 启动Socket
startSocket();
}
@Override
@ -54,7 +57,7 @@ public final class OpenCommandPlugin extends Plugin {
.priority(HandlerPriority.HIGH)
.listener(EventListeners::onCommandResponse)
.register(this);
if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
if (runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
// 仅运行游戏服务器时注册玩家加入和离开事件
new EventHandler<>(PlayerJoinEvent.class)
.priority(HandlerPriority.HIGH)
@ -64,7 +67,7 @@ public final class OpenCommandPlugin extends Plugin {
.priority(HandlerPriority.HIGH)
.listener(EventListeners::onPlayerQuit)
.register(this);
} else if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) {
} else if (runMode == Grasscutter.ServerRunMode.DISPATCH_ONLY) {
getHandle().addRouter(OpenCommandOnlyHttpHandler.class);
} else {
getHandle().addRouter(OpenCommandHandler.class);
@ -86,29 +89,32 @@ public final class OpenCommandPlugin extends Plugin {
if (!configFile.exists()) {
config = new OpenCommandConfig();
try (var file = new FileWriter(configFile)) {
file.write(Grasscutter.getGsonFactory().toJson(config));
file.write(JsonUtils.encode(config));
} catch (IOException e) {
getLogger().error("[OpenCommand] Unable to write to config file.");
} catch (Exception e) {
getLogger().error("[OpenCommand] Unable to save config file.");
}
} else {
try (var file = new FileReader(configFile)) {
config = Grasscutter.getGsonFactory().fromJson(file, OpenCommandConfig.class);
try {
config = JsonUtils.loadToClass(configFile.getAbsolutePath(), OpenCommandConfig.class);
} catch (Exception exception) {
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.");
}
}
// 启动Socket
startSocket();
try {
runMode = Grasscutter.getConfig().server.runMode;
} catch (Exception ex) {
getLogger().warn("[OpenCommand] Failed to load server configuration, default HYBRID mode is being used.");
}
}
private void startSocket() {
if (Grasscutter.getConfig().server.runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
if (runMode == Grasscutter.ServerRunMode.GAME_ONLY) {
getLogger().info("[OpenCommand] Starting socket client...");
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...");
try {
SocketServer.startServer();

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;
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;
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 emu.grasscutter.Grasscutter;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.utils.JsonUtils;
import emu.grasscutter.utils.MessageHandler;
import org.slf4j.Logger;
@ -122,18 +140,17 @@ public class SocketClient {
@Override
public void run() {
//noinspection InfiniteLoopStatement
while (true) {
try {
if (exit) {
return;
}
String data = SocketUtils.readString(is);
Packet packet = Grasscutter.getGsonFactory().fromJson(data, Packet.class);
Packet packet = JsonUtils.decode(data, Packet.class);
switch (packet.type) {
// 玩家类
case Player:
var player = Grasscutter.getGsonFactory().fromJson(packet.data, Player.class);
var player = JsonUtils.decode(packet.data, Player.class);
switch (player.type) {
// 运行命令
case RunCommand -> {
@ -170,7 +187,7 @@ public class SocketClient {
}
break;
case RunConsoleCommand:
var consoleCommand = Grasscutter.getGsonFactory().fromJson(packet.data, RunConsoleCommand.class);
var consoleCommand = JsonUtils.decode(packet.data, RunConsoleCommand.class);
var plugin = OpenCommandPlugin.getInstance();
//noinspection SynchronizationOnLocalVariableOrMethodParameter
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;
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;
// 异步等待数据返回

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;
import com.github.jie65535.opencommand.OpenCommandPlugin;
import com.github.jie65535.opencommand.socket.packet.*;
import com.github.jie65535.opencommand.socket.packet.player.PlayerList;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
import org.slf4j.Logger;
import java.io.IOException;
@ -190,13 +207,12 @@ public class SocketServer {
@Override
public void run() {
// noinspection InfiniteLoopStatement
while (true) {
try {
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) {
AuthPacket authPacket = Grasscutter.getGsonFactory().fromJson(packet.data, AuthPacket.class);
AuthPacket authPacket = JsonUtils.decode(packet.data, AuthPacket.class);
if (authPacket.token.equals(token)) {
auth = true;
displayName = authPacket.displayName;
@ -217,12 +233,12 @@ public class SocketServer {
switch (packet.type) {
// 缓存玩家列表
case PlayerList -> {
PlayerList playerList = Grasscutter.getGsonFactory().fromJson(packet.data, PlayerList.class);
PlayerList playerList = JsonUtils.decode(packet.data, PlayerList.class);
SocketData.playerList.put(address, playerList);
}
// Http信息返回
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);
if (socketWait == null) {
mLogger.error("[OpenCommand] HttpPacket: " + packet.packetID + " not found");
@ -232,9 +248,7 @@ public class SocketServer {
socketDataWaitList.remove(packet.packetID);
}
// 心跳包
case HeartBeat -> {
clientTimeout.put(address, 0);
}
case HeartBeat -> clientTimeout.put(address, 0);
}
} catch (Throwable e) {
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;
import com.github.jie65535.opencommand.OpenCommandPlugin;
import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.Packet;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
import java.io.IOException;
import java.io.InputStream;
@ -26,7 +42,7 @@ public class SocketUtils {
packet.type = bPacket.getType();
packet.data = bPacket.getPacket();
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.add(packet.packetID);
list.add(Grasscutter.getGsonFactory().toJson(packet));
list.add(JsonUtils.encode(packet));
return list;
}
@ -59,7 +75,7 @@ public class SocketUtils {
packet.type = bPacket.getType();
packet.data = bPacket.getPacket();
packet.packetID = packetID;
return Grasscutter.getGsonFactory().toJson(packet);
return JsonUtils.encode(packet);
}
/**
@ -117,8 +133,7 @@ public class SocketUtils {
} catch (IOException e) {
e.printStackTrace();
}
String s = new String(sByte);
return s;
return new String(sByte);
}
/**

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;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
public class AuthPacket extends BasePacket {
public String token;
@ -13,7 +30,7 @@ public class AuthPacket extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@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;
// 基本数据包

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;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
// 心跳包
public class HeartBeat extends BasePacket {
@ -12,7 +29,7 @@ public class HeartBeat extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@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;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
// http返回数据
public class HttpPacket extends BasePacket {
@ -24,7 +41,7 @@ public class HttpPacket extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@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;
// 数据包结构

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;
// 数据包类型列表

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;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
public class RunConsoleCommand extends BasePacket {
public String command;
@ -11,7 +28,7 @@ public class RunConsoleCommand extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@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;
import com.github.jie65535.opencommand.socket.SocketServer;
import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.PacketEnum;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.utils.JsonUtils;
// 玩家操作类
public class Player extends BasePacket {
@ -13,7 +30,7 @@ public class Player extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@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.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;
import com.github.jie65535.opencommand.socket.packet.BasePacket;
import com.github.jie65535.opencommand.socket.packet.PacketEnum;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.utils.JsonUtils;
import java.util.ArrayList;
import java.util.HashMap;
@ -18,7 +34,7 @@ public class PlayerList extends BasePacket {
@Override
public String getPacket() {
return Grasscutter.getGsonFactory().toJson(this);
return JsonUtils.encode(this);
}
@Override

View File

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