-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.mjs
232 lines (219 loc) · 6.3 KB
/
index.mjs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { spawn } from 'child_process';
import express from 'express';
import si from 'systeminformation';
import readline from 'readline';
import { log, color } from './utils/logging.mjs';
import { tribool, generateUid, strSize, dateToString } from './utils/functions.mjs';
import db from './db/index.mjs';
const BOT_PATH = process.env.BOT_PATH || '/home/ubuntu/chatgpt-on-wechat';
const PORT = process.env.PORT || 14514;
const OUTPUT_LIMIT = process.env.OUTPUT_LIMIT || 4096;
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: ''
});
const input = (q) =>
new Promise((resolve, reject) => {
rl.question(q, (a) => {
resolve(a);
});
}
);
const ask = async (q, p = '', d = undefined) => {
let a = undefined;
while (a === undefined) {
a = (await input(
`${color.bright}${color.cyan}${q}${color.reset}${(p||d)?' ':''}` +
`${color.cyan}${p}${d?`(${d})`:''}: ${color.reset}`
)) || d;
}
return a;
};
const app = express();
const PROCPOOL = [];
const killProc = (proc) => {
log.info(`Killing PID ${proc.pid}`);
proc.stdin.end();
proc.stdout.destroy();
proc.stderr.destroy();
const res = proc.kill();
if (!res) log.warn(`Kill failed on PID ${proc.pid}`);
return res;
};
const addProc = async (params) => {
const procId = params.key ?? generateUid();
if (PROCPOOL[procId]) {
log.warn(`Spawn request with key ${params.key}, but proc already exists`);
return procId;
}
const proc = spawn('python3', ['app.py'], {
cwd: BOT_PATH,
stdio: 'pipe',
env: {
CONFIG_PATH: (params?.info?.configPath || params?.info?.config_path) ?? 'config-template.json'
}
});
const output = {
text: '',
length: 0
};
proc.stdout.on('data', (chunk) => {
if (Date.now() > params.info.expires) {
killProc(proc);
log.notice(`Killed expired process ${procId}`);
return PROCPOOL[procId] = null;
}
output.length += chunk.length;
output.text = `(${output.length}) ` +
(output.text.replace(/^\([0-9]+\) /, '') + chunk).slice(-OUTPUT_LIMIT);
});
proc.on('error', () => {
log.error(`Error occured at proc ${procId}`);
});
proc.on('disconnect', () => {
log.warn(`Disconnected from proc ${procId}`);
});
PROCPOOL[procId] = {
proc, output, info: params.info
};
log.notice(`Spawned proc ${procId} | Key: ${params.key} | Owner: ${params.info.owner}`);
return procId;
};
app.use('/', express.static('frontend', {
index: 'webui.html'
}));
app.get('/spawn/:key', async (req, res) => {
const key = req.params.key;
const ip = req.headers['x-real-ip'] || req.ip || 'N/A';
const dbRes = await db.getByKey(key);
if (dbRes === db.STAT.key_nonexistent) {
log.warn(`User entered invalid key ${key} (ip: ${ip})`);
return res.status(401).send('无效密钥');
} else if (Date.now() > dbRes.expires) {
log.warn(`User tries to use expired key ${key} (ip: ${ip})`);
return res.status(404).send('授权已到期');
}
let procId = await addProc({
key,
info: dbRes
});
log.notice(`Spawn request finished with procId ${procId} (ip: ${ip})`);
res.status(201).send(`${procId}`);
});
app.get('/kill/:id', async (req, res) => {
const procId = req.params.id;
const ip = req.headers['x-real-ip'] || req.ip || 'N/A';
if (!PROCPOOL[procId]) {
log.error(`Can't locate proc ${procId} to kill (ip: ${ip})`);
return res.status(404).send('找不到进程');
}
killProc(PROCPOOL[procId].proc);
PROCPOOL[procId] = null;
log.notice(`Killed proc ${procId} (ip: ${ip})`);
res.status(200).send('成功结束');
});
app.get('/stream/:id', (req, res) => {
const procId = req.params.id;
const ip = req.headers['x-real-ip'] || req.ip || 'N/A';
if (!PROCPOOL[procId]) {
log.warn(`Can't locate proc ${procId} (ip: ${ip})`);
return res.status(404).send('找不到进程');
}
res.status(200).send(PROCPOOL[procId].output.text);
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});
const inputAddKey = async () => {
const isFixedKey = await ask('Specify Key?', '[Y/N]', 'N');
let key;
if (tribool(isFixedKey) === 1) {
key = await ask('Key');
} else key = generateUid();
const owner = await ask('Owner Name', '', 'N/A');
let expires = NaN;
while (isNaN(expires)) {
expires = Date.parse(await ask('Expire Date', '[YYYY-MM-DD]'));
}
const configPath = await ask('Config File Path', '[configs/something.json]');
const res = await db.addKey({
key,
owner,
expires,
configPath
});
if (res === db.STAT.okay) {
console.log(`${color.green}Successfully added key ${color.bright}${key}${color.reset}`);
}
return;
}
rl.on('line', async (line) => {
if (line.match(/^\//)) {
try {
return console.log(eval(line.replace(/^\//, '')));
} catch (e) {
return console.error(e);
}
}
const cmd = line.trim().split(' ');
switch (cmd[0]) {
case '':
break;
case 'exit':
process.exit();
case 'stop':
process.exit();
case 'mem':
let mem = await si.mem();
console.log(`${strSize(mem.used)}/${strSize(mem.total)}`);
break;
case 'list':
console.log('Use `lsdb` or `lsproc` instead');
break;
case 'lsdb':
console.table(
(await db.listKeys()
.catch(e => console.error(e))).map((obj) => {
const expires = dateToString(obj.expires);
return {...obj, expires};
})
);
break;
case 'lsproc':
let procs = [];
for (let procId in PROCPOOL) {
if (!PROCPOOL[procId]) continue;
procs.push({
procId,
owner: PROCPOOL[procId].info.owner,
expires: dateToString(PROCPOOL[procId].info.expires),
configPath: PROCPOOL[procId].info.config_path
});
}
console.table(procs);
break;
case 'add':
await inputAddKey();
break;
case 'new':
await inputAddKey();
break;
case 'del':
if (cmd[1] && await db.deleteWithKey(cmd[1]) === db.STAT.okay) {
console.log(`Deleted key ${cmd[1]}`);
}
break;
case 'kill':
if (cmd[1] && cmd[1] in PROCPOOL) {
killProc(PROCPOOL[cmd[1]].proc);
PROCPOOL[cmd[1]] = null;
console.log(`Killed process ${cmd[1]}`);
}
break;
default:
console.log('Unknown command');
break;
}
return;
});