-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
executable file
·312 lines (290 loc) · 9.96 KB
/
main.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
#!/bin/sh
':'; //; exec "$(command -v nodejs || command -v node)" "${0}" "${@}"
'use strict';
// imports
const pkgjson = require('./package.json'),
fs = require('fs'),
https = require('https'),
readline = require('readline'),
isOnline = require('is-online'),
ora = require('ora'),
chalk = require('chalk'),
github = require('github-api'),
whoami = require('username');
// colors
const error = chalk.bgRed.bold.white,
warning = chalk.bold.yellow,
extra = chalk.bgMagenta.bold.white,
info = chalk.bold.underline.cyan,
emphasis = chalk.bold.underline.green,
highlight = chalk.bold.bgGreen.black;
// github repo vars
const github_raw = 'https://raw.githubusercontent.com',
username = 'v1s1t0r1sh3r3',
project = 'airgeddon';
var branch = 'master',
availableBranches = [],
filenameOne = 'airgeddon.sh',
filenameTwo = 'language_strings.sh',
filenameThree = 'known_pins.db',
filenameFour = 'README.md',
filenameFive = 'LICENSE.md',
filenameSix = '.airgeddonrc',
airgeddon_raw,
langstrings_raw,
pindb_raw,
readme_raw,
license_raw,
options_raw,
customBranch = false,
args = process.argv.slice(2);
function init () {
switch (args[0]) {
case undefined:
isOnline().then(online => {
if (online) {
updateRawFiles();
prepare()
} else {
console.error(error('No Internet connection'));
process.exit(1)
}
});
break;
case '-b':
case '--branch':
isOnline().then(online => {
if (online) {
getRepoBranches(username, project);
var i,
branchesString = '',
interval = setInterval(function () {
if (availableBranches.length > 0) {
clearInterval(interval);
for (i = 0; i < availableBranches.length; i++) { branchesString += availableBranches[i] + ', ' }
console.log('Available branches: ' +
extra(branchesString.replace(/,\s*$/, '')));
insertBranch()
}
}, 10);
var intervalTwo = setInterval(function () {
if (customBranch) {
clearInterval(intervalTwo);
updateRawFiles();
prepare()
}
}, 10)
} else {
console.error(error('No Internet connection'));
process.exit(1)
}
});
break;
case '-v':
case '--version':
console.log(' ' + pkgjson.name + ' ' + info('v' + pkgjson.version));
break;
case '-h':
case '--help':
usage();
process.exit(0);
break;
default:
console.log('\n' + ' Invalid option ' + '"' + error(args[0]) + '"');
usage();
process.exit(1)
}
return true
}
function usage () {
console.log('\n' +
' Version: ' + info(pkgjson.version) + ' Usage: ' + pkgjson.name + ' [options]' + '\n' + '\n' +
' ' + emphasis('Download airgeddon required files from github') + '\n' + '\n' +
' Options:' + '\n' + '\n' +
' -h, --help' + '\t' + 'output usage information' + '\n' +
' -v, --version' + '\t' + 'output the version number' + '\n' +
' -b, --branch' + '\t' + 'output available branches and specify from which one to download' + '\n' + '\n');
return true
}
function canWrite (path, callback) {
fs.access(path, fs.W_OK, function (err) {
callback(null, !err)
});
return true
}
function newGitApi () {
return new github()
}
function getRepoBranches (userName, projectName) {
const api = newGitApi();
const repo = api.getRepo(userName, projectName);
var i, branchesString = '';
repo.listBranches(function (err, branches) {
if (err) {
console.error(error('Failed to list branches'));
process.exit(1)
}
for (i = 0; i < branches.length; i++) { availableBranches.push(branches[i].name) }
}
);
return true
}
function insertBranch () {
/* create I/O interface */
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
rl.question('Insert preferred branch and press [ENTER]: ', (input) => {
if (availableBranches.indexOf(input) > -1) {
branch = input;
customBranch = true
} else if (input === '') {
branch = 'master';
customBranch = true
} else {
console.error(error('Branch ' + '"' + input + '"' + ' does not exist!'));
process.exit(1)
}
/* close the I/O interface */
rl.close()
});
return true
}
function updateRawFiles () {
airgeddon_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameOne;
langstrings_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameTwo;
pindb_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameThree;
readme_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameFour;
license_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameFive;
options_raw = github_raw + '/' + username + '/' + project + '/' + branch + '/' + filenameSix;
return true
}
function prepare () {
/* create I/O interface */
const rl = readline.createInterface({input: process.stdin, output: process.stdout});
/* validate input, dirs, and handle fs */
rl.question('Insert directory and press [ENTER]: ', (path) => {
if (path === '') {
canWrite('./', function (err, isWritable) {
if (err) console.error(error('No write permissions, run as root'));
if (isWritable) {
if (!fs.existsSync('./airgeddon/')) { fs.mkdirSync('./airgeddon/') }
download('./airgeddon/' + filenameOne,
'./airgeddon/' + filenameTwo,
'./airgeddon/' + filenameThree,
'./airgeddon/' + filenameFour,
'./airgeddon/' + filenameFive,
'./airgeddon/' + filenameSix,
)
}
})
} else if (path.charAt(0) === '~') {
var fixedPath = '/home/' + whoami.sync() + '/';
if (fs.existsSync(fixedPath)) {
canWrite(fixedPath, function (err, isWritable) {
if (err) console.error(error('No write permissions, run as root'));
if (isWritable) {
if (!fs.existsSync(fixedPath + 'airgeddon/')) { fs.mkdirSync(fixedPath + 'airgeddon/') }
download(fixedPath + 'airgeddon/' + filenameOne,
fixedPath + 'airgeddon/' + filenameTwo,
fixedPath + 'airgeddon/' + filenameThree,
fixedPath + 'airgeddon/' + filenameFour,
fixedPath + 'airgeddon/' + filenameFive,
fixedPath + 'airgeddon/' + filenameSix,
)
}
})
}
} else if (path.slice(-1) === '/') {
if (fs.existsSync(path)) {
canWrite(path, function (err, isWritable) {
if (err) console.error(error('No write permissions, run as root'));
if (isWritable) {
if (!fs.existsSync(path + 'airgeddon/')) { fs.mkdirSync(path + 'airgeddon/') }
download(path + 'airgeddon/' + filenameOne,
path + 'airgeddon/' + filenameTwo,
path + 'airgeddon/' + filenameThree,
path + 'airgeddon/' + filenameFour,
path + 'airgeddon/' + filenameFive,
path + 'airgeddon/' + filenameSix,
)
}
})
}
} else {
if (fs.existsSync(path)) {
canWrite(path, function (err, isWritable) {
if (err) console.error(error('No write permissions, run as root'));
if (isWritable) {
if (!fs.existsSync(path + '/airgeddon/')) { fs.mkdirSync(path + '/airgeddon/') }
download(path + 'airgeddon/' + filenameOne,
path + 'airgeddon/' + filenameTwo,
path + 'airgeddon/' + filenameThree,
path + 'airgeddon/' + filenameFour,
path + 'airgeddon/' + filenameFive,
path + 'airgeddon/' + filenameSix,
)
}
})
}
}
/* close the I/O interface */
rl.close()
});
return true
}
function download (file1, file2, file3, file4, file5, file6) {
/* create spinners with proper messages */
const spinnerOne = ora('Downloading ' + filenameOne);
const spinnerTwo = ora('Downloading ' + filenameTwo);
const spinnerThree = ora('Downloading ' + filenameThree);
const spinnerFour = ora('Downloading ' + filenameFour);
const spinnerFive = ora('Downloading ' + filenameFive);
const spinnerSix = ora('Downloading ' + filenameSix);
setTimeout(function () {
spinnerOne.start();
var airgeddon = fs.createWriteStream(file1);
https.get(airgeddon_raw, function (response) {
response.pipe(airgeddon)
});
spinnerOne.succeed('Downloaded ' + filenameOne);
spinnerTwo.start()
}, 300);
setTimeout(function () {
var langstrings = fs.createWriteStream(file2);
https.get(langstrings_raw, function (response) {
response.pipe(langstrings)
});
spinnerTwo.succeed('Downloaded ' + filenameTwo);
spinnerThree.start()
}, 450);
setTimeout(function () {
var pindb = fs.createWriteStream(file3);
https.get(pindb_raw, function (response) {
response.pipe(pindb)
});
spinnerThree.succeed('Downloaded ' + filenameThree)
}, 500);
setTimeout(function () {
var readme = fs.createWriteStream(file4);
https.get(readme_raw, function (response) {
response.pipe(readme)
});
spinnerThree.succeed('Downloaded ' + filenameFour)
}, 600);
setTimeout(function () {
var license = fs.createWriteStream(file5);
https.get(license_raw, function (response) {
response.pipe(license)
});
spinnerThree.succeed('Downloaded ' + filenameFive)
}, 650);
setTimeout(function () {
var license = fs.createWriteStream(file6);
https.get(options_raw, function (response) {
response.pipe(license)
});
spinnerThree.succeed('Downloaded ' + filenameSix)
}, 700);
return true
}
/* export the module / start */
module.exports = init();