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

seham

v1.0.2

Published

A simple HTTP Logging middleware function for Express APIs that stores the results in MongoDB and also provides the ability to blacklist malicious/unwanted IPs.

Downloads

3

Readme

Seham - Store Express HTTP Activity to MongoDB

Motivation

The following node package was created for the scope of my Bachelor Thesis.

Its purpose is not olny to provide a simple HTTP Logging function for Express APIs but also to give the web developer the ability to blacklist malicious/unwanted IPs. HTTP traffic as well as Client IPs are stored to different collections in a Mongo Database. Furthermore, this node package can be used in conjunction with a Python Flask API that leverages Machine Learning techniques to classify each client's behavior (as normal or abnormal) based on their logged HTTP activity/sessions. It may also be used to automaticaly blacklist clients if their activity/behavior is commonly classified as abnormal.

More specifically, this Node JS Middleware function can be used to:

  • Log Client IPs (blacklistIP option available).
  • Log HTTP Metadata into MongoDB.
  • Group HTTP Metadata into Sessions based on timestamps.

If used in conjuction with the provided Session Behavior Classifier API, it can also be used to:

  • Extract Features from HTTP Metadata.
  • Use Pretrained Python Classifiers to Classify Web Sessions and Blacklist Malicious IP Addresses.

Prerequisites

  • Node JS - as a Backend Service
  • Express JS - REST API (express - node package)
  • MongoDB - Database (mongoose - node package)

Dependencies - Node Packages

  • mime: ^2.4.6
  • mongoose: ^5.10.3

Usage

Installation

Install the node package:

npm install seham

Require the module to your app:

const seham = require("seham");

Insert the following line-of-code in your main file (app.js/index.js):

app.use(seham(options)); 	// options argument is optional

between the lines:

app.use(express.json());
... // code
/* INSERT THE LINE-OF-CODE HERE */
... // code
app.use(express.static(path.join(__dirname, "public")));

where options is a JavaScript Object with the properties listed below.

Additional properties (Optional):

The various optional properties are listed below:

| Name | Default | Description | | ---------------- | ---------------- | ---------------- | | blockDuration | 60 | Duration (in seconds) that an IP Address gets blocked.| | groupToSessions| true | Group HTTP to sessions based on time.| | custGroupToSess| timestamp based | Use a custom function to group HTTP into sessions.| | sessTimeThres | 15 | Maximum Time Difference (in seconds) between two consecutive requests of the same session. If Time Def greater than this value, requests are split into separate sessions.| | maxReqsPerSess | 180 | Maximum Number of Requests in a session.| | pythonAPIOpt | - | Session-Behavior-Classifier-API Connection Options.| | customIPHeader | - | The name of request-header to get the client IP from. If not provided common headers/request-properties are used instead.|

The default pythonAPIOpt object is shown below:

pythonAPIOpt : {
  useAPI: false 		// Whether to use the Flask API (if available) or not.
  host: "localhost" 	// The Hostname of the Flask API.
  port: 4000 			// The Port number of the Flask API.
}

When the parameter customIPHeader is not provided then the headers and request properties listed above are used in the following order:

Request Headers:

X-Client-IP
X-Forwarded-For
CF-Connecting-IP (Cloudflare)
Fastly-Client-Ip (Fastly CDN and Firebase hosting header when forwared to a cloud function)
True-Client-Ip (Akamai and Cloudflare)
X-Real-IP (Nginx proxy/FastCGI)
X-Cluster-Client-IP (Rackspace LB, Riverbed Stingray)
X-Forwarded
Forwarded-For

Request Properties:

req.connection.remoteAddress
req.socket.remoteAddress
req.connection.socket.remoteAddress
req.info.remoteAddress

An example of the options object could be:

const options = {
        blockDuration : 24 * 60 * 60,     // 24 hours
        groupToSessions : true,
        sessTimeThres : 30,               // 30 seconds
        maxReqsPerSess : 200,             // 200 requests
        pythonAPIOpt : {useAPI: true, host: "localhost", port: 4000},
        customIPHeader : "Forwarded-IP"
}