-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first commit, no tests or docs yet of course
- Loading branch information
0 parents
commit ef836f6
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#IP Carefully | ||
|
||
Creates HTTP or HTTPS agents for node.js that can filter based on IP. | ||
Accepts whitelists or blacklists. | ||
|
||
|
||
```javascript | ||
|
||
const IPCarefully = require('ipcarefully'); | ||
const https = IPCarefully.https({ type: 'blacklist', iplist: ['127.0.0.1'], agent: { maxSockets: 5 }); | ||
|
||
https.get('localhost'); //Will throw an error | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
const IPRangeCheck = require('ip-range-check'); | ||
const Http = require('http'); | ||
const Https = require('https'); | ||
const Dns = require('dns'); | ||
|
||
class Lookup { | ||
|
||
constructor(settings) { | ||
|
||
assert(this.type === 'whitelist' || this.type === 'blacklist', 'type must be whitelist or blacklist'); | ||
this.type = settings.type; | ||
this.iplist = settings.iplist; | ||
}, | ||
|
||
filter(hostname, options, callback) { | ||
|
||
if (arguments.length ===2) { | ||
callback = options; | ||
options = {}; | ||
} | ||
|
||
Dns.lookup(hostname, options, (err, address, family) => { | ||
|
||
if (err) { | ||
callback(err); | ||
return; | ||
} | ||
|
||
const ip_test = IPRangeCheck(address, this.iplist); | ||
|
||
if (ip_test && this.type === 'whitelist') { //IP on whitelist, pass | ||
callback(err, address, family); | ||
return; | ||
} | ||
if (!ip_test && this.type === 'blacklist') { //IP not on blacklist, pass | ||
callback(err, address, family); | ||
return; | ||
} | ||
//Fail | ||
callback(new Error(`Connection to IP ${address} not allowed`)); | ||
return; | ||
}); | ||
} | ||
|
||
}; | ||
|
||
exports.http = function (settings) { | ||
|
||
const httpAgent = new Http.Agent(settings.agent); | ||
const lookup = new (Lookup(settings)); | ||
|
||
httpAgent._oldCreateConnection_ = httpAgent.createConnection; | ||
httpAgent.createConnection = function (options) { | ||
|
||
options.lookup = lookup.filter; | ||
return this._oldCreateConnection_(...arguments); | ||
}; | ||
|
||
return httpAgent; | ||
}; | ||
|
||
exports.https = function (options) { | ||
|
||
const httpsAgent = new Https.Agent(settings.agent); | ||
const lookup = new (Lookup(settings)); | ||
|
||
httpsAgent._oldCreateConnection_ = httpsAgent.createConnection; | ||
httpsAgent.createConnection = function (options) { | ||
|
||
options.lookup = lookup.filter; | ||
return this._oldCreateConnection_(...arguments); | ||
}; | ||
|
||
return httpsAgent; | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "ipcarefully", | ||
"version": "1.0.0", | ||
"description": "Creates HTTP or HTTPS agents for node.js that can filter based on IP", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"ip", | ||
"filter", | ||
"http", | ||
"https", | ||
"agent" | ||
], | ||
"author": "Gar <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"ip-range-check": "0.0.2" | ||
} | ||
} |