This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #403 from aleitner/master
v3.2.0
- Loading branch information
Showing
15 changed files
with
364 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
var fs = require('fs'); | ||
var utils = require('../utils'); | ||
var path = require('path'); | ||
|
||
/** | ||
* Manage a blacklist file containing an object with key value pairs of | ||
* nodeids: timestamp | ||
* @constructor | ||
* @license LGPL-3.0 | ||
* @see https://github.com/storj/bridge | ||
* @param {String} path - blacklist folder location | ||
*/ | ||
function Blacklist(folder) { | ||
if (!(this instanceof Blacklist)) { | ||
return new Blacklist(folder); | ||
} | ||
|
||
assert.ok(utils.existsSync(folder), 'Invalid Blacklist Folder'); | ||
|
||
this.blacklistFile = path.join(folder,'.blacklist'); | ||
this.blacklist = this._loadFromDisk(); | ||
} | ||
|
||
Blacklist.TTL = 86400000; | ||
|
||
/** | ||
* Push node to blacklist | ||
* @param {String} nodeid - Node id to be added to blacklist | ||
*/ | ||
Blacklist.prototype.push = function(nodeid) { | ||
this.blacklist[nodeid] = Date.now(); | ||
this._saveToDisk(); | ||
}; | ||
|
||
/** | ||
* Save blacklist to disk | ||
* @private | ||
*/ | ||
Blacklist.prototype._saveToDisk = function() { | ||
fs.writeFileSync(this.blacklistFile, JSON.stringify(this.blacklist)); | ||
}; | ||
|
||
/** | ||
* Read blacklist from disk and Reap old nodeids | ||
* @private | ||
*/ | ||
Blacklist.prototype._loadFromDisk = function() { | ||
if (!utils.existsSync(this.blacklistFile)) { | ||
fs.writeFileSync(this.blacklistFile, JSON.stringify([])); | ||
} | ||
|
||
return this._reap(JSON.parse(fs.readFileSync(this.blacklistFile))); | ||
}; | ||
|
||
/** | ||
* Reap old nodeids from blacklist | ||
* @private | ||
*/ | ||
Blacklist.prototype._reap = function(blacklist) { | ||
var now = Date.now(); | ||
|
||
for (var nodeid in blacklist) { | ||
if ((blacklist[nodeid] - now) > Blacklist.TTL) { | ||
delete blacklist[nodeid]; | ||
} | ||
} | ||
|
||
this.blacklist = blacklist; | ||
|
||
return blacklist; | ||
}; | ||
|
||
/** | ||
* Return list of blacklisted nodeids | ||
*/ | ||
Blacklist.prototype.toObject = function() { | ||
return Object.keys(this._reap(this.blacklist)); | ||
}; | ||
|
||
module.exports = Blacklist; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.