mrfrys-node-server/utils/logger.js
2019-10-27 14:08:33 +01:00

139 lines
3.5 KiB
JavaScript

/* ----------------------------------------------------------------------------
Question Server
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------- */
module.exports = {
GetDateString: GetDateString,
Log: Log,
GetColor: GetColor,
LogReq: LogReq,
setNoLogReadInterval: setNoLogReadInterval
}
const DELIM = C('green') + '|' + C()
const utils = require('../utils/utils.js')
const locLogFile = './stats/logs'
const logFile = '/nlogs/nlogs'
const allLogFile = '/nlogs/log'
const colors = [
'green',
'red',
'yellow',
'blue',
'magenta',
'cyan'
]
let noLogips = []
function setNoLogReadInterval () {
noLogips = utils.ReadFile('./nolog').split('\n')
setInterval(() => {
noLogips = utils.ReadFile('./nolog').split('\n')
}, 1000 * 60 * 30)
}
function GetDateString () {
const m = new Date()
const d = m.getFullYear() + '/' +
('0' + (m.getMonth() + 1)).slice(-2) + '/' +
('0' + m.getDate()).slice(-2) + ' ' +
('0' + m.getHours()).slice(-2) + ':' +
('0' + m.getMinutes()).slice(-2) + ':' +
('0' + m.getSeconds()).slice(-2)
return GetRandomColor(m.getHours().toString()) + d + C()
}
function Log (s, c) {
let dl = DELIM + C(c)
let log = C(c) + GetDateString() + dl + s
console.log(log)
utils.AppendToFile(log, logFile)
}
function LogReq (req, toFile, sc) {
try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
let nolog = noLogips.some((x) => {
return x.includes(ip)
})
if (nolog) {
return
}
let logEntry = GetRandomColor(ip) + ip + C()
let dl = DELIM
if (req.url.includes('lred')) {
dl += C('red')
}
const hostname = req.hostname.replace('www.', '').split('.')[0]
logEntry += dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
if (sc !== undefined && sc === 404) { logEntry += dl + sc }
logEntry += C()
if (!toFile) {
Log(logEntry)
} else {
let defLogs = GetDateString() + dl + logEntry
utils.AppendToFile(defLogs, locLogFile)
utils.AppendToFile(defLogs, allLogFile)
}
} catch (e) {
console.log(e)
Log('Error at logging lol', GetColor('redbg'), true)
}
}
function GetRandomColor (ip) {
if (!ip) {
return 'red'
}
let res = ip.split('').reduce((res, x) => {
return res + x.charCodeAt(0)
}, 0)
return C(colors[res % colors.length])
}
function GetColor (c) {
return c
}
function C (c) {
if (c !== undefined) { c = c.toLowerCase() }
if (c === 'redbg') { return '\x1b[41m' }
if (c === 'bluebg') { return '\x1b[44m' }
if (c === 'green') { return '\x1b[32m' }
if (c === 'red') { return '\x1b[31m' }
if (c === 'yellow') { return '\x1b[33m' }
if (c === 'blue') { return '\x1b[34m' }
if (c === 'magenta') { return '\x1b[35m' }
if (c === 'cyan') { return '\x1b[36m' }
return '\x1b[0m'
}