Prettied all js in src/

This commit is contained in:
mrfry 2020-10-01 14:44:24 +02:00
parent 3f081d8dff
commit ee0f0a9f3b
17 changed files with 1012 additions and 688 deletions

View file

@ -18,7 +18,8 @@
------------------------------------------------------------------------- */
const hr = '---------------------------------------------------------------------------------'
const hr =
'---------------------------------------------------------------------------------'
module.exports = {
GetDateString: GetDateString,
@ -30,7 +31,7 @@ module.exports = {
Load: Load,
logHashed: logHashed,
hr: hr,
C: C
C: C,
}
const DELIM = C('green') + '|' + C()
@ -45,14 +46,7 @@ const uStatsFile = 'stats/ustats'
const uvStatsFile = 'stats/uvstats'
const nologFile = './nolog'
const colors = [
'green',
'red',
'yellow',
'blue',
'magenta',
'cyan'
]
const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan']
const writeInterval = 10
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
@ -66,19 +60,21 @@ let writes = 0
let noLogips = []
function GetDateString () {
function GetDateString() {
const m = new Date()
const d = utils.GetDateString()
return GetRandomColor(m.getHours().toString()) + d + C()
}
function DebugLog (msg, name, lvl) {
function DebugLog(msg, name, lvl) {
if (lvl <= debugLevel) {
if (msg === 'hr') {
msg = hr
}
let s = msg
let header = `${C('red')}#DEBUG${lvl}#${C('yellow')}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
let header = `${C('red')}#DEBUG${lvl}#${C(
'yellow'
)}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
if (typeof msg !== 'object') {
s = header + msg
} else {
@ -89,7 +85,7 @@ function DebugLog (msg, name, lvl) {
}
}
function Log (s, c) {
function Log(s, c) {
let log = s
if (typeof s !== 'object') {
let dl = DELIM + C(c)
@ -100,7 +96,7 @@ function Log (s, c) {
utils.AppendToFile(log, logFile)
}
function LogReq (req, toFile, sc) {
function LogReq(req, toFile, sc) {
try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
@ -122,12 +118,13 @@ function LogReq (req, toFile, sc) {
hostname = req.hostname.replace('www.', '').split('.')[0]
} else {
hostname = 'NOHOST'
Log('req.hostname is undefined! req.hostname: ' + req.hostname, GetColor('redbg'))
Log(
'req.hostname is undefined! req.hostname: ' + req.hostname,
GetColor('redbg')
)
}
logEntry += dl +
hostname + dl +
req.headers['user-agent'] + dl +
req.method + dl
logEntry +=
dl + hostname + dl + req.headers['user-agent'] + dl + req.method + dl
if (req.session && req.session.user) {
logEntry += C('cyan') + req.session.user.id + C() + dl
@ -139,7 +136,9 @@ function LogReq (req, toFile, sc) {
logEntry += GetRandomColor(req.url.split('?')[0]) + req.url
if (sc !== undefined) { logEntry += dl + sc }
if (sc !== undefined) {
logEntry += dl + sc
}
logEntry += C()
if (!toFile) {
@ -156,7 +155,7 @@ function LogReq (req, toFile, sc) {
}
}
function parseNoLogFile (newData) {
function parseNoLogFile(newData) {
noLogips = newData.split('\n')
if (noLogips[noLogips.length - 1] === '') {
noLogips.pop()
@ -167,7 +166,7 @@ function parseNoLogFile (newData) {
Log('\tNo Log IP-s changed: ' + noLogips.join(', '))
}
function setNoLogReadInterval () {
function setNoLogReadInterval() {
utils.WatchFile(nologFile, (newData) => {
parseNoLogFile(newData)
})
@ -175,7 +174,7 @@ function setNoLogReadInterval () {
parseNoLogFile(utils.ReadFile(nologFile))
}
function Load () {
function Load() {
Log('Loading logger...')
try {
uData = JSON.parse(utils.ReadFile(uStatsFile))
@ -203,13 +202,16 @@ function Load () {
var prevVData = utils.ReadFile(vStatFile)
vData = JSON.parse(prevVData)
} catch (e) {
Log('Error at loading visit logs! (@ first run its normal)', GetColor('redbg'))
Log(
'Error at loading visit logs! (@ first run its normal)',
GetColor('redbg')
)
console.log(e)
}
setNoLogReadInterval()
}
function LogStat (url, ip, hostname, userId) {
function LogStat(url, ip, hostname, userId) {
let nolog = noLogips.some((x) => {
return x.includes(ip)
})
@ -225,9 +227,11 @@ function LogStat (url, ip, hostname, userId) {
Save()
}
function IncUserStat (userId) {
function IncUserStat(userId) {
try {
if (uData[userId] === undefined) { uData[userId] = 0 }
if (uData[userId] === undefined) {
uData[userId] = 0
}
uData[userId]++
} catch (e) {
Log('Error at making user ID stats!', GetColor('redbg'))
@ -235,12 +239,21 @@ function IncUserStat (userId) {
}
}
function AddUserIdStat (userId) {
function AddUserIdStat(userId) {
try {
var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2)
if (uvData[now] === undefined) { uvData[now] = {} }
if (uvData[now][userId] === undefined) { uvData[now][userId] = 0 }
const now =
m.getFullYear() +
'/' +
('0' + (m.getMonth() + 1)).slice(-2) +
'/' +
('0' + m.getDate()).slice(-2)
if (uvData[now] === undefined) {
uvData[now] = {}
}
if (uvData[now][userId] === undefined) {
uvData[now][userId] = 0
}
uvData[now][userId]++
} catch (e) {
Log('Error at making user ID stats!', GetColor('redbg'))
@ -248,21 +261,34 @@ function AddUserIdStat (userId) {
}
}
function Inc (value) {
if (value.startsWith('/?')) { value = '/' }
if (data[value] === undefined) { data[value] = 0 }
function Inc(value) {
if (value.startsWith('/?')) {
value = '/'
}
if (data[value] === undefined) {
data[value] = 0
}
data[value]++
}
function AddVisitStat (name) {
function AddVisitStat(name) {
var m = new Date()
const now = m.getFullYear() + '/' + ('0' + (m.getMonth() + 1)).slice(-2) + '/' + ('0' + m.getDate()).slice(-2)
if (vData[now] === undefined) { vData[now] = {} }
if (vData[now][name] === undefined) { vData[now][name] = 0 }
const now =
m.getFullYear() +
'/' +
('0' + (m.getMonth() + 1)).slice(-2) +
'/' +
('0' + m.getDate()).slice(-2)
if (vData[now] === undefined) {
vData[now] = {}
}
if (vData[now][name] === undefined) {
vData[now][name] = 0
}
vData[now][name]++
}
function Save () {
function Save() {
writes++
if (writes === writeInterval) {
try {
@ -295,11 +321,11 @@ function Save () {
}
}
function logHashed (x) {
function logHashed(x) {
return GetRandomColor(x.toString()) + x + C()
}
function GetRandomColor (ip) {
function GetRandomColor(ip) {
if (!ip) {
return 'red'
}
@ -310,20 +336,38 @@ function GetRandomColor (ip) {
return C(colors[res % colors.length])
}
function GetColor (c) {
function GetColor(c) {
return c
}
function C (c) {
if (c !== undefined) { c = c.toLowerCase() }
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' }
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'
}