ip-filtering-tree
v2.1.0
Published
On memory database indexed by IP addresses.
Downloads
108
Maintainers
Readme
ip-filtering-tree
ip-filtering-tree is high performance on-memory database with key of ip address written in JavaScript. Due to benefits of applying tree algorithm, there are little lag even if a lots of record were registered in the db as long as the permit the space of memory.
It can also search data various types flexibly with Longest prefix match(also called Maximum prefix length match) rules by using tree algorithms.
Getting Started
Installing
Installing the module is just run the command like below.
# npm install ip-filtering-tree
Usage
The basic usage of ip-filtering-tree is to call push, find and delete functions after import this module. Examples are like below.
- Instantiate
var IPFilteringTree = require('ip-filtering-tree').IPFilteringTree;
var db = new IPFilteringTree();
- push
// push(<ipaddr>, <subnet mask length>, <data>);
db.push("192.168.1.0", 24, "Data of 192.168.1.0/24");
db.push("192.168.2.0", 24, "Data of 192.168.2.0/24");
db.push("192.168.0.0", 16, "Data of 192.168.0.0/16");
- find
db.find("192.168.1.0"); // -> Data of 192.168.1.0/24
db.find("192.168.2.100"); // -> Data of 192.168.2.0/24
db.find("192.168.101.32"); // -> Data of 192.168.0.0/16
- getAllIndexes
getAllIndexes(); // -> [{ip: "192.168.2.0", mask: 24}, {ip: "192.168.1.0", mask: 24}, {ip: "192.168.0.0", 16}]
// It does not guarantee that the order
- delete
db.delete("192.168.1.0", 24); // -> Data of 192.168.1.0/24
db.find("192.168.1.0"); // -> data of 192.168.0.0/16
db.delete("192.168.0.0", 16); // -> Data of 192.168.0.0/16
db.find("192.168.1.0"); // -> undefined (Data of 0.0.0.0/0)
Use examples
ip-filtering-tree can push various data such as string, boolean, number or function indexed by IP address. Specific usage examples in the program are as follows.
- Push file names and open appropriate files for each source ip address.
var IPFilteringTree = require("ip-filtering-tree").IPFilteringTree;
var http = require('http');
var fs = require('fs');
var db = new IPFilteringTree();
db.push("0.0.0.0", 0, "./html/accept.html"); /* It is a default */
db.push("192.168.1.0", 24, "./html/deny.html");
db.push("127.0.0.0", 8, "./html/monitor.html");
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
// This instruction assumes always returns correct IPv4 address.
var srcip = request.connection.remoteAddress;
response.end(fs.readFileSync(db.find(srcip)));
}).listen(8080);
console.log("Listing on port " + 8080);
- Push functions and call them for each source ip address.
var IPFilteringTree = require("ip-filtering-tree").IPFilteringTree;
var http = require('http');
var ipaddr = require('ipaddr.js');
var fs = require('fs');
var db = new IPFilteringTree();
db.push("0.0.0.0", 0, function(response) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.end("404 Not Found\n");
});
db.push("192.168.1.0", 24, function(response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end(fs.readFileSync('./html/accept.html'));
});
db.push("127.0.0.0", 8, function(response) {
var request = http.request({
host: 'monitor-server',
port: 80,
path: "/",
method: "GET"
});
request.end();
request.on('response', function(proxyResponse) {
var data = "";
proxyResponse.on('data', function(chunk) {
data += chunk;
});
proxyResponse.on('end', function () {
response.writeHead(proxyResponse.statusCode, proxyResponse.headers);
response.end(data);
});
});
});
http.createServer(function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
// This instruction assumes always returns correct IPv4 address.
var srcip = request.connection.remoteAddress;
/* Find the appropriate function and call it with the parameter 'response' */
db.find(srcip)(response);
}).listen(8080);
console.log("Listing on port " + 8080);
Illustration of processing
ip-filtering-tree searches data by using tree algorithm. In this section, I will illustrate the processing in details to find the data from the ip-filtering-tree.
Case 1
It is the case to search the data reaches end of the leaf without mismatch.
- 00-00 -----------------------------------------------------------------------
- 00-01 -----------------------------------------------------------------------
- 00-02 -----------------------------------------------------------------------
Case 2
It is the case to search the data reaches middle of the leaf with mismatch.
- 01-00 -----------------------------------------------------------------------
- 01-01 -----------------------------------------------------------------------
- 01-02 -----------------------------------------------------------------------
- 01-03 -----------------------------------------------------------------------
TODO
- Apply IPv6 features
- Apply cache features
License
This software is released under the MIT License, see LICENSE.txt.