mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
Fixed some files
This commit is contained in:
parent
7fcb15da88
commit
b7ac485689
8 changed files with 250 additions and 199 deletions
|
@ -21,6 +21,7 @@ module.exports = {
|
|||
eqeqeq: ['warn', 'smart'],
|
||||
'no-unused-vars': 'warn',
|
||||
'no-prototype-builtins': 'off',
|
||||
'@typescript-eslint/ban-types': 'off',
|
||||
'id-length': [
|
||||
'warn',
|
||||
{ exceptions: ['i', 'j', 't', 'Q', 'A', 'C', 'q', 'a', 'b'] },
|
||||
|
|
5
package-lock.json
generated
5
package-lock.json
generated
|
@ -2626,6 +2626,11 @@
|
|||
"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz",
|
||||
"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": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
"express-ejs-layouts": "^1.1.0",
|
||||
"sqlite3": "^4.1.1",
|
||||
"typescript": "^4.1.2",
|
||||
"typescript-eslint": "0.0.1-alpha.0",
|
||||
"url": "^0.11.0",
|
||||
"uuid": "^7.0.3",
|
||||
"vhost": "^3.0.2"
|
||||
|
|
|
@ -28,31 +28,43 @@ const port = isRoot ? 80 : 8080
|
|||
const httpsport = isRoot ? 443 : 5001
|
||||
|
||||
import express from 'express'
|
||||
const vhost = require('vhost')
|
||||
import logger from './utils/logger.js'
|
||||
const utils = require('./utils/utils.js')
|
||||
const http = require('http')
|
||||
const https = require('https')
|
||||
const cors = require('cors')
|
||||
const cookieParser = require('cookie-parser')
|
||||
const { v4: uuidv4 } = require('uuid')
|
||||
import vhost from 'vhost'
|
||||
import utils from './utils/utils.js'
|
||||
import http from 'http'
|
||||
import https from 'https'
|
||||
import cors from 'cors'
|
||||
import cookieParser from 'cookie-parser'
|
||||
import { uuidv4 } from 'uuid'
|
||||
|
||||
const dbtools = require('./utils/dbtools.js')
|
||||
const reqlogger = require('./middlewares/reqlogger.middleware.js')
|
||||
const extraModulesFile = './src/extraModules.json'
|
||||
const modulesFile = './src/modules.json'
|
||||
const usersDBPath = 'data/dbs/users.db'
|
||||
const idStats = require('./utils/ids.js')
|
||||
import dbtools from './utils/dbtools.js'
|
||||
import reqlogger from './middlewares/reqlogger.middleware.js'
|
||||
import extraModulesFile from rc / extraModules.json
|
||||
import modulesFile from rc / modules.json
|
||||
import usersDBPath from ata / dbs / users.db
|
||||
import idStats from './utils/ids.js'
|
||||
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)) {
|
||||
throw new Error('No user DB exists yet! please run utils/dbSetup.js first!')
|
||||
}
|
||||
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
|
||||
logger.Log('Loglevel is: ' + debugLevel)
|
||||
|
||||
|
@ -69,8 +81,6 @@ try {
|
|||
console.error(err)
|
||||
}
|
||||
|
||||
// Setting up exits
|
||||
// process.on('exit', () => exit('exit'))
|
||||
process.on('SIGINT', () => exit('SIGINT'))
|
||||
process.on('SIGTERM', () => exit('SIGTERM'))
|
||||
|
||||
|
@ -138,9 +148,10 @@ app.use(
|
|||
)
|
||||
|
||||
Object.keys(modules).forEach(function(key) {
|
||||
let module = modules[key]
|
||||
const module = modules[key]
|
||||
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'))
|
||||
|
||||
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 chainFile = '/etc/letsencrypt/live/frylabs.net/chain.pem'
|
||||
|
||||
var certsLoaded = false
|
||||
let certsLoaded = false
|
||||
let certs
|
||||
if (
|
||||
startHTTPS &&
|
||||
utils.FileExists(privkeyFile) &&
|
||||
|
@ -185,7 +197,7 @@ if (
|
|||
const key = utils.ReadFile(privkeyFile)
|
||||
const cert = utils.ReadFile(fullchainFile)
|
||||
const ca = utils.ReadFile(chainFile)
|
||||
var certs = {
|
||||
certs = {
|
||||
key: key,
|
||||
cert: cert,
|
||||
ca: ca,
|
||||
|
|
|
@ -1,11 +1,6 @@
|
|||
const {
|
||||
Worker,
|
||||
isMainThread,
|
||||
parentPort,
|
||||
workerData,
|
||||
} = require('worker_threads')
|
||||
|
||||
const logger = require('./logger.js')
|
||||
import { Worker, isMainThread, parentPort, workerData } from 'worker_threads'
|
||||
import logger from './logger.js'
|
||||
import { Question, QuestionData, Subject } from '../types/basicTypes'
|
||||
|
||||
const searchDataWorkerFile = './src/utils/classes.js'
|
||||
|
||||
|
@ -42,8 +37,8 @@ const minMatchToNotSearchOtherSubjects = 90
|
|||
|
||||
// Exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function getSubjNameWithoutYear(subjName) {
|
||||
let t = subjName.split(' - ')
|
||||
function getSubjNameWithoutYear(subjName: string) {
|
||||
const t = subjName.split(' - ')
|
||||
if (t[0].match(/^[0-9]{4}\/[0-9]{2}\/[0-9]{1}$/i)) {
|
||||
return t[1] || subjName
|
||||
} else {
|
||||
|
@ -53,16 +48,20 @@ function getSubjNameWithoutYear(subjName) {
|
|||
|
||||
// Not exported
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function removeStuff(value, removableStrings, toReplace) {
|
||||
function removeStuff(
|
||||
value: string,
|
||||
removableStrings: Array<string>,
|
||||
toReplace: string
|
||||
) {
|
||||
removableStrings.forEach((removableString) => {
|
||||
var regex = new RegExp(removableString, 'g')
|
||||
const regex = new RegExp(removableString, 'g')
|
||||
value = value.replace(regex, toReplace || '')
|
||||
})
|
||||
return value
|
||||
}
|
||||
|
||||
// removes whitespace from begining and and, and replaces multiple spaces with one space
|
||||
function removeUnnecesarySpaces(toremove) {
|
||||
function removeUnnecesarySpaces(toremove: string) {
|
||||
assert(toremove)
|
||||
|
||||
toremove = normalizeSpaces(toremove)
|
||||
|
@ -73,27 +72,27 @@ function removeUnnecesarySpaces(toremove) {
|
|||
}
|
||||
|
||||
// simplifies a string for easier comparison
|
||||
function simplifyStringForComparison(value) {
|
||||
function simplifyStringForComparison(value: string) {
|
||||
assert(value)
|
||||
|
||||
value = removeUnnecesarySpaces(value).toLowerCase()
|
||||
return removeStuff(value, commonUselessStringParts)
|
||||
}
|
||||
|
||||
function removeSpecialChars(value) {
|
||||
function removeSpecialChars(value: string) {
|
||||
assert(value)
|
||||
|
||||
return removeStuff(value, specialChars, ' ')
|
||||
}
|
||||
|
||||
// damn nonbreaking space
|
||||
function normalizeSpaces(input) {
|
||||
function normalizeSpaces(input: string) {
|
||||
assert(input)
|
||||
|
||||
return input.replace(/\s/g, ' ')
|
||||
}
|
||||
|
||||
function compareString(s1, s2) {
|
||||
function compareString(s1: string, s2: string) {
|
||||
if (!s1 || !s2) {
|
||||
if (!s1 && !s2) {
|
||||
return 100
|
||||
|
@ -104,14 +103,14 @@ function compareString(s1, s2) {
|
|||
|
||||
s1 = simplifyStringForComparison(s1).split(' ')
|
||||
s2 = simplifyStringForComparison(s2).split(' ')
|
||||
var match = 0
|
||||
for (var i = 0; i < s1.length; i++) {
|
||||
let match = 0
|
||||
for (let i = 0; i < s1.length; i++) {
|
||||
if (s2.includes(s1[i])) {
|
||||
match++
|
||||
}
|
||||
}
|
||||
var percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
var lengthDifference = Math.abs(s2.length - s1.length)
|
||||
let percent = Math.round(((match / s1.length) * 100).toFixed(2)) // matched words percent
|
||||
const lengthDifference = Math.abs(s2.length - s1.length)
|
||||
percent -= lengthDifference * lengthDiffMultiplier
|
||||
if (percent < 0) {
|
||||
percent = 0
|
||||
|
@ -119,19 +118,19 @@ function compareString(s1, s2) {
|
|||
return percent
|
||||
}
|
||||
|
||||
function answerPreProcessor(value) {
|
||||
function answerPreProcessor(value: string) {
|
||||
assert(value)
|
||||
|
||||
return removeStuff(value, commonUselessAnswerParts)
|
||||
}
|
||||
|
||||
// 'a. pécsi sör' -> 'pécsi sör'
|
||||
function removeAnswerLetters(value) {
|
||||
function removeAnswerLetters(value: string) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
||||
let val = value.split('. ')
|
||||
const val = value.split('. ')
|
||||
if (val[0].length < 2 && val.length > 1) {
|
||||
val.shift()
|
||||
return val.join(' ')
|
||||
|
@ -140,7 +139,7 @@ function removeAnswerLetters(value) {
|
|||
}
|
||||
}
|
||||
|
||||
function simplifyQA(value, mods) {
|
||||
function simplifyQA(value: string, mods: Array<Function>) {
|
||||
if (!value) {
|
||||
return
|
||||
}
|
||||
|
@ -150,7 +149,7 @@ function simplifyQA(value, mods) {
|
|||
}, value)
|
||||
}
|
||||
|
||||
function simplifyAnswer(value) {
|
||||
function simplifyAnswer(value: string) {
|
||||
if (!value) {
|
||||
return value
|
||||
}
|
||||
|
@ -162,7 +161,7 @@ function simplifyAnswer(value) {
|
|||
])
|
||||
}
|
||||
|
||||
function simplifyQuestion(question) {
|
||||
function simplifyQuestion(question: Question) {
|
||||
if (!question) {
|
||||
return
|
||||
}
|
||||
|
@ -195,7 +194,7 @@ function simplifyQuestion(question) {
|
|||
// Question
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
|
||||
function createQuestion(question, answer, data) {
|
||||
function createQuestion(question: string, answer: string, data: QuestionData) {
|
||||
return {
|
||||
Q: simplifyQuestion(question),
|
||||
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(' '))
|
||||
}
|
||||
|
||||
function compareData(q1, q2) {
|
||||
function compareData(q1: Question, q2: Question) {
|
||||
try {
|
||||
if (q1.data.type === q2.data.type) {
|
||||
let dataType = q1.data.type
|
||||
const dataType = q1.data.type
|
||||
if (dataType === 'simple') {
|
||||
return -1
|
||||
} else if (dataType === 'image') {
|
||||
|
@ -234,15 +233,21 @@ function compareData(q1, q2) {
|
|||
return 0
|
||||
}
|
||||
|
||||
function compareQuestion(q1, q2) {
|
||||
function compareQuestion(q1: Question, q2: Question) {
|
||||
return compareString(q1.Q, q2.Q)
|
||||
}
|
||||
|
||||
function compareAnswer(q1, q2) {
|
||||
function compareAnswer(q1: Question, q2: Question) {
|
||||
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(q1)
|
||||
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
|
||||
|
||||
if (data.type !== 'simple') {
|
||||
|
@ -300,12 +305,17 @@ function questionToString(question) {
|
|||
// ---------------------------------------------------------------------------------------------------------
|
||||
// Subject
|
||||
// ---------------------------------------------------------------------------------------------------------
|
||||
function searchQuestion(subj, question, questionData, subjName) {
|
||||
function searchQuestion(
|
||||
subj: Subject,
|
||||
question: Question,
|
||||
questionData: QuestionData,
|
||||
subjName: string
|
||||
) {
|
||||
assert(question)
|
||||
|
||||
var result = []
|
||||
const result = []
|
||||
subj.Questions.forEach((currentQuestion) => {
|
||||
let percent = compareQuestionObj(
|
||||
const percent = compareQuestionObj(
|
||||
currentQuestion,
|
||||
subjName,
|
||||
question,
|
||||
|
@ -335,10 +345,10 @@ function searchQuestion(subj, question, questionData, subjName) {
|
|||
return result
|
||||
}
|
||||
|
||||
function subjectToString(subj) {
|
||||
function subjectToString(subj: Subject) {
|
||||
const { Questions, Name } = subj
|
||||
|
||||
var result = []
|
||||
const result = []
|
||||
Questions.forEach((question) => {
|
||||
result.push(questionToString(question))
|
||||
})
|
||||
|
@ -349,7 +359,7 @@ function subjectToString(subj) {
|
|||
// ---------------------------------------------------------------------------------------------------------
|
||||
// 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(question, 'qdb add', 3)
|
||||
assert(data)
|
||||
|
@ -357,7 +367,7 @@ function addQuestion(data, subj, question) {
|
|||
assert(question)
|
||||
assert(typeof question === 'object')
|
||||
|
||||
var i = 0
|
||||
let i = 0
|
||||
while (
|
||||
i < data.length &&
|
||||
!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) => {
|
||||
assert(data)
|
||||
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(subj)
|
||||
|
||||
var i = 0
|
||||
let i = 0
|
||||
while (i < length && subj.Name !== data[i].Name) {
|
||||
i++
|
||||
}
|
||||
|
@ -461,8 +476,8 @@ function addSubject(data, subj) {
|
|||
}
|
||||
}
|
||||
|
||||
function dataToString(data) {
|
||||
var result = []
|
||||
function dataToString(data: Array<Subject>) {
|
||||
const result = []
|
||||
data.forEach((subj) => {
|
||||
result.push(subjectToString(subj))
|
||||
})
|
||||
|
|
|
@ -23,21 +23,20 @@ module.exports = {
|
|||
Load: Load,
|
||||
}
|
||||
|
||||
const utils = require('../utils/utils.js')
|
||||
const logger = require('../utils/logger.js')
|
||||
import utils from '../utils/utils.js'
|
||||
import logger from '../utils/logger.js'
|
||||
const idStatFile = 'stats/idstats'
|
||||
const idVStatFile = 'stats/idvstats'
|
||||
|
||||
const writeInterval = 1
|
||||
|
||||
let data = {}
|
||||
let vData = {}
|
||||
let idStatsData = {}
|
||||
let idvStatsData = {}
|
||||
let writes = 0
|
||||
|
||||
function Load() {
|
||||
try {
|
||||
var prevData = utils.ReadFile(idStatFile)
|
||||
data = JSON.parse(prevData)
|
||||
idStatsData = utils.ReadJSON(idStatFile)
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
'Error at loading id logs! (@ first run its normal)',
|
||||
|
@ -47,8 +46,8 @@ function Load() {
|
|||
}
|
||||
|
||||
try {
|
||||
var prevVData = utils.ReadFile(idVStatFile)
|
||||
vData = JSON.parse(prevVData)
|
||||
const prevVData = utils.ReadFile(idVStatFile)
|
||||
idvStatsData = JSON.parse(prevVData)
|
||||
} catch (err) {
|
||||
logger.Log(
|
||||
'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)
|
||||
AddVisitStat(id, subj, newQuestions, allQuestions)
|
||||
Save()
|
||||
}
|
||||
|
||||
function AddSubjToList(list, subj) {
|
||||
function AddSubjToList(list: Array<string>, subj: string) {
|
||||
if (!list[subj]) {
|
||||
list[subj] = 0
|
||||
}
|
||||
list[subj]++
|
||||
}
|
||||
|
||||
function Inc(value, subj, newQuestions, allQuestions) {
|
||||
if (data[value] === undefined) {
|
||||
data[value] = {
|
||||
function Inc(
|
||||
value: string,
|
||||
subj: string,
|
||||
newQuestions: number,
|
||||
allQuestions: number
|
||||
) {
|
||||
if (idStatsData[value] === undefined) {
|
||||
idStatsData[value] = {
|
||||
count: 0,
|
||||
newQuestions: 0,
|
||||
allQuestions: 0,
|
||||
subjs: {},
|
||||
}
|
||||
}
|
||||
data[value].count++
|
||||
data[value].newQuestions += newQuestions
|
||||
data[value].allQuestions += allQuestions
|
||||
AddSubjToList(data[value].subjs, subj)
|
||||
idStatsData[value].count++
|
||||
idStatsData[value].newQuestions += newQuestions
|
||||
idStatsData[value].allQuestions += allQuestions
|
||||
AddSubjToList(idStatsData[value].subjs, subj)
|
||||
}
|
||||
|
||||
function AddVisitStat(name, subj, newQuestions, allQuestions) {
|
||||
var date = new Date()
|
||||
function AddVisitStat(
|
||||
name: string,
|
||||
subj: string,
|
||||
newQuestions: number,
|
||||
allQuestions: number
|
||||
) {
|
||||
const date = new Date()
|
||||
const now =
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
('0' + (date.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + date.getDate()).slice(-2)
|
||||
if (vData[now] === undefined) {
|
||||
vData[now] = {}
|
||||
if (idvStatsData[now] === undefined) {
|
||||
idvStatsData[now] = {}
|
||||
}
|
||||
if (vData[now][name] === undefined) {
|
||||
vData[now][name] = {
|
||||
if (idvStatsData[now][name] === undefined) {
|
||||
idvStatsData[now][name] = {
|
||||
count: 0,
|
||||
newQuestions: 0,
|
||||
allQuestions: 0,
|
||||
subjs: {},
|
||||
}
|
||||
}
|
||||
vData[now][name].count++
|
||||
vData[now][name].newQuestions += newQuestions
|
||||
vData[now][name].allQuestions += allQuestions
|
||||
AddSubjToList(vData[now][name].subjs, subj)
|
||||
idvStatsData[now][name].count++
|
||||
idvStatsData[now][name].newQuestions += newQuestions
|
||||
idvStatsData[now][name].allQuestions += allQuestions
|
||||
AddSubjToList(idvStatsData[now][name].subjs, subj)
|
||||
}
|
||||
|
||||
function Save() {
|
||||
writes++
|
||||
if (writes === writeInterval) {
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(data), idStatFile)
|
||||
utils.WriteFile(JSON.stringify(idStatsData), idStatFile)
|
||||
// Log("Stats wrote.");
|
||||
} catch (err) {
|
||||
logger.Log('Error at writing logs!', logger.GetColor('redbg'))
|
||||
console.error(err)
|
||||
}
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(vData), idVStatFile)
|
||||
utils.WriteFile(JSON.stringify(idvStatsData), idVStatFile)
|
||||
// Log("Stats wrote.");
|
||||
} catch (err) {
|
||||
logger.Log('Error at writing visit logs!', logger.GetColor('redbg'))
|
||||
|
|
|
@ -37,7 +37,7 @@ module.exports = {
|
|||
|
||||
const DELIM = C('green') + '|' + C()
|
||||
|
||||
const utils = require('../utils/utils.js')
|
||||
import utils from '../utils/utils'
|
||||
const vlogDir = './stats/vlogs/'
|
||||
const logDir = './stats/logs/'
|
||||
const statFile = 'stats/stats'
|
||||
|
@ -47,15 +47,20 @@ const uvStatsFile = 'stats/uvstats'
|
|||
const nologFile = './nolog'
|
||||
|
||||
const colors = ['green', 'red', 'yellow', 'blue', 'magenta', 'cyan']
|
||||
var logFileName = utils.GetDateString(true)
|
||||
let logFileName = utils.GetDateString(true)
|
||||
|
||||
const writeInterval = 10
|
||||
const debugLevel = parseInt(process.env.NS_LOGLEVEL) || 0
|
||||
|
||||
let data = {} // visit data
|
||||
let vData = {} // visit data, but daily
|
||||
let uData = {} // visit data, but per user
|
||||
let uvData = {} // visit data, but per user and daily
|
||||
// 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 = []
|
||||
|
@ -75,25 +80,25 @@ function DebugLog(msg, name, lvl) {
|
|||
if (msg === 'hr') {
|
||||
msg = hr
|
||||
}
|
||||
let s = msg
|
||||
let header = `${C('red')}#DEBUG${lvl}#${C(
|
||||
let res = msg
|
||||
const header = `${C('red')}#DEBUG${lvl}#${C(
|
||||
'yellow'
|
||||
)}${name.toUpperCase()}${C('red')}#${C()}${DELIM}${C()}`
|
||||
if (typeof msg !== 'object') {
|
||||
s = header + msg
|
||||
res = header + msg
|
||||
} else {
|
||||
Log(header + 'OBJECT:', 'yellow')
|
||||
s = msg
|
||||
res = msg
|
||||
}
|
||||
Log(s, 'yellow')
|
||||
Log(res, 'yellow')
|
||||
}
|
||||
}
|
||||
|
||||
function Log(s, c) {
|
||||
let log = s
|
||||
function Log(msg, color) {
|
||||
let log = msg
|
||||
if (typeof s !== 'object') {
|
||||
let dl = DELIM + C(c)
|
||||
log = C(c) + GetDateString() + dl + s + C()
|
||||
const delimiter = DELIM + C(color)
|
||||
log = C(color) + GetDateString() + delimiter + msg + C()
|
||||
}
|
||||
|
||||
console.log(log)
|
||||
|
@ -102,10 +107,10 @@ function Log(s, c) {
|
|||
|
||||
function LogReq(req, toFile, sc) {
|
||||
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) => {
|
||||
return ip.includes(x)
|
||||
const nolog = noLogips.some((noLogip) => {
|
||||
return ip.includes(noLogip)
|
||||
})
|
||||
if (nolog) {
|
||||
return
|
||||
|
@ -148,7 +153,7 @@ function LogReq(req, toFile, sc) {
|
|||
if (!toFile) {
|
||||
Log(logEntry)
|
||||
} else {
|
||||
let defLogs = GetDateString() + dl + logEntry
|
||||
const defLogs = GetDateString() + dl + logEntry
|
||||
|
||||
utils.AppendToFile(defLogs, vlogDir + logFileName)
|
||||
}
|
||||
|
@ -163,8 +168,8 @@ function parseNoLogFile(newData) {
|
|||
if (noLogips[noLogips.length - 1] === '') {
|
||||
noLogips.pop()
|
||||
}
|
||||
noLogips = noLogips.filter((x) => {
|
||||
return x !== ''
|
||||
noLogips = noLogips.filter((noLogip) => {
|
||||
return noLogip !== ''
|
||||
})
|
||||
Log('\tNo Log IP-s changed: ' + noLogips.join(', '))
|
||||
}
|
||||
|
@ -180,43 +185,41 @@ function setNoLogReadInterval() {
|
|||
function Load() {
|
||||
Log('Loading logger...')
|
||||
try {
|
||||
uData = JSON.parse(utils.ReadFile(uStatsFile))
|
||||
} catch (e) {
|
||||
uvData = JSON.parse(utils.ReadFile(uStatsFile))
|
||||
} catch (err) {
|
||||
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
|
||||
console.log(e)
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
try {
|
||||
uvData = JSON.parse(utils.ReadFile(uvStatsFile))
|
||||
} catch (e) {
|
||||
udvData = JSON.parse(utils.ReadFile(uvStatsFile))
|
||||
} catch (err) {
|
||||
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
|
||||
console.log(e)
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
try {
|
||||
var prevData = utils.ReadFile(statFile)
|
||||
data = JSON.parse(prevData)
|
||||
} catch (e) {
|
||||
vData = utils.ReadJSON(statFile)
|
||||
} catch (err) {
|
||||
Log('Error at loading logs! (@ first run its normal)', GetColor('redbg'))
|
||||
console.log(e)
|
||||
console.error(err)
|
||||
}
|
||||
|
||||
try {
|
||||
var prevVData = utils.ReadFile(vStatFile)
|
||||
vData = JSON.parse(prevVData)
|
||||
} catch (e) {
|
||||
dvData = utils.ReadJSON(vStatFile)
|
||||
} catch (err) {
|
||||
Log(
|
||||
'Error at loading visit logs! (@ first run its normal)',
|
||||
GetColor('redbg')
|
||||
)
|
||||
console.log(e)
|
||||
console.error(err)
|
||||
}
|
||||
setNoLogReadInterval()
|
||||
}
|
||||
|
||||
function LogStat(url, ip, hostname, userId) {
|
||||
let nolog = noLogips.some((x) => {
|
||||
return x.includes(ip)
|
||||
const nolog = noLogips.some((noLogips) => {
|
||||
return ip.includes(noLogips)
|
||||
})
|
||||
if (nolog) {
|
||||
return
|
||||
|
@ -232,35 +235,35 @@ function LogStat(url, ip, hostname, userId) {
|
|||
|
||||
function IncUserStat(userId) {
|
||||
try {
|
||||
if (uData[userId] === undefined) {
|
||||
uData[userId] = 0
|
||||
if (uvData[userId] === undefined) {
|
||||
uvData[userId] = 0
|
||||
}
|
||||
uData[userId]++
|
||||
} catch (e) {
|
||||
uvData[userId]++
|
||||
} catch (err) {
|
||||
Log('Error at making user ID stats!', GetColor('redbg'))
|
||||
console.error(e)
|
||||
console.error(err)
|
||||
}
|
||||
}
|
||||
|
||||
function AddUserIdStat(userId) {
|
||||
try {
|
||||
var m = new Date()
|
||||
const date = new Date()
|
||||
const now =
|
||||
m.getFullYear() +
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
('0' + (m.getMonth() + 1)).slice(-2) +
|
||||
('0' + (date.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + m.getDate()).slice(-2)
|
||||
if (uvData[now] === undefined) {
|
||||
uvData[now] = {}
|
||||
('0' + date.getDate()).slice(-2)
|
||||
if (udvData[now] === undefined) {
|
||||
udvData[now] = {}
|
||||
}
|
||||
if (uvData[now][userId] === undefined) {
|
||||
uvData[now][userId] = 0
|
||||
if (udvData[now][userId] === undefined) {
|
||||
udvData[now][userId] = 0
|
||||
}
|
||||
uvData[now][userId]++
|
||||
} catch (e) {
|
||||
udvData[now][userId]++
|
||||
} catch (err) {
|
||||
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('/?')) {
|
||||
value = '/'
|
||||
}
|
||||
if (data[value] === undefined) {
|
||||
data[value] = 0
|
||||
if (vData[value] === undefined) {
|
||||
vData[value] = 0
|
||||
}
|
||||
data[value]++
|
||||
vData[value]++
|
||||
}
|
||||
|
||||
function AddVisitStat(name) {
|
||||
var m = new Date()
|
||||
const date = new Date()
|
||||
const now =
|
||||
m.getFullYear() +
|
||||
date.getFullYear() +
|
||||
'-' +
|
||||
('0' + (m.getMonth() + 1)).slice(-2) +
|
||||
('0' + (date.getMonth() + 1)).slice(-2) +
|
||||
'-' +
|
||||
('0' + m.getDate()).slice(-2)
|
||||
if (vData[now] === undefined) {
|
||||
vData[now] = {}
|
||||
('0' + date.getDate()).slice(-2)
|
||||
if (dvData[now] === undefined) {
|
||||
dvData[now] = {}
|
||||
}
|
||||
if (vData[now][name] === undefined) {
|
||||
vData[now][name] = 0
|
||||
if (dvData[now][name] === undefined) {
|
||||
dvData[now][name] = 0
|
||||
}
|
||||
vData[now][name]++
|
||||
dvData[now][name]++
|
||||
}
|
||||
|
||||
function Save() {
|
||||
writes++
|
||||
if (writes === writeInterval) {
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(uData), uStatsFile)
|
||||
} catch (e) {
|
||||
utils.WriteFile(JSON.stringify(uvData), uStatsFile)
|
||||
} catch (err) {
|
||||
Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
|
||||
console.error(e)
|
||||
console.error(err)
|
||||
}
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(uvData), uvStatsFile)
|
||||
} catch (e) {
|
||||
utils.WriteFile(JSON.stringify(udvData), uvStatsFile)
|
||||
} catch (err) {
|
||||
Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
|
||||
console.error(e)
|
||||
console.error(err)
|
||||
}
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(data), statFile)
|
||||
utils.WriteFile(JSON.stringify(vData), statFile)
|
||||
// Log("Stats wrote.");
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
Log('Error at writing logs! (more in stderr)', GetColor('redbg'))
|
||||
console.error(e)
|
||||
console.error(err)
|
||||
}
|
||||
try {
|
||||
utils.WriteFile(JSON.stringify(vData), vStatFile)
|
||||
utils.WriteFile(JSON.stringify(dvData), vStatFile)
|
||||
// Log("Stats wrote.");
|
||||
} catch (e) {
|
||||
} catch (err) {
|
||||
Log('Error at writing visit logs! (more in stderr)', GetColor('redbg'))
|
||||
console.error(e)
|
||||
console.error(err)
|
||||
}
|
||||
writes = 0
|
||||
}
|
||||
}
|
||||
|
||||
function logHashed(x) {
|
||||
return GetRandomColor(x.toString()) + x + C()
|
||||
function logHashed(msg) {
|
||||
return GetRandomColor(msg.toString()) + msg + C()
|
||||
}
|
||||
|
||||
function GetRandomColor(ip) {
|
||||
if (!ip) {
|
||||
function GetRandomColor(msg) {
|
||||
if (!msg) {
|
||||
return 'red'
|
||||
}
|
||||
|
||||
let res = ip.split('').reduce((res, x) => {
|
||||
return res + x.charCodeAt(0)
|
||||
const res = msg.split('').reduce((res, character) => {
|
||||
return res + character.charCodeAt(0)
|
||||
}, 0)
|
||||
return C(colors[res % colors.length])
|
||||
}
|
||||
|
||||
function GetColor(c) {
|
||||
return c
|
||||
function GetColor(color) {
|
||||
return color
|
||||
}
|
||||
|
||||
function C(c) {
|
||||
if (c !== undefined) {
|
||||
c = c.toLowerCase()
|
||||
function C(color?: string) {
|
||||
if (color !== undefined) {
|
||||
color = color.toLowerCase()
|
||||
}
|
||||
|
||||
if (c === 'redbg') {
|
||||
if (color === 'redbg') {
|
||||
return '\x1b[41m'
|
||||
}
|
||||
if (c === 'bluebg') {
|
||||
if (color === 'bluebg') {
|
||||
return '\x1b[44m'
|
||||
}
|
||||
if (c === 'green') {
|
||||
if (color === 'green') {
|
||||
return '\x1b[32m'
|
||||
}
|
||||
if (c === 'red') {
|
||||
if (color === 'red') {
|
||||
return '\x1b[31m'
|
||||
}
|
||||
if (c === 'yellow') {
|
||||
if (color === 'yellow') {
|
||||
return '\x1b[33m'
|
||||
}
|
||||
if (c === 'blue') {
|
||||
if (color === 'blue') {
|
||||
return '\x1b[34m'
|
||||
}
|
||||
if (c === 'magenta') {
|
||||
if (color === 'magenta') {
|
||||
return '\x1b[35m'
|
||||
}
|
||||
if (c === 'cyan') {
|
||||
if (color === 'coloryan') {
|
||||
return '\x1b[36m'
|
||||
}
|
||||
return '\x1b[0m'
|
||||
|
|
|
@ -12,8 +12,8 @@ module.exports = {
|
|||
GetDateString: GetDateString,
|
||||
}
|
||||
|
||||
const fs = require('fs')
|
||||
const logger = require('../utils/logger.js')
|
||||
import fs from 'fs'
|
||||
import logger from '../utils/logger.js'
|
||||
|
||||
function GetDateString(noTime) {
|
||||
const date = new Date()
|
||||
|
@ -96,9 +96,9 @@ function CreatePath(path, onlyPath) {
|
|||
return
|
||||
}
|
||||
|
||||
var spath = path.split('/')
|
||||
var currDir = spath[0]
|
||||
for (var i = 1; i < spath.length; i++) {
|
||||
const spath = path.split('/')
|
||||
let currDir = spath[0]
|
||||
for (let i = 1; i < spath.length; i++) {
|
||||
if (currDir !== '' && !fs.existsSync(currDir)) {
|
||||
try {
|
||||
fs.mkdirSync(currDir)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue