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.
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.
Version 1.0.0 is released, it is a complete rewrite of the module. Please check the documentation!
https://github.com/michaeldegroot/socket-anti-spam/commits/master
yarn add socket-anti-spam
const antiSpam = require('socket-anti-spam')
const socket-io = require('socket.io').listen(8080)
antiSpam.init({
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
heartBeatStale: 40, // Removes a heartbeat after this many seconds
heartBeatCheck: 4, // Checks a heartbeat per this many seconds
io: socket-io, // Bind the socket.io variable
})
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).
Event fires when a socket authenticates with the socket-anti-spam module
Example
const antiSpam = require('socket-anti-spam')
antiSpam.event.on('authenticate', socket => {
// We have the socket var that tried to authenticate
// We could get his IP
console.log(socket.ip)
})
Event fires when a socket was kicked
Example
const antiSpam = require('socket-anti-spam')
antiSpam.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 fires when a socket was banned
Example
const antiSpam = require('socket-anti-spam')
antiSpam.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 fires when a socket received a new spamscore
Example
const antiSpam = require('socket-anti-spam')
antiSpam.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)
})
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')
const antiSpam = require('socket-anti-spam')
io.sockets.on('connection', socket => {
socket.on('chatMessage', () => {
antiSpam.addSpam(socket) // Adds a spamscore because this socket sent a emit
// The rest of your code
})
})
Returns a array full of ip's that are currently banned
Example
const antiSpam = require('socket-anti-spam')
const bans = antiSpam.getBans()
console.log(bans) // Returns a array full of ip's that are currently banned
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
const antiSpam = require('socket-anti-spam')
antiSpam.ban('127.0.0.1') // Bye!
Example banning a socket, and set ban time for 5 minutes
const antiSpam = require('socket-anti-spam')
io.sockets.on('connection', socket => {
antiSpam.ban(socket, 5)
})
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
const antiSpam = require('socket-anti-spam')
antiSpam.unban('127.0.0.1') // He's back!
Example unbanning a socket
const antiSpam = require('socket-anti-spam')
io.sockets.on('connection', socket => {
antiSpam.unBan(socket)
})
You can contact me at specamps@gmail.com