mirror of
https://github.com/jie65535/gc-opencommand-plugin.git
synced 2025-06-02 17:49:12 +08:00
Update version to v1.2.0
Add config Add run console command
This commit is contained in:
parent
1df94e613e
commit
d190576c33
@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'com.github.jie65535.opencommand'
|
group 'com.github.jie65535.opencommand'
|
||||||
version 'dev-1.1.0'
|
version 'dev-1.2.0'
|
||||||
|
|
||||||
sourceCompatibility = 17
|
sourceCompatibility = 17
|
||||||
targetCompatibility = 17
|
targetCompatibility = 17
|
||||||
|
@ -0,0 +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;
|
||||||
|
|
||||||
|
public class OpenCommandConfig {
|
||||||
|
public String consoleToken = "";
|
||||||
|
public int codeExpirationTime_S = 60;
|
||||||
|
public int tempTokenExpirationTime_S = 300;
|
||||||
|
public int tokenLastUseExpirationTime_H = 48;
|
||||||
|
}
|
@ -19,7 +19,6 @@ 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 emu.grasscutter.Grasscutter;
|
|
||||||
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;
|
||||||
@ -32,7 +31,6 @@ import io.javalin.Javalin;
|
|||||||
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.io.IOException;
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@ -52,13 +50,15 @@ public final class OpenCommandHandler implements Router {
|
|||||||
public static void handle(Request request, Response response) {
|
public static void handle(Request request, Response response) {
|
||||||
// Trigger cleanup action
|
// Trigger cleanup action
|
||||||
cleanupExpiredData();
|
cleanupExpiredData();
|
||||||
|
var plugin = OpenCommandPlugin.getInstance();
|
||||||
|
var config = plugin.getConfig();
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
|
|
||||||
var req = request.body(JsonRequest.class);
|
var req = request.body(JsonRequest.class);
|
||||||
response.type("application/json");
|
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 = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
var player = plugin.getServer().getPlayerByUid(playerId);
|
||||||
if (player == null) {
|
if (player == null) {
|
||||||
response.json(new JsonResponse(404, "Player Not Found."));
|
response.json(new JsonResponse(404, "Player Not Found."));
|
||||||
} else {
|
} else {
|
||||||
@ -74,33 +74,53 @@ public final class OpenCommandHandler implements Router {
|
|||||||
if (token == null || token.isEmpty())
|
if (token == null || token.isEmpty())
|
||||||
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() + 60 * 1000));
|
codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L));
|
||||||
tokenExpireTime.put(token, new Date(now.getTime() + 5 * 60 * 1000));
|
tokenExpireTime.put(token, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L));
|
||||||
codes.put(token, code);
|
codes.put(token, code);
|
||||||
clients.put(token, playerId);
|
clients.put(token, playerId);
|
||||||
player.dropMessage("[Open Command] Verification code: {code}".replace("{code}", Integer.toString(code)));
|
player.dropMessage("[Open Command] Verification code: " + code);
|
||||||
|
|
||||||
response.json(new JsonResponse(token));
|
response.json(new JsonResponse(token));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
} else if (req.action.equals("ping")) {
|
} else if (req.action.equals("ping")) {
|
||||||
response.json(new JsonResponse());
|
response.json(new JsonResponse());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// token is required
|
// token is required
|
||||||
if (!clients.containsKey(req.token)) {
|
if (req.token == null || req.token.isEmpty()) {
|
||||||
|
response.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"));
|
response.json(new JsonResponse(401, "Unauthorized"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (codes.containsKey(req.token)) {
|
if (isConsole) {
|
||||||
|
if (req.action.equals("verify")) {
|
||||||
|
response.json(new JsonResponse());
|
||||||
|
return;
|
||||||
|
} else if (req.action.equals("command")) {
|
||||||
|
try {
|
||||||
|
plugin.getLogger().info(String.format("IP: %s run command in console > %s", request.ip(), req.data));
|
||||||
|
CommandMap.getInstance().invoke(null, null, req.data.toString());
|
||||||
|
response.json(new JsonResponse());
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().warn("Run command failed.", e);
|
||||||
|
response.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else if (codes.containsKey(req.token)) {
|
||||||
if (req.action.equals("verify")) {
|
if (req.action.equals("verify")) {
|
||||||
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() + 60 * 60 * 1000));
|
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L));
|
||||||
response.json(new JsonResponse());
|
response.json(new JsonResponse());
|
||||||
|
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip()));
|
||||||
} else {
|
} else {
|
||||||
response.json(new JsonResponse(400, "Verification failed"));
|
response.json(new JsonResponse(400, "Verification failed"));
|
||||||
}
|
}
|
||||||
@ -109,9 +129,9 @@ public final class OpenCommandHandler implements Router {
|
|||||||
} 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() + 4 * 60 * 60 * 1000));
|
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L));
|
||||||
var playerId = clients.get(req.token);
|
var playerId = clients.get(req.token);
|
||||||
var player = Grasscutter.getGameServer().getPlayerByUid(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"));
|
response.json(new JsonResponse(404, "Player not found"));
|
||||||
@ -126,6 +146,7 @@ public final class OpenCommandHandler implements Router {
|
|||||||
CommandMap.getInstance().invoke(player, player, command);
|
CommandMap.getInstance().invoke(player, player, command);
|
||||||
response.json(new JsonResponse(resultCollector.getMessage()));
|
response.json(new JsonResponse(resultCollector.getMessage()));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().warn("Run command failed.", e);
|
||||||
response.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
|
response.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
|
||||||
} finally {
|
} finally {
|
||||||
player.setMessageHandler(null);
|
player.setMessageHandler(null);
|
||||||
|
@ -20,20 +20,57 @@ package com.github.jie65535.opencommand;
|
|||||||
import emu.grasscutter.Grasscutter;
|
import emu.grasscutter.Grasscutter;
|
||||||
import emu.grasscutter.plugin.Plugin;
|
import emu.grasscutter.plugin.Plugin;
|
||||||
|
|
||||||
public class OpenCommandPlugin extends Plugin {
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public final class OpenCommandPlugin extends Plugin {
|
||||||
|
|
||||||
|
private static OpenCommandPlugin instance;
|
||||||
|
public static OpenCommandPlugin getInstance() { return instance; }
|
||||||
|
|
||||||
|
private OpenCommandConfig config;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
|
instance = this;
|
||||||
|
loadConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
Grasscutter.getHttpServer().addRouter(OpenCommandHandler.class);
|
getHandle().addRouter(OpenCommandHandler.class);
|
||||||
Grasscutter.getLogger().info("[OpenCommand] Enabled");
|
getLogger().info("[OpenCommand] Enabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
Grasscutter.getLogger().info("[OpenCommand] Disabled");
|
getLogger().info("[OpenCommand] Disabled");
|
||||||
|
}
|
||||||
|
|
||||||
|
public OpenCommandConfig getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadConfig() {
|
||||||
|
var configFile = new File(getDataFolder(), "config.json");
|
||||||
|
if (!configFile.exists()) {
|
||||||
|
config = new OpenCommandConfig();
|
||||||
|
try (var file = new FileWriter(configFile)){
|
||||||
|
file.write(Grasscutter.getGsonFactory().toJson(config));
|
||||||
|
} catch (IOException e) {
|
||||||
|
getLogger().error("Unable to write to config file.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
getLogger().error("Unable to save config file.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try (var file = new FileReader(configFile)) {
|
||||||
|
config = Grasscutter.getGsonFactory().fromJson(file, OpenCommandConfig.class);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
config = new OpenCommandConfig();
|
||||||
|
getLogger().error("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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.1.0",
|
"version": "dev-1.2.0",
|
||||||
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
||||||
"authors": ["jie65535"]
|
"authors": ["jie65535"]
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user