npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ip-filtering-tree

v2.1.0

Published

On memory database indexed by IP addresses.

Downloads

108

Readme

ip-filtering-tree

NPM

Build Status npm GitHub license npm total downloads

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 -----------------------------------------------------------------------

algorithms00_00

- 00-01 -----------------------------------------------------------------------

algorithms00_01

- 00-02 -----------------------------------------------------------------------

algorithms00_02

Case 2

It is the case to search the data reaches middle of the leaf with mismatch.

- 01-00 -----------------------------------------------------------------------

algorithms01_00

- 01-01 -----------------------------------------------------------------------

algorithms01_01

- 01-02 -----------------------------------------------------------------------

algorithms01_02

- 01-03 -----------------------------------------------------------------------

algorithms01_03

TODO

  • Apply IPv6 features
  • Apply cache features

License

This software is released under the MIT License, see LICENSE.txt.