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

tspu

v0.11.2

Published

Technical tools for danger mitigation

Downloads

22

Readme

tspu

Technical tools for danger mitigation

Install

npm i --save tspu

API

import EventEmitter from "node:events";
import { Http2ServerRequest, StreamState } from "node:http2";

type Ip = string;
type Domain = string;
type Bytes = number[];
type Ciphers = string[];

type DomainOrDomainEntries = Domain | [Domain, Ip][] | [Ip, Domain][];

type Http = {
  valid: (request: string) => boolean;
  blocked: (request: string, domain: DomainOrDomainEntries) => boolean;
};

type SessionOptions = {
  autoblock?: boolean;
  sensitivity?: number;
};

type FirewallRuleDictionary =
  | {
      [key: string]: {
        in: number[];
      };
    }
  | {
      [key: string]: {
        not: {
          in: number[];
        };
      };
    };

type FirewallOptions = {
  rules: FirewallRuleDictionary;
};

interface TcpSession extends EventEmitter {
  bytes: Bytes;
  extensions: Bytes;

  blocked: () => boolean;
  feed: (...arrayOrInteger: Bytes | Bytes[] | ArrayBuffer[]) => this;
  extend: (...arrayOrInteger: Bytes | Bytes[] | ArrayBuffer[]) => this;
  banCipher: (ciphers: Ciphers) => this;
  feedCipher: (ciphers: Ciphers) => this;
}

interface Firewall {
  state: FirewallOptions;

  legit: (session: Http2ServerRequest) => boolean;
}

type Tcp = {
  Session: new (options?: SessionOptions) => TcpSession;
};

type Http2 = {
  Firewall: new (options?: FirewallOptions) => Firewall;
};

export const http2: Http2;
export const http: Http;
export const tcp: Tcp;

http

const { http } = require("tspu");

valid - checks if raw http request is valid

http.valid("GET /  HTTP/1.1\r\nHost: example.com\r\nUser-Agent: node\r\n"); // false

http.valid("GET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: node\r\n"); // true

blocked - checks if raw http request is blocked

http.blocked(
  "GET / HTTP/1.1\r\nHost: api.example.com\r\nUser-Agent: node\r\n",
  "example.com"
); // true

http.blocked(
  "GET / HTTP/1.1\r\nHost: exampled.com\r\nUser-Agent: node\r\n",
  "example.com"
); // false

Cross domains

http.blocked("GET / HTTP/1.1\r\nHost: example.com\r\nUser-Agent: node\r\n", [
  ["example.com", "123.123.123.123:443"],
]); // false

http.blocked(
  "GET / HTTP/1.1\r\nHost: example.com:443\r\nUser-Agent: node\r\n",
  [
    ["example.com", "123.123.123.123:443"],
    ["example.com", "123.123.123.123:444"],
    ["example.com", "123.123.123.123:445"],
  ]
); // true

tcp

const { tcp } = require("tspu");

const session = new tcp.Session();

session.feed(0x04, 0x00, 0x00, 0x00); // openvpn handshake
session.feed(0x13, 0x37, 0x13, 0x37);

session.blocked(); // true
import { tcp } from "tspu";

const session = new tcp.Session();

session.feed(1, 2, 3, 4);
session.feed(new Uint16Array([1, 2, 3, 4]));
session.feed([1, 2, 3], [3, 4, 5]);
session.feed(new Uint16Array([1, 2, 3, 4]), new Uint8Array([5, 6, 7, 8]));

console.log(session.bytes); // all the bytes that have been feed
console.log(session.blocked());

Custom extensions

const session = new tcp.Session();

session
  .extend([0x01, 0x03, 0x04])
  .extend([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]);

session
  .feed(new Uint8Array([0x01, 0x02, 0x03, 0x04]))
  .feed(new Uint8Array([0x05, 0x06, 0x07, 0x08]))
  .feed(new Uint8Array([0x5, 0x37, 0x13, 0x37]));

console.log(session.blocked());

Cipher manual block

const session = new tcp.Session();
session.banCipher(["aes128-gcm-sha256", "aes128-sha", "aes128-sha"]);

session.feedCipher(["aes128-gcm-sha256", "aes128-sha", "aes128-sha"]);
session.blocked(); // true

Cipher autoblock based on diff between the known and the current cipher

const session = new tcp.Session({ autoblock: true, sensitivity: 2 });

session.banCipher(["aes128-gcm-sha256", "aes128-sha", "aes128-sha"]);

session.feedCipher([
  "aes128-gcm-sha256",
  "aes128-sha",
  "___",
  "aes128-sha",
  "___",
]);

session.blocked(); // true

tcp module allows to subscribe to blocked event

session.on("blocked", (reason) => {
  console.log(reason.ciphers);
});

http2 module

const session = {
  stream: {
    state: {
      localWindowSize: 16,
    },
  },
} as Http2ServerRequest;

const firewall = new http2.Firewall({
  rules: {
    localWindowSize: {
      in: [16],
    },
  },
});

firewall.legit(session); // true

Test

npm test