-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
331 lines (309 loc) · 12.7 KB
/
index.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
331
const fs = require('fs');
const { spawn } = require('child_process');
const path = require('path');
const minecraftCheck = require('./minecraftCheck.js');
const config = require('./config.json');
function fullPort(port) {
const dupeCheck = new Set();
const queue = [];
const writeStream = fs.createWriteStream('./ips1');
async function write() {
if (queue.length > 0) {
const buffer = Buffer.concat(queue);
queue.splice(0);
if (!writeStream.write(buffer)) await new Promise((res) => writeStream.once('drain', res));
}
setTimeout(write);
}
write();
const childProcess = spawn('sh', ['-c', `${config.sudo ? 'sudo ' : '' }masscan -p ${port} 0.0.0.0/0 --rate=${config.packetLimit} --excludefile ./exclude.conf -oJ -`]);
let leftOver = '';
childProcess.stdout.on('data', async (data) => {
let string = data.toString();
string = leftOver + string;
leftOver = '';
const items = string.split('\n,\n');
for (let i = 0; i < items.length; i++) {
let line = items[i];
if (line.startsWith('[\n')) line = line.substring(2);
if (line.endsWith('\n]\n')) line = line.substring(0, line.length - 3);
try {
const obj = JSON.parse(line);
for (const port of obj.ports) {
const splitIP = obj.ip.split('.');
const buffer = Buffer.from([
parseInt(splitIP[0]),
parseInt(splitIP[1]),
parseInt(splitIP[2]),
parseInt(splitIP[3]),
Math.floor(port.port / 256),
port.port % 256
]);
if (!dupeCheck.has(buffer.toString('hex'))) {
dupeCheck.add(buffer.toString('hex'));
queue.push(buffer);
}
}
} catch (err) {
leftOver = items[items.length - 1];
}
}
});
childProcess.stderr.on('data', (data) => console.log('[1] ', data.toString()));
childProcess.on('close', async (code) => {
if (code === 0) {
console.log('[1] ', 'Masscan finished.');
await (new Promise(res => {
const interval = setInterval(() => {
if (queue.length == 0) {
clearInterval(interval);
res();
} else console.log('[1] ', `Finishing write queue: ${queue.length} servers remanining.`);
}, 300);
}));
writeStream.end();
dupeCheck.clear();
await minecraftCheck('./ips1', './ips1Filtered', '[1] ');
known24s();
} else console.error('[1] ', `Command exited with code ${code}`);
});
}
async function known24s() {
fs.copyFileSync('./ips1Filtered', './ips2Filtered');
if (config.scan24s) {
const dupeCheck = new Set();
const queue = [];
const writeStream = fs.createWriteStream('./ips2');
const includeWriteStream = fs.createWriteStream('./includeFile.txt');
await (new Promise((resolve, reject) => {
const size = fs.statSync('ips1Filtered').size;
const stream = fs.createReadStream('ips1Filtered');
let sizeWritten = 0;
console.log('[2] ', `Gathering last scan data: ${sizeWritten}/${size} (${Math.floor(sizeWritten / size * 100)}%)`);
const logInterval = setInterval(() => { console.log('[2] ', `Gathering last scan data: ${sizeWritten}/${size} (${Math.floor(sizeWritten / size * 100)}%)`); }, 2000);
const written24s = new Set();
let lastData = null;
stream.on('data', (data) => {
if (lastData != null) data = Buffer.concat([lastData, data]);
for (let i = 0; i < Math.floor(data.length / 6) * 6; i += 6) {
sizeWritten += 6;
if (written24s.has(data.subarray(i, i + 3).toString())) continue;
written24s.add(data.subarray(i, i + 3).toString());
queue.push(`${(sizeWritten == 6 && i == 0) ? '' : ',\n'}${data[i]}.${data[i + 1]}.${data[i + 2]}.0/24`)
}
lastData = data.length % 6 == 0 ? null : data.slice(Math.floor(data.length / 6) * 6);
}).on('error', err => {
throw err;
}).on('end', () => {});
async function finishCheck() {
if (sizeWritten == size && queue.length == 0) {
console.log('[2] ', 'Finished gathering last scan data.');
includeWriteStream.close();
clearInterval(logInterval);
written24s.clear();
resolve();
} else {
if (queue.length > 0) {
const queueCopy = [...queue];
queue.splice(0);
if (!includeWriteStream.write(queueCopy.join(''))) await new Promise((res) => includeWriteStream.once('drain', res));
}
setTimeout(finishCheck);
}
}
finishCheck();
}));
async function write() {
if (queue.length > 0) {
const buffer = Buffer.concat(queue);
queue.splice(0);
if (!writeStream.write(buffer)) await new Promise((res) => writeStream.once('drain', res));
}
setTimeout(write);
}
write();
const childProcess = spawn('sh', ['-c', `${config.sudo ? 'sudo ' : '' }masscan -p 25500-25564,25566-25700 --include-file includeFile.txt --rate=${config.packetLimit} --excludefile exclude.conf -oJ -`]);
let leftOver = '';
childProcess.stdout.on('data', async (data) => {
let string = data.toString();
string = leftOver + string;
leftOver = '';
const items = string.split('\n,\n');
for (let i = 0; i < items.length; i++) {
let line = items[i];
if (line.startsWith('[\n')) line = line.substring(2);
if (line.endsWith('\n]\n')) line = line.substring(0, line.length - 3);
try {
const obj = JSON.parse(line);
for (const port of obj.ports) {
const splitIP = obj.ip.split('.');
const buffer = Buffer.from([
parseInt(splitIP[0]),
parseInt(splitIP[1]),
parseInt(splitIP[2]),
parseInt(splitIP[3]),
Math.floor(port.port / 256),
port.port % 256
]);
if (!dupeCheck.has(buffer.toString('hex'))) {
dupeCheck.add(buffer.toString('hex'));
queue.push(buffer);
}
}
} catch (err) {
leftOver = items[items.length - 1];
}
}
});
childProcess.stderr.on('data', (data) => console.log('[2] ', data.toString()));
childProcess.on('close', async (code) => {
if (code === 0) {
fs.unlinkSync('./includeFile.txt');
console.log('[2] ', 'Masscan finished');
await (new Promise(res => {
const interval = setInterval(() => {
if (queue.length == 0) {
clearInterval(interval);
res();
} else console.log('[2] ', `Finishing write queue: ${queue.length} servers remanining.`);
}, 300);
}));
writeStream.end();
await minecraftCheck('./ips2', './ips2Filtered', '[2] ', 'a');
knownIps();
} else console.error('[2] ', `Command exited with code ${code}`);
});
} else {
knownIps();
}
}
async function knownIps() {
fs.copyFileSync('./ips2Filtered', './ips');
if (config.scanAllPorts) {
const dupeCheck = new Set();
const queue = [];
const writeStream = fs.createWriteStream('./ipsUnfiltered');
const includeWriteStream = fs.createWriteStream('./includeFile.txt');
await (new Promise((resolve, reject) => {
const size = fs.statSync('ips2Filtered').size;
const stream = fs.createReadStream('ips2Filtered');
let sizeWritten = 0;
console.log('[3] ', `Gathering last scan data: ${sizeWritten}/${size} (${Math.floor(sizeWritten / size * 100)}%)`);
const logInterval = setInterval(() => { console.log('[3] ', `Gathering last scan data: ${sizeWritten}/${size} (${Math.floor(sizeWritten / size * 100)}%)`); }, 2000);
const written24s = new Set();
let lastData = null;
stream.on('data', (data) => {
if (lastData != null) data = Buffer.concat([lastData, data]);
for (let i = 0; i < Math.floor(data.length / 6) * 6; i += 6) {
sizeWritten += 6;
if (written24s.has(data.subarray(i, i + 3).toString())) continue;
written24s.add(data.subarray(i, i + 3).toString());
queue.push(`${(sizeWritten == 6 && i == 0) ? '' : ',\n'}${data[i]}.${data[i + 1]}.${data[i + 2]}.${data[i + 3]}`)
}
lastData = data.length % 6 == 0 ? null : data.slice(Math.floor(data.length / 6) * 6);
}).on('error', err => {
throw err;
}).on('end', () => {});
async function finishCheck() {
if (sizeWritten == size && queue.length == 0) {
console.log('[3] ', 'Finished gathering last scan data.');
includeWriteStream.close();
clearInterval(logInterval);
written24s.clear();
resolve();
} else {
if (queue.length > 0) {
const queueCopy = [...queue];
queue.splice(0);
if (!includeWriteStream.write(queueCopy.join(''))) await new Promise((res) => includeWriteStream.once('drain', res));
}
setTimeout(finishCheck);
}
}
finishCheck();
}));
async function write() {
if (queue.length > 0) {
const buffer = Buffer.concat(queue);
queue.splice(0);
if (!writeStream.write(buffer)) await new Promise((res) => writeStream.once('drain', res));
}
setTimeout(write);
}
write();
const childProcess = spawn('sh', ['-c', `${config.sudo ? 'sudo ' : '' }masscan -p 1025-25499,25701-65535 --include-file includeFile.txt --rate=${config.packetLimit} --excludefile exclude.conf -oJ -`]);
let leftOver = '';
childProcess.stdout.on('data', async (data) => {
let string = data.toString();
string = leftOver + string;
leftOver = '';
const items = string.split('\n,\n');
for (let i = 0; i < items.length; i++) {
let line = items[i];
if (line.startsWith('[\n')) line = line.substring(2);
if (line.endsWith('\n]\n')) line = line.substring(0, line.length - 3);
try {
const obj = JSON.parse(line);
for (const port of obj.ports) {
const splitIP = obj.ip.split('.');
const buffer = Buffer.from([
parseInt(splitIP[0]),
parseInt(splitIP[1]),
parseInt(splitIP[2]),
parseInt(splitIP[3]),
Math.floor(port.port / 256),
port.port % 256
]);
if (!dupeCheck.has(buffer.toString('hex'))) {
dupeCheck.add(buffer.toString('hex'));
queue.push(buffer);
}
}
} catch (err) {
leftOver = items[items.length - 1];
}
}
});
childProcess.stderr.on('data', (data) => console.log('[3] ', data.toString()));
childProcess.on('close', async (code) => {
if (code === 0) {
fs.unlinkSync('./includeFile.txt');
console.log('[3] ', 'Masscan finished');
await (new Promise(res => {
const interval = setInterval(() => {
if (queue.length == 0) {
clearInterval(interval);
res();
} else console.log('[3] ', `Finishing write queue: ${queue.length} servers remanining.`);
}, 300);
}));
writeStream.end();
await minecraftCheck('./ipsUnfiltered', './ips', '[3] ', 'a');
if (config.gitPush) {
const childProcess = spawn('sh', ['-c', `git config --global user.email "${config.gitEmail}" ; git config --global user.name "${config.gitUser}" ; git add ips ; git commit -m "${Math.round((new Date()).getTime() / 1000)}" ; git push`]);
childProcess.stdout.on('data', (data) => console.log('[3] ', data.toString()));
childProcess.stderr.on('data', (data) => console.error('[3] ', data.toString()));
childProcess.on('close', async (code) => {
if (code != 0) console.error(`Command exited with code ${code}`);
if (config.repeat) fullPort(25565);
});
} else {
if (config.repeat) fullPort(25565);
}
} else console.error('[3] ', `Command exited with code ${code}`);
});
} else {
if (config.gitPush) {
const childProcess = spawn('sh', ['-c', `git config --global user.email "${config.gitEmail}" ; git config --global user.name "${config.gitUser}" ; git add ips ; git commit -m "${Math.round((new Date()).getTime() / 1000)}" ; git push`]);
childProcess.stdout.on('data', (data) => console.log('[3] ', data.toString()));
childProcess.stderr.on('data', (data) => console.error('[3] ', data.toString()));
childProcess.on('close', async (code) => {
if (code != 0) console.error(`Command exited with code ${code}`);
if (config.repeat) fullPort(25565);
});
} else {
if (config.repeat) fullPort(25565);
}
}
}
fullPort(25565);