-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
330 lines (330 loc) · 12.8 KB
/
server.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Discord = require('discord.js');
var dotenv = require('dotenv');
var _a = require('./config.json'), prefix = _a.prefix, cmdPrefix = _a.cmdPrefix;
var exec = require('child_process').exec;
var fs = require('fs');
var fileSize = 0;
var errorfileSize = 0;
var adminfileSize = 0;
var erroradminfileSize = 0;
fs.readFile(__dirname + '/log', function (err, data) {
fileSize = data.length;
});
fs.readFile(__dirname + '/errorlog', function (err, data) {
errorfileSize = data.length;
});
fs.readFile(__dirname + '/adminlog', function (err, data) {
adminfileSize = data.length;
});
fs.readFile(__dirname + '/erroradminlog', function (err, data) {
erroradminfileSize = data.length;
});
var client = new Discord.Client();
dotenv.config();
client.on('ready', function () {
console.log('a');
console.log("Logged in as " + client.user.tag + "!");
});
client.on('message', function (msg) {
if (msg.author.bot) {
console.log("bot");
return;
}
// return;
msg.channel.send('ping');
console.log(msg.content);
var line = msg.content;
// msg.channel.send(line);
if (msg.author.bot) {
console.log("bot");
return;
}
if (line[0] == '!') {
var command = line.slice(1);
if (command == 'start') {
msg.channel.send('Starting...');
exec('sudo systemctl start compilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
fs.writeFile(__dirname + '/log', '', function (err) {
if (err)
msg.channel.send('Could not empty log file');
// else msg.channel.send('Start process complete');
});
fs.writeFile(__dirname + '/errorlog', '', function (err) {
if (err)
msg.channel.send('Could not empty error log file');
else
msg.channel.send('Start process complete');
});
}
else if (command == 'startadmin') {
msg.channel.send('Starting admin...');
exec('sudo systemctl start admincompilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
fs.writeFile(__dirname + '/adminlog', '', function (err) {
if (err)
msg.channel.send('Could not empty log file');
// else msg.channel.send('Start process complete');
});
fs.writeFile(__dirname + '/erroradminlog', '', function (err) {
if (err)
msg.channel.send('Could not empty error log file');
else
msg.channel.send('Start process complete');
});
}
else if (command == 'stop') {
msg.channel.send('Stopping...');
exec('sudo systemctl stop compilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
}
else if (command == 'stopadmin') {
msg.channel.send('Stopping admin...');
exec('sudo systemctl stop admincompilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
}
else if (command == 'restart') {
msg.channel.send('Restarting...');
exec('sudo systemctl restart compilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
fs.writeFile(__dirname + '/log', '', function (err) {
if (err)
msg.channel.send('Could not empty log file');
// else msg.channel.send('Restart complete');
});
fs.writeFile(__dirname + '/errorlog', '', function (err) {
if (err)
msg.channel.send('Could not empty error log file');
else
msg.channel.send('Restart complete');
});
}
else if (command == 'restartadmin') {
msg.channel.send("Restarting...これもスパムしないほうがいいね");
exec('sudo systemctl restart admincompilerserver', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
fs.writeFile(__dirname + '/adminlog', '', function (err) {
if (err)
msg.channel.send('Could not empty log file');
else
msg.channel.send('Restart complete');
});
fs.writeFile(__dirname + '/erroradminlog', '', function (err) {
if (err)
msg.channel.send('Could not empty error log file');
else
msg.channel.send('Restart complete');
});
}
else if (command == 'update') {
msg.channel.send("Updating... スパムしないで頑張ってるから");
exec('git -C /home/pi/Compiler stash', function (err, stdout, stderr) {
if (err)
msg.channel.send('Compiler Stash Failed');
else
msg.channel.send('Compiler Stash Successful');
exec('git -C /home/pi/Compiler pull ', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
exec('chmod +x /home/pi/Compiler/server/nodejs/https_server.js', function (err, stdout, stderr) {
if (err)
msg.channel.send('Chmod Failed');
else
msg.channel.send('Chmod Successful');
});
});
});
exec('git -C /home/pi/AdminCompilerServer stash', function (err, stdout, stderr) {
if (err)
msg.channel.send('Admin Stash Failed');
else
msg.channel.send('Admin Stash Successful');
exec('git -C /home/pi/AdminCompilerServer pull ', function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
exec('chmod +x /home/pi/AdminCompilerServer/server/nodejs/https_server.js', function (err, stdout, stderr) {
if (err)
msg.channel.send('Admin Chmod Failed');
else
msg.channel.send('Admin Chmod Successful');
});
});
});
}
else {
var args_1 = command.split(' ');
if (args_1[0] == 'ipblock') {
if (!(args_1[1] === undefined)) {
fs.appendFile('/home/pi/ipBlacklist', args_1[1] + ';\n', function (err) {
if (!err) {
msg.channel.send('Blacklisted ' + args_1[1]);
}
});
}
else {
msg.channel.send('Expected ip address as second argument.');
}
}
}
}
else {
var command = line;
msg.channel.send(command);
exec(command, function (err, stdout, stderr) {
msg.channel.send('標準出力');
if (stdout)
msg.channel.send(stdout);
msg.channel.send('標準エラー');
if (stderr)
msg.channel.send(stderr);
if (err)
msg.channel.send('Command Failed');
else
msg.channel.send('Command Successful');
});
}
});
fs.watchFile(__dirname + '/log', function (curr, prev) {
console.log('file changed');
fs.readFile(__dirname + '/log', function (err, data) {
if (data.length == 0) {
fileSize = 0;
}
else {
console.log(data.length);
console.log(fileSize);
var change_1 = data.slice(fileSize);
console.log(change_1.toString());
client.channels.fetch('824546860655837194').then(function (channel) {
channel.send('```' + change_1.toString() + '```');
});
fileSize = data.length;
}
});
});
fs.watchFile(__dirname + '/errorlog', function (curr, prev) {
console.log('file changed');
fs.readFile(__dirname + '/errorlog', function (err, data) {
if (data.length == 0) {
errorfileSize = 0;
}
else {
console.log(data.length);
console.log(errorfileSize);
var change_2 = data.slice(errorfileSize);
console.log(change_2.toString());
client.channels.fetch('824546860655837194').then(function (channel) {
channel.send('```' + change_2.toString() + '```');
});
errorfileSize = data.length;
}
});
});
fs.watchFile(__dirname + '/adminlog', function (curr, prev) {
console.log('admin file changed');
fs.readFile(__dirname + '/adminlog', function (err, data) {
if (data.length == 0) {
adminfileSize = 0;
}
else {
console.log(data.length);
console.log(adminfileSize);
var change_3 = data.slice(adminfileSize);
console.log(change_3.toString());
client.channels.fetch('828560653341163550').then(function (channel) {
channel.send('```' + change_3.toString() + '```');
});
adminfileSize = data.length;
}
});
});
fs.watchFile(__dirname + '/erroradminlog', function (curr, prev) {
console.log('admin file changed');
fs.readFile(__dirname + '/erroradminlog', function (err, data) {
if (data.length == 0) {
erroradminfileSize = 0;
}
else {
console.log(data.length);
console.log(erroradminfileSize);
var change_4 = data.slice(erroradminfileSize);
console.log(change_4.toString());
client.channels.fetch('828560653341163550').then(function (channel) {
channel.send('```' + change_4.toString() + '```');
});
erroradminfileSize = data.length;
}
});
});
client.login(process.env.TOKEN);