/* ---------------------------------------------------------------------------- Question Server GitLab: 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 . ------------------------------------------------------------------------- */ const hr = '---------------------------------------------------------------------------------' const DELIM = C('green') + '|' + C() // import express from 'express' import utils from '../utils/utils' import { Request } from '../types/basicTypes' const vlogDir = 'stats/vlogs/' const logDir = 'stats/logs/' const statFile = 'stats/stats' const vStatFile = 'stats/vstats' const uStatsFile = 'stats/ustats' const uvStatsFile = 'stats/uvstats' const nologFile = './data/nolog' const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan'] const logFileName = 'log' const writeInterval = 10 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 // let vdata = {} // visit data // let dvData = {} // visit data, but daily // let uData = {} // visit data, but per user // let udvData = {} // visit data, but per user and daily let vData = {} // visit data let dvData = {} // visit data, but daily let uvData = {} // visit data, but per user let udvData = {} // visit data, but per user and daily let writes = 0 let noLogips = [] function getColoredDateString(): string { const date = new Date() const dateString = utils.GetDateString() return GetRandomColor(date.getHours().toString()) + dateString + C() } function DebugLog(msg: string | object, name: string, lvl: number): void { if (lvl <= debugLevel) { if (msg === 'hr') { msg = hr } let res = msg const header = `${C('red')}#DEBUG${lvl}#${C( 'yellow' )}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}` if (typeof msg !== 'object') { res = header + msg } else { Log(header + 'OBJECT:', 'yellow') res = msg } Log(res, 'yellow') } } function Log(msg: string | object, color?: string): void { let log = msg if (typeof msg !== 'object') { const delimiter = DELIM + C(color) log = getColoredDateString() + delimiter + C(color) + msg + C() } console.log(log) utils.AppendToFile( typeof log === 'string' ? log : JSON.stringify(log), logDir + logFileName ) } function expandWithSpaces(text, count) { while (text.length < count) { text += ' ' } return text } function LogReq(req: Request, toFile?: boolean, statusCode?: string): void { try { const ip: any = req.headers['cf-connecting-ip'] || req.connection.remoteAddress // if (!toFile) { // ip = expandWithSpaces(ip, 39) // } const nolog = noLogips.some((noLogip) => { return ip.includes(noLogip) }) if (nolog) { return } let logEntry = '' // logHashed(ip) let dl = DELIM if (req.url.includes('lred')) { dl += C('red') } let hostname if (req.hostname) { hostname = req.hostname.replace('www.', '').split('.')[0] } else { hostname = 'NOHOST' Log( 'req.hostname is undefined! req.hostname: ' + req.hostname, GetColor('redbg') ) } if (!toFile) { hostname = expandWithSpaces(hostname, 10) } logEntry += dl + logHashed(hostname) + dl if (toFile) { logEntry += req.headers['user-agent'] + dl logEntry += req.method + dl } let uid = '' if (req.session && req.session.user) { uid = req.session.user.id.toString() } else if (req.session && req.session.isException === true) { uid = 'EX' } else { uid = 'NOUSR' } if (!toFile) { uid = expandWithSpaces(uid, 5) } logEntry += GetRandomColor(uid.toString()) + uid + C() + dl logEntry += GetRandomColor(req.url.split('?')[0]) + req.url + C() if (statusCode !== undefined) { logEntry += dl + statusCode } logEntry += C() if (!toFile) { Log(logEntry) } else { const defLogs = utils.GetDateString() + dl + logEntry utils.AppendToFile(defLogs, vlogDir + logFileName) } } catch (err) { console.log(err) Log('Error at logging lol', GetColor('redbg')) } } function parseNoLogFile(newData) { noLogips = newData.split('\n') if (noLogips[noLogips.length - 1] === '') { noLogips.pop() } noLogips = noLogips.filter((noLogip) => { return noLogip !== '' }) Log('\tNo Log IP-s changed: ' + noLogips.join(', ')) } function setNoLogReadInterval() { utils.WatchFile(nologFile, (newData) => { parseNoLogFile(newData) }) parseNoLogFile(utils.ReadFile(nologFile)) } function Load(): void { Log('Loading logger...') try { uvData = JSON.parse(utils.ReadFile(uStatsFile)) } catch (err) { Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) console.error(err) } try { udvData = JSON.parse(utils.ReadFile(uvStatsFile)) } catch (err) { Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) console.error(err) } try { vData = utils.ReadJSON(statFile) } catch (err) { Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) console.error(err) } try { dvData = utils.ReadJSON(vStatFile) } catch (err) { Log( 'Error at loading visit logs! (@ first run its normal)', GetColor('redbg') ) console.error(err) } setNoLogReadInterval() } function LogStat( url: string, ip: string, hostname: string, userId: number ): void { const nolog = noLogips.some((noLogips) => { return ip.includes(noLogips) }) if (nolog) { return } url = hostname + url.split('?')[0] Inc(url) AddUserIdStat(userId) IncUserStat(userId) AddVisitStat(url) Save() } function IncUserStat(userId) { try { if (uvData[userId] === undefined) { uvData[userId] = 0 } uvData[userId]++ } catch (err) { Log('Error at making user ID stats!', GetColor('redbg')) console.error(err) } } function AddUserIdStat(userId) { try { const date = new Date() const now = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2) if (udvData[now] === undefined) { udvData[now] = {} } if (udvData[now][userId] === undefined) { udvData[now][userId] = 0 } udvData[now][userId]++ } catch (err) { Log('Error at making user ID stats!', GetColor('redbg')) console.error(err) } } function Inc(value) { if (value.startsWith('/?')) { value = '/' } if (vData[value] === undefined) { vData[value] = 0 } vData[value]++ } function AddVisitStat(name) { const date = new Date() const now = date.getFullYear() + '-' + ('0' + (date.getMonth() + 1)).slice(-2) + '-' + ('0' + date.getDate()).slice(-2) if (dvData[now] === undefined) { dvData[now] = {} } if (dvData[now][name] === undefined) { dvData[now][name] = 0 } dvData[now][name]++ } function Save() { writes++ if (writes === writeInterval) { try { utils.WriteFile(JSON.stringify(uvData), uStatsFile) } catch (err) { Log('Error at writing logs! (more in stderr)', GetColor('redbg')) console.error(err) } try { utils.WriteFile(JSON.stringify(udvData), uvStatsFile) } catch (err) { Log('Error at writing logs! (more in stderr)', GetColor('redbg')) console.error(err) } try { utils.WriteFile(JSON.stringify(vData), statFile) // Log("Stats wrote."); } catch (err) { Log('Error at writing logs! (more in stderr)', GetColor('redbg')) console.error(err) } try { utils.WriteFile(JSON.stringify(dvData), vStatFile) // Log("Stats wrote."); } catch (err) { Log('Error at writing visit logs! (more in stderr)', GetColor('redbg')) console.error(err) } writes = 0 } } function logHashed(msg: string): string { return GetRandomColor(msg.toString()) + msg + C() } function GetRandomColor(msg): string { if (!msg) { return 'red' } const res = msg.split('').reduce((res, character) => { return res + character.charCodeAt(0) }, 0) return C(colors[res % colors.length]) } function GetColor(color: string): string { return color } function C(color?: string): string { if (color !== undefined) { color = color.toLowerCase() } if (color === 'redbg') { return '\x1b[41m' } if (color === 'bluebg') { return '\x1b[44m' } if (color === 'cyanbg') { return '\x1b[46m' } if (color === 'green') { return '\x1b[32m' } if (color === 'red') { return '\x1b[31m' } if (color === 'yellow') { return '\x1b[33m' } if (color === 'blue') { return '\x1b[34m' } if (color === 'magenta') { return '\x1b[35m' } if (color === 'cyan') { return '\x1b[36m' } return '\x1b[0m' } export default { getColoredDateString: getColoredDateString, Log: Log, DebugLog: DebugLog, GetColor: GetColor, LogReq: LogReq, LogStat: LogStat, Load: Load, logHashed: logHashed, hr: hr, C: C, logFileName: logFileName, logDir: logDir, vlogDir: vlogDir, }