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')
|
|
|
|
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-01-23 20:06:30 +00:00
|
|
|
const safe = express()
|
|
|
|
|
|
|
|
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-04-18 21:00:36 +00:00
|
|
|
nunjucks.configure('views', {
|
|
|
|
autoescape: true,
|
|
|
|
express: safe
|
|
|
|
})
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-10-03 22:48:26 +00:00
|
|
|
if (config.serveFilesWithNode) {
|
2018-01-24 16:04:21 +00:00
|
|
|
safe.use('/', express.static(config.uploads.folder, { setHeaders }))
|
2017-10-03 22:48:26 +00:00
|
|
|
}
|
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-04-05 10:52:57 +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', {
|
|
|
|
urlMaxSize: config.uploads.urlMaxSize
|
|
|
|
}))
|
|
|
|
} 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-04-28 17:26:39 +00:00
|
|
|
res.status(404).sendFile('404.html', { root: './pages/error/' })
|
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-04-28 17:26:39 +00:00
|
|
|
res.status(500).sendFile('500.html', { root: './pages/error/' })
|
2018-01-24 18:13:17 +00:00
|
|
|
})
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
process.on('uncaughtException', error => {
|
2018-05-10 17:25:52 +00:00
|
|
|
console.error('Uncaught Exception:')
|
|
|
|
console.error(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2017-01-13 07:34:21 +00:00
|
|
|
|
2018-03-29 23:22:08 +00:00
|
|
|
process.on('unhandledRejection', error => {
|
2018-05-10 17:25:52 +00:00
|
|
|
console.error('Unhandled Rejection (Promise):')
|
|
|
|
console.error(error)
|
2018-01-23 20:06:30 +00:00
|
|
|
})
|
2018-09-01 20:37:26 +00:00
|
|
|
|
|
|
|
async function start () {
|
|
|
|
if (config.uploads.scan) {
|
|
|
|
// Placing require() here so the package does not have to exist when the option is not enabled
|
|
|
|
const clam = require('clam-engine')
|
|
|
|
const created = await new Promise(resolve => {
|
|
|
|
process.stdout.write('Creating clam-engine...')
|
|
|
|
clam.createEngine(function (error, engine) {
|
|
|
|
if (error) {
|
|
|
|
process.stdout.write(' ERROR\n')
|
|
|
|
console.error(error)
|
|
|
|
return resolve(false)
|
|
|
|
}
|
|
|
|
safe.set('clam-engine', engine)
|
|
|
|
process.stdout.write(' OK\n')
|
|
|
|
console.log(`ClamAV ${engine.version} (${engine.signatures} sigs)`)
|
|
|
|
resolve(true)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
if (!created) { return }
|
|
|
|
}
|
|
|
|
|
|
|
|
safe.listen(config.port, () => console.log(`lolisafe started on port ${config.port}`))
|
|
|
|
}
|
|
|
|
|
|
|
|
start()
|