-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_backend.js
134 lines (126 loc) · 5.4 KB
/
api_backend.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env node
const fs = require('fs')
const http = require('http')
const { execSync } = require('child_process')
const { performance } = require('perf_hooks')
const PORT = 9303
const HOST = `http://localhost:${PORT}`
const TOKEN = process.env.PL_WEB_API_TOKEN
class ShellCommand {
constructor (cmd) {
this.cmd = cmd
this.lastCache = 0
this.lastRequest = null
this.lastRequest2 = null
this.cache = ''
}
run (arg, arg2) {
const timeNow = performance.now()
const diff = timeNow - this.lastCache
if (diff < 2000 && arg === this.lastRequest && arg2 === this.lastRequest2) {
return this.cache
}
this.lastCache = timeNow
if (arg && arg2) {
this.lastRequest = arg
this.lastRequest2 = arg2
arg = Buffer.from(arg).toString('base64')
arg2 = Buffer.from(arg2).toString('base64')
this.cache = execSync(`${this.cmd} "$(echo "${arg}" | base64 -d)" "$(echo "${arg2}" | base64 -d)"`).toString().replace(/\n+$/, '')
} else if (arg) {
this.lastRequest = arg
this.lastRequest2 = null
arg = Buffer.from(arg).toString('base64')
this.cache = execSync(`${this.cmd} "$(echo "${arg}" | base64 -d)"`).toString().replace(/\n+$/, '')
} else {
this.cache = execSync(this.cmd).toString().replace(/\n+$/, '')
}
return this.cache
}
}
const ShellCpu = new ShellCommand('bash ./lib/plugins/server-plugin-web/get_cpu.sh --script')
const ShellIpByName = new ShellCommand("cd $(./lib/eval_lib.sh 'echo $LOGS_PATH_FULL') && tw_get_ip_by_name")
const ShellNameByIp = new ShellCommand("cd $(./lib/eval_lib.sh 'echo $LOGS_PATH_FULL') && tw_get_name_by_ip")
const ShellAccByName = new ShellCommand('bash ./lib/plugins/server-plugin-web/bin/tw_get_acc_by_name -a')
const ShellNameByAcc = new ShellCommand('bash ./lib/plugins/server-plugin-web/bin/tw_get_name_by_acc -a')
const ShellEcon = new ShellCommand('bash ./lib/econ.sh')
const ShellGetLatestLog = new ShellCommand('./show_log.sh --filepath')
const ShellGetLogfiles = new ShellCommand("cd $(./lib/eval_lib.sh 'echo $LOGS_PATH_FULL') && find . -type f")
const ShellGetLogfilesSearch = new ShellCommand("cd $(./lib/eval_lib.sh 'echo $LOGS_PATH_FULL') && rg -lF")
const ShellChatByIp = new ShellCommand("cd $(./lib/eval_lib.sh 'echo $LOGS_PATH_FULL') && bash tw_filter_ip")
const log = (msg) => {
const ts = new Date()
.toISOString()
.split('.')[0]
.replace('T', ' ')
console.log(`[${ts}] ${msg}`)
}
log(`starting server on ${HOST} ...`)
http.createServer((request, response) => {
response.setHeader('Access-Control-Allow-Origin', '*')
response.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE')
response.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type')
log(`request ${request.url}`)
const url = new URL(`${HOST}${request.url}`)
const urlParams = new URLSearchParams(url.search)
const token = urlParams.get('t')
if (token !== TOKEN) {
log(' invalid token')
response.end(JSON.stringify({ error: 'invalid token' }))
return
}
const cmd = urlParams.get('cmd')
const args = urlParams.get('args') ? urlParams.get('args').split(' ') : []
log(` cmd '${cmd}' args: ${JSON.stringify(args)}`)
if (cmd === 'cpu') {
response.end(JSON.stringify({ message: 'cpu usage', stdout: ShellCpu.run() }))
} else if (cmd === 'traffic') {
response.end(JSON.stringify({ message: 'traffic', stdout: fs.readFileSync('logs/traffic.txt').toString() }))
} else if (cmd === 'ip_by_name') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: username' }))
return
}
response.end(JSON.stringify({ message: 'ips used', stdout: ShellIpByName.run(args.join(' ')) }))
} else if (cmd === 'name_by_ip') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: ip' }))
return
}
response.end(JSON.stringify({ message: 'names used', stdout: ShellNameByIp.run(args[0]) }))
} else if (cmd === 'acc_by_name') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: username' }))
return
}
response.end(JSON.stringify({ message: 'names used', stdout: ShellAccByName.run(args.join(' ')) }))
} else if (cmd === 'name_by_acc') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: account' }))
return
}
response.end(JSON.stringify({ message: 'names used', stdout: ShellNameByAcc.run(args[0]) }))
} else if (cmd === 'filter_ip') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: ip' }))
return
}
const logPath = args[1] === 'latest' ? ShellGetLatestLog.run() : args[1]
response.end(JSON.stringify({ message: 'filter chat', stdout: ShellChatByIp.run(args[0], logPath) }))
} else if (cmd === 'get_logfiles') {
if(args[0] !== undefined) {
const logfiles = ShellGetLogfilesSearch.run(args[0])
response.end(JSON.stringify({ message: 'logfiles', stdout: logfiles }))
return
}
response.end(JSON.stringify({ message: 'logfiles', stdout: ShellGetLogfiles.run() }))
} else if (cmd === 'econ') {
if (args[0] === undefined) {
response.end(JSON.stringify({ error: 'missing arg: command' }))
return
}
response.end(JSON.stringify({ message: 'econ', stdout: ShellEcon.run(args.join(' ')) }))
} else {
response.end(JSON.stringify({ error: 'invalid command' }))
}
}).listen(PORT)