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.
1. Start by installing the package:
npm install socket-anti-spam
2. Load the code
const SocketAntiSpam =const socket-io =// Redis is not needed, but can be usedconst redis =const client = redisconst socketAntiSpam =banTime: 30 // Ban time in minuteskickThreshold: 2 // User gets kicked after this many spam scorekickTimesBeforeBan: 1 // User gets banned after this many kicksbanning: true // Uses temp IP banning after kickTimesBeforeBanio: socket-io // Bind the socket.io variableredis: client // Redis client if you are sharing multiple servers// Call functions with created reference 'socketAntiSpam'socketAntiSpamevent
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
Event fires when a socket was kicked
Event fires when a socket was banned
Event fires when a socket received a new spamscore
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
const io =iosockets
Returns a array full of ip's that are currently banned
const bans = socketAntiSpamconsole // 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 banminutes: 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 // Bye!
Example banning a socket, and set ban time for 5 minutes
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 // He's back!
Example unbanning a socket
You can contact me at email@example.com