2022-03-03 19:04:46 +00:00
|
|
|
const logger = require('./logger')
|
|
|
|
|
|
|
|
// Stray errors and exceptions capturers
|
|
|
|
process.on('uncaughtException', error => {
|
|
|
|
logger.error(error, { prefix: 'Uncaught Exception: ' })
|
|
|
|
})
|
|
|
|
|
|
|
|
process.on('unhandledRejection', error => {
|
|
|
|
logger.error(error, { prefix: 'Unhandled Rejection (Promise): ' })
|
|
|
|
})
|
|
|
|
|
2022-07-11 23:24:04 +00:00
|
|
|
// Libraries
|
2022-07-25 00:35:26 +00:00
|
|
|
const fs = require('fs')
|
2018-04-18 21:00:36 +00:00
|
|
|
const helmet = require('helmet')
|
2022-07-10 12:46:25 +00:00
|
|
|
const HyperExpress = require('hyper-express')
|
2022-04-15 08:36:50 +00:00
|
|
|
const NodeClam = require('clamscan')
|
2022-03-03 19:04:46 +00:00
|
|
|
|
|
|
|
// Check required config files
|
|
|
|
const configFiles = ['config.js', 'views/_globals.njk']
|
2022-06-28 04:48:22 +00:00
|
|
|
for (const _file of configFiles) {
|
2022-03-03 19:04:46 +00:00
|
|
|
try {
|
2022-07-25 00:35:26 +00:00
|
|
|
fs.accessSync(_file, fs.constants.R_OK)
|
2022-03-03 19:04:46 +00:00
|
|
|
} catch (error) {
|
2022-06-28 04:48:22 +00:00
|
|
|
logger.error(`Config file '${_file}' cannot be found or read.`)
|
2022-03-03 19:04:46 +00:00
|
|
|
logger.error('Please copy the provided sample file and modify it according to your needs.')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-11 23:24:04 +00:00
|
|
|
// Config files
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
const config = require('./config')
|
2019-09-19 12:10:37 +00:00
|
|
|
const versions = require('./src/versions')
|
2018-01-23 20:06:30 +00:00
|
|
|
|
2022-03-03 19:04:46 +00:00
|
|
|
// lolisafe
|
2020-05-09 07:54:09 +00:00
|
|
|
logger.log('Starting lolisafe\u2026')
|
2022-07-10 12:46:25 +00:00
|
|
|
const safe = new HyperExpress.Server({
|
|
|
|
trust_proxy: Boolean(config.trustProxy)
|
|
|
|
})
|
2018-09-20 11:41:17 +00:00
|
|
|
|
2022-07-11 23:21:21 +00:00
|
|
|
const errors = require('./controllers/errorsController')
|
2019-09-08 01:56:29 +00:00
|
|
|
const paths = require('./controllers/pathsController')
|
2022-06-22 07:23:54 +00:00
|
|
|
paths.initSync()
|
2019-08-26 17:02:06 +00:00
|
|
|
const utils = require('./controllers/utilsController')
|
|
|
|
|
2022-07-21 18:12:35 +00:00
|
|
|
// Middlewares
|
2022-07-25 00:48:31 +00:00
|
|
|
const ExpressCompat = require('./controllers/middlewares/ExpressCompat')
|
|
|
|
const NunjucksRenderer = require('./controllers/middlewares/NunjucksRenderer')
|
|
|
|
const RateLimiter = require('./controllers/middlewares/RateLimiter')
|
|
|
|
const ServeLiveDirectory = require('./controllers/middlewares/ServeLiveDirectory')
|
2022-07-21 18:12:35 +00:00
|
|
|
|
|
|
|
// Handlers
|
2022-07-25 00:48:31 +00:00
|
|
|
const ServeStatic = require('./controllers/handlers/ServeStatic')
|
2022-07-11 23:24:04 +00:00
|
|
|
|
|
|
|
// Routes
|
2019-08-26 17:02:06 +00:00
|
|
|
const album = require('./routes/album')
|
|
|
|
const api = require('./routes/api')
|
2022-06-28 05:03:49 +00:00
|
|
|
const file = require('./routes/file')
|
2019-08-26 17:02:06 +00:00
|
|
|
const nojs = require('./routes/nojs')
|
2020-11-03 13:56:32 +00:00
|
|
|
const player = require('./routes/player')
|
2019-08-26 17:02:06 +00:00
|
|
|
|
2022-07-21 14:13:46 +00:00
|
|
|
// Express-compat
|
|
|
|
const expressCompatInstance = new ExpressCompat()
|
|
|
|
safe.use(expressCompatInstance.middleware)
|
|
|
|
|
2022-07-12 01:48:09 +00:00
|
|
|
// Rate limiters
|
|
|
|
if (Array.isArray(config.rateLimiters)) {
|
|
|
|
let whitelistedKeys
|
|
|
|
if (Array.isArray(config.rateLimitersWhitelist)) {
|
|
|
|
whitelistedKeys = new Set(config.rateLimitersWhitelist)
|
|
|
|
}
|
|
|
|
for (const rateLimit of config.rateLimiters) {
|
|
|
|
// Init RateLimiter using Request.ip as key
|
|
|
|
const rateLimiterInstance = new RateLimiter('ip', rateLimit.options, whitelistedKeys)
|
|
|
|
for (const route of rateLimit.routes) {
|
|
|
|
safe.use(route, rateLimiterInstance.middleware)
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 11:34:50 +00:00
|
|
|
} else if (config.rateLimits) {
|
2022-07-29 02:14:55 +00:00
|
|
|
logger.error('Config option "rateLimits" is DEPRECATED.')
|
2022-07-14 11:34:50 +00:00
|
|
|
logger.error('Please consult the provided sample file for the new option "rateLimiters".')
|
2022-07-12 01:48:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-03 19:04:16 +00:00
|
|
|
// Helmet security headers
|
2022-04-30 05:01:12 +00:00
|
|
|
if (config.helmet instanceof Object) {
|
|
|
|
// If an empty object, simply do not use Helmet
|
|
|
|
if (Object.keys(config.helmet).length) {
|
|
|
|
safe.use(helmet(config.helmet))
|
|
|
|
}
|
2022-03-03 19:04:16 +00:00
|
|
|
} else {
|
|
|
|
// Fallback to old behavior when the whole helmet option was not configurable from the config file
|
2022-04-30 05:01:12 +00:00
|
|
|
const defaults = {
|
2022-03-03 19:04:46 +00:00
|
|
|
contentSecurityPolicy: false,
|
2022-04-20 11:37:10 +00:00
|
|
|
crossOriginEmbedderPolicy: false,
|
|
|
|
crossOriginOpenerPolicy: false,
|
|
|
|
crossOriginResourcePolicy: false,
|
|
|
|
hsts: false,
|
|
|
|
originAgentCluster: false
|
2022-04-30 05:01:12 +00:00
|
|
|
}
|
2020-02-13 08:03:31 +00:00
|
|
|
|
2022-03-03 19:04:46 +00:00
|
|
|
if (config.hsts instanceof Object && Object.keys(config.hsts).length) {
|
2022-04-30 05:01:12 +00:00
|
|
|
defaults.hsts = config.hsts
|
2022-03-03 19:04:16 +00:00
|
|
|
}
|
2022-04-30 05:01:12 +00:00
|
|
|
|
|
|
|
safe.use(helmet(defaults))
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2020-02-13 08:03:31 +00:00
|
|
|
|
2022-05-02 06:58:04 +00:00
|
|
|
// Access-Control-Allow-Origin
|
|
|
|
if (config.accessControlAllowOrigin) {
|
|
|
|
if (config.accessControlAllowOrigin === true) {
|
|
|
|
config.accessControlAllowOrigin = '*'
|
|
|
|
}
|
|
|
|
safe.use((req, res, next) => {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Access-Control-Allow-Origin', config.accessControlAllowOrigin)
|
2022-05-02 06:58:04 +00:00
|
|
|
if (config.accessControlAllowOrigin !== '*') {
|
|
|
|
res.vary('Origin')
|
|
|
|
}
|
|
|
|
next()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-11 23:24:04 +00:00
|
|
|
// NunjucksRenderer middleware
|
|
|
|
const nunjucksRendererInstance = new NunjucksRenderer('views', {
|
2022-07-25 00:39:35 +00:00
|
|
|
watch: utils.devmode
|
2022-07-11 23:24:04 +00:00
|
|
|
})
|
2022-07-21 16:59:46 +00:00
|
|
|
safe.use(nunjucksRendererInstance.middleware)
|
2022-07-11 23:24:04 +00:00
|
|
|
|
2022-07-21 14:13:46 +00:00
|
|
|
// Array of routes to apply CDN Cache-Control onto,
|
|
|
|
// and additionally call Cloudflare API to have their CDN caches purged when lolisafe starts
|
|
|
|
const cdnRoutes = [...config.pages]
|
2022-07-11 23:27:02 +00:00
|
|
|
|
|
|
|
// Defaults to no-op
|
|
|
|
let setHeadersForStaticAssets = () => {}
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
// Cache control
|
2019-01-05 21:09:47 +00:00
|
|
|
if (config.cacheControl) {
|
2019-01-09 10:11:45 +00:00
|
|
|
const cacheControls = {
|
2020-02-11 10:18:04 +00:00
|
|
|
// max-age: 6 months
|
2020-10-12 15:02:29 +00:00
|
|
|
static: 'public, max-age=15778800, immutable',
|
2020-02-11 10:18:04 +00:00
|
|
|
// s-max-age: 6 months (only cache in CDN)
|
2020-10-12 15:02:29 +00:00
|
|
|
cdn: 's-max-age=15778800, proxy-revalidate',
|
2020-02-11 10:18:04 +00:00
|
|
|
// validate cache's validity before using them (soft cache)
|
|
|
|
validate: 'no-cache',
|
|
|
|
// do not use cache at all
|
2019-01-09 10:11:45 +00:00
|
|
|
disable: 'no-store'
|
|
|
|
}
|
|
|
|
|
2020-02-11 10:18:04 +00:00
|
|
|
// By default soft cache everything
|
2022-07-21 16:59:46 +00:00
|
|
|
safe.use((req, res, next) => {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Cache-Control', cacheControls.validate)
|
2022-07-11 23:27:02 +00:00
|
|
|
return next()
|
2019-01-05 21:09:47 +00:00
|
|
|
})
|
|
|
|
|
2022-06-28 06:38:55 +00:00
|
|
|
switch (config.cacheControl) {
|
|
|
|
case 1:
|
|
|
|
case true:
|
2022-07-21 14:13:46 +00:00
|
|
|
// If using CDN, cache most front-end pages in CDN
|
|
|
|
// Include /api/check since it will only reply with persistent JSON payload
|
|
|
|
// that will not change, unless config file is edited and lolisafe is then restarted
|
|
|
|
cdnRoutes.push('api/check')
|
2022-07-12 07:52:46 +00:00
|
|
|
safe.use((req, res, next) => {
|
|
|
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
|
|
const page = req.path === '/' ? 'home' : req.path.substring(1)
|
2022-07-21 14:13:46 +00:00
|
|
|
if (cdnRoutes.includes(page)) {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Cache-Control', cacheControls.cdn)
|
2022-07-12 07:52:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return next()
|
|
|
|
})
|
2022-06-28 06:38:55 +00:00
|
|
|
break
|
2020-02-11 10:18:04 +00:00
|
|
|
}
|
2019-01-09 10:11:45 +00:00
|
|
|
|
2020-02-11 10:18:04 +00:00
|
|
|
// Function for static assets.
|
|
|
|
// This requires the assets to use version in their query string,
|
|
|
|
// as they will be cached by clients for a very long time.
|
2022-07-11 23:27:02 +00:00
|
|
|
setHeadersForStaticAssets = (req, res) => {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Cache-Control', cacheControls.static)
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
}
|
2017-09-20 06:03:31 +00:00
|
|
|
|
2020-02-11 10:18:04 +00:00
|
|
|
// Consider album ZIPs static as well, since they use version in their query string
|
2022-07-11 23:27:02 +00:00
|
|
|
safe.use('/api/album/zip', (req, res, next) => {
|
2022-07-21 19:02:59 +00:00
|
|
|
const versionString = parseInt(req.query_parameters.v)
|
2020-10-30 18:12:09 +00:00
|
|
|
if (versionString > 0) {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Cache-Control', cacheControls.static)
|
2020-10-30 18:12:09 +00:00
|
|
|
} else {
|
2022-07-21 13:28:10 +00:00
|
|
|
res.header('Cache-Control', cacheControls.disable)
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2022-07-11 23:27:02 +00:00
|
|
|
return next()
|
2020-02-11 10:18:04 +00:00
|
|
|
})
|
|
|
|
}
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
|
2022-07-21 18:09:34 +00:00
|
|
|
// Init LiveDirectory middlewares for static assets
|
|
|
|
// Static assets in /public directory
|
|
|
|
const serveLiveDirectoryPublicInstance = new ServeLiveDirectory({ path: paths.public }, {
|
|
|
|
setHeaders: setHeadersForStaticAssets
|
|
|
|
})
|
2022-07-21 16:59:46 +00:00
|
|
|
safe.use(serveLiveDirectoryPublicInstance.middleware)
|
2022-07-21 18:09:34 +00:00
|
|
|
// Static assets in /dist directory
|
|
|
|
const serveLiveDirectoryDistInstance = new ServeLiveDirectory({ path: paths.dist }, {
|
|
|
|
setHeaders: setHeadersForStaticAssets
|
|
|
|
})
|
2022-07-21 16:59:46 +00:00
|
|
|
safe.use(serveLiveDirectoryDistInstance.middleware)
|
Updates (very important to read)
Client-side CSS & JS files will now be processed with Gulp.
Gulp tasks are configured in gulpfile.js file.
CSS files will be optimized with postcss-preset-env, which will
auto-add vendor prefixes and convert any parts necessary for browsers
compatibility.
Afterwards they will be minified with cssnano.
JS files will be optimized with bublé,
likewise for browsers compatibility.
Afterwards they will be minified with terser.
Unprocessed CSS & JS files will now be located at src directory, while
the processed results will be located at dist directory.
Due to bublé, the JS files should now be compatible up to IE 11
at the minimum.
Previously the safe would not work in IE 11 due to extensive usage of
template literals.
Due to that as well, JS files in src directory will now extensively use
arrow functions for my personal comfort (as they will be converted too).
The server will use the processed files at dist directory by default.
If you want to rebuild the files by your own, you can run "yarn build".
Gulp is a development dependency, so make sure you have installed all
development dependencies (e.i. NOT using "yarn install --production").
---
yarn lint -> gulp lint
yarn build -> gulp default
yarn watch -> gulp watch
yarn develop -> env NODE_ENV=development yarn watch
---
Fixed not being able to demote staff into normal users.
/api/token/verify will no longer respond with 401 HTTP error code,
unless an error occurred (which will be 500 HTTP error code).
Fixed /nojs route not displaying file's original name when a duplicate
is found on the server.
Removed is-breeze CSS class name, in favor of Bulma's is-info.
Removed custom styling from auth page, in favor of global styling.
Removed all usage of style HTML attribute in favor of CSS classes.
Renamed js/s/ to js/misc/.
Use loading spinners on dashboard's sidebar menus.
Disable all other sidebar menus when something is loading.
Changed title HTML attribute of disabled control buttons in
uploads & users list.
Hid checkboxes and WIP controls from users list.
Better error messages handling.
Especially homepage will now support CF's HTTP error codes.
Updated various icons.
Also, added fontello config file at public/libs/fontello/config.json.
This should let you edit them more easily with fontello.
Use Gatsby icon for my blog's link in homepage's footer.
A bunch of other improvements here & there.
2019-09-15 06:20:11 +00:00
|
|
|
|
2022-07-10 12:46:25 +00:00
|
|
|
// Routes
|
2022-07-21 16:59:46 +00:00
|
|
|
safe.use(album)
|
|
|
|
safe.use(file)
|
|
|
|
safe.use(nojs)
|
|
|
|
safe.use(player)
|
2018-01-23 20:06:30 +00:00
|
|
|
safe.use('/api', api)
|
2017-01-14 06:01:23 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
;(async () => {
|
|
|
|
try {
|
2019-10-21 10:49:52 +00:00
|
|
|
// Init database
|
2022-07-10 12:46:25 +00:00
|
|
|
await require('./controllers/utils/initDatabase')(utils.db)
|
2019-10-21 10:49:52 +00:00
|
|
|
|
2022-06-22 07:23:54 +00:00
|
|
|
// Purge any leftover in chunks directory, do not wait
|
|
|
|
paths.purgeChunks()
|
2017-02-06 03:06:33 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
if (!Array.isArray(config.pages) || !config.pages.length) {
|
|
|
|
logger.error('Config file does not have any frontend pages enabled')
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2018-12-20 12:25:41 +00:00
|
|
|
|
2019-09-19 12:10:37 +00:00
|
|
|
// Re-map version strings if cache control is enabled (safe.fiery.me)
|
|
|
|
utils.versionStrings = {}
|
2020-04-29 22:56:28 +00:00
|
|
|
if (config.cacheControl) {
|
2020-10-30 18:12:09 +00:00
|
|
|
for (const type in versions) {
|
2019-09-19 12:10:37 +00:00
|
|
|
utils.versionStrings[type] = `?_=${versions[type]}`
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
|
|
|
if (versions['1']) {
|
2020-04-29 22:56:28 +00:00
|
|
|
utils.clientVersion = versions['1']
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2020-04-29 22:56:28 +00:00
|
|
|
}
|
2019-09-19 12:10:37 +00:00
|
|
|
|
2022-07-21 18:09:17 +00:00
|
|
|
const serveLiveDirectoryCustomPagesInstance = new ServeLiveDirectory({
|
2022-07-10 12:46:25 +00:00
|
|
|
path: paths.customPages,
|
2022-07-21 18:09:17 +00:00
|
|
|
keep: ['.html']
|
2022-07-10 12:46:25 +00:00
|
|
|
})
|
|
|
|
|
2020-05-16 20:35:54 +00:00
|
|
|
// Cookie Policy
|
2020-10-30 18:12:09 +00:00
|
|
|
if (config.cookiePolicy) {
|
2020-05-16 20:35:54 +00:00
|
|
|
config.pages.push('cookiepolicy')
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2020-05-16 20:35:54 +00:00
|
|
|
|
2022-07-12 03:31:59 +00:00
|
|
|
// Front-end pages middleware
|
|
|
|
// HTML files in customPages directory can also override any built-in pages,
|
|
|
|
// if they have matching names with the routes (e.g. home.html can override the homepage)
|
|
|
|
// Aside from that, due to using LiveDirectory,
|
|
|
|
// custom pages can be added/removed on the fly while lolisafe is running
|
|
|
|
safe.use((req, res, next) => {
|
|
|
|
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
|
|
const page = req.path === '/' ? 'home' : req.path.substring(1)
|
2022-07-21 18:09:17 +00:00
|
|
|
const customPage = serveLiveDirectoryCustomPagesInstance.instance.get(`${page}.html`)
|
2022-07-12 03:31:59 +00:00
|
|
|
if (customPage) {
|
2022-07-21 18:09:17 +00:00
|
|
|
return serveLiveDirectoryCustomPagesInstance.handler(req, res, customPage)
|
2022-07-12 03:31:59 +00:00
|
|
|
} else if (config.pages.includes(page)) {
|
|
|
|
// These rendered pages are persistently cached during production
|
|
|
|
return res.render(page, {
|
|
|
|
config, utils, versions: utils.versionStrings
|
2022-07-25 00:39:35 +00:00
|
|
|
}, !utils.devmode)
|
2022-07-12 03:31:59 +00:00
|
|
|
}
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2022-07-12 03:31:59 +00:00
|
|
|
return next()
|
|
|
|
})
|
2019-09-08 01:56:29 +00:00
|
|
|
|
2022-07-11 23:29:21 +00:00
|
|
|
// Init ServerStatic last if serving uploaded files with node
|
|
|
|
if (config.serveFilesWithNode) {
|
|
|
|
const serveStaticInstance = new ServeStatic(paths.uploads, {
|
|
|
|
contentDispositionOptions: config.contentDispositionOptions,
|
2022-07-21 17:01:25 +00:00
|
|
|
ignorePatterns: [
|
|
|
|
'/chunks/'
|
|
|
|
],
|
2022-07-11 23:29:21 +00:00
|
|
|
overrideContentTypes: config.overrideContentTypes,
|
|
|
|
setContentDisposition: config.setContentDisposition
|
|
|
|
})
|
2022-07-21 18:12:35 +00:00
|
|
|
safe.get('/*', serveStaticInstance.handler)
|
|
|
|
safe.head('/*', serveStaticInstance.handler)
|
2022-07-11 23:29:21 +00:00
|
|
|
utils.contentDispositionStore = serveStaticInstance.contentDispositionStore
|
|
|
|
}
|
|
|
|
|
2022-07-11 23:21:21 +00:00
|
|
|
// Web server error handlers (must always be set after all routes/middlewares)
|
|
|
|
safe.set_not_found_handler(errors.handleNotFound)
|
|
|
|
safe.set_error_handler(errors.handleError)
|
2018-09-01 20:37:26 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
// Git hash
|
|
|
|
if (config.showGitHash) {
|
|
|
|
utils.gitHash = await new Promise((resolve, reject) => {
|
|
|
|
require('child_process').exec('git rev-parse HEAD', (error, stdout) => {
|
|
|
|
if (error) return reject(error)
|
|
|
|
resolve(stdout.replace(/\n$/, ''))
|
|
|
|
})
|
2018-09-20 11:41:17 +00:00
|
|
|
})
|
2019-09-08 01:56:29 +00:00
|
|
|
logger.log(`Git commit: ${utils.gitHash}`)
|
|
|
|
}
|
2018-09-20 11:41:17 +00:00
|
|
|
|
2020-10-31 23:35:56 +00:00
|
|
|
// ClamAV scanner
|
2019-09-08 01:56:29 +00:00
|
|
|
if (config.uploads.scan && config.uploads.scan.enabled) {
|
2020-10-31 23:35:56 +00:00
|
|
|
if (!config.uploads.scan.clamOptions) {
|
2021-01-08 02:20:00 +00:00
|
|
|
logger.error('Missing object config.uploads.scan.clamOptions (check config.sample.js)')
|
|
|
|
process.exit(1)
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2022-04-22 21:44:01 +00:00
|
|
|
utils.scan.instance = await new NodeClam().init(config.uploads.scan.clamOptions)
|
|
|
|
utils.scan.version = await utils.scan.instance.getVersion().then(s => s.trim())
|
|
|
|
logger.log(`Connection established with ${utils.scan.version}`)
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
2019-08-20 02:16:34 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
// Binds Express to port
|
2022-07-10 12:46:25 +00:00
|
|
|
await safe.listen(utils.conf.port)
|
2022-06-22 06:27:38 +00:00
|
|
|
logger.log(`lolisafe started on port ${utils.conf.port}`)
|
2019-01-09 10:11:45 +00:00
|
|
|
|
2019-09-08 01:56:29 +00:00
|
|
|
// Cache control (safe.fiery.me)
|
2020-02-11 10:18:04 +00:00
|
|
|
// Purge Cloudflare cache
|
2020-10-30 18:12:09 +00:00
|
|
|
if (config.cacheControl && config.cacheControl !== 2) {
|
2019-09-19 12:10:37 +00:00
|
|
|
if (config.cloudflare.purgeCache) {
|
|
|
|
logger.log('Cache control enabled, purging Cloudflare\'s cache...')
|
2022-07-21 14:13:46 +00:00
|
|
|
const results = await utils.purgeCloudflareCache(cdnRoutes)
|
2019-09-19 12:10:37 +00:00
|
|
|
let errored = false
|
|
|
|
let succeeded = 0
|
|
|
|
for (const result of results) {
|
|
|
|
if (result.errors.length) {
|
|
|
|
if (!errored) errored = true
|
|
|
|
result.errors.forEach(error => logger.log(`[CF]: ${error}`))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
succeeded += result.files.length
|
2019-01-31 09:36:16 +00:00
|
|
|
}
|
2020-10-30 18:12:09 +00:00
|
|
|
if (!errored) {
|
2019-09-19 12:10:37 +00:00
|
|
|
logger.log(`Successfully purged ${succeeded} cache`)
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2019-09-19 12:10:37 +00:00
|
|
|
} else {
|
|
|
|
logger.log('Cache control enabled without Cloudflare\'s cache purging')
|
2019-01-31 09:36:16 +00:00
|
|
|
}
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2019-01-09 10:11:45 +00:00
|
|
|
|
2022-05-06 19:17:31 +00:00
|
|
|
// Initiate internal periodical check ups of temporary uploads if required
|
|
|
|
if (utils.retentions && utils.retentions.enabled && config.uploads.temporaryUploadsInterval > 0) {
|
2019-09-08 01:56:29 +00:00
|
|
|
let temporaryUploadsInProgress = false
|
|
|
|
const temporaryUploadCheck = async () => {
|
2020-10-30 18:12:09 +00:00
|
|
|
if (temporaryUploadsInProgress) return
|
2019-09-08 01:56:29 +00:00
|
|
|
|
|
|
|
temporaryUploadsInProgress = true
|
2020-05-26 18:18:25 +00:00
|
|
|
try {
|
2022-07-25 00:39:35 +00:00
|
|
|
const result = await utils.bulkDeleteExpired(false, utils.devmode)
|
2022-05-06 19:01:33 +00:00
|
|
|
|
|
|
|
if (result.expired.length || result.failed.length) {
|
2022-07-25 00:39:35 +00:00
|
|
|
if (utils.devmode) {
|
2022-06-28 04:48:22 +00:00
|
|
|
let logMessage = `Expired uploads (${result.expired.length}): ${result.expired.map(_file => _file.name).join(', ')}`
|
2022-05-06 19:01:33 +00:00
|
|
|
if (result.failed.length) {
|
2022-06-28 04:48:22 +00:00
|
|
|
logMessage += `\nErrored (${result.failed.length}): ${result.failed.map(_file => _file.name).join(', ')}`
|
2022-05-06 19:01:33 +00:00
|
|
|
}
|
|
|
|
logger.debug(logMessage)
|
|
|
|
} else {
|
|
|
|
let logMessage = `Expired uploads: ${result.expired.length} deleted`
|
|
|
|
if (result.failed.length) {
|
|
|
|
logMessage += `, ${result.failed.length} errored`
|
|
|
|
}
|
|
|
|
logger.log(logMessage)
|
2020-10-30 18:12:09 +00:00
|
|
|
}
|
2020-05-26 18:18:25 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Simply print-out errors, then continue
|
|
|
|
logger.error(error)
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
temporaryUploadsInProgress = false
|
|
|
|
}
|
|
|
|
|
2020-05-16 14:45:14 +00:00
|
|
|
temporaryUploadCheck()
|
|
|
|
setInterval(temporaryUploadCheck, config.uploads.temporaryUploadsInterval)
|
2019-09-08 01:56:29 +00:00
|
|
|
}
|
|
|
|
|
2019-01-01 05:34:16 +00:00
|
|
|
// NODE_ENV=development yarn start
|
2022-07-25 00:39:35 +00:00
|
|
|
if (utils.devmode) {
|
2022-05-06 18:35:07 +00:00
|
|
|
const { inspect } = require('util')
|
2018-12-18 17:41:42 +00:00
|
|
|
// Add readline interface to allow evaluating arbitrary JavaScript from console
|
2022-05-06 18:35:07 +00:00
|
|
|
require('readline').createInterface({
|
|
|
|
input: process.stdin
|
2018-12-18 17:41:42 +00:00
|
|
|
}).on('line', line => {
|
|
|
|
try {
|
2020-10-30 18:12:09 +00:00
|
|
|
if (line === 'rs') return
|
|
|
|
if (line === '.exit') return process.exit(0)
|
2018-12-18 17:41:42 +00:00
|
|
|
// eslint-disable-next-line no-eval
|
2022-05-06 18:35:07 +00:00
|
|
|
const evaled = eval(line)
|
|
|
|
process.stdout.write(`${typeof evaled === 'string' ? evaled : inspect(evaled)}\n`)
|
2018-12-18 17:41:42 +00:00
|
|
|
} catch (error) {
|
2022-05-06 18:35:07 +00:00
|
|
|
process.stderr.write(`${error.stack}\n`)
|
2018-12-18 17:41:42 +00:00
|
|
|
}
|
|
|
|
}).on('SIGINT', () => {
|
|
|
|
process.exit(0)
|
|
|
|
})
|
2022-05-06 18:35:07 +00:00
|
|
|
logger.log(utils.stripIndents(`!!! DEVELOPMENT MODE !!!
|
|
|
|
[=] Nunjucks will auto rebuild (not live reload)
|
|
|
|
[=] HTTP rate limits disabled
|
|
|
|
[=] Readline interface enabled (eval arbitrary JS input)`))
|
2018-10-09 19:52:41 +00:00
|
|
|
}
|
2019-09-08 01:56:29 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(error)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
})()
|