-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.js
223 lines (216 loc) · 8.34 KB
/
config.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
'use strict';
var imgur = require('imgur'),
fork = require('child_process').fork,
spawn = require('child_process').spawn,
fs = require('fs-extra'),
os = require('os'),
path = require('path'),
rls = require('readline-sync');
//Initialize some parameters that will be used before module.exports can be set
var autoUpdate = true;
var hasUpdate = false;
var botPath = __dirname;
var project = {};
var config = {};
var warning = "";
var showWarning = function(warn) {
rls.question(warn, {
hideEchoBack: true,
mask: ''
});
};
imgur.setClientId('0e74360592046d7');
//If the update/ directory exists, the program is waiting to apply an update
try {
var stats = fs.statSync(path.join(botPath, 'update'));
if (stats.isDirectory()) {
hasUpdate = true;
}
} catch (e) {}
//Some parameters are fetched from package.json
try {
var projectFile = fs.readFileSync(path.join(botPath, 'package.json'), 'utf8');
project = JSON.parse(projectFile);
} catch (e) {
//Show the warning right in the beginning
showWarning('The package.json file is missing from the working directory or it is not valid JSON. Auto updates will be disabled, and you might encounter unexpected behavior. Make sure the project was downloaded/installed properly. Press Enter to continue anyway');
autoUpdate = false;
}
//Other parameters are fetched from a .botatorc file
try { //Check in working directory first
var rcFile = fs.readFileSync(path.join(botPath, '.botatorc'), 'utf8');
try {
config = JSON.parse(rcFile);
} catch (ex) {
warning = 'The .botatorc file found in your working directory is not valid JSON. Press Enter to use default config values';
}
} catch (e) {
try { //Check in home directory next
var rcHomeFile = fs.readFileSync(path.join(os.homedir(), '.botatorc'), 'utf8');
try {
config = JSON.parse(rcHomeFile);
} catch (exh) {
warning = 'The .botatorc file found in your home directory is not valid JSON. Press Enter to use default config values';
}
} catch (e) {
warning = 'No .botatorc file found in working directory or your home directory. It is recommended to create one to password protect your bot and skip manually typing runtime arguments. With the default config values, everyone in the same network will have shell access to your device. Press Enter if you know what you\'re doing';
}
}
//Finally bundle all the parameters into an object and export it
module.exports = {
shells: [],
history: [],
authorized: [],
name: project.name || 'Botato',
executable: project.main || 'botato.js',
version: project.version,
release: 'alpha',
path: botPath,
imgur: imgur,
hasUpdate: hasUpdate,
updateURL: 'https://api.github.com/repos/EnKrypt/Botato/releases/',
autoUpdate: (!autoUpdate) ? autoUpdate : ((typeof config.autoUpdate === 'undefined') ? autoUpdate : config.autoUpdate),
updateInterval: config.updateInterval || 18000000,
promptForArgs: (typeof config.promptForArgs === 'undefined') ? true : config.autoUpdate, //Set to false while running purely headless or for strictness testing
args: process.argv.slice(2).length ? process.argv.slice(2) : (config.args ? config.args : []),
shortName: config.shortName || 'Bot',
commandPrefix: config.commandPrefix || '!',
shellPrefix: config.shellPrefix || '~',
nickSeperator: config.nickSeperator || '@',
usePassword: config.usePassword || false,
password: config.password,
warn: warning,
showWarning: showWarning,
//Offers basic protection from attacks via network-type and command names
noSpecialChars: function(str) {
if (/^[a-zA-Z0-9- ]*$/.test(str)) {
return str;
} else {
throw new Error('Special characters not allowed');
}
},
shellWithIDexists: function(bot, id) {
if (!id) {
return true;
}
for (var key in bot.shells) {
if (bot.shells[key].id == id) {
return true;
}
}
return false;
},
// I unfortunately gave my word to someone that this would be what I would call this next function
createChildren: function(bot, shellObject, command) {
var proc = spawn(command[0], command.slice(1), {
detached: true,
stdio: ['pipe', 'pipe', 'pipe']
});
proc.stdout.on('data', function(data) {
bot.connection.send('Shell ' + shellObject.id, data.toString('utf8'), false);
});
proc.stderr.on('data', function(data) {
bot.connection.send('Shell ' + shellObject.id, data.toString('utf8'), false);
});
proc.on('close', function(code) {
if (shellObject.verbose) {
bot.connection.send('Shell ' + shellObject.id, 'exited with code: ' + code, true);
}
shellObject.running = false;
if (!shellObject.interactive) {
bot.removeShell(bot, shellObject.id);
}
});
proc.on('error', function(err) {
bot.connection.send('Shell ' + shellObject.id, 'Could not run command `' + command.join(' ') + '`', true);
console.log('Error while running shell command', err);
});
return proc;
},
addShell: function(bot, id, command) {
var shellObject = {
id: id,
interactive: false,
running: command.length ? true : false,
suppressed: [],
verbose: true,
command: command
};
if (command.length) {
shellObject.proc = bot.createChildren(bot, shellObject, command);
}
bot.shells.push(shellObject);
},
removeShell: function(bot, id) {
for (var key in bot.shells) {
if (bot.shells[key].id == id) {
bot.shells.splice(key, 1);
}
}
},
runShellCommand: function(bot, command) {
var interactiveShellObject = bot.shells[bot.getInteractiveIndex(bot)];
interactiveShellObject.verbose = false;
interactiveShellObject.proc = bot.createChildren(bot, interactiveShellObject, command);
},
getInteractiveIndex: function(bot) {
for (var key in bot.shells) {
if (bot.shells[key].interactive) {
return key;
}
}
return -1;
},
removeInteractive: function(bot, id, out) {
var flag = true;
for (var key in bot.shells) {
if (bot.shells[key].id == id) {
flag = false;
if (bot.shells[key].interactive) {
bot.shells[key].interactive = false;
if (bot.shells[key].running) {
out('Removed interactive mode on a shell with ID: ' + bot.shells[key].id + ' - It will be closed when its current job is done');
} else {
bot.removeShell(bot, key);
out('Closed idle interactive shell with ID: ' + bot.shells[key].id);
}
} else {
out('Shell with ID: ' + id + ' is not in interactive mode');
}
}
}
if (flag) {
out('No shell found with ID: ' + id);
}
},
makeInteractive: function(bot, id, out) {
var set = false;
for (var key in bot.shells) {
if (bot.shells[key].interactive) {
bot.removeInteractive(bot, bot.shells[key].id, out);
} else if (bot.shells[key].id == id) {
set = true;
if (!bot.shells[key].interactive) {
bot.shells[key].interactive = true;
out('Shell with ID: ' + id + ' is now in interactive mode');
} else {
out('Shell with ID: ' + id + ' is already in interactive mode');
}
}
}
if (!set) {
out('No shell found with ID: ' + id);
}
},
doUpdate: function(bot, out) {
out('Applying update');
fs.copy(path.join(botPath, 'update/'), botPath, {
clobber: true
}, function() {
fs.remove(path.join(botPath, 'update'), function() {
out('Relaunching ' + bot.name);
fork(bot.executable, bot.args);
});
});
}
};