mirror of
https://github.com/taobataoma/meanTorrent.git
synced 2026-08-07 17:19:44 +02:00
feat(logger): new announce logger logic for special user id list
This commit is contained in:
16
config/env/torrents.js
vendored
16
config/env/torrents.js
vendored
@@ -20,7 +20,9 @@ module.exports = {
|
||||
* @admin: site admin mail address
|
||||
* @showDemoWarningPopup: if is demo site, show warning popup whene home is opened for the first time.
|
||||
* @cronTimeZone: timezone of cron
|
||||
* @showDebugLog: if true, will console.log all debug info at server side and client side. when your site is prod env, please change this
|
||||
* @showClientDebugLog: if true, will console.log all debug info at client side. when your site is prod env, please change this
|
||||
* value to false, then console.log info is not output
|
||||
* @writeServerDebugLog: if true, will console.log all debug info at server side. when your site is prod env, please change this
|
||||
* value to false, then console.log info is not output
|
||||
*/
|
||||
app: {
|
||||
@@ -29,7 +31,8 @@ module.exports = {
|
||||
admin: 'admin@mean.im',
|
||||
showDemoWarningPopup: true,
|
||||
cronTimeZone: 'Asia/Shanghai',
|
||||
showDebugLog: true
|
||||
showClientDebugLog: true,
|
||||
writeServerDebugLog: true
|
||||
},
|
||||
|
||||
/**
|
||||
@@ -112,6 +115,9 @@ module.exports = {
|
||||
* @warningCheck:
|
||||
* @userHnrWarningCheckInterval: setting check users H&R warning interval time, default to 2 hours
|
||||
* @userHnrWarningCheckInterval_str: string desc of @userHnrWarningCheckInterval
|
||||
* @debugAnnounceUser: setting of debug announce user, NOTE: enable this need @app.writeServerDebugLog must be true too
|
||||
* @debugAll: if true, debug all announce user, else debug user in ids list below
|
||||
* @ids: debug announce user id list
|
||||
*/
|
||||
announce: {
|
||||
url: 'http://localhost:3000/announce',
|
||||
@@ -140,6 +146,12 @@ module.exports = {
|
||||
warningCheck: {
|
||||
userHnrWarningCheckInterval: 60 * 60 * 1000 * 2,
|
||||
userHnrWarningCheckInterval_str: '2h'
|
||||
},
|
||||
debugAnnounceUser: {
|
||||
debugAll: false,
|
||||
ids: [
|
||||
'59227f9095602327ea1d96ba'
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ var path = require('path'),
|
||||
config = require(path.resolve('./config/config'));
|
||||
|
||||
var appConfig = config.meanTorrentConfig.app;
|
||||
var announceConfig = config.meanTorrentConfig.announce;
|
||||
|
||||
module.exports.colorFunctionList = {
|
||||
red: chalk.red,
|
||||
@@ -22,13 +23,24 @@ module.exports.colorFunctionList = {
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.info = function (obj, section = undefined, colorFunction = undefined, isAnnounce = false) {
|
||||
if (appConfig.showDebugLog) {
|
||||
module.exports.info = function (obj, section = undefined, colorFunction = undefined, isAnnounce = false, user = undefined) {
|
||||
if (appConfig.writeServerDebugLog) {
|
||||
var tools = isAnnounce ? loggerAnnounce : logger;
|
||||
|
||||
var userPrefix = '';
|
||||
if (isAnnounce && !announceConfig.debugAnnounceUser.debugAll) {
|
||||
if (user) {
|
||||
if (!announceConfig.debugAnnounceUser.ids.includes(user._id.toString())) {
|
||||
return;
|
||||
} else {
|
||||
userPrefix = ' - ' + user.displayName + ' - ' + user._id.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!colorFunction) {
|
||||
if (Array.isArray(obj)) {
|
||||
tools.info('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : ''));
|
||||
tools.info((section ? ' - ' + section : '') + userPrefix);
|
||||
obj.forEach(function (a) {
|
||||
if (typeof a !== 'object') {
|
||||
tools.info(a + '');
|
||||
@@ -37,14 +49,14 @@ module.exports.info = function (obj, section = undefined, colorFunction = undefi
|
||||
}
|
||||
});
|
||||
} else if (typeof obj !== 'object') {
|
||||
tools.info('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : '') + ' - ' + obj);
|
||||
tools.info((section ? ' - ' + section : '') + userPrefix + ' - ' + obj);
|
||||
} else {
|
||||
tools.info('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : ''));
|
||||
tools.info((section ? ' - ' + section : '') + userPrefix);
|
||||
tools.info(JSON.stringify(obj));
|
||||
}
|
||||
} else {
|
||||
if (Array.isArray(obj)) {
|
||||
tools.info(colorFunction('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : '')));
|
||||
tools.info(colorFunction((section ? ' - ' + section : '') + userPrefix));
|
||||
obj.forEach(function (a) {
|
||||
if (typeof a !== 'object') {
|
||||
tools.info(a + '');
|
||||
@@ -53,9 +65,9 @@ module.exports.info = function (obj, section = undefined, colorFunction = undefi
|
||||
}
|
||||
});
|
||||
} else if (typeof obj !== 'object') {
|
||||
tools.info(colorFunction('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : '') + ' - ' + obj));
|
||||
tools.info(colorFunction((section ? ' - ' + section : '') + userPrefix + ' - ' + obj));
|
||||
} else {
|
||||
tools.info(colorFunction('[' + moment().format('YYYY-MM-DD HH:mm:ss') + ']' + (section ? ' - ' + section : '')));
|
||||
tools.info(colorFunction((section ? ' - ' + section : '') + userPrefix));
|
||||
tools.info(JSON.stringify(obj));
|
||||
}
|
||||
}
|
||||
@@ -67,8 +79,8 @@ module.exports.info = function (obj, section = undefined, colorFunction = undefi
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debug = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, undefined, isAnnounce);
|
||||
module.exports.debug = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, undefined, isAnnounce, user);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -76,8 +88,8 @@ module.exports.debug = function (obj, section = undefined, isAnnounce = false) {
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debugGreen = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, this.colorFunctionList.green, isAnnounce);
|
||||
module.exports.debugGreen = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, this.colorFunctionList.green, isAnnounce, user);
|
||||
|
||||
};
|
||||
|
||||
@@ -86,8 +98,8 @@ module.exports.debugGreen = function (obj, section = undefined, isAnnounce = fal
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debugRed = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, this.colorFunctionList.red, isAnnounce);
|
||||
module.exports.debugRed = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, this.colorFunctionList.red, isAnnounce, user);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -95,8 +107,8 @@ module.exports.debugRed = function (obj, section = undefined, isAnnounce = false
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debugError = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, this.colorFunctionList.error, isAnnounce);
|
||||
module.exports.debugError = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, this.colorFunctionList.error, isAnnounce, user);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -104,8 +116,8 @@ module.exports.debugError = function (obj, section = undefined, isAnnounce = fal
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debugBlue = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, this.colorFunctionList.blue, isAnnounce);
|
||||
module.exports.debugBlue = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, this.colorFunctionList.blue, isAnnounce, user);
|
||||
|
||||
};
|
||||
|
||||
@@ -114,7 +126,7 @@ module.exports.debugBlue = function (obj, section = undefined, isAnnounce = fals
|
||||
* @param obj
|
||||
* @param section
|
||||
*/
|
||||
module.exports.debugYellow = function (obj, section = undefined, isAnnounce = false) {
|
||||
this.info(obj, section, this.colorFunctionList.yellow, isAnnounce);
|
||||
module.exports.debugYellow = function (obj, section = undefined, isAnnounce = false, user = undefined) {
|
||||
this.info(obj, section, this.colorFunctionList.yellow, isAnnounce, user);
|
||||
};
|
||||
|
||||
|
||||
@@ -148,8 +148,8 @@ exports.announce = function (req, res) {
|
||||
req.selfpeer = [];
|
||||
req.seeder = false;
|
||||
|
||||
mtDebug.debugGreen('------------ Announce request ----------------', 'ANNOUNCE_REQUEST', true);
|
||||
// mtDebug.debugGreen(req.url, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('------------ Announce request ----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
// mtDebug.debugGreen(req.url, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
|
||||
var s = req.url.split('?');
|
||||
var query = common.querystringParse(s[1]);
|
||||
@@ -390,7 +390,7 @@ exports.announce = function (req, res) {
|
||||
_id: {$nin: req.torrent._peers}
|
||||
}, function (err, removed) {
|
||||
if (removed.n > 0) {
|
||||
mtDebug.debugRed('Removed ' + removed + ' peers not in torrent._peers: ' + req.torrent._id, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugRed('Removed ' + removed + ' peers not in torrent._peers: ' + req.torrent._id, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
}
|
||||
done(null);
|
||||
});
|
||||
@@ -478,17 +478,17 @@ exports.announce = function (req, res) {
|
||||
---------------------------------------------------------------*/
|
||||
function (done) {
|
||||
if (event(query.event) === EVENT_STARTED) {
|
||||
mtDebug.debugGreen('---------------EVENT_STARTED----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------EVENT_STARTED----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
|
||||
var lcount = getSelfLeecherCount();
|
||||
var scount = getSelfSeederCount();
|
||||
|
||||
if (lcount > announceConfig.announceCheck.maxLeechNumberPerUserPerTorrent && !req.seeder) {
|
||||
mtDebug.debugYellow('getSelfLeecherCount = ' + lcount, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugYellow('getSelfLeecherCount = ' + lcount, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
removeCurrPeer();
|
||||
done(180);
|
||||
} else if (scount > announceConfig.announceCheck.maxSeedNumberPerUserPerTorrent && req.seeder) {
|
||||
mtDebug.debugYellow('getSelfSeederCount = ' + scount, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugYellow('getSelfSeederCount = ' + scount, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
removeCurrPeer();
|
||||
done(181);
|
||||
} else {
|
||||
@@ -507,7 +507,7 @@ exports.announce = function (req, res) {
|
||||
write time of seed(complete) whether or not u/d is 0
|
||||
---------------------------------------------------------------*/
|
||||
function (done) {
|
||||
mtDebug.debugGreen('---------------WRITE_UP_DOWN_DATA----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------WRITE_UP_DOWN_DATA----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
|
||||
var curru = 0;
|
||||
var currd = 0;
|
||||
@@ -765,7 +765,7 @@ exports.announce = function (req, res) {
|
||||
---------------------------------------------------------------*/
|
||||
function (done) {
|
||||
if (event(query.event) === EVENT_COMPLETED) {
|
||||
mtDebug.debugGreen('---------------EVENT_COMPLETED----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------EVENT_COMPLETED----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
//write completed torrent data into finished
|
||||
var finished = new Finished();
|
||||
finished.user = req.passkeyuser;
|
||||
@@ -810,7 +810,7 @@ exports.announce = function (req, res) {
|
||||
---------------------------------------------------------------*/
|
||||
function (done) {
|
||||
if (event(query.event) === EVENT_STOPPED) {
|
||||
mtDebug.debugGreen('---------------EVENT_STOPPED----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------EVENT_STOPPED----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
|
||||
if (req.completeTorrent) {
|
||||
req.completeTorrent.countHnRWarning(true, false);
|
||||
@@ -831,7 +831,7 @@ exports.announce = function (req, res) {
|
||||
req.passkeyuser.updateSeedLeechNumbers();
|
||||
|
||||
if (slCount) {
|
||||
mtDebug.debugYellow(JSON.stringify(slCount), 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugYellow(JSON.stringify(slCount), 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
req.torrent.torrent_seeds = slCount.seedCount;
|
||||
req.torrent.torrent_leechers = slCount.leechCount;
|
||||
}
|
||||
@@ -853,7 +853,7 @@ exports.announce = function (req, res) {
|
||||
peerBuffer = peerBuffer.slice(0, len);
|
||||
|
||||
var resp = 'd8:intervali' + ANNOUNCE_INTERVAL + 'e8:completei' + req.torrent.torrent_seeds + 'e10:incompletei' + req.torrent.torrent_leechers + 'e10:downloadedi' + req.torrent.torrent_finished + 'e5:peers' + len + ':';
|
||||
mtDebug.debugGreen(resp, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen(resp, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Length': resp.length + peerBuffer.length + 1,
|
||||
@@ -861,7 +861,7 @@ exports.announce = function (req, res) {
|
||||
});
|
||||
|
||||
if (len > 0) {
|
||||
mtDebug.debug(peerBuffer, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debug(peerBuffer, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
}
|
||||
res.write(resp);
|
||||
res.write(peerBuffer);
|
||||
@@ -886,7 +886,7 @@ exports.announce = function (req, res) {
|
||||
req.currentPeer.isNewCreated = false;
|
||||
|
||||
if (req.seeder && req.currentPeer.peer_status !== PEERSTATE_SEEDER && event(query.event) !== EVENT_COMPLETED) {
|
||||
mtDebug.debugGreen('---------------PEER STATUS CHANGED: Seeder----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------PEER STATUS CHANGED: Seeder----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
doCompleteEvent(function () {
|
||||
if (callback) callback();
|
||||
});
|
||||
@@ -962,7 +962,7 @@ exports.announce = function (req, res) {
|
||||
peer.save(function (err) {
|
||||
if (!err) {
|
||||
req.currentPeer = peer;
|
||||
mtDebug.debugGreen('---------------createCurrentPeer()----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------createCurrentPeer()----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
if (callback) callback();
|
||||
}
|
||||
});
|
||||
@@ -980,7 +980,7 @@ exports.announce = function (req, res) {
|
||||
|
||||
req.currentPeer.remove(function () {
|
||||
if (callback) callback();
|
||||
mtDebug.debugGreen('---------------removeCurrPeer()----------------', 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugGreen('---------------removeCurrPeer()----------------', 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1107,7 +1107,7 @@ exports.announce = function (req, res) {
|
||||
*/
|
||||
function sendError(failure) {
|
||||
var respc = failure.bencode();
|
||||
mtDebug.debugRed(respc, 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debugRed(respc, 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
res.writeHead(200, {
|
||||
'Content-Length': respc.length,
|
||||
'Content-Type': 'text/plain'
|
||||
@@ -1143,14 +1143,14 @@ exports.announce = function (req, res) {
|
||||
if (p.last_announce_at > (Date.now() - announceConfig.announceInterval - 60 * 1000)) { //do not send ghost peer
|
||||
if (p.user.equals(req.passkeyuser._id)) {
|
||||
if (announceConfig.peersCheck.peersSendListIncludeOwnSeed) {
|
||||
mtDebug.debug(p._id.toString(), 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debug(p._id.toString(), 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
bc = compact(p);
|
||||
if (bc) {
|
||||
bc.copy(buf, c++ * PEER_COMPACT_SIZE);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mtDebug.debug(p._id.toString(), 'ANNOUNCE_REQUEST', true);
|
||||
mtDebug.debug(p._id.toString(), 'ANNOUNCE_REQUEST', true, req.passkeyuser);
|
||||
bc = compact(p);
|
||||
if (bc) {
|
||||
bc.copy(buf, c++ * PEER_COMPACT_SIZE);
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
return service;
|
||||
|
||||
function debugInfo(obj) {
|
||||
if (appConfig.showDebugLog) {
|
||||
if (appConfig.showClientDebugLog) {
|
||||
console.log(obj);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user