From b69184eaea160f5ecaaaf1a5bd138bddb7882331 Mon Sep 17 00:00:00 2001 From: Evert Prants Date: Tue, 6 Aug 2019 15:35:01 +0300 Subject: [PATCH] Check for user agent, return raw --- applications/highlight-termbin/index.js | 3 +++ applications/tempfiles/index.js | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/applications/highlight-termbin/index.js b/applications/highlight-termbin/index.js index 02054ac..34fa19a 100644 --- a/applications/highlight-termbin/index.js +++ b/applications/highlight-termbin/index.js @@ -30,6 +30,9 @@ async function init () { let fichePath = path.join(root, name, 'index.txt') let reqRaw = req.query.raw != null + let ua = req.get('User-Agent') + + if (!reqRaw && ua && (ua.match(/curl\//i) != null || ua.match(/wget\//i) != null)) reqRaw = true let text try { diff --git a/applications/tempfiles/index.js b/applications/tempfiles/index.js index 7bfd0f7..2fb8829 100644 --- a/applications/tempfiles/index.js +++ b/applications/tempfiles/index.js @@ -26,6 +26,8 @@ function asyncForm (req, form) { async function clearDatabase (config, dbPromise) { let db = await dbPromise + + // Remove expired files let files = await db.all('SELECT * FROM File WHERE upload < ?', new Date() - (config.expiry * 1000)) if (files.length > 0) { for (let i in files) { @@ -49,17 +51,22 @@ async function clearDatabase (config, dbPromise) { } async function init () { + // Load configuration let config = await cfgLoader let root = path.resolve(config.root) + // Check for root directory await fs.access(root, fsa.constants.F_OK) + // Initialize database const dbPromise = Promise.resolve() .then(() => sqlite.open(path.join(__dirname, config.database), { Promise, cache: true })) .then(db => db.migrate({ migrationsPath: path.join(__dirname, 'migrations') })) await clearDatabase(config, dbPromise) + // Serve a file or a hash + // Files should be served from an external web server (such as nginx) whenever possible. router.get('/:hash', async (req, res, next) => { if (!req.params.hash) return res.status(400).send('Invalid request') @@ -77,6 +84,7 @@ async function init () { res.sendFile(path.join(root, file.path)) }) + // Upload a file or publish a hash router.post('/publish', async (req, res, next) => { let ip = req.ip let token = req.header('token') || req.body.token