mirror of
https://github.com/jie65535/gc-opencommand-plugin.git
synced 2025-12-15 19:31:35 +08:00
Compare commits
58 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7833a5ca0 | |||
| 497d13ef97 | |||
| 67b34e73b4 | |||
| 58cd171296 | |||
| 896b802bff | |||
| 4c4840be36 | |||
| 03663406de | |||
| de765c575e | |||
| f8a4e6f205 | |||
| 6279ed2982 | |||
| 1b86226951 | |||
| e9e2805738 | |||
|
|
d5162a6eac | ||
| 54e63d300d | |||
| a084d39b02 | |||
| 6c19c09c00 | |||
| c6e0b51ea6 | |||
| bcb88740f1 | |||
| 290c4fbf8c | |||
| 564f4d1e56 | |||
| 66fb25aa9b | |||
| c3c2ed08a7 | |||
| 82477321d1 | |||
| f19c4b8e77 | |||
| 3f1ecfe8a6 | |||
|
|
79564ff41c | ||
| ded44804d4 | |||
| 4b8eb490f5 | |||
| 70261df520 | |||
| 5c2be0e776 | |||
| 72948121d6 | |||
| 42c748ad2e | |||
| 1d03d3e476 | |||
| ef885af137 | |||
| b70b73667d | |||
| 623856ca99 | |||
|
|
b3bc0f051a | ||
|
|
1439139404 | ||
|
|
2799a06312 | ||
| 9fe473d6cc | |||
| d2499bad5f | |||
|
|
6db7c89196 | ||
|
|
dc5cbcc981 | ||
|
|
3ed8e233bc | ||
|
|
4712e2cd73 | ||
|
|
6aa19ce860 | ||
|
|
4d08acd084 | ||
|
|
37dcd0f1a7 | ||
|
|
a4048d4fd1 | ||
|
|
e62b1d4967 | ||
|
|
8030ef8034 | ||
|
|
67f3eb180d | ||
|
|
3bc9b0ab14 | ||
|
|
8b91e50d02 | ||
|
|
684ab421bd | ||
| 12740c64e3 | |||
| a1ef41f373 | |||
| 1ec9e3f09a |
51
.github/workflows/build.yml
vendored
Normal file
51
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
name: "Build"
|
||||||
|
on:
|
||||||
|
workflow_dispatch: ~
|
||||||
|
push:
|
||||||
|
paths:
|
||||||
|
- "**.java"
|
||||||
|
branches:
|
||||||
|
- "master"
|
||||||
|
- "LunarCode"
|
||||||
|
pull_request:
|
||||||
|
paths:
|
||||||
|
- "**.java"
|
||||||
|
types:
|
||||||
|
- opened
|
||||||
|
- synchronize
|
||||||
|
- reopened
|
||||||
|
jobs:
|
||||||
|
Build-Jar:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
- name: Setup Java
|
||||||
|
uses: actions/setup-java@v3
|
||||||
|
with:
|
||||||
|
distribution: temurin
|
||||||
|
java-version: '17'
|
||||||
|
- name: Cache gradle files
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: |
|
||||||
|
~/.gradle/caches
|
||||||
|
~/.gradle/wrapper
|
||||||
|
./.gradle/loom-cache
|
||||||
|
key: ${{ runner.os }}-gradle-${{ hashFiles('*.gradle', 'gradle.properties', '**/*.accesswidener') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-gradle-
|
||||||
|
- name: Download latest grasscutter jar
|
||||||
|
run: wget https://nightly.link/Melledy/LunarCore/workflows/build/development/LunarCore.zip && mkdir lib && unzip LunarCore.zip -d lib
|
||||||
|
|
||||||
|
- name: Change permission
|
||||||
|
run: chmod +x gradlew
|
||||||
|
|
||||||
|
- name: Run Gradle
|
||||||
|
run: ./gradlew jar
|
||||||
|
|
||||||
|
- name: Upload build
|
||||||
|
uses: actions/upload-artifact@v3
|
||||||
|
with:
|
||||||
|
name: OpenCommand
|
||||||
|
path: '*.jar'
|
||||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -23,3 +23,6 @@
|
|||||||
hs_err_pid*
|
hs_err_pid*
|
||||||
|
|
||||||
/lib/
|
/lib/
|
||||||
|
.gradle
|
||||||
|
.idea
|
||||||
|
/build
|
||||||
183
README.md
183
README.md
@@ -2,98 +2,201 @@
|
|||||||
|
|
||||||
中文 | [English](README_en-US.md)
|
中文 | [English](README_en-US.md)
|
||||||
|
|
||||||
一个为第三方客户端开放GC命令执行接口的插件
|
一个为第三方客户端开放LC命令执行接口的插件
|
||||||
|
|
||||||
|
自 `1.7.0` 起可以通过 `|` 或者换行来分隔多条命令,例如:
|
||||||
|
```shell
|
||||||
|
/a 1 | /a 2
|
||||||
|
/a 3
|
||||||
|
```
|
||||||
|
|
||||||
|
调用 `ping` 响应数据将包含插件版本号。
|
||||||
|
|
||||||
## 服务端安装
|
## 服务端安装
|
||||||
|
|
||||||
1. 在 [Release](https://github.com/jie65535/gc-opencommand-plugin/releases) 下载 `jar`
|
1. 在 [Release](https://github.com/jie65535/gc-opencommand-plugin/releases) 下载 `jar`
|
||||||
2. 放入 `plugins` 文件夹即可
|
2. 放入 `LunarCore/plugins` 文件夹
|
||||||
|
3. 重启 `LunarCore` 即可生效
|
||||||
## 控制台连接
|
|
||||||
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,发送验证码
|
1. 在远程工具中填写服务地址,查询插件状态
|
||||||
3. 将游戏内收到的**4位整数验证码**填入客户端校验
|
2. 填写UID,发送验证码(需要在线)
|
||||||
|
3. 将游戏内收到的**4位整数验证码**填入工具校验
|
||||||
4. 享受便利!
|
4. 享受便利!
|
||||||
|
|
||||||
|
## 控制台连接
|
||||||
|
|
||||||
|
1. 首次启动时,会在 `plugins` 目录下生成一个 `opencommand-plugin` 目录,打开并编辑 `config.json`
|
||||||
|
2. 设置 `consoleToken` 的值为你的连接秘钥,建议使用至少32字符的长随机字符串。(检测到为空时会自动生成,生成时会在控制台中输出)
|
||||||
|
3. 重新启动服务端即可生效配置
|
||||||
|
4. 在工具中选择控制台身份,并填写你的 `consoleToken` 即可以控制台身份运行指令
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
## 客户端请求流程
|
## 客户端请求流程
|
||||||
|
|
||||||
1. `ping` 确认是否支持 `opencommand` 插件
|
1. `ping` 确认是否支持 `opencommand` 插件
|
||||||
2. `sendCode` 向指定玩家发送验证码(1分钟内不允许重发),保存返回的 `token`
|
2. `sendCode` 向指定玩家发送验证码(1分钟内不允许重发),保存返回的 `token`
|
||||||
3. 使用 `token` 和**4位整数验证码**发送 `verify` 校验
|
3. 使用 `token` 和**4位整数验证码**发送 `verify` 校验
|
||||||
4. 如果验证通过,可以使用该 `token` 执行 `command` 动作
|
4. 如果验证通过,可以使用该 `token` 执行 `command` 动作
|
||||||
|
|
||||||
|
## 插件构建说明
|
||||||
|
|
||||||
|
1. 克隆仓库
|
||||||
|
2. 在目录下新建 `lib` 目录
|
||||||
|
3. 将 `LunarCore.jar` 放入 `lib` 目录
|
||||||
|
4. 执行 `gradle build`
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## `config.json`
|
## `config.json`
|
||||||
```json
|
|
||||||
|
```json5
|
||||||
{
|
{
|
||||||
// 控制台连接令牌
|
// 控制台连接令牌(检测到空时会自动生成)
|
||||||
"consoleToken": "",
|
"consoleToken": "",
|
||||||
// 验证码过期时间(秒)
|
// 验证码过期时间(秒)
|
||||||
"codeExpirationTime_S": 60,
|
"codeExpirationTime_S": 60,
|
||||||
// 临时令牌过期时间(秒)
|
// 临时令牌过期时间(秒)
|
||||||
"tempTokenExpirationTime_S": 300,
|
"tempTokenExpirationTime_S": 300,
|
||||||
// 授权令牌最后使用过期时间(小时)
|
// 授权令牌最后使用过期时间(小时)
|
||||||
"tokenLastUseExpirationTime_H": 48
|
"tokenLastUseExpirationTime_H": 48,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## API `/opencommand/api`
|
## API `/opencommand/api`
|
||||||
|
|
||||||
示例
|
示例
|
||||||
|
|
||||||
```
|
```
|
||||||
https://127.0.0.1/opencommand/api
|
https://127.0.0.1/opencommand/api
|
||||||
```
|
```
|
||||||
|
|
||||||
### Request 请求
|
### Request 请求
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public final class JsonRequest {
|
public final class JsonRequest {
|
||||||
public String token = "";
|
public String token = "";
|
||||||
public String action = "";
|
public String action = "";
|
||||||
|
public String server = "";
|
||||||
public Object data = null;
|
public Object data = null;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Response 响应
|
### Response 响应
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public final class JsonResponse {
|
public final class JsonResponse {
|
||||||
public int retcode = 200;
|
public int retcode = 200;
|
||||||
public String message = "success";
|
public String message = "Success";
|
||||||
public Object data;
|
public Object data;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Actions 动作
|
### Actions 动作
|
||||||
#### `ping`
|
|
||||||
data = null
|
|
||||||
|
|
||||||
#### `sendCode`
|
#### `测试连接`
|
||||||
##### Request
|
|
||||||
data = uid (int)
|
|
||||||
##### Response
|
|
||||||
data = token (string)
|
|
||||||
|
|
||||||
#### `verify` 要求 `token`
|
|
||||||
##### Request
|
##### Request
|
||||||
data = code (int)
|
|
||||||
##### Response
|
|
||||||
###### Success:
|
|
||||||
code = 200
|
|
||||||
###### Verification failed:
|
|
||||||
code = 400
|
|
||||||
|
|
||||||
#### `command` 要求 `token`
|
| 请求参数 | 请求数据 | 类型 |
|
||||||
##### Request
|
|--------|--------|----------|
|
||||||
data = command (string)
|
| action | `ping` | `String` |
|
||||||
|
|
||||||
##### Response
|
##### Response
|
||||||
data = message (string)
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|-----------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
#### `发送验证码`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| 请求参数 | 请求数据 | 类型 |
|
||||||
|
|--------|------------|----------|
|
||||||
|
| action | `sendCode` | `String` |
|
||||||
|
| data | `uid` | `Int` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|-----------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `token` | `String` |
|
||||||
|
|
||||||
|
#### `验证验证码`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| 请求参数 | 请求数据 | 类型 |
|
||||||
|
|--------|----------|----------|
|
||||||
|
| action | `verify` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| data | `code` | `Int` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
成功
|
||||||
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|-----------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
失败
|
||||||
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|-----------------------|----------|
|
||||||
|
| retcode | `400` | `Int` |
|
||||||
|
| message | `Verification failed` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
#### `执行命令`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| 请求参数 | 请求数据 | 类型 |
|
||||||
|
|--------|-----------|----------|
|
||||||
|
| action | `command` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| data | `command` | `String` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
成功
|
||||||
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|------------------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `Command return` | `String` |
|
||||||
|
|
||||||
|
### 执行控制台命令
|
||||||
|
#### `执行命令`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
> 如果为单服务器则无需填写服务器 UUID
|
||||||
|
|
||||||
|
| 请求参数 | 请求数据 | 类型 |
|
||||||
|
|--------|-----------|----------|
|
||||||
|
| action | `command` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| server | `UUID` | `String` |
|
||||||
|
| data | `command` | `String` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
成功
|
||||||
|
|
||||||
|
| 返回参数 | 返回数据 | 类型 |
|
||||||
|
|---------|------------------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `Command return` | `String` |
|
||||||
|
|||||||
195
README_en-US.md
195
README_en-US.md
@@ -2,95 +2,206 @@
|
|||||||
|
|
||||||
[中文](README.md) | English
|
[中文](README.md) | English
|
||||||
|
|
||||||
A plugin that opens the GC command execution interface for third-party clients
|
A plugin that opens the LC command execution interface for third-party clients
|
||||||
|
|
||||||
|
multiple commands can be separated by `|` or newline, for example:
|
||||||
|
```shell
|
||||||
|
/a 1 | /a 2
|
||||||
|
/a 3
|
||||||
|
```
|
||||||
|
|
||||||
|
Invoking `ping` the response data will contain the plugin version.
|
||||||
|
|
||||||
## Server installation
|
## Server installation
|
||||||
|
|
||||||
1. Download the `jar` in [Release](https://github.com/jie65535/gc-opencommand-plugin/releases)
|
1. Download the `jar` in [Release](https://github.com/jie65535/gc-opencommand-plugin/releases)
|
||||||
2. Put it in the `plugins` folder
|
2. Put it in the `LunarCore/plugins` folder
|
||||||
|
3. Restart `LunarCore` server
|
||||||
|
|
||||||
|
## Player
|
||||||
|
|
||||||
|
1. Fill in the service address in the Tool to check plugin status
|
||||||
|
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 Tool verification
|
||||||
|
4. Enjoy the convenience!
|
||||||
|
|
||||||
## Console connection
|
## 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.
|
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. (automatically generated when empty is detected)
|
||||||
3. Restart the server to take effect
|
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
|
4. Select the console identity in the client, and fill in your `consoleToken` to run the command as the console identity
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
## Build
|
## Build
|
||||||
|
|
||||||
1. `git clone https://github.com/jie65535/gc-opencommand-plugin`
|
1. `git clone https://github.com/jie65535/gc-opencommand-plugin`
|
||||||
2. `cd gc-opencommand-plugin`
|
2. `cd gc-opencommand-plugin`
|
||||||
3. `mkdir lib`
|
3. `mkdir lib`
|
||||||
4. `mv path/to/grasscutter-1.x.x-dev.jar ./lib`
|
4. `mv path/to/LunarCore.jar ./lib`
|
||||||
5. `gradle build`
|
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`
|
## `config.json`
|
||||||
```json
|
|
||||||
|
```json5
|
||||||
{
|
{
|
||||||
|
// console connection token (automatically generated when empty is detected)
|
||||||
"consoleToken": "",
|
"consoleToken": "",
|
||||||
|
// Verification code expiration time (seconds)
|
||||||
"codeExpirationTime_S": 60,
|
"codeExpirationTime_S": 60,
|
||||||
|
// Temporary token expiration time (seconds)
|
||||||
"tempTokenExpirationTime_S": 300,
|
"tempTokenExpirationTime_S": 300,
|
||||||
"tokenLastUseExpirationTime_H": 48
|
// Authorization token last used expiration time (hours)
|
||||||
|
"tokenLastUseExpirationTime_H": 48,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
## API `/opencommand/api`
|
## API `/opencommand/api`
|
||||||
|
|
||||||
Example
|
Example
|
||||||
|
|
||||||
```
|
```
|
||||||
https://127.0.0.1/opencommand/api
|
https://127.0.0.1/opencommand/api
|
||||||
```
|
```
|
||||||
|
|
||||||
## Request
|
## Request
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public final class JsonRequest {
|
public final class JsonRequest {
|
||||||
public String token = "";
|
public String token = "";
|
||||||
public String action = "";
|
public String action = "";
|
||||||
|
public Seting server = "";
|
||||||
public Object data = null;
|
public Object data = null;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Response
|
## Response
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public final class JsonResponse {
|
public final class JsonResponse {
|
||||||
public int retcode = 200;
|
public int retcode = 200;
|
||||||
public String message = "success";
|
public String message = "Success";
|
||||||
public Object data;
|
public Object data;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## Actions
|
### Actions
|
||||||
### `ping`
|
|
||||||
data = null
|
|
||||||
|
|
||||||
### `sendCode`
|
#### `Test connect`
|
||||||
#### Request
|
|
||||||
data = uid (int)
|
|
||||||
#### Response
|
|
||||||
data = token (string)
|
|
||||||
|
|
||||||
### `verify`: Requires `token`
|
##### Request
|
||||||
#### Request
|
|
||||||
data = code (int)
|
|
||||||
#### Response
|
|
||||||
##### Success:
|
|
||||||
code = 200
|
|
||||||
##### Verification failed:
|
|
||||||
code = 400
|
|
||||||
|
|
||||||
### `command`: Requires `token`
|
| Request | Request data | type |
|
||||||
#### Request
|
|---------|--------------|----------|
|
||||||
data = command (string)
|
| action | `ping` | `String` |
|
||||||
#### Response
|
|
||||||
data = message (string)
|
##### Response
|
||||||
|
|
||||||
|
| Response | Response data | type |
|
||||||
|
|----------|---------------|----------|
|
||||||
|
| retcode | `200` | `String` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
#### `Send code`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| Request | Request data | type |
|
||||||
|
|---------|--------------|----------|
|
||||||
|
| action | `sendCode` | `String` |
|
||||||
|
| data | `uid` | `Int` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
| Response | Response data | type |
|
||||||
|
|----------|---------------|----------|
|
||||||
|
| retcode | `200` | `String` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `token` | `String` |
|
||||||
|
|
||||||
|
#### `Verify code`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| Request | Request data | type |
|
||||||
|
|---------|--------------|----------|
|
||||||
|
| action | `verify` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| data | `code` | `Int` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
Success
|
||||||
|
|
||||||
|
| Response | Response data | type |
|
||||||
|
|----------|---------------|----------|
|
||||||
|
| retcode | `200` | `String` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
Failed
|
||||||
|
|
||||||
|
| Response | Response data | type |
|
||||||
|
|----------|-----------------------|----------|
|
||||||
|
| retcode | `400` | `String` |
|
||||||
|
| message | `Verification failed` | `String` |
|
||||||
|
| data | `null` | `null` |
|
||||||
|
|
||||||
|
#### `Run command`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
| Request | Request data | type |
|
||||||
|
|---------|--------------|----------|
|
||||||
|
| action | `command` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| data | `command` | `String` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
Success
|
||||||
|
|
||||||
|
| Response | Response data | type |
|
||||||
|
|----------|------------------|----------|
|
||||||
|
| retcode | `200` | `String` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `Command return` | `String` |
|
||||||
|
|
||||||
|
### Run console command
|
||||||
|
|
||||||
|
#### `Run command`
|
||||||
|
|
||||||
|
##### Request
|
||||||
|
|
||||||
|
> If it is a single server, there is no need to fill in server UUID.
|
||||||
|
|
||||||
|
| Request | Request data | Type |
|
||||||
|
|---------|--------------|----------|
|
||||||
|
| action | `command` | `String` |
|
||||||
|
| token | `token` | `String` |
|
||||||
|
| server | `UUID` | `String` |
|
||||||
|
| data | `command` | `String` |
|
||||||
|
|
||||||
|
##### Response
|
||||||
|
|
||||||
|
Success
|
||||||
|
|
||||||
|
| Request | Response data | Type |
|
||||||
|
|---------|------------------|----------|
|
||||||
|
| retcode | `200` | `Int` |
|
||||||
|
| message | `Success` | `String` |
|
||||||
|
| data | `Command return` | `String` |
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group 'com.github.jie65535.opencommand'
|
group 'com.github.jie65535.opencommand'
|
||||||
version 'dev-1.2.3'
|
version '1.0.0'
|
||||||
|
|
||||||
sourceCompatibility = 17
|
sourceCompatibility = 17
|
||||||
targetCompatibility = 17
|
targetCompatibility = 17
|
||||||
@@ -18,7 +18,7 @@ dependencies {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jar {
|
jar {
|
||||||
jar.baseName = 'opencommand'
|
jar.baseName = 'OpenCommand-LunarCore'
|
||||||
|
|
||||||
destinationDir = file(".")
|
destinationDir = file(".")
|
||||||
}
|
}
|
||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -1,5 +1,5 @@
|
|||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
@@ -1,17 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* gc-opencommand
|
||||||
|
* Copyright (C) 2022-2023 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 emu.grasscutter.server.event.game.CommandResponseEvent;
|
|
||||||
import emu.grasscutter.utils.MessageHandler;
|
|
||||||
|
|
||||||
public final class EventListeners {
|
public final class EventListeners {
|
||||||
|
|
||||||
private static MessageHandler consoleMessageHandler;
|
// private static StringBuilder consoleMessageHandler;
|
||||||
public static void setConsoleMessageHandler(MessageHandler handler) {
|
// private static final Int2ObjectMap<StringBuilder> playerMessageHandlers = new Int2ObjectOpenHashMap<>();
|
||||||
consoleMessageHandler = handler;
|
//
|
||||||
}
|
// public static void setConsoleMessageHandler(StringBuilder handler) {
|
||||||
public static void onCommandResponse(CommandResponseEvent event) {
|
// consoleMessageHandler = handler;
|
||||||
if (consoleMessageHandler != null && event.getPlayer() == null) {
|
// }
|
||||||
consoleMessageHandler.setMessage(event.getMessage());
|
//
|
||||||
}
|
// /**
|
||||||
}
|
// * 获取新的玩家消息处理类
|
||||||
|
// * 获取时将创建或清空消息处理器并返回实例,**在执行命令前获取!**
|
||||||
|
// * @param uid 玩家uid
|
||||||
|
// * @return 新的玩家消息处理类
|
||||||
|
// */
|
||||||
|
// public static StringBuilder getPlayerMessageHandler(int uid) {
|
||||||
|
// var handler = playerMessageHandlers.get(uid);
|
||||||
|
// if (handler == null) {
|
||||||
|
// handler = new StringBuilder();
|
||||||
|
// playerMessageHandlers.put(uid, handler);
|
||||||
|
// }
|
||||||
|
// return handler;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// /**
|
||||||
|
// * 命令执行反馈事件处理
|
||||||
|
// */
|
||||||
|
// public static void onCommandResponse(ReceiveCommandFeedbackEvent event) {
|
||||||
|
// StringBuilder handler;
|
||||||
|
// if (event.getPlayer() == null) {
|
||||||
|
// handler = consoleMessageHandler;
|
||||||
|
// } else {
|
||||||
|
// handler = playerMessageHandlers.get(event.getPlayer().getUid());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// if (handler != null) {
|
||||||
|
// if (!handler.isEmpty()) {
|
||||||
|
// // New line
|
||||||
|
// handler.append(System.lineSeparator());
|
||||||
|
// }
|
||||||
|
// handler.append(event.getMessage());
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* gc-opencommand
|
* gc-opencommand
|
||||||
* Copyright (C) 2022 jie65535
|
* Copyright (C) 2022-2023 jie65535
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -18,8 +18,23 @@
|
|||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* gc-opencommand
|
* gc-opencommand
|
||||||
* Copyright (C) 2022 jie65535
|
* Copyright (C) 2022-2023 jie65535
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -19,163 +19,156 @@ 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.command.CommandMap;
|
import com.github.jie65535.opencommand.model.Client;
|
||||||
import emu.grasscutter.server.http.Router;
|
import emu.lunarcore.LunarCore;
|
||||||
import emu.grasscutter.utils.Crypto;
|
import emu.lunarcore.game.player.Player;
|
||||||
import emu.grasscutter.utils.MessageHandler;
|
import emu.lunarcore.util.Crypto;
|
||||||
import emu.grasscutter.utils.Utils;
|
import io.javalin.http.Context;
|
||||||
import express.Express;
|
|
||||||
import express.http.Request;
|
|
||||||
import express.http.Response;
|
|
||||||
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.security.SecureRandom;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public final class OpenCommandHandler implements Router {
|
public final class OpenCommandHandler {
|
||||||
|
|
||||||
@Override
|
|
||||||
public void applyRoutes(Express express, Javalin javalin) {
|
|
||||||
express.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) {
|
private static final SecureRandom secureRandom = new SecureRandom();
|
||||||
// Trigger cleanup action
|
|
||||||
cleanupExpiredData();
|
public static void handle(Context context) {
|
||||||
var plugin = OpenCommandPlugin.getInstance();
|
var plugin = OpenCommandPlugin.getInstance();
|
||||||
var config = plugin.getConfig();
|
try {
|
||||||
var now = new Date();
|
var config = plugin.getConfig();
|
||||||
|
var data = plugin.getData();
|
||||||
|
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)Double.parseDouble(req.data.toString());
|
||||||
int playerId = (int) req.data;
|
var player = LunarCore.getGameServer().getPlayerByUid(playerId, false);
|
||||||
var player = plugin.getServer().getPlayerByUid(playerId);
|
|
||||||
if (player == null) {
|
|
||||||
response.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"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String token = req.token;
|
|
||||||
if (token == null || token.isEmpty())
|
|
||||||
token = Utils.bytesToHex(Crypto.createSessionKey(32));
|
|
||||||
int code = Utils.randomRange(1000, 9999);
|
|
||||||
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);
|
|
||||||
response.json(new JsonResponse(token));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
} else if (req.action.equals("ping")) {
|
|
||||||
response.json(new JsonResponse());
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// token is required
|
|
||||||
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 (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() + 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"));
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (req.action.equals("command")) {
|
|
||||||
// update token expire time
|
|
||||||
tokenExpireTime.put(req.token, new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L));
|
|
||||||
var playerId = clients.get(req.token);
|
|
||||||
var player = plugin.getServer().getPlayerByUid(playerId);
|
|
||||||
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."));
|
||||||
|
} else {
|
||||||
|
if (codeExpireTime.containsKey(playerId)) {
|
||||||
|
var expireTime = codeExpireTime.get(playerId);
|
||||||
|
if (now.before(expireTime)) {
|
||||||
|
context.json(new JsonResponse(403, "Requests are too frequent"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String token = req.token;
|
||||||
|
if (token == null || token.isEmpty())
|
||||||
|
token = Crypto.createSessionKey(player.getAccountUid());
|
||||||
|
int code = secureRandom.nextInt(1000, 9999);
|
||||||
|
codeExpireTime.put(playerId, new Date(now.getTime() + config.codeExpirationTime_S * 1000L));
|
||||||
|
codes.put(token, code);
|
||||||
|
data.addClient(new Client(token, playerId, new Date(now.getTime() + config.tempTokenExpirationTime_S * 1000L)));
|
||||||
|
player.sendMessage("[Open Command] Verification code: " + code);
|
||||||
|
context.json(new JsonResponse(token));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else if (req.action.equals("ping")) {
|
||||||
|
context.json(new JsonResponse(plugin.getVersion()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// token is required
|
||||||
|
if (req.token == null || req.token.isEmpty()) {
|
||||||
|
context.json(new JsonResponse(401, "Unauthorized"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var isConsole = req.token.equals(config.consoleToken);
|
||||||
|
var client = data.getClientByToken(req.token);
|
||||||
|
if (!isConsole && client == null) {
|
||||||
|
context.json(new JsonResponse(401, "Unauthorized"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isConsole) {
|
||||||
|
if (req.action.equals("verify")) {
|
||||||
|
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", context.ip(), req.data));
|
||||||
|
tryInvokeCommand(null, req.data.toString());
|
||||||
|
context.json(new JsonResponse("OK"));
|
||||||
|
} catch (Exception e) {
|
||||||
|
plugin.getLogger().warn("Run command failed.", e);
|
||||||
|
context.json(new JsonResponse(500, "error", e.getLocalizedMessage()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
} else if (req.action.equals("runmode")) {
|
||||||
|
context.json(new JsonResponse(200, "Success", 0));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Player MessageHandler do not support concurrency
|
} else if (codes.containsKey(req.token)) {
|
||||||
//noinspection SynchronizationOnLocalVariableOrMethodParameter
|
if (req.action.equals("verify")) {
|
||||||
synchronized (player) {
|
if (codes.get(req.token) == (int)Double.parseDouble(req.data.toString())) {
|
||||||
|
codes.remove(req.token);
|
||||||
|
// update token expire time
|
||||||
|
client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
|
||||||
|
context.json(new JsonResponse());
|
||||||
|
plugin.getLogger().info(String.format("Player %d has passed the verification, ip: %s", client.playerId, context.ip()));
|
||||||
|
plugin.saveData();
|
||||||
|
} else {
|
||||||
|
context.json(new JsonResponse(400, "Verification failed"));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (req.action.equals("command")) {
|
||||||
|
// update token expire time
|
||||||
|
client.tokenExpireTime = new Date(now.getTime() + config.tokenLastUseExpirationTime_H * 60L * 60L * 1000L);
|
||||||
|
var player = LunarCore.getGameServer().getPlayerByUid(client.playerId, false);
|
||||||
|
if (player == null) {
|
||||||
|
context.json(new JsonResponse(404, "Player not found"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// var history = player.getChatManager().getHistoryByUid(GameConstants.SERVER_CONSOLE_UID);
|
||||||
try {
|
try {
|
||||||
var resultCollector = new MessageHandler();
|
tryInvokeCommand(player, req.data.toString());
|
||||||
player.setMessageHandler(resultCollector);
|
context.json(new JsonResponse("OK"));
|
||||||
CommandMap.getInstance().invoke(player, player, command);
|
|
||||||
response.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 {
|
|
||||||
player.setMessageHandler(null);
|
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
context.json(new JsonResponse(403, "forbidden"));
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
plugin.getLogger().error("[OpenCommand] handler error.", ex);
|
||||||
}
|
}
|
||||||
response.json(new JsonResponse(403, "forbidden"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void cleanupExpiredData() {
|
private static void tryInvokeCommand(Player sender, String rawMessage) {
|
||||||
|
for (var command : rawMessage.split("\n[/!]|\\|")) {
|
||||||
|
command = command.trim();
|
||||||
|
if (command.isEmpty()) continue;
|
||||||
|
if (command.charAt(0) == '/' || command.charAt(0) == '!') {
|
||||||
|
command = command.substring(1);
|
||||||
|
}
|
||||||
|
LunarCore.getCommandManager().invoke(sender, command);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* gc-opencommand
|
* gc-opencommand
|
||||||
* Copyright (C) 2022 jie65535
|
* Copyright (C) 2022-2023 jie65535
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -17,42 +17,51 @@
|
|||||||
*/
|
*/
|
||||||
package com.github.jie65535.opencommand;
|
package com.github.jie65535.opencommand;
|
||||||
|
|
||||||
import emu.grasscutter.Grasscutter;
|
import emu.lunarcore.LunarCore;
|
||||||
import emu.grasscutter.plugin.Plugin;
|
import emu.lunarcore.plugin.Plugin;
|
||||||
import emu.grasscutter.server.event.EventHandler;
|
import emu.lunarcore.util.Crypto;
|
||||||
import emu.grasscutter.server.event.HandlerPriority;
|
import emu.lunarcore.util.JsonUtils;
|
||||||
import emu.grasscutter.server.event.game.CommandResponseEvent;
|
import org.slf4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.FileReader;
|
import java.net.URLClassLoader;
|
||||||
import java.io.FileWriter;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.io.IOException;
|
import java.nio.file.Files;
|
||||||
|
|
||||||
public final class OpenCommandPlugin extends Plugin {
|
public final class OpenCommandPlugin extends Plugin {
|
||||||
|
|
||||||
private static OpenCommandPlugin instance;
|
private static OpenCommandPlugin instance;
|
||||||
public static OpenCommandPlugin getInstance() { return instance; }
|
|
||||||
|
public OpenCommandPlugin(Identifier identifier, URLClassLoader classLoader, File dataFolder, Logger logger) {
|
||||||
|
super(identifier, classLoader, dataFolder, logger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static OpenCommandPlugin getInstance() {
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
private OpenCommandConfig config;
|
private OpenCommandConfig config;
|
||||||
|
|
||||||
|
private OpenCommandData data;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
instance = this;
|
instance = this;
|
||||||
|
// 加载配置
|
||||||
loadConfig();
|
loadConfig();
|
||||||
|
// 加载数据
|
||||||
|
loadData();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
new EventHandler<>(CommandResponseEvent.class)
|
LunarCore.getHttpServer().getApp().post("/opencommand/api", OpenCommandHandler::handle);
|
||||||
.priority(HandlerPriority.HIGH)
|
getLogger().info("[OpenCommand] Enabled. https://github.com/jie65535/gc-opencommand-plugin");
|
||||||
.listener(EventListeners::onCommandResponse)
|
|
||||||
.register(this);
|
|
||||||
getHandle().addRouter(OpenCommandHandler.class);
|
|
||||||
getLogger().info("[OpenCommand] Enabled");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
|
saveData();
|
||||||
getLogger().info("[OpenCommand] Disabled");
|
getLogger().info("[OpenCommand] Disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,24 +69,69 @@ 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)){
|
saveConfig();
|
||||||
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 {
|
} else {
|
||||||
try (var file = new FileReader(configFile)) {
|
try {
|
||||||
config = Grasscutter.getGsonFactory().fromJson(file, OpenCommandConfig.class);
|
config = JsonUtils.decode(Files.readString(configFile.toPath(), StandardCharsets.UTF_8),
|
||||||
|
OpenCommandConfig.class);
|
||||||
} catch (Exception exception) {
|
} catch (Exception exception) {
|
||||||
config = new OpenCommandConfig();
|
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.");
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查控制台Token
|
||||||
|
if (config.consoleToken == null || config.consoleToken.isEmpty()) {
|
||||||
|
config.consoleToken = Crypto.createSessionKey("1");
|
||||||
|
saveConfig();
|
||||||
|
getLogger().warn("Detected that consoleToken is empty, automatically generated Token for you as follows: {}", config.consoleToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveConfig() {
|
||||||
|
var configFile = new File(getDataFolder(), "config.json");
|
||||||
|
try (var file = new FileWriter(configFile)) {
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadData() {
|
||||||
|
var dataFile = new File(getDataFolder(), "data.json");
|
||||||
|
if (!dataFile.exists()) {
|
||||||
|
data = new OpenCommandData();
|
||||||
|
saveData();
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
data = JsonUtils.decode(Files.readString(dataFile.toPath(), StandardCharsets.UTF_8),
|
||||||
|
OpenCommandData.class);
|
||||||
|
} catch (Exception exception) {
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
if (data == null) {
|
||||||
|
data = new OpenCommandData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* gc-opencommand
|
* gc-opencommand
|
||||||
* Copyright (C) 2022 jie65535
|
* Copyright (C) 2022-2023 jie65535
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -20,5 +20,6 @@ package com.github.jie65535.opencommand.json;
|
|||||||
public final class JsonRequest {
|
public final class JsonRequest {
|
||||||
public String token = "";
|
public String token = "";
|
||||||
public String action = "";
|
public String action = "";
|
||||||
|
public String server = "";
|
||||||
public Object data = null;
|
public Object data = null;
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* gc-opencommand
|
* gc-opencommand
|
||||||
* Copyright (C) 2022 jie65535
|
* Copyright (C) 2022-2023 jie65535
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify
|
* 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
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
@@ -19,7 +19,7 @@ package com.github.jie65535.opencommand.json;
|
|||||||
|
|
||||||
public final class JsonResponse {
|
public final class JsonResponse {
|
||||||
public int retcode = 200;
|
public int retcode = 200;
|
||||||
public String message = "success";
|
public String message = "Success";
|
||||||
public Object data;
|
public Object data;
|
||||||
|
|
||||||
public JsonResponse() {
|
public JsonResponse() {
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
{
|
{
|
||||||
"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.2.3",
|
"version": "1.0.0",
|
||||||
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
"mainClass": "com.github.jie65535.opencommand.OpenCommandPlugin",
|
||||||
"authors": ["jie65535"]
|
"authors": ["jie65535"],
|
||||||
|
"api": 1
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user