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

socket-io-anti-spam

v2.5.0

Published

This module prevents socket.emit spams by clients via ip bans

Downloads

36

Readme


What it does

Keeps track of how many socket.emit's an ip has submitted under a certain timeframe and determine if it is spammy behaviour. If the module determined the user is spamming the socket will receive a temp ip ban. Everything is customizable.


How it works

All connected sockets will have a object binded to them full of information that socket-anti-spam keeps track of. This includes how much 'spamScore'someone has. If a socket is doing a socket.emit his spamScore will increase. The module will give all sockets connected a -1 spamScore every second (no intervals!). if the spamScore is above a certain spamScore threshold the socket will be disconnected. If the socket keeps spamming after a certain kick threshold, the socket will be temp ip banned.

You can see a demo of the module in action here, please remember that this is from previous versions and the appearances might look different


Changelog

https://github.com/michaeldegroot/socket-anti-spam/commits/master


Getting started

1. Start by installing the package:

npm install socket-anti-spam

2. Load the code
  const SocketAntiSpam  = require('socket-anti-spam')
  const socket-io = require('socket.io').listen(8080)

  // Redis is not needed, but can be used
  const redis = require('redis')
  const client = redis.createClient()

  const socketAntiSpam = new SocketAntiSpam({
    banTime:            30,         // Ban time in minutes
    kickThreshold:      2,          // User gets kicked after this many spam score
    kickTimesBeforeBan: 1,          // User gets banned after this many kicks
    banning:            true,       // Uses temp IP banning after kickTimesBeforeBan
    io:                 socket-io,  // Bind the socket.io variable
    redis:              client,      // Redis client if you are sharing multiple servers
  })

  // Call functions with created reference 'socketAntiSpam'
  socketAntiSpam.event.on('ban', data => {
    // Do stuff
  })

Now all sockets will be individually checked if they spam your socket.emits and if they do they will be disconnected, after to many repeated offenses they will be temp banned (ip based).


Events

event.on('authenticate', callback)

Event fires when a socket authenticates with the socket-anti-spam module

Example

socketAntiSpam.event.on('authenticate', socket => {
  // We have the socket var that tried to authenticate


  // We could get his IP
  console.log(socket.ip)
})

event.on('kick', callback)

Event fires when a socket was kicked

Example

socketAntiSpam.event.on('kick', (socket, data) => {
  // We have the socket var that was kicked

  // The second parameter is a object that was binded to the socket with some extra information
  // It's how socket-anti-spam keeps track of sockets and their states
})

event.on('ban', callback)

Event fires when a socket was banned

Example

socketAntiSpam.event.on('ban', (socket, data) => {
  // We have the socket var that was banned

  // The second parameter is a object that was binded to the socket with some extra information
  // It's how socket-anti-spam keeps track of sockets and their states
})

event.on('spamscore', callback)

Event fires when a socket received a new spamscore

Example

socketAntiSpam.event.on('spamscore', (socket, data) => {
  // We have the socket var that received a new spamscore update

  // The second parameter is a object that was binded to the socket with some extra information
  // It's how socket-anti-spam keeps track of sockets and their states

  // If you want the spamscore you can get it via:
  console.log(data.score)
})

API

.addSpam(socket)

socket:     Object      // The user socket variable

Can be used to increase the spam score of a socket, if you set the io variable in the init function you do not need this. Unless you want to do something other then adding a spamscore for every socket emit

Example

const io = require('socket.io')
io.sockets.on('connection', socket => {
    socket.on('chatMessage', () => {
        socketAntiSpam.addSpam(socket) // Adds a spamscore because this socket sent a emit
        // The rest of your code
    })
})

.getBans()

Returns a array full of ip's that are currently banned

Example

const bans = socketAntiSpam.getBans()
console.log(bans)   // Returns a array full of ip's that are currently banned

.ban(data,minutes)

data:       Object / String     //  Can be either socket.ip or a ip in string format you want to ban
minutes:    Number              // Number in minutes how long the ban will be active, if not supplied default will be used (60)

Simply bans a socket or ip

Example banning a ip in string format

socketAntiSpam.ban('127.0.0.1') // Bye!

Example banning a socket, and set ban time for 5 minutes

io.sockets.on('connection', socket => {
    socketAntiSpam.ban(socket, 5)
})

.unBan(data)

data:   Object / String    //  Can be either socket.ip or a ip in string format you want to unban

Simply unbans a socket or ip

Example unbanning a ip in string format

socketAntiSpam.unban('127.0.0.1') // He's back!

Example unbanning a socket

io.sockets.on('connection', socket => {
    socketAntiSpam.unBan(socket)
})

Contact

You can contact me at [email protected]