Fixed some files

This commit is contained in:
mrfry 2020-11-23 17:11:16 +01:00
parent 7fcb15da88
commit b7ac485689
8 changed files with 250 additions and 199 deletions

View file

@ -21,6 +21,7 @@ module.exports = {
eqeqeq: ['warn', 'smart'], eqeqeq: ['warn', 'smart'],
'no-unused-vars': 'warn', 'no-unused-vars': 'warn',
'no-prototype-builtins': 'off', 'no-prototype-builtins': 'off',
'@typescript-eslint/ban-types': 'off',
'id-length': [ 'id-length': [
'warn', 'warn',
{ exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b'] }, { exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b'] },

5
package-lock.json generated
View file

@ -2626,6 +2626,11 @@
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz",
"integrity": "sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ==" "integrity": "sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ=="
}, },
"typescript-eslint": {
"version": "0.0.1-alpha.0",
"resolved": "https://registry.npmjs.org/typescript-eslint/-/typescript-eslint-0.0.1-alpha.0.tgz",
"integrity": "sha512-1hNKM37dAWML/2ltRXupOq2uqcdRQyDFphl+341NTPXFLLLiDhErXx8VtaSLh3xP7SyHZdcCgpt9boYYVb3fQg=="
},
"unpipe": { "unpipe": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",

View file

@ -14,6 +14,7 @@
"express-ejs-layouts": "^1.1.0", "express-ejs-layouts": "^1.1.0",
"sqlite3": "^4.1.1", "sqlite3": "^4.1.1",
"typescript": "^4.1.2", "typescript": "^4.1.2",
"typescript-eslint": "0.0.1-alpha.0",
"url": "^0.11.0", "url": "^0.11.0",
"uuid": "^7.0.3", "uuid": "^7.0.3",
"vhost": "^3.0.2" "vhost": "^3.0.2"

View file

@ -28,31 +28,43 @@ const port = isRoot ? 80 : 8080
const httpsport = isRoot ? 443 : 5001 const httpsport = isRoot ? 443 : 5001
import express from 'express' import express from 'express'
const vhost = require('vhost')
import logger from './utils/logger.js' import logger from './utils/logger.js'
const utils = require('./utils/utils.js') import vhost from 'vhost'
const http = require('http') import utils from './utils/utils.js'
const https = require('https') import http from 'http'
const cors = require('cors') import https from 'https'
const cookieParser = require('cookie-parser') import cors from 'cors'
const { v4: uuidv4 } = require('uuid') import cookieParser from 'cookie-parser'
import { uuidv4 } from 'uuid'
const dbtools = require('./utils/dbtools.js') import dbtools from './utils/dbtools.js'
const reqlogger = require('./middlewares/reqlogger.middleware.js') import reqlogger from './middlewares/reqlogger.middleware.js'
const extraModulesFile = './src/extraModules.json' import extraModulesFile from rc / extraModules.json
const modulesFile = './src/modules.json' import modulesFile from rc / modules.json
const usersDBPath = 'data/dbs/users.db' import usersDBPath from ata / dbs / users.db
const idStats = require('./utils/ids.js') import idStats from './utils/ids.js'
idStats.Load() idStats.Load()
logger.Load()
interface Modules {
[name: string]: Module
}
interface Module {
path: string
publicdirs: Array<string>
name: string
urls: Array<string>
nextdir?: string
isNextJs?: boolean
}
if (!utils.FileExists(usersDBPath)) { if (!utils.FileExists(usersDBPath)) {
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!') throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
} }
const userDB = dbtools.GetDB(usersDBPath) const userDB = dbtools.GetDB(usersDBPath)
let modules: Modules = utils.ReadJSON(modulesFile)
let modules = JSON.parse(utils.ReadFile(modulesFile))
logger.Load()
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
logger.Log('Loglevel is: ' + debugLevel) logger.Log('Loglevel is: ' + debugLevel)
@ -69,8 +81,6 @@ try {
console.error(err) console.error(err)
} }
// Setting up exits
// process.on('exit', () => exit('exit'))
process.on('SIGINT', () => exit('SIGINT')) process.on('SIGINT', () => exit('SIGINT'))
process.on('SIGTERM', () => exit('SIGTERM')) process.on('SIGTERM', () => exit('SIGTERM'))
@ -138,9 +148,10 @@ app.use(
) )
Object.keys(modules).forEach(function(key) { Object.keys(modules).forEach(function(key) {
let module = modules[key] const module = modules[key]
try { try {
let mod = require(module.path) import mod from module.path
// const mod = require(module.path)
logger.Log(`Loading ${mod.name} module`, logger.GetColor('yellow')) logger.Log(`Loading ${mod.name} module`, logger.GetColor('yellow'))
module.publicdirs.forEach((pdir) => { module.publicdirs.forEach((pdir) => {
@ -174,7 +185,8 @@ const privkeyFile = '/etc/letsencrypt/live/frylabs.net/privkey.pem'
const fullchainFile = '/etc/letsencrypt/live/frylabs.net/fullchain.pem' const fullchainFile = '/etc/letsencrypt/live/frylabs.net/fullchain.pem'
const chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem' const chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem'
var certsLoaded = false let certsLoaded = false
let certs
if ( if (
startHTTPS && startHTTPS &&
utils.FileExists(privkeyFile) && utils.FileExists(privkeyFile) &&
@ -185,7 +197,7 @@ if (
const key = utils.ReadFile(privkeyFile) const key = utils.ReadFile(privkeyFile)
const cert = utils.ReadFile(fullchainFile) const cert = utils.ReadFile(fullchainFile)
const ca = utils.ReadFile(chainFile) const ca = utils.ReadFile(chainFile)
var certs = { certs = {
key: key, key: key,
cert: cert, cert: cert,
ca: ca, ca: ca,

View file

@ -1,11 +1,6 @@
const { import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'
Worker, import logger from './logger.js'
isMainThread, import { Question, QuestionData, Subject } from '../types/basicTypes'
parentPort,
workerData,
} = require('worker_threads')
const logger = require('./logger.js')
const searchDataWorkerFile = './src/utils/classes.js' const searchDataWorkerFile = './src/utils/classes.js'
@ -42,8 +37,8 @@ const minMatchToNotSearchOtherSubjects = 90
// Exported // Exported
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
function getSubjNameWithoutYear(subjName) { function getSubjNameWithoutYear(subjName: string) {
let t = subjName.split(' - ') const t = subjName.split(' - ')
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) { if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
return t[1] || subjName return t[1] || subjName
} else { } else {
@ -53,16 +48,20 @@ function getSubjNameWithoutYear(subjName) {
// Not exported // Not exported
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
function removeStuff(value, removableStrings, toReplace) { function removeStuff(
value: string,
removableStrings: Array<string>,
toReplace: string
) {
removableStrings.forEach((removableString) => { removableStrings.forEach((removableString) => {
var regex = new RegExp(removableString, 'g') const regex = new RegExp(removableString, 'g')
value = value.replace(regex, toReplace || '') value = value.replace(regex, toReplace || '')
}) })
return value return value
} }
// removes whitespace from begining and and, and replaces multiple spaces with one space // removes whitespace from begining and and, and replaces multiple spaces with one space
function removeUnnecesarySpaces(toremove) { function removeUnnecesarySpaces(toremove: string) {
assert(toremove) assert(toremove)
toremove = normalizeSpaces(toremove) toremove = normalizeSpaces(toremove)
@ -73,27 +72,27 @@ function removeUnnecesarySpaces(toremove) {
} }
// simplifies a string for easier comparison // simplifies a string for easier comparison
function simplifyStringForComparison(value) { function simplifyStringForComparison(value: string) {
assert(value) assert(value)
value = removeUnnecesarySpaces(value).toLowerCase() value = removeUnnecesarySpaces(value).toLowerCase()
return removeStuff(value, commonUselessStringParts) return removeStuff(value, commonUselessStringParts)
} }
function removeSpecialChars(value) { function removeSpecialChars(value: string) {
assert(value) assert(value)
return removeStuff(value, specialChars, ' ') return removeStuff(value, specialChars, ' ')
} }
// damn nonbreaking space // damn nonbreaking space
function normalizeSpaces(input) { function normalizeSpaces(input: string) {
assert(input) assert(input)
return input.replace(/\s/g, ' ') return input.replace(/\s/g, ' ')
} }
function compareString(s1, s2) { function compareString(s1: string, s2: string) {
if (!s1 || !s2) { if (!s1 || !s2) {
if (!s1 && !s2) { if (!s1 && !s2) {
return 100 return 100
@ -104,14 +103,14 @@ function compareString(s1, s2) {
s1 = simplifyStringForComparison(s1).split(' ') s1 = simplifyStringForComparison(s1).split(' ')
s2 = simplifyStringForComparison(s2).split(' ') s2 = simplifyStringForComparison(s2).split(' ')
var match = 0 let match = 0
for (var i = 0; i < s1.length; i++) { for (let i = 0; i < s1.length; i++) {
if (s2.includes(s1[i])) { if (s2.includes(s1[i])) {
match++ match++
} }
} }
var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent let percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
var lengthDifference = Math.abs(s2.length - s1.length) const lengthDifference = Math.abs(s2.length - s1.length)
percent -= lengthDifference * lengthDiffMultiplier percent -= lengthDifference * lengthDiffMultiplier
if (percent < 0) { if (percent < 0) {
percent = 0 percent = 0
@ -119,19 +118,19 @@ function compareString(s1, s2) {
return percent return percent
} }
function answerPreProcessor(value) { function answerPreProcessor(value: string) {
assert(value) assert(value)
return removeStuff(value, commonUselessAnswerParts) return removeStuff(value, commonUselessAnswerParts)
} }
// 'a. pécsi sör' -> 'pécsi sör' // 'a. pécsi sör' -> 'pécsi sör'
function removeAnswerLetters(value) { function removeAnswerLetters(value: string) {
if (!value) { if (!value) {
return return
} }
let val = value.split('. ') const val = value.split('. ')
if (val[0].length < 2 && val.length > 1) { if (val[0].length < 2 && val.length > 1) {
val.shift() val.shift()
return val.join(' ') return val.join(' ')
@ -140,7 +139,7 @@ function removeAnswerLetters(value) {
} }
} }
function simplifyQA(value, mods) { function simplifyQA(value: string, mods: Array<Function>) {
if (!value) { if (!value) {
return return
} }
@ -150,7 +149,7 @@ function simplifyQA(value, mods) {
}, value) }, value)
} }
function simplifyAnswer(value) { function simplifyAnswer(value: string) {
if (!value) { if (!value) {
return value return value
} }
@ -162,7 +161,7 @@ function simplifyAnswer(value) {
]) ])
} }
function simplifyQuestion(question) { function simplifyQuestion(question: Question) {
if (!question) { if (!question) {
return return
} }
@ -195,7 +194,7 @@ function simplifyQuestion(question) {
// Question // Question
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
function createQuestion(question, answer, data) { function createQuestion(question: string, answer: string, data: QuestionData) {
return { return {
Q: simplifyQuestion(question), Q: simplifyQuestion(question),
A: simplifyAnswer(answer), A: simplifyAnswer(answer),
@ -203,14 +202,14 @@ function createQuestion(question, answer, data) {
} }
} }
function compareImage(data, data2) { function compareImage(data: QuestionData, data2: QuestionData) {
return compareString(data.images.join(' '), data2.images.join(' ')) return compareString(data.images.join(' '), data2.images.join(' '))
} }
function compareData(q1, q2) { function compareData(q1: Question, q2: Question) {
try { try {
if (q1.data.type === q2.data.type) { if (q1.data.type === q2.data.type) {
let dataType = q1.data.type const dataType = q1.data.type
if (dataType === 'simple') { if (dataType === 'simple') {
return -1 return -1
} else if (dataType === 'image') { } else if (dataType === 'image') {
@ -234,15 +233,21 @@ function compareData(q1, q2) {
return 0 return 0
} }
function compareQuestion(q1, q2) { function compareQuestion(q1: Question, q2: Question) {
return compareString(q1.Q, q2.Q) return compareString(q1.Q, q2.Q)
} }
function compareAnswer(q1, q2) { function compareAnswer(q1: Question, q2: Question) {
return compareString(q1.A, q2.A) return compareString(q1.A, q2.A)
} }
function compareQuestionObj(q1, q1subjName, q2, q2subjName, data) { function compareQuestionObj(
q1: Question,
q1subjName: string,
q2: Question,
q2subjName: string,
data: QuestionData
) {
assert(data) assert(data)
assert(q1) assert(q1)
assert(typeof q1 === 'object') assert(typeof q1 === 'object')
@ -287,7 +292,7 @@ function compareQuestionObj(q1, q1subjName, q2, q2subjName, data) {
} }
} }
function questionToString(question) { function questionToString(question: Question) {
const { Q, A, data } = question const { Q, A, data } = question
if (data.type !== 'simple') { if (data.type !== 'simple') {
@ -300,12 +305,17 @@ function questionToString(question) {
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
// Subject // Subject
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
function searchQuestion(subj, question, questionData, subjName) { function searchQuestion(
subj: Subject,
question: Question,
questionData: QuestionData,
subjName: string
) {
assert(question) assert(question)
var result = [] const result = []
subj.Questions.forEach((currentQuestion) => { subj.Questions.forEach((currentQuestion) => {
let percent = compareQuestionObj( const percent = compareQuestionObj(
currentQuestion, currentQuestion,
subjName, subjName,
question, question,
@ -335,10 +345,10 @@ function searchQuestion(subj, question, questionData, subjName) {
return result return result
} }
function subjectToString(subj) { function subjectToString(subj: Subject) {
const { Questions, Name } = subj const { Questions, Name } = subj
var result = [] const result = []
Questions.forEach((question) => { Questions.forEach((question) => {
result.push(questionToString(question)) result.push(questionToString(question))
}) })
@ -349,7 +359,7 @@ function subjectToString(subj) {
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
// QuestionDB // QuestionDB
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
function addQuestion(data, subj, question) { function addQuestion(data: Array<Subject>, subj: string, question: Question) {
logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1) logger.DebugLog('Adding new question with subjName: ' + subj, 'qdb add', 1)
logger.DebugLog(question, 'qdb add', 3) logger.DebugLog(question, 'qdb add', 3)
assert(data) assert(data)
@ -357,7 +367,7 @@ function addQuestion(data, subj, question) {
assert(question) assert(question)
assert(typeof question === 'object') assert(typeof question === 'object')
var i = 0 let i = 0
while ( while (
i < data.length && i < data.length &&
!subj !subj
@ -379,7 +389,12 @@ function addQuestion(data, subj, question) {
} }
} }
function searchData(data, question, subjName, questionData) { function searchData(
data: Array<Subject>,
question: Question,
subjName: string,
questionData: QuestionData
) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
assert(data) assert(data)
assert(question) assert(question)
@ -436,11 +451,11 @@ function searchData(data, question, subjName, questionData) {
}) })
} }
function addSubject(data, subj) { function addSubject(data: Array<Subject>, subj) {
assert(data) assert(data)
assert(subj) assert(subj)
var i = 0 let i = 0
while (i < length && subj.Name !== data[i].Name) { while (i < length && subj.Name !== data[i].Name) {
i++ i++
} }
@ -461,8 +476,8 @@ function addSubject(data, subj) {
} }
} }
function dataToString(data) { function dataToString(data: Array<Subject>) {
var result = [] const result = []
data.forEach((subj) => { data.forEach((subj) => {
result.push(subjectToString(subj)) result.push(subjectToString(subj))
}) })

View file

@ -23,21 +23,20 @@ module.exports = {
Load: Load, Load: Load,
} }
const utils = require('../utils/utils.js') import utils from '../utils/utils.js'
const logger = require('../utils/logger.js') import logger from '../utils/logger.js'
const idStatFile = 'stats/idstats' const idStatFile = 'stats/idstats'
const idVStatFile = 'stats/idvstats' const idVStatFile = 'stats/idvstats'
const writeInterval = 1 const writeInterval = 1
let data = {} let idStatsData = {}
let vData = {} let idvStatsData = {}
let writes = 0 let writes = 0
function Load() { function Load() {
try { try {
var prevData = utils.ReadFile(idStatFile) idStatsData = utils.ReadJSON(idStatFile)
data = JSON.parse(prevData)
} catch (err) { } catch (err) {
logger.Log( logger.Log(
'Error at loading id logs! (@ first run its normal)', 'Error at loading id logs! (@ first run its normal)',
@ -47,8 +46,8 @@ function Load() {
} }
try { try {
var prevVData = utils.ReadFile(idVStatFile) const prevVData = utils.ReadFile(idVStatFile)
vData = JSON.parse(prevVData) idvStatsData = JSON.parse(prevVData)
} catch (err) { } catch (err) {
logger.Log( logger.Log(
'Error at loading id logs! (@ first run its normal)', 'Error at loading id logs! (@ first run its normal)',
@ -58,71 +57,86 @@ function Load() {
} }
} }
function LogId(id, subj, newQuestions, allQuestions) { function LogId(
id: number,
subj: string,
newQuestions: number,
allQuestions: number
) {
Inc(id, subj, newQuestions, allQuestions) Inc(id, subj, newQuestions, allQuestions)
AddVisitStat(id, subj, newQuestions, allQuestions) AddVisitStat(id, subj, newQuestions, allQuestions)
Save() Save()
} }
function AddSubjToList(list, subj) { function AddSubjToList(list: Array<string>, subj: string) {
if (!list[subj]) { if (!list[subj]) {
list[subj] = 0 list[subj] = 0
} }
list[subj]++ list[subj]++
} }
function Inc(value, subj, newQuestions, allQuestions) { function Inc(
if (data[value] === undefined) { value: string,
data[value] = { subj: string,
newQuestions: number,
allQuestions: number
) {
if (idStatsData[value] === undefined) {
idStatsData[value] = {
count: 0, count: 0,
newQuestions: 0, newQuestions: 0,
allQuestions: 0, allQuestions: 0,
subjs: {}, subjs: {},
} }
} }
data[value].count++ idStatsData[value].count++
data[value].newQuestions += newQuestions idStatsData[value].newQuestions += newQuestions
data[value].allQuestions += allQuestions idStatsData[value].allQuestions += allQuestions
AddSubjToList(data[value].subjs, subj) AddSubjToList(idStatsData[value].subjs, subj)
} }
function AddVisitStat(name, subj, newQuestions, allQuestions) { function AddVisitStat(
var date = new Date() name: string,
subj: string,
newQuestions: number,
allQuestions: number
) {
const date = new Date()
const now = const now =
date.getFullYear() + date.getFullYear() +
'-' + '-' +
('0' + (date.getMonth() + 1)).slice(-2) + ('0' + (date.getMonth() + 1)).slice(-2) +
'-' + '-' +
('0' + date.getDate()).slice(-2) ('0' + date.getDate()).slice(-2)
if (vData[now] === undefined) { if (idvStatsData[now] === undefined) {
vData[now] = {} idvStatsData[now] = {}
} }
if (vData[now][name] === undefined) { if (idvStatsData[now][name] === undefined) {
vData[now][name] = { idvStatsData[now][name] = {
count: 0, count: 0,
newQuestions: 0, newQuestions: 0,
allQuestions: 0, allQuestions: 0,
subjs: {}, subjs: {},
} }
} }
vData[now][name].count++ idvStatsData[now][name].count++
vData[now][name].newQuestions += newQuestions idvStatsData[now][name].newQuestions += newQuestions
vData[now][name].allQuestions += allQuestions idvStatsData[now][name].allQuestions += allQuestions
AddSubjToList(vData[now][name].subjs, subj) AddSubjToList(idvStatsData[now][name].subjs, subj)
} }
function Save() { function Save() {
writes++ writes++
if (writes === writeInterval) { if (writes === writeInterval) {
try { try {
utils.WriteFile(JSON.stringify(data), idStatFile) utils.WriteFile(JSON.stringify(idStatsData), idStatFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (err) { } catch (err) {
logger.Log('Error at writing logs!', logger.GetColor('redbg')) logger.Log('Error at writing logs!', logger.GetColor('redbg'))
console.error(err) console.error(err)
} }
try { try {
utils.WriteFile(JSON.stringify(vData), idVStatFile) utils.WriteFile(JSON.stringify(idvStatsData), idVStatFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (err) { } catch (err) {
logger.Log('Error at writing visit logs!', logger.GetColor('redbg')) logger.Log('Error at writing visit logs!', logger.GetColor('redbg'))

View file

@ -37,7 +37,7 @@ module.exports = {
const DELIM = C('green') + '|' + C() const DELIM = C('green') + '|' + C()
const utils = require('../utils/utils.js') import utils from '../utils/utils'
const vlogDir = './stats/vlogs/' const vlogDir = './stats/vlogs/'
const logDir = './stats/logs/' const logDir = './stats/logs/'
const statFile = 'stats/stats' const statFile = 'stats/stats'
@ -47,15 +47,20 @@ const uvStatsFile = 'stats/uvstats'
const nologFile = './nolog' const nologFile = './nolog'
const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan'] const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan']
var logFileName = utils.GetDateString(true) let logFileName = utils.GetDateString(true)
const writeInterval = 10 const writeInterval = 10
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0 const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
let data = {} // visit data // let vdata = {} // visit data
let vData = {} // visit data, but daily // let dvData = {} // visit data, but daily
let uData = {} // visit data, but per user // let uData = {} // visit data, but per user
let uvData = {} // visit data, but per user and daily // 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 writes = 0
let noLogips = [] let noLogips = []
@ -75,25 +80,25 @@ function DebugLog(msg, name, lvl) {
if (msg === 'hr') { if (msg === 'hr') {
msg = hr msg = hr
} }
let s = msg let res = msg
let header = `${C('red')}#DEBUG${lvl}#${C( const header = `${C('red')}#DEBUG${lvl}#${C(
'yellow' 'yellow'
)}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}` )}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
if (typeof msg !== 'object') { if (typeof msg !== 'object') {
s = header + msg res = header + msg
} else { } else {
Log(header + 'OBJECT:', 'yellow') Log(header + 'OBJECT:', 'yellow')
s = msg res = msg
} }
Log(s, 'yellow') Log(res, 'yellow')
} }
} }
function Log(s, c) { function Log(msg, color) {
let log = s let log = msg
if (typeof s !== 'object') { if (typeof s !== 'object') {
let dl = DELIM + C(c) const delimiter = DELIM + C(color)
log = C(c) + GetDateString() + dl + s + C() log = C(color) + GetDateString() + delimiter + msg + C()
} }
console.log(log) console.log(log)
@ -102,10 +107,10 @@ function Log(s, c) {
function LogReq(req, toFile, sc) { function LogReq(req, toFile, sc) {
try { try {
let ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress const ip = req.headers['cf-connecting-ip'] || req.connection.remoteAddress
let nolog = noLogips.some((x) => { const nolog = noLogips.some((noLogip) => {
return ip.includes(x) return ip.includes(noLogip)
}) })
if (nolog) { if (nolog) {
return return
@ -148,7 +153,7 @@ function LogReq(req, toFile, sc) {
if (!toFile) { if (!toFile) {
Log(logEntry) Log(logEntry)
} else { } else {
let defLogs = GetDateString() + dl + logEntry const defLogs = GetDateString() + dl + logEntry
utils.AppendToFile(defLogs, vlogDir + logFileName) utils.AppendToFile(defLogs, vlogDir + logFileName)
} }
@ -163,8 +168,8 @@ function parseNoLogFile(newData) {
if (noLogips[noLogips.length - 1] === '') { if (noLogips[noLogips.length - 1] === '') {
noLogips.pop() noLogips.pop()
} }
noLogips = noLogips.filter((x) => { noLogips = noLogips.filter((noLogip) => {
return x !== '' return noLogip !== ''
}) })
Log('\tNo Log IP-s changed: ' + noLogips.join(', ')) Log('\tNo Log IP-s changed: ' + noLogips.join(', '))
} }
@ -180,43 +185,41 @@ function setNoLogReadInterval() {
function Load() { function Load() {
Log('Loading logger...') Log('Loading logger...')
try { try {
uData = JSON.parse(utils.ReadFile(uStatsFile)) uvData = JSON.parse(utils.ReadFile(uStatsFile))
} catch (e) { } catch (err) {
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
console.log(e) console.error(err)
} }
try { try {
uvData = JSON.parse(utils.ReadFile(uvStatsFile)) udvData = JSON.parse(utils.ReadFile(uvStatsFile))
} catch (e) { } catch (err) {
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
console.log(e) console.error(err)
} }
try { try {
var prevData = utils.ReadFile(statFile) vData = utils.ReadJSON(statFile)
data = JSON.parse(prevData) } catch (err) {
} catch (e) {
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg')) Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
console.log(e) console.error(err)
} }
try { try {
var prevVData = utils.ReadFile(vStatFile) dvData = utils.ReadJSON(vStatFile)
vData = JSON.parse(prevVData) } catch (err) {
} catch (e) {
Log( Log(
'Error at loading visit logs! (@ first run its normal)', 'Error at loading visit logs! (@ first run its normal)',
GetColor('redbg') GetColor('redbg')
) )
console.log(e) console.error(err)
} }
setNoLogReadInterval() setNoLogReadInterval()
} }
function LogStat(url, ip, hostname, userId) { function LogStat(url, ip, hostname, userId) {
let nolog = noLogips.some((x) => { const nolog = noLogips.some((noLogips) => {
return x.includes(ip) return ip.includes(noLogips)
}) })
if (nolog) { if (nolog) {
return return
@ -232,35 +235,35 @@ function LogStat(url, ip, hostname, userId) {
function IncUserStat(userId) { function IncUserStat(userId) {
try { try {
if (uData[userId] === undefined) { if (uvData[userId] === undefined) {
uData[userId] = 0 uvData[userId] = 0
} }
uData[userId]++ uvData[userId]++
} catch (e) { } catch (err) {
Log('Error at making user ID stats!', GetColor('redbg')) Log('Error at making user ID stats!', GetColor('redbg'))
console.error(e) console.error(err)
} }
} }
function AddUserIdStat(userId) { function AddUserIdStat(userId) {
try { try {
var m = new Date() const date = new Date()
const now = const now =
m.getFullYear() + date.getFullYear() +
'-' + '-' +
('0' + (m.getMonth() + 1)).slice(-2) + ('0' + (date.getMonth() + 1)).slice(-2) +
'-' + '-' +
('0' + m.getDate()).slice(-2) ('0' + date.getDate()).slice(-2)
if (uvData[now] === undefined) { if (udvData[now] === undefined) {
uvData[now] = {} udvData[now] = {}
} }
if (uvData[now][userId] === undefined) { if (udvData[now][userId] === undefined) {
uvData[now][userId] = 0 udvData[now][userId] = 0
} }
uvData[now][userId]++ udvData[now][userId]++
} catch (e) { } catch (err) {
Log('Error at making user ID stats!', GetColor('redbg')) Log('Error at making user ID stats!', GetColor('redbg'))
console.error(e) console.error(err)
} }
} }
@ -268,108 +271,108 @@ function Inc(value) {
if (value.startsWith('/?')) { if (value.startsWith('/?')) {
value = '/' value = '/'
} }
if (data[value] === undefined) { if (vData[value] === undefined) {
data[value] = 0 vData[value] = 0
} }
data[value]++ vData[value]++
} }
function AddVisitStat(name) { function AddVisitStat(name) {
var m = new Date() const date = new Date()
const now = const now =
m.getFullYear() + date.getFullYear() +
'-' + '-' +
('0' + (m.getMonth() + 1)).slice(-2) + ('0' + (date.getMonth() + 1)).slice(-2) +
'-' + '-' +
('0' + m.getDate()).slice(-2) ('0' + date.getDate()).slice(-2)
if (vData[now] === undefined) { if (dvData[now] === undefined) {
vData[now] = {} dvData[now] = {}
} }
if (vData[now][name] === undefined) { if (dvData[now][name] === undefined) {
vData[now][name] = 0 dvData[now][name] = 0
} }
vData[now][name]++ dvData[now][name]++
} }
function Save() { function Save() {
writes++ writes++
if (writes === writeInterval) { if (writes === writeInterval) {
try { try {
utils.WriteFile(JSON.stringify(uData), uStatsFile) utils.WriteFile(JSON.stringify(uvData), uStatsFile)
} catch (e) { } catch (err) {
Log('Error at writing logs! (more in stderr)', GetColor('redbg')) Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
console.error(e) console.error(err)
} }
try { try {
utils.WriteFile(JSON.stringify(uvData), uvStatsFile) utils.WriteFile(JSON.stringify(udvData), uvStatsFile)
} catch (e) { } catch (err) {
Log('Error at writing logs! (more in stderr)', GetColor('redbg')) Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
console.error(e) console.error(err)
} }
try { try {
utils.WriteFile(JSON.stringify(data), statFile) utils.WriteFile(JSON.stringify(vData), statFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (e) { } catch (err) {
Log('Error at writing logs! (more in stderr)', GetColor('redbg')) Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
console.error(e) console.error(err)
} }
try { try {
utils.WriteFile(JSON.stringify(vData), vStatFile) utils.WriteFile(JSON.stringify(dvData), vStatFile)
// Log("Stats wrote."); // Log("Stats wrote.");
} catch (e) { } catch (err) {
Log('Error at writing visit logs! (more in stderr)', GetColor('redbg')) Log('Error at writing visit logs! (more in stderr)', GetColor('redbg'))
console.error(e) console.error(err)
} }
writes = 0 writes = 0
} }
} }
function logHashed(x) { function logHashed(msg) {
return GetRandomColor(x.toString()) + x + C() return GetRandomColor(msg.toString()) + msg + C()
} }
function GetRandomColor(ip) { function GetRandomColor(msg) {
if (!ip) { if (!msg) {
return 'red' return 'red'
} }
let res = ip.split('').reduce((res, x) => { const res = msg.split('').reduce((res, character) => {
return res + x.charCodeAt(0) return res + character.charCodeAt(0)
}, 0) }, 0)
return C(colors[res % colors.length]) return C(colors[res % colors.length])
} }
function GetColor(c) { function GetColor(color) {
return c return color
} }
function C(c) { function C(color?: string) {
if (c !== undefined) { if (color !== undefined) {
c = c.toLowerCase() color = color.toLowerCase()
} }
if (c === 'redbg') { if (color === 'redbg') {
return '\x1b[41m' return '\x1b[41m'
} }
if (c === 'bluebg') { if (color === 'bluebg') {
return '\x1b[44m' return '\x1b[44m'
} }
if (c === 'green') { if (color === 'green') {
return '\x1b[32m' return '\x1b[32m'
} }
if (c === 'red') { if (color === 'red') {
return '\x1b[31m' return '\x1b[31m'
} }
if (c === 'yellow') { if (color === 'yellow') {
return '\x1b[33m' return '\x1b[33m'
} }
if (c === 'blue') { if (color === 'blue') {
return '\x1b[34m' return '\x1b[34m'
} }
if (c === 'magenta') { if (color === 'magenta') {
return '\x1b[35m' return '\x1b[35m'
} }
if (c === 'cyan') { if (color === 'coloryan') {
return '\x1b[36m' return '\x1b[36m'
} }
return '\x1b[0m' return '\x1b[0m'

View file

@ -12,8 +12,8 @@ module.exports = {
GetDateString: GetDateString, GetDateString: GetDateString,
} }
const fs = require('fs') import fs from 'fs'
const logger = require('../utils/logger.js') import logger from '../utils/logger.js'
function GetDateString(noTime) { function GetDateString(noTime) {
const date = new Date() const date = new Date()
@ -96,9 +96,9 @@ function CreatePath(path, onlyPath) {
return return
} }
var spath = path.split('/') const spath = path.split('/')
var currDir = spath[0] let currDir = spath[0]
for (var i = 1; i < spath.length; i++) { for (let i = 1; i < spath.length; i++) {
if (currDir !== '' && !fs.existsSync(currDir)) { if (currDir !== '' && !fs.existsSync(currDir)) {
try { try {
fs.mkdirSync(currDir) fs.mkdirSync(currDir)