addSpam = function (socket) { if (not(socket)) throw new Error('socket variable is not defined') authenticate(socket, function(data) { if (data.banned) return var lastInteraction = moment.duration(moment().diff(data.lastInteraction)).asSeconds() data.lastInteraction = moment() if (lastInteraction < 1) data.score++ if (lastInteraction >= 1) { var newScore = data.score - Math.round(lastInteraction) data.score = newScore if (newScore <= 0) data.score = 0 } var lastLowerKick = moment.duration(moment().diff(data.lastLowerKick)).asSeconds() if (lastLowerKick >= 1800 && data.kickCount >= 1) { data.lastLowerKick = moment() data.kickCount-- } eventEmitter.emit('spamscore', socket, data) if (data.score >= options.kickThreshold) { eventEmitter.emit('kick', socket, data) data.score = 0 data.kickCount = data.kickCount + 1 if (data.kickCount >= options.kickTimesBeforeBan && options.banning) { eventEmitter.emit('ban', socket, data) clearHeart(socket) data.kickCount = 0 data.banned = true data.lastLowerKick = moment() data.bannedUntil = moment().add(options.banTime, 'minutes') } socket.disconnect() } }) }
...
if (not(sets.heartBeatCheck))
sets.heartBeatCheck = defaultOptions.heartBeatCheck
if (sets.io) {
sets.io.on('connection', function(socket) {
var emit = socket.emit
socket.emit = function() {
if (!socket.banned) {
exports.addSpam(socket)
emit.apply(socket, arguments)
}
}
authenticate(socket)
socket.on('disconnect', function() {
clearHeart(socket)
...
ban = function (data, min) { if (not(data)) throw new Error('No options defined') if (not(min)) min = options.banTime var ip = false if (typeof(users[data]) !='undefined') ip = data if (typeof(users[data.ip]) != 'undefined') ip = data.ip if (ip) return ban(true, ip) return false }
...
````js
const antiSpam = require('socket-anti-spam')
const bans = antiSpam.getBans()
console.log(bans) // Returns a array full of ip's that are currently banned
````
### .ban(data,minutes)
```js
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
...
getBans = function () { var banned = [] var user for (user in users) { if (users[user].banned) banned.push({ ip: user, until: users[user].bannedUntil, }) } return banned }
...
// Lets create server for index.html
http.createServer(function(req, res) {
file.serve(req, res)
}).listen(80)
// Everyone has this line already when using socket-anti-spam
io.sockets.on('connection', function(socket) {
console.log(antiSpam.getBans())
socket.join("kappa")
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
...
init = function (sets) { if (not(sets.banTime)) sets.banTime = defaultOptions.banTime if (not(sets.kickThreshold)) sets.kickThreshold = defaultOptions.kickThreshold if (not(sets.kickTimesBeforeBan)) sets.kickTimesBeforeBan = defaultOptions.kickTimesBeforeBan if (not(sets.banning)) sets.banning = defaultOptions.banning if (not(sets.heartBeatStale)) sets.heartBeatStale = defaultOptions.heartBeatStale if (not(sets.heartBeatCheck)) sets.heartBeatCheck = defaultOptions.heartBeatCheck if (sets.io) { sets.io.on('connection', function(socket) { var emit = socket.emit socket.emit = function() { if (!socket.banned) { exports.addSpam(socket) emit.apply(socket, arguments) } } authenticate(socket) socket.on('disconnect', function() { clearHeart(socket) }) socket.on('error', function(err) { console.error(err) }) }) } options = sets }
...
// This is just for the index.html
var static = require('node-static')
var http = require('http')
var file = new static.Server('./public')
// Actually needed for antispam
var antiSpam = require('./antispam')
antiSpam.init({
banTime: 1, // Ban time in minutes
kickThreshold: 7, // User gets kicked after this many spam score
kickTimesBeforeBan: 3, // 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: io, // Bind the socketio variable
...
unBan = function (data) { if (not(data)) throw new Error('No options defined') var ip = false if (typeof(users[data])!='undefined') ip = data if (typeof(users[data.ip])!='undefined') ip = data.ip if (ip) return ban(false, ip) return false }
...
````js
const antiSpam = require('socket-anti-spam')
io.sockets.on('connection', socket => {
antiSpam.ban(socket, 5)
})
````
### .unBan(data)
```js
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
...
addSpam = function (socket) { if (not(socket)) throw new Error('socket variable is not defined') authenticate(socket, function(data) { if (data.banned) return var lastInteraction = moment.duration(moment().diff(data.lastInteraction)).asSeconds() data.lastInteraction = moment() if (lastInteraction < 1) data.score++ if (lastInteraction >= 1) { var newScore = data.score - Math.round(lastInteraction) data.score = newScore if (newScore <= 0) data.score = 0 } var lastLowerKick = moment.duration(moment().diff(data.lastLowerKick)).asSeconds() if (lastLowerKick >= 1800 && data.kickCount >= 1) { data.lastLowerKick = moment() data.kickCount-- } eventEmitter.emit('spamscore', socket, data) if (data.score >= options.kickThreshold) { eventEmitter.emit('kick', socket, data) data.score = 0 data.kickCount = data.kickCount + 1 if (data.kickCount >= options.kickTimesBeforeBan && options.banning) { eventEmitter.emit('ban', socket, data) clearHeart(socket) data.kickCount = 0 data.banned = true data.lastLowerKick = moment() data.bannedUntil = moment().add(options.banTime, 'minutes') } socket.disconnect() } }) }
...
if (not(sets.heartBeatCheck))
sets.heartBeatCheck = defaultOptions.heartBeatCheck
if (sets.io) {
sets.io.on('connection', function(socket) {
var emit = socket.emit
socket.emit = function() {
if (!socket.banned) {
exports.addSpam(socket)
emit.apply(socket, arguments)
}
}
authenticate(socket)
socket.on('disconnect', function() {
clearHeart(socket)
...
ban = function (data, min) { if (not(data)) throw new Error('No options defined') if (not(min)) min = options.banTime var ip = false if (typeof(users[data]) !='undefined') ip = data if (typeof(users[data.ip]) != 'undefined') ip = data.ip if (ip) return ban(true, ip) return false }
...
````js
const antiSpam = require('socket-anti-spam')
const bans = antiSpam.getBans()
console.log(bans) // Returns a array full of ip's that are currently banned
````
### .ban(data,minutes)
```js
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
...
getBans = function () { var banned = [] var user for (user in users) { if (users[user].banned) banned.push({ ip: user, until: users[user].bannedUntil, }) } return banned }
...
// Lets create server for index.html
http.createServer(function(req, res) {
file.serve(req, res)
}).listen(80)
// Everyone has this line already when using socket-anti-spam
io.sockets.on('connection', function(socket) {
console.log(antiSpam.getBans())
socket.join("kappa")
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
io.sockets.in("kappa").emit('roomer_msg', {kappa:"kappa"})
...
init = function (sets) { if (not(sets.banTime)) sets.banTime = defaultOptions.banTime if (not(sets.kickThreshold)) sets.kickThreshold = defaultOptions.kickThreshold if (not(sets.kickTimesBeforeBan)) sets.kickTimesBeforeBan = defaultOptions.kickTimesBeforeBan if (not(sets.banning)) sets.banning = defaultOptions.banning if (not(sets.heartBeatStale)) sets.heartBeatStale = defaultOptions.heartBeatStale if (not(sets.heartBeatCheck)) sets.heartBeatCheck = defaultOptions.heartBeatCheck if (sets.io) { sets.io.on('connection', function(socket) { var emit = socket.emit socket.emit = function() { if (!socket.banned) { exports.addSpam(socket) emit.apply(socket, arguments) } } authenticate(socket) socket.on('disconnect', function() { clearHeart(socket) }) socket.on('error', function(err) { console.error(err) }) }) } options = sets }
...
// This is just for the index.html
var static = require('node-static')
var http = require('http')
var file = new static.Server('./public')
// Actually needed for antispam
var antiSpam = require('./antispam')
antiSpam.init({
banTime: 1, // Ban time in minutes
kickThreshold: 7, // User gets kicked after this many spam score
kickTimesBeforeBan: 3, // 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: io, // Bind the socketio variable
...
unBan = function (data) { if (not(data)) throw new Error('No options defined') var ip = false if (typeof(users[data])!='undefined') ip = data if (typeof(users[data.ip])!='undefined') ip = data.ip if (ip) return ban(false, ip) return false }
...
````js
const antiSpam = require('socket-anti-spam')
io.sockets.on('connection', socket => {
antiSpam.ban(socket, 5)
})
````
### .unBan(data)
```js
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
...