2018-04-13 16:20:57 +00:00
|
|
|
const config = require('./config')
|
|
|
|
const api = require('./routes/api')
|
|
|
|
const album = require('./routes/album')
|
|
|
|
const nojs = require('./routes/nojs')
|
2018-01-23 20:06:30 +00:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
2018-09-04 15:48:24 +00:00
|
|
|
const clamd = require('clamdjs')
|
2018-01-23 20:06:30 +00:00
|
|
|
const db = require('knex')(config.database)
|
|
|
|
const fs = require('fs')
|
2018-04-18 21:00:36 +00:00
|
|
|
const helmet = require('helmet')
|
|
|
|
const nunjucks = require('nunjucks')
|
|
|
|
const RateLimit = require('express-rate-limit')
|
2018-12-03 09:18:52 +00:00
|
|
|
const readline = require('readline')
|
2018-01-23 20:06:30 +00:00
|
|
|
const safe = express()
|
|
|
|
|
2018-09-20 11:41:17 +00:00
|
|
|
// It appears to be best to catch these before doing anything else
|
|
|
|
process.on('uncaughtException', error => {
|
|
|
|
console.error('Uncaught Exception:', error)
|
|
|
|
})
|
|
|
|
process.on('unhandledRejection', error => {
|
|
|
|
console.error('Unhandled Rejection (Promise):', error)
|
|
|
|
})
|
|
|
|
|
2018-01-23 20:06:30 +00:00
|
|
|
require('./database/db.js')(db)
|
|
|
|
|
|
|
|
fs.existsSync('./pages/custom') || fs.mkdirSync('./pages/custom')
|
2018-07-14 03:42:18 +00:00
|
|
|
fs.existsSync(`./${config.logsFolder}`) || fs.mkdirSync(`./${config.logsFolder}`)
|
|
|
|
fs.existsSync(`./${config.uploads.folder}`) || fs.mkdirSync(`./${config.uploads.folder}`)
|
|
|
|
fs.existsSync(`./${config.uploads.folder}/chunks`) || fs.mkdirSync(`./${config.uploads.folder}/chunks`)
|
|
|
|
fs.existsSync(`./${config.uploads.folder}/thumbs`) || fs.mkdirSync(`./${config.uploads.folder}/thumbs`)
|
|
|
|
fs.existsSync(`./${config.uploads.folder}/zips`) || fs.mkdirSync(`./${config.uploads.folder}/zips`)
|
2018-01-23 20:06:30 +00:00
|
|
|
|
|
|
|
safe.use(helmet())
|
|
|
|
safe.set('trust proxy', 1)
|
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
// https://mozilla.github.io/nunjucks/api.html#configure
|
2018-04-18 21:00:36 +00:00
|
|
|
nunjucks.configure('views', {
|
|
|
|
autoescape: true,
|
2018-10-09 19:52:41 +00:00
|
|
|
express: safe,
|
|
|
|
// watch: true, // will this be fine in production?
|
|
|
|
noCache: process.env.DEV === '1'
|
2018-04-18 21:00:36 +00:00
|
|
|
})
|
|
|
|
safe.set('view engine', 'njk')
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.enable('view cache')
|
|
|
|
|
2018-04-05 10:52:57 +00:00
|
|
|
const limiter = new RateLimit({ windowMs: 5000, max: 2 })
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/api/login/', limiter)
|
|
|
|
safe.use('/api/register/', limiter)
|
|
|
|
|
|
|
|
safe.use(bodyParser.urlencoded({ extended: true }))
|
|
|
|
safe.use(bodyParser.json())
|
2017-01-19 05:37:35 +00:00
|
|
|
|
2018-04-25 07:30:38 +00:00
|
|
|
const setHeaders = res => {
|
|
|
|
// Apply Cache-Control to all static files
|
|
|
|
res.set('Access-Control-Allow-Origin', '*')
|
|
|
|
res.set('Cache-Control', 'public, max-age=2592000, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800') // max-age: 30 days
|
2018-01-24 16:04:21 +00:00
|
|
|
}
|
|
|
|
|
2018-12-18 17:41:42 +00:00
|
|
|
if (config.serveFilesWithNode)
|
2018-01-24 16:04:21 +00:00
|
|
|
safe.use('/', express.static(config.uploads.folder, { setHeaders }))
|
2017-09-20 06:03:31 +00:00
|
|
|
|
2018-01-24 16:04:21 +00:00
|
|
|
safe.use('/', express.static('./public', { setHeaders }))
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/', album)
|
2018-04-12 14:37:42 +00:00
|
|
|
safe.use('/', nojs)
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/api', api)
|
2017-01-14 06:01:23 +00:00
|
|
|
|
2018-12-18 17:41:42 +00:00
|
|
|
for (const page of config.pages)
|
2018-01-23 20:06:30 +00:00
|
|
|
if (fs.existsSync(`./pages/custom/${page}.html`)) {
|
2018-05-12 16:34:19 +00:00
|
|
|
safe.get(`/${page}`, (req, res, next) => res.sendFile(`${page}.html`, {
|
|
|
|
root: './pages/custom/'
|
|
|
|
}))
|
2018-04-20 21:39:06 +00:00
|
|
|
} else if (page === 'home') {
|
2018-05-12 16:34:19 +00:00
|
|
|
safe.get('/', (req, res, next) => res.render('home', {
|
2018-09-29 22:38:13 +00:00
|
|
|
maxSize: config.uploads.maxSize,
|
2018-09-20 11:41:17 +00:00
|
|
|
urlMaxSize: config.uploads.urlMaxSize,
|
2018-09-29 22:38:13 +00:00
|
|
|
gitHash: safe.get('git-hash'),
|
|
|
|
urlDuckDuckGoProxy: config.uploads.urlDuckDuckGoProxy
|
2018-05-12 16:34:19 +00:00
|
|
|
}))
|
|
|
|
} else if (page === 'faq') {
|
|
|
|
const fileLength = config.uploads.fileLength
|
|
|
|
safe.get('/faq', (req, res, next) => res.render('faq', {
|
|
|
|
filterBlacklist: config.filterBlacklist,
|
|
|
|
extensionsFilter: config.extensionsFilter,
|
|
|
|
fileLength,
|
|
|
|
tooShort: (fileLength.max - fileLength.default) > (fileLength.default - fileLength.min),
|
|
|
|
noJsMaxSize: parseInt(config.cloudflare.noJsMaxSize) < parseInt(config.uploads.maxSize),
|
|
|
|
chunkSize: config.uploads.chunkSize
|
|
|
|
}))
|
2018-01-23 20:06:30 +00:00
|
|
|
} else {
|
2018-04-20 21:39:06 +00:00
|
|
|
safe.get(`/${page}`, (req, res, next) => res.render(page))
|
2018-01-23 20:06:30 +00:00
|
|
|
}
|
2017-02-06 03:06:33 +00:00
|
|
|
|
2018-01-24 18:13:17 +00:00
|
|
|
safe.use((req, res, next) => {
|
2018-09-20 11:41:17 +00:00
|
|
|
res.status(404).sendFile(config.errorPages[404], { root: config.errorPages.rootDir })
|
2018-01-24 18:31:31 +00:00
|
|
|
})
|
2018-03-29 23:22:08 +00:00
|
|
|
safe.use((error, req, res, next) => {
|
|
|
|
console.error(error)
|
2018-09-20 11:41:17 +00:00
|
|
|
res.status(500).sendFile(config.errorPages[500], { root: config.errorPages.rootDir })
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2018-09-01 20:37:26 +00:00
|
|
|
|
2018-09-04 15:48:24 +00:00
|
|
|
const start = async () => {
|
2018-09-20 11:41:17 +00:00
|
|
|
if (config.showGitHash) {
|
|
|
|
const gitHash = await new Promise((resolve, reject) => {
|
|
|
|
require('child_process').exec('git rev-parse HEAD', (error, stdout) => {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (error) return reject(error)
|
2018-09-20 11:41:17 +00:00
|
|
|
resolve(stdout.replace(/\n$/, ''))
|
|
|
|
})
|
|
|
|
}).catch(console.error)
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!gitHash) return
|
2018-09-20 11:41:17 +00:00
|
|
|
console.log(`Git commit: ${gitHash}`)
|
|
|
|
safe.set('git-hash', gitHash)
|
|
|
|
}
|
|
|
|
|
2018-09-04 15:48:24 +00:00
|
|
|
if (config.uploads.scan && config.uploads.scan.enabled) {
|
|
|
|
const created = await new Promise(async (resolve, reject) => {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!config.uploads.scan.ip || !config.uploads.scan.port)
|
2018-09-20 11:41:17 +00:00
|
|
|
return reject(new Error('clamd IP or port is missing'))
|
2018-12-18 17:41:42 +00:00
|
|
|
|
2018-09-04 15:48:24 +00:00
|
|
|
const ping = await clamd.ping(config.uploads.scan.ip, config.uploads.scan.port).catch(reject)
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!ping)
|
2018-09-04 15:48:24 +00:00
|
|
|
return reject(new Error('Could not ping clamd'))
|
2018-12-18 17:41:42 +00:00
|
|
|
|
2018-09-04 15:48:24 +00:00
|
|
|
const version = await clamd.version(config.uploads.scan.ip, config.uploads.scan.port).catch(reject)
|
2018-09-20 11:41:17 +00:00
|
|
|
console.log(`${config.uploads.scan.ip}:${config.uploads.scan.port} ${version}`)
|
2018-09-04 15:48:24 +00:00
|
|
|
const scanner = clamd.createScanner(config.uploads.scan.ip, config.uploads.scan.port)
|
|
|
|
safe.set('clam-scanner', scanner)
|
|
|
|
return resolve(true)
|
|
|
|
}).catch(error => console.error(error.toString()))
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!created) return process.exit(1)
|
2018-09-01 20:37:26 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 13:35:49 +00:00
|
|
|
if (config.uploads.cacheFileIdentifiers) {
|
2018-12-03 09:18:52 +00:00
|
|
|
// Cache tree of uploads directory
|
|
|
|
process.stdout.write('Caching identifiers in uploads directory ...')
|
|
|
|
const setSize = await new Promise((resolve, reject) => {
|
|
|
|
const uploadsDir = `./${config.uploads.folder}`
|
|
|
|
fs.readdir(uploadsDir, (error, names) => {
|
2018-12-18 17:41:42 +00:00
|
|
|
if (error) return reject(error)
|
2018-12-03 09:18:52 +00:00
|
|
|
const set = new Set()
|
|
|
|
names.forEach(name => set.add(name.split('.')[0]))
|
|
|
|
safe.set('uploads-set', set)
|
|
|
|
resolve(set.size)
|
|
|
|
})
|
|
|
|
}).catch(error => console.error(error.toString()))
|
2018-12-18 17:41:42 +00:00
|
|
|
if (!setSize) return process.exit(1)
|
2018-12-03 09:18:52 +00:00
|
|
|
process.stdout.write(` ${setSize} OK!\n`)
|
|
|
|
}
|
|
|
|
|
2018-10-09 19:52:41 +00:00
|
|
|
safe.listen(config.port, () => {
|
|
|
|
console.log(`lolisafe started on port ${config.port}`)
|
2018-12-18 17:41:42 +00:00
|
|
|
// DEV=1 yarn start
|
2018-10-09 19:52:41 +00:00
|
|
|
if (process.env.DEV === '1') {
|
2018-12-18 17:41:42 +00:00
|
|
|
// Add readline interface to allow evaluating arbitrary JavaScript from console
|
|
|
|
readline.createInterface({
|
|
|
|
input: process.stdin,
|
|
|
|
output: process.stdout,
|
|
|
|
prompt: ''
|
|
|
|
}).on('line', line => {
|
|
|
|
try {
|
|
|
|
if (line === '.exit') process.exit(0)
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
process.stdout.write(`${require('util').inspect(eval(line), { depth: 0 })}\n`)
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error.toString())
|
|
|
|
}
|
|
|
|
}).on('SIGINT', () => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
console.warn('development mode enabled (disabled nunjucks caching & enabled readline interface)')
|
2018-10-09 19:52:41 +00:00
|
|
|
}
|
|
|
|
})
|
2018-09-01 20:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start()
|