Skip to content

Commit 73d9589

Browse files
Feat: 优化 Task Name 对应关系
Docs: 新增 Hello Version
1 parent e5d9b0b commit 73d9589

32 files changed

Lines changed: 679 additions & 545 deletions

‎agent/src/tasks/mod.rs‎

Lines changed: 70 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,60 @@ pub mod ping;
1717
// PTY(伪终端)模块
1818
mod pty;
1919

20+
// 检查服务器是否允许执行特定任务
21+
fn is_task_allowed(server: &nodeget_lib::config::agent::Server, task_type: &TaskEventType) -> bool {
22+
match task_type {
23+
TaskEventType::Ping(_) => server.allow_icmp_ping.unwrap_or(false),
24+
TaskEventType::TcpPing(_) => server.allow_tcp_ping.unwrap_or(false),
25+
TaskEventType::HttpPing(_) => server.allow_http_ping.unwrap_or(false),
26+
TaskEventType::WebShell(_) => server.allow_web_shell.unwrap_or(false),
27+
TaskEventType::Execute(_) => server.allow_execute.unwrap_or(false),
28+
TaskEventType::Ip => server.allow_ip.unwrap_or(false),
29+
}
30+
}
31+
32+
// 执行具体任务
33+
async fn execute_task(
34+
task_type: &TaskEventType,
35+
task_id: u64,
36+
task_token: &str,
37+
) -> Result<TaskEventResult, String> {
38+
match task_type {
39+
TaskEventType::Ping(target) => ping::icmp::ping_target(target.clone())
40+
.await
41+
.map(|d| task_type.result_from_duration(d))
42+
.map_err(|e| e),
43+
44+
TaskEventType::TcpPing(target) => ping::tcp::tcping_target(target.clone())
45+
.await
46+
.map(|d| task_type.result_from_duration(d))
47+
.map_err(|e| e),
48+
49+
TaskEventType::HttpPing(target) => ping::http::httping_target(target.clone())
50+
.await
51+
.map(|d| task_type.result_from_duration(d))
52+
.map_err(|e| e),
53+
54+
TaskEventType::WebShell(url) => {
55+
let url = pty::parse_url(url.clone(), task_id, task_token);
56+
pty::handle_pty_url(url)
57+
.await
58+
.map(|_| TaskEventResult::WebShell(true))
59+
.map_err(|e| e)
60+
}
61+
62+
TaskEventType::Execute(command) => execute::execute_command(command.clone())
63+
.await
64+
.map(TaskEventResult::Execute)
65+
.map_err(|e| e),
66+
67+
TaskEventType::Ip => {
68+
let ip_info = ip::ip().await;
69+
Ok(TaskEventResult::Ip(ip_info.ipv4, ip_info.ipv6))
70+
}
71+
}
72+
}
73+
2074
// 处理来自服务器的任务请求
2175
//
2276
// 该函数订阅各个服务器的任务通道,接收并执行不同类型的任务(如 Ping、TCP Ping、HTTP Ping、WebShell、命令执行、IP 查询),
@@ -45,99 +99,34 @@ pub async fn handle_task() {
4599
while let Ok(message) = rx.recv().await {
46100
let server_name = server.name.clone();
47101
let server_token = server.token.clone();
102+
let server_config = server.clone();
48103
tokio::spawn(async move {
49104
let rpc = match message {
50105
Message::Text(text) => text.to_string(),
51-
_ => {
52-
return;
53-
}
106+
_ => return,
54107
};
55108

56109
let json_rpc: JsonRpcTask = match serde_json::from_str(&rpc) {
57110
Ok(json_rpc) => json_rpc,
58-
Err(_) => {
59-
return;
60-
}
111+
Err(_) => return,
61112
};
62113

63114
if json_rpc.method != "task_register_task" {
64115
return;
65116
}
66117

67-
let task_result: Result<TaskEventResult, String> =
68-
match json_rpc.params.result.task_event_type {
69-
TaskEventType::Ping(target) => {
70-
if server.allow_icmp_ping.unwrap_or(false) {
71-
match ping::icmp::ping_target(target).await {
72-
Ok(duration) => {
73-
Ok(TaskEventResult::Ping(duration.as_millis_f64()))
74-
}
75-
Err(e) => Err(e),
76-
}
77-
} else {
78-
Err("102: Permission Denied".to_string())
79-
}
80-
}
81-
TaskEventType::TcpPing(target) => {
82-
if server.allow_tcp_ping.unwrap_or(false) {
83-
match ping::tcp::tcping_target(target).await {
84-
Ok(duration) => {
85-
Ok(TaskEventResult::Ping(duration.as_millis_f64()))
86-
}
87-
Err(e) => Err(e),
88-
}
89-
} else {
90-
Err("102: Permission Denied".to_string())
91-
}
92-
}
93-
TaskEventType::HttpPing(target) => {
94-
if server.allow_http_ping.unwrap_or(false) {
95-
match ping::http::httping_target(target).await {
96-
Ok(duration) => {
97-
Ok(TaskEventResult::Ping(duration.as_millis_f64()))
98-
}
99-
Err(e) => Err(e),
100-
}
101-
} else {
102-
Err("102: Permission Denied".to_string())
103-
}
104-
}
105-
TaskEventType::WebShell(url) => {
106-
if server.allow_web_shell.unwrap_or(false) {
107-
let task_id = json_rpc.params.result.task_id;
108-
let url = pty::parse_url(
109-
url,
110-
task_id,
111-
&json_rpc.params.result.task_token,
112-
);
113-
114-
match pty::handle_pty_url(url).await {
115-
Ok(()) => Ok(TaskEventResult::WebShell(true)),
116-
Err(e) => Err(e),
117-
}
118-
} else {
119-
Err("102: Permission Denied".to_string())
120-
}
121-
}
122-
TaskEventType::Execute(command) => {
123-
if server.allow_execute.unwrap_or(false) {
124-
match execute::execute_command(command).await {
125-
Ok(output) => Ok(TaskEventResult::Execute(output)),
126-
Err(e) => Err(e),
127-
}
128-
} else {
129-
Err("102: Permission Denied".to_string())
130-
}
131-
}
132-
TaskEventType::Ip => {
133-
if server.allow_ip.unwrap_or(false) {
134-
let ip_info = ip::ip().await;
135-
Ok(TaskEventResult::Ip(ip_info.ipv4, ip_info.ipv6))
136-
} else {
137-
Err("102: Permission Denied".to_string())
138-
}
139-
}
140-
};
118+
let task_type = &json_rpc.params.result.task_event_type;
119+
120+
let task_result = if is_task_allowed(&server_config, task_type) {
121+
execute_task(
122+
task_type,
123+
json_rpc.params.result.task_id,
124+
&json_rpc.params.result.task_token,
125+
)
126+
.await
127+
} else {
128+
Err("102: Permission Denied".to_string())
129+
};
141130

142131
let response = match task_result {
143132
Ok(task_result) => TaskEventResponse {
@@ -168,8 +157,7 @@ pub async fn handle_task() {
168157
],
169158
);
170159

171-
if let Err(e) = send_to(&server_name, Message::Text(Utf8Bytes::from(rpc))).await
172-
{
160+
if let Err(e) = send_to(&server_name, Message::Text(Utf8Bytes::from(rpc))).await {
173161
error!("{e}");
174162
}
175163
});

‎docs/.vitepress/theme-zh.config.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ export const themeZhConfig = {
2525
text: '项目框架',
2626
link: '/api/framework.md'
2727
},
28+
// Nodeget
29+
{
30+
text: 'Nodeget',
31+
collapsed: false,
32+
items: [{
33+
text: '介绍',
34+
link: '/api/nodeget/index.md'
35+
},
36+
{
37+
text: 'Hello',
38+
link: '/api/nodeget/hello.md'
39+
},
40+
{
41+
text: 'Version',
42+
link: '/api/nodeget/version.md'
43+
}]
44+
},
2845

2946
// Monitoring
3047
{

‎docs/api/index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ outline: deep
88

99
## 功能模块
1010

11+
- [Nodeget](./nodeget/index.md)
1112
- [Token](./token/index.md)
1213
- [Monitoring](./monitoring/index.md)
1314
- [Task](./task/index.md)

‎docs/api/metadata/write.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## 写入方法
66

7-
`metadata_get` 是用于写入的方法,需要提供:
7+
`metadata_write` 是用于写入的方法,需要提供:
88

99
- `token`: 具有对应权限的 Token
1010
- `metadata`: 写入的 Metadata,基本类型为 `Metadata 总览` 中的结构体

‎docs/api/nodeget/hello.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# nodeget-server_hello
2+
3+
测试服务是否正常运行。
4+
5+
## 请求
6+
7+
```json
8+
{
9+
"jsonrpc": "2.0",
10+
"method": "nodeget-server_hello",
11+
"params": [],
12+
"id": 1
13+
}
14+
```
15+
16+
## 响应
17+
18+
```json
19+
{
20+
"jsonrpc": "2.0",
21+
"result": "NodeGet Server Is Running!",
22+
"id": 1
23+
}
24+
```
25+
26+
## 返回值
27+
28+
| 类型 | 描述 |
29+
|------|------|
30+
| `String` | 返回字符串 `"NodeGet Server Is Running!"` 表示服务正常运行 |
31+
32+
## 使用场景
33+
34+
- 健康检查
35+
- 服务可用性测试
36+
- 连接测试

‎docs/api/nodeget/index.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Nodeget API
2+
3+
Nodeget API 提供服务器基本信息和状态查询功能。
4+
5+
## 方法列表
6+
7+
| 方法名 | 描述 |
8+
|--------|------|
9+
| [hello](./hello.md) | 测试服务是否正常运行 |
10+
| [version](./version.md) | 获取服务器版本信息 |

‎docs/api/nodeget/version.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# nodeget-server_version
2+
3+
获取服务器版本信息。
4+
5+
## 请求
6+
7+
```json
8+
{
9+
"jsonrpc": "2.0",
10+
"method": "nodeget-server_version",
11+
"params": [],
12+
"id": 1
13+
}
14+
```
15+
16+
## 响应
17+
18+
```json
19+
{
20+
"jsonrpc": "2.0",
21+
"result": {
22+
"version": "0.1.0",
23+
"build_date": "2024-01-01",
24+
"git_commit": "abc123"
25+
},
26+
"id": 1
27+
}
28+
```
29+
30+
## 返回值
31+
32+
| 字段 | 类型 | 描述 |
33+
|------|------|------|
34+
| `version` | `String` | 版本号 |
35+
| `build_date` | `String` | 构建日期 |
36+
| `git_commit` | `String` | Git 提交哈希 |
37+
38+
## 使用场景
39+
40+
- 版本兼容性检查
41+
- 调试信息收集
42+
- 服务信息展示

‎docs/api/task/query.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"token": "demo_token",
1010
"task_data_query": {
1111
"condition": [
12-
// QueryCondition 结构体,该结构体参考 Monitoring 总览
12+
// QueryCondition 结构体,该结构体参考 Task 总览
1313
// 该字段为 Vec<_>,可指定多个
1414
]
1515
}

0 commit comments

Comments
 (0)