Skip to content

Commit

Permalink
first commit, no tests or docs yet of course
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Oct 19, 2017
0 parents commit ef836f6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
13 changes: 13 additions & 0 deletions README.md
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
```
77 changes: 77 additions & 0 deletions index.js
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;
};
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions package.json
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"
}
}

0 comments on commit ef836f6

Please sign in to comment.