This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
/
mongo.js
57 lines (45 loc) · 1.45 KB
/
mongo.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
/*
Copyright Jesús Pérez <jesusprubio@gmail.com>
This code may only be used under the GPLv3 license found at
http://www.gnu.org/licenses/gpl-3.0.txt.
*/
'use strict';
const mongo = require('mongodb');
const utils = require('../utils');
const Promise = utils.Promise;
const dbg = utils.dbg(__filename);
const client = mongo.MongoClient;
module.exports.brute = (rhost, credPair, opts = {}) =>
new Promise((resolve, reject) => {
const result = { up: false, authed: false };
const port = opts.rport || 27017;
const timeout = opts.timeout || 5000;
let authStr = '';
if (credPair && credPair[0]) { authStr = `${credPair[0]}:${credPair[1]}@`; }
dbg('Client setup:', { rhost, port, timeout, authStr });
client.connect(
`mongodb://${rhost}:${port}/admin?autoReconnect=false&connectTimeoutMS=${timeout}`,
{
numberOfRetries: 0, // by default tries 5 times
retryMiliSeconds: 0, // just in case
},
(err) => {
// TODO: Destroy/close client, not supported by the module
if (err) {
// Only in this case we want to stop the chain
if (/auth failed/.exec(err)) {
dbg('NOT valid', credPair);
result.up = true;
resolve(result);
} else {
reject(err);
}
return;
}
dbg('Valid', credPair);
result.up = true;
result.authed = true;
resolve(result);
}
);
});