2019-10-15 09:22:08 +02:00
|
|
|
const express = require('express')
|
|
|
|
const bodyParser = require('body-parser')
|
|
|
|
const path = require('path')
|
2019-10-20 19:30:32 +02:00
|
|
|
const favicon = require('serve-favicon')
|
2019-10-21 20:44:34 +02:00
|
|
|
const rateLimit = require('express-rate-limit')
|
2019-10-15 09:22:08 +02:00
|
|
|
|
2020-07-17 22:20:48 +02:00
|
|
|
const { retrieveICS } = require('./services/ics-retriever')
|
2020-07-17 20:54:11 +02:00
|
|
|
const crawl = require('./services/crawler')
|
2019-10-23 20:37:06 +02:00
|
|
|
const {
|
|
|
|
genericErrorHandler,
|
|
|
|
checkURLParameter,
|
|
|
|
forceSecure,
|
2019-10-24 10:21:41 +02:00
|
|
|
createRouteLogger,
|
|
|
|
createErrorLogger,
|
2019-10-23 20:37:06 +02:00
|
|
|
} = require('./middlewares')
|
2020-07-17 21:34:52 +02:00
|
|
|
const { createAppLogger } = require('./log-utils')
|
2020-07-17 22:20:48 +02:00
|
|
|
const { getNormalizedUrl } = require('./utils')
|
2019-10-15 09:22:08 +02:00
|
|
|
|
2019-10-15 22:55:12 +02:00
|
|
|
const port = process.env.PORT
|
2019-10-22 22:26:11 +02:00
|
|
|
const certEndpoint = process.env.CERT_ENDPOINT || ''
|
2019-10-23 15:15:20 +02:00
|
|
|
const certSecret = process.env.CERT_SECRET || ''
|
2019-10-24 10:21:41 +02:00
|
|
|
const isDevelopment = process.env.NODE_ENV === 'development'
|
2019-11-04 20:12:24 +01:00
|
|
|
const enforceHTTPS = Boolean(process.env.ENFORCE_HTTPS)
|
2019-10-23 15:15:20 +02:00
|
|
|
|
2019-10-15 09:22:08 +02:00
|
|
|
const app = express()
|
2019-10-24 10:21:41 +02:00
|
|
|
const appLogger = createAppLogger({ dev: isDevelopment })
|
2019-11-05 21:08:59 +01:00
|
|
|
const limiter = rateLimit({
|
|
|
|
windowMs: 60 * 1000,
|
|
|
|
max: 10,
|
|
|
|
})
|
2019-11-14 08:56:30 +01:00
|
|
|
const pkg = require('../package.json')
|
|
|
|
const version = pkg.version || ''
|
2019-10-15 09:22:08 +02:00
|
|
|
|
2019-11-04 20:12:24 +01:00
|
|
|
// Force app to always redirect to HTTPS
|
|
|
|
// use when you can't configure web server
|
|
|
|
if (enforceHTTPS) {
|
|
|
|
app.use(forceSecure)
|
|
|
|
}
|
2019-10-23 15:15:20 +02:00
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
// Server logs
|
2019-11-27 12:47:36 +01:00
|
|
|
// You can alternatively enable these to mimic logs created
|
|
|
|
// by your web server
|
|
|
|
if (isDevelopment) {
|
|
|
|
app.use(createRouteLogger({ dev: isDevelopment }))
|
|
|
|
}
|
2019-10-24 10:21:41 +02:00
|
|
|
|
2019-10-15 09:22:08 +02:00
|
|
|
app.set('view engine', 'ejs')
|
|
|
|
app.set('views', path.join(__dirname, 'views'))
|
2019-11-05 21:08:59 +01:00
|
|
|
app.set('trust proxy', 1)
|
2019-10-20 19:30:32 +02:00
|
|
|
|
2020-07-17 19:29:18 +02:00
|
|
|
app.use(express.static(path.join(__dirname, '..', 'dist')))
|
|
|
|
//app.use(favicon(path.join(__dirname, '..', 'dist', 'favicon.ico')))
|
2019-10-24 21:55:05 +02:00
|
|
|
app.use(bodyParser.urlencoded({ extended: true }))
|
2019-10-15 09:22:08 +02:00
|
|
|
|
2020-07-17 19:29:18 +02:00
|
|
|
const indexFile = path.join(__dirname, '..', 'dist', 'index.html')
|
|
|
|
|
2019-10-23 15:15:20 +02:00
|
|
|
if (certEndpoint) {
|
|
|
|
app.get(`/${certEndpoint}`, (req, res) => {
|
|
|
|
res.status(200).send(certSecret)
|
|
|
|
})
|
|
|
|
}
|
2019-10-22 22:26:11 +02:00
|
|
|
|
2019-10-17 22:36:29 +02:00
|
|
|
app.get('/error', (req, res) => {
|
2019-10-20 19:25:13 +02:00
|
|
|
const error = req.error || req.query.error || ''
|
|
|
|
|
2019-10-17 22:36:29 +02:00
|
|
|
res
|
|
|
|
.status(500)
|
2019-10-20 19:25:13 +02:00
|
|
|
.render('error', { error })
|
|
|
|
})
|
|
|
|
|
2019-10-23 22:29:01 +02:00
|
|
|
app.get('/about', (req, res) => {
|
2019-11-14 08:56:30 +01:00
|
|
|
res.render('about', { version })
|
2019-10-23 22:29:01 +02:00
|
|
|
})
|
|
|
|
|
2019-10-20 19:25:13 +02:00
|
|
|
// NOTE: Capture all unkown URLs
|
|
|
|
app.get('*', (req, res) => {
|
|
|
|
res.status(400).render('404')
|
2019-10-17 22:36:29 +02:00
|
|
|
})
|
|
|
|
|
2020-07-17 22:20:48 +02:00
|
|
|
app.use('/download/html', limiter)
|
|
|
|
app.use('/download/html', checkURLParameter)
|
|
|
|
app.post('/download/html', async (req, res, next) => {
|
|
|
|
try {
|
|
|
|
const { url } = req.body
|
|
|
|
|
|
|
|
const facebookURL = getNormalizedUrl(url)
|
|
|
|
const html = await crawl(facebookURL, { logger: appLogger })
|
|
|
|
|
|
|
|
res
|
|
|
|
.contentType('text/html')
|
|
|
|
.status(200)
|
|
|
|
.send(Buffer.from(html, 'utf8'))
|
|
|
|
} catch (err) {
|
|
|
|
next(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-11-05 21:08:59 +01:00
|
|
|
app.use('/download', limiter)
|
2019-10-22 20:25:09 +02:00
|
|
|
app.use('/download', checkURLParameter)
|
|
|
|
app.post('/download', async (req, res, next) => {
|
2019-10-15 09:22:08 +02:00
|
|
|
try {
|
2019-10-23 20:37:06 +02:00
|
|
|
const { url } = req.body
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
const ics = await retrieveICS(url, {
|
|
|
|
logger: appLogger,
|
2020-07-17 20:54:11 +02:00
|
|
|
crawl,
|
2019-10-24 10:21:41 +02:00
|
|
|
})
|
2019-10-23 20:37:06 +02:00
|
|
|
|
|
|
|
res
|
|
|
|
.contentType('text/calendar')
|
|
|
|
.status(200)
|
2019-11-27 12:51:13 +01:00
|
|
|
.send(Buffer.from(ics, 'utf8'))
|
2019-10-15 09:22:08 +02:00
|
|
|
} catch (err) {
|
2019-10-22 20:25:09 +02:00
|
|
|
next(err)
|
2019-10-15 09:22:08 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
app.use(createErrorLogger({ dev: isDevelopment }))
|
2019-10-17 22:36:29 +02:00
|
|
|
app.use(genericErrorHandler)
|
|
|
|
|
2019-10-24 10:21:41 +02:00
|
|
|
app.listen(port)
|