mirror of
https://github.com/jie65535/gc-opencommand-plugin.git
synced 2025-12-15 19:31:35 +08:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12740c64e3 | |||
| a1ef41f373 | |||
| 1ec9e3f09a | |||
| 1a3b2e4904 | |||
| 957098a2cf | |||
|
|
910842c460 | ||
| 8b05c9aa10 | |||
| d5c29b61ff | |||
| f719ba97ad | |||
| 9a15414f3c | |||
| 95f688e970 | |||
| d291d2a8d8 | |||
| ff04a5f107 | |||
| d9b09e48ce | |||
| d190576c33 | |||
| 8cdfcf3dc3 |
71
README.md
71
README.md
@@ -1,24 +1,40 @@
|
||||
# gc-opencommand-plugin
|
||||
|
||||
中文 | [English](README_en-US.md)
|
||||
|
||||
一个为第三方客户端开放GC命令执行接口的插件
|
||||
|
||||
# 服务端安装
|
||||
## 服务端安装
|
||||
1. 在 [Release](https://github.com/jie65535/gc-opencommand-plugin/releases) 下载 `jar`
|
||||
2. 放入 `plugins` 文件夹即可
|
||||
> 注意,如果出现以下错误:
|
||||
> ```log
|
||||
> INFO:PluginManager Enabling plugin: opencommand-plugin
|
||||
> Exception in thread "main" java.lang.NoSuchMethodError: 'void emu.grasscutter.server.event.EventHandler.register(emu.grasscutter.plugin.Plugin)'
|
||||
> at com.github.jie65535.opencommand.OpenCommandPlugin.onEnable(OpenCommandPlugin.java:49)
|
||||
> at emu.grasscutter.plugin.PluginManager.lambda$enablePlugins$3(PluginManager.java:131)
|
||||
> ```
|
||||
> 请使用v1.2.1版本插件,因为该报错表示你的服务端是旧版!
|
||||
|
||||
# 构建说明
|
||||
## 控制台连接
|
||||
1. 首次启动时,会在 `plugins` 目录下生成一个 `opencommand-plugin` 目录,打开并编辑 `config.json`
|
||||
2. 设置 `consoleToken` 的值为你的连接秘钥,建议使用至少32字符的长随机字符串。
|
||||
3. 重新启动服务端即可生效配置
|
||||
4. 在客户端中选择控制台身份,并填写你的 `consoleToken` 即可以控制台身份运行指令
|
||||
|
||||
## 构建说明
|
||||
1. 克隆仓库
|
||||
2. 在目录下新建 `lib` 目录
|
||||
3. 将 `grasscutter-1.1.x-dev.jar` 放入 `lib` 目录
|
||||
4. `gradle build`
|
||||
|
||||
# 玩家使用流程
|
||||
## 玩家使用流程
|
||||
1. 在客户端中填写服务地址,确认是否支持
|
||||
2. 填写UID,发送验证码
|
||||
3. 将游戏内收到的**4位整数验证码**填入客户端校验
|
||||
4. 享受便利!
|
||||
|
||||
# 客户端请求流程
|
||||
## 客户端请求流程
|
||||
1. `ping` 确认是否支持 `opencommand` 插件
|
||||
2. `sendCode` 向指定玩家发送验证码(1分钟内不允许重发),保存返回的 `token`
|
||||
3. 使用 `token` 和**4位整数验证码**发送 `verify` 校验
|
||||
@@ -26,13 +42,28 @@
|
||||
|
||||
---
|
||||
|
||||
# API `/opencommand/api`
|
||||
## `config.json`
|
||||
```json
|
||||
{
|
||||
// 控制台连接令牌
|
||||
"consoleToken": "",
|
||||
// 验证码过期时间(秒)
|
||||
"codeExpirationTime_S": 60,
|
||||
// 临时令牌过期时间(秒)
|
||||
"tempTokenExpirationTime_S": 300,
|
||||
// 授权令牌最后使用过期时间(小时)
|
||||
"tokenLastUseExpirationTime_H": 48
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## API `/opencommand/api`
|
||||
示例
|
||||
```
|
||||
https://127.0.0.1/opencommand/api
|
||||
```
|
||||
|
||||
# Request 请求
|
||||
### Request 请求
|
||||
```java
|
||||
public final class JsonRequest {
|
||||
public String token = "";
|
||||
@@ -41,7 +72,7 @@ public final class JsonRequest {
|
||||
}
|
||||
```
|
||||
|
||||
# Response 响应
|
||||
### Response 响应
|
||||
```java
|
||||
public final class JsonResponse {
|
||||
public int retcode = 200;
|
||||
@@ -50,27 +81,27 @@ public final class JsonResponse {
|
||||
}
|
||||
```
|
||||
|
||||
# Actions 动作
|
||||
## `ping`
|
||||
### Actions 动作
|
||||
#### `ping`
|
||||
data = null
|
||||
|
||||
## `sendCode`
|
||||
### Request
|
||||
#### `sendCode`
|
||||
##### Request
|
||||
data = uid (int)
|
||||
### Response
|
||||
##### Response
|
||||
data = token (string)
|
||||
|
||||
## `verify` 要求 `token`
|
||||
### Request
|
||||
#### `verify` 要求 `token`
|
||||
##### Request
|
||||
data = code (int)
|
||||
### Response
|
||||
#### Success:
|
||||
##### Response
|
||||
###### Success:
|
||||
code = 200
|
||||
#### Verification failed:
|
||||
###### Verification failed:
|
||||
code = 400
|
||||
|
||||
## `command` 要求 `token`
|
||||
### Request
|
||||
#### `command` 要求 `token`
|
||||
##### Request
|
||||
data = command (string)
|
||||
### Response
|
||||
##### Response
|
||||
data = message (string)
|
||||
|
||||
96
README_en-US.md
Normal file
96
README_en-US.md
Normal file
@@ -0,0 +1,96 @@
|
||||
# gc-opencommand-plugin
|
||||
|
||||
[中文](README.md) | English
|
||||
|
||||
A plugin that opens the GC command execution interface for third-party clients
|
||||
|
||||
## Server installation
|
||||
1. Download the `jar` in [Release](https://github.com/jie65535/gc-opencommand-plugin/releases)
|
||||
2. Put it in the `plugins` folder
|
||||
|
||||
## Console connection
|
||||
1. When starting for the first time, a `opencommand-plugin` directory will be generated under the `plugins` directory, open and edit `config.json`
|
||||
2. Set the value of `consoleToken` to your connection key. It is recommended to use a long random string of at least 32 characters.
|
||||
3. Restart the server to take effect
|
||||
4. Select the console identity in the client, and fill in your `consoleToken` to run the command as the console identity
|
||||
|
||||
## Build
|
||||
1. `git clone https://github.com/jie65535/gc-opencommand-plugin`
|
||||
2. `cd gc-opencommand-plugin`
|
||||
3. `mkdir lib`
|
||||
4. `mv path/to/grasscutter-1.x.x-dev.jar ./lib`
|
||||
5. `gradle build`
|
||||
|
||||
## Player
|
||||
1. Fill in the service address in the client to confirm whether it supports
|
||||
2. Fill in the UID and send the verification code
|
||||
3. Fill in the **4-digit integer verification code** received in the game into the client verification
|
||||
4. Enjoy the convenience!
|
||||
|
||||
## Client request
|
||||
1. `ping` to confirm whether the `opencommand` plugin is supported
|
||||
2. `sendCode` sends a verification code to the specified player (re-send is not allowed within 1 minute), and save the returned `token`
|
||||
3. Send `verify` check using `token` and **4-digit integer verification code**
|
||||
4. If the verification is passed, you can use the `token` to execute the `command` action
|
||||
|
||||
---
|
||||
|
||||
## `config.json`
|
||||
```json
|
||||
{
|
||||
"consoleToken": "",
|
||||
"codeExpirationTime_S": 60,
|
||||
"tempTokenExpirationTime_S": 300,
|
||||
"tokenLastUseExpirationTime_H": 48
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## API `/opencommand/api`
|
||||
Example
|
||||
```
|
||||
https://127.0.0.1/opencommand/api
|
||||
```
|
||||
|
||||
## Request
|
||||
```java
|
||||
public final class JsonRequest {
|
||||
public String token = "";
|
||||
public String action = "";
|
||||
public Object data = null;
|
||||
}
|
||||
```
|
||||
|
||||
## Response
|
||||
```java
|
||||
public final class JsonResponse {
|
||||
public int retcode = 200;
|
||||
public String message = "success";
|
||||
public Object data;
|
||||
}
|
||||
```
|
||||
|
||||
## Actions
|
||||
### `ping`
|
||||
data = null
|
||||
|
||||
### `sendCode`
|
||||
#### Request
|
||||
data = uid (int)
|
||||
#### Response
|
||||
data = token (string)
|
||||
|
||||
### `verify`: Requires `token`
|
||||
#### Request
|
||||
data = code (int)
|
||||
#### Response
|
||||
##### Success:
|
||||
code = 200
|
||||
##### Verification failed:
|
||||
code = 400
|
||||
|
||||
### `command`: Requires `token`
|
||||
#### Request
|
||||
data = command (string)
|
||||
#### Response
|
||||
data = message (string)
|
||||
@@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
group 'com.github.jie65535.opencommand'
|
||||
version 'dev-1.1.0'
|
||||
version 'dev-1.2.4'
|
||||
|
||||
sourceCompatibility = 17
|
||||
targetCompatibility = 17
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.github.jie65535.opencommand;
|
||||
|
||||
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
|
||||
import emu.grasscutter.utils.MessageHandler;
|
||||
|
||||
public final class EventListeners {
|
||||
|
||||
private static MessageHandler consoleMessageHandler;
|
||||
public static void setConsoleMessageHandler(MessageHandler handler) {
|
||||
consoleMessageHandler = handler;
|
||||
}
|
||||
public static void onCommandResponse(ReceiveCommandFeedbackEvent event) {
|
||||
if (consoleMessageHandler != null && event.getPlayer() == null) {
|
||||
consoleMessageHandler.setMessage(event.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.JsonResponse;
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.command.CommandMap;
|
||||
import emu.grasscutter.server.http.Router;
|
||||
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.Int2ObjectOpenHashMap;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -52,13 +50,15 @@ public final class OpenCommandHandler implements Router {
|
||||
public static void handle(Request request, Response response) {
|
||||
// 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");
|
||||
if (req.action.equals("sendCode")) {
|
||||
int playerId = (int) req.data;
|
||||
var player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
var player = plugin.getServer().getPlayerByUid(playerId);
|
||||
if (player == null) {
|
||||
response.json(new JsonResponse(404, "Player Not Found."));
|
||||
} else {
|
||||
@@ -74,33 +74,59 @@ public final class OpenCommandHandler implements Router {
|
||||
if (token == null || token.isEmpty())
|
||||
token = Utils.bytesToHex(Crypto.createSessionKey(32));
|
||||
int code = Utils.randomRange(1000, 9999);
|
||||
codeExpireTime.put(playerId, new Date(now.getTime() + 60 * 1000));
|
||||
tokenExpireTime.put(token, new Date(now.getTime() + 5 * 60 * 1000));
|
||||
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);
|
||||
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));
|
||||
return;
|
||||
}
|
||||
return;
|
||||
} else if (req.action.equals("ping")) {
|
||||
response.json(new JsonResponse());
|
||||
return;
|
||||
}
|
||||
|
||||
// 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"));
|
||||
return;
|
||||
}
|
||||
|
||||
if (codes.containsKey(req.token)) {
|
||||
if (isConsole) {
|
||||
if (req.action.equals("verify")) {
|
||||
response.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));
|
||||
var resultCollector = new MessageHandler();
|
||||
EventListeners.setConsoleMessageHandler(resultCollector);
|
||||
CommandMap.getInstance().invoke(null, null, req.data.toString());
|
||||
response.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()));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
} else if (codes.containsKey(req.token)) {
|
||||
if (req.action.equals("verify")) {
|
||||
if (codes.get(req.token).equals(req.data)) {
|
||||
codes.remove(req.token);
|
||||
// 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());
|
||||
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", clients.get(req.token), request.ip()));
|
||||
} else {
|
||||
response.json(new JsonResponse(400, "Verification failed"));
|
||||
}
|
||||
@@ -109,9 +135,9 @@ public final class OpenCommandHandler implements Router {
|
||||
} else {
|
||||
if (req.action.equals("command")) {
|
||||
// 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 player = Grasscutter.getGameServer().getPlayerByUid(playerId);
|
||||
var player = plugin.getServer().getPlayerByUid(playerId);
|
||||
var command = req.data.toString();
|
||||
if (player == null) {
|
||||
response.json(new JsonResponse(404, "Player not found"));
|
||||
@@ -126,6 +152,7 @@ public final class OpenCommandHandler implements Router {
|
||||
CommandMap.getInstance().invoke(player, player, command);
|
||||
response.json(new JsonResponse(resultCollector.getMessage()));
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warn("Run command failed.", e);
|
||||
response.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
|
||||
} finally {
|
||||
player.setMessageHandler(null);
|
||||
|
||||
@@ -19,21 +19,65 @@ package com.github.jie65535.opencommand;
|
||||
|
||||
import emu.grasscutter.Grasscutter;
|
||||
import emu.grasscutter.plugin.Plugin;
|
||||
import emu.grasscutter.server.event.EventHandler;
|
||||
import emu.grasscutter.server.event.HandlerPriority;
|
||||
import emu.grasscutter.server.event.game.ReceiveCommandFeedbackEvent;
|
||||
|
||||
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;
|
||||
|
||||
public class OpenCommandPlugin extends Plugin {
|
||||
@Override
|
||||
public void onLoad() {
|
||||
|
||||
instance = this;
|
||||
loadConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
Grasscutter.getHttpServer().addRouter(OpenCommandHandler.class);
|
||||
Grasscutter.getLogger().info("[OpenCommand] Enabled");
|
||||
new EventHandler<>(ReceiveCommandFeedbackEvent.class)
|
||||
.priority(HandlerPriority.HIGH)
|
||||
.listener(EventListeners::onCommandResponse)
|
||||
.register(this);
|
||||
getHandle().addRouter(OpenCommandHandler.class);
|
||||
getLogger().info("[OpenCommand] Enabled");
|
||||
}
|
||||
|
||||
@Override
|
||||
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",
|
||||
"description": "Open command interface for third-party clients",
|
||||
"version": "dev-1.1.0",
|
||||
"version": "dev-1.2.4",
|
||||
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
||||
"authors": ["jie65535"]
|
||||
}
|
||||
Reference in New Issue
Block a user