On memory database indexed by IP addresses.
npm install 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.
```npm install ip-filtering-tree
* Instantiate
`javascript`
var IPFilteringTree = require('ip-filtering-tree').IPFilteringTree;
var db = new IPFilteringTree();
* push
`javascript`
// push(
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
`javascript`
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
`javascript`
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
`javascript`
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)
* Push file names and open appropriate files for each source ip address.
`javascript
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.
`javascript
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);
``