From 4c11fcbd99f47148d8ed07b914e9e5dfb347d454 Mon Sep 17 00:00:00 2001 From: milahu Date: Sat, 18 Jul 2020 21:54:00 +0200 Subject: [PATCH 1/4] new URL format --- src/params.js | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/params.js b/src/params.js index 39447799..f5b30d3a 100644 --- a/src/params.js +++ b/src/params.js @@ -1,16 +1,28 @@ const DEFAULT_QUALITY = 40 function params(req, res, next) { - let url = req.query.url - if (Array.isArray(url)) url = url.join('&url=') - if (!url) return res.end('bandwidth-hero-proxy') - - url = url.replace(/http:\/\/1\.1\.\d\.\d\/bmi\/(https?:\/\/)?/i, 'http://') - req.params.url = url - req.params.webp = !req.query.jpeg - req.params.grayscale = req.query.bw != 0 - req.params.quality = parseInt(req.query.l, 10) || DEFAULT_QUALITY + if (req._parsedUrl.pathname == "/") { + // old URL format /?url=https%3A%2F%2Fhost%2Fpath%2Ffile.jpg&jpeg=0&bw=0&l=50 + let url = req.query.url + if (Array.isArray(url)) url = url.join('&url=') + if (!url) return res.end('bandwidth-hero-proxy') + url = url.replace(/http:\/\/1\.1\.\d\.\d\/bmi\/(https?:\/\/)?/i, 'http://') + req.params.url = url + req.params.webp = !req.query.jpeg + req.params.grayscale = req.query.bw != 0 + req.params.quality = parseInt(req.query.l, 10) || DEFAULT_QUALITY + } + else { + // new URL format /https/host/path/file.jpg.c50.webp + let match = req.url.match(/^\/(\w+)\/(.*?).([bc])(\d{1,3})\.(\w+)$/) + if (match) { + req.params.url = match[1] + "://" + match[2]; + req.params.grayscale = (match[3] == "b"); + req.params.quality = parseInt(match[4], 10) || DEFAULT_QUALITY; + req.params.webp = (match[5] == "webp"); + } + } next() } From f4cfaed9d216f3a9136f826572c5fc7c65222a2f Mon Sep 17 00:00:00 2001 From: milahu Date: Sat, 18 Jul 2020 21:57:27 +0200 Subject: [PATCH 2/4] catch all routes for new URL format --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 983b92dd..597be4ba 100644 --- a/server.js +++ b/server.js @@ -8,6 +8,6 @@ const proxy = require('./src/proxy') const PORT = process.env.PORT || 8080 app.enable('trust proxy') -app.get('/', authenticate, params, proxy) app.get('/favicon.ico', (req, res) => res.status(204).end()) +app.get('*', authenticate, params, proxy) app.listen(PORT, () => console.log(`Listening on ${PORT}`)) From eadb8a2a893af85b1b2f0e51315798776e14511e Mon Sep 17 00:00:00 2001 From: milahu Date: Mon, 20 Jul 2020 08:46:27 +0200 Subject: [PATCH 3/4] parse suffix between path and query string --- src/params.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/params.js b/src/params.js index f5b30d3a..9d450424 100644 --- a/src/params.js +++ b/src/params.js @@ -2,7 +2,7 @@ const DEFAULT_QUALITY = 40 function params(req, res, next) { if (req._parsedUrl.pathname == "/") { - // old URL format /?url=https%3A%2F%2Fhost%2Fpath%2Ffile.jpg&jpeg=0&bw=0&l=50 + // old URL format /?url=https%3A%2F%2Fhost%2Fpath%2Ffile.jpg%3FqueryString&jpeg=0&bw=0&l=50 let url = req.query.url if (Array.isArray(url)) url = url.join('&url=') if (!url) return res.end('bandwidth-hero-proxy') @@ -14,10 +14,13 @@ function params(req, res, next) { req.params.quality = parseInt(req.query.l, 10) || DEFAULT_QUALITY } else { - // new URL format /https/host/path/file.jpg.c50.webp - let match = req.url.match(/^\/(\w+)\/(.*?).([bc])(\d{1,3})\.(\w+)$/) + // new URL format /https/host/path/file.jpg.c50.webp?queryString + const match = req.url.match(/^\/(\w+)\/(.*?).([bc])(\d{1,3})\.(\w+)(\?.*)?$/); if (match) { - req.params.url = match[1] + "://" + match[2]; + req.params.url = match[1] + "://" + match[2]; // protocol + host + path + if(match[6]) { + req.params.url += match[6]; // query string + } req.params.grayscale = (match[3] == "b"); req.params.quality = parseInt(match[4], 10) || DEFAULT_QUALITY; req.params.webp = (match[5] == "webp"); From c105f7d216b9f0d47c38c97b8e86dc924e8ce587 Mon Sep 17 00:00:00 2001 From: milahu Date: Tue, 21 Jul 2020 12:39:29 +0200 Subject: [PATCH 4/4] parse internal query string before protocol --- src/params.js | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/params.js b/src/params.js index 9d450424..20e5bd41 100644 --- a/src/params.js +++ b/src/params.js @@ -14,16 +14,34 @@ function params(req, res, next) { req.params.quality = parseInt(req.query.l, 10) || DEFAULT_QUALITY } else { - // new URL format /https/host/path/file.jpg.c50.webp?queryString - const match = req.url.match(/^\/(\w+)\/(.*?).([bc])(\d{1,3})\.(\w+)(\?.*)?$/); + // new URL format /k=v&k2=v2/https/host/path/file.jpg.c50.webp?queryString + const match = req.url.match(/^(?:\/([^/]+?=[^/]+))?\/(\w+)\/(.*?).([bc])(\d{1,3})\.(\w+)(\?.*)?$/); if (match) { - req.params.url = match[1] + "://" + match[2]; // protocol + host + path - if(match[6]) { - req.params.url += match[6]; // query string + + let queryObj = {}; + if (match[1]) { + // parse internal query string before protocol + let input = match[1]; + const re = /([^?&=]+)(?:=([^&]+))?/g; + let match2; + while (match2 = re.exec(input)) { + queryObj[match2[1]] = decodeURIComponent(match2[2]); + } } - req.params.grayscale = (match[3] == "b"); - req.params.quality = parseInt(match[4], 10) || DEFAULT_QUALITY; - req.params.webp = (match[5] == "webp"); + + // reconstruct original URL + req.params.url = match[2] + "://" + match[3]; // protocol + host + path + if (queryObj.appendPath) { + req.params.url += queryObj.appendPath; + } + if(match[7]) { + req.params.url += match[7]; // query string + } + + // set compression parameters + req.params.grayscale = (match[4] == "b"); + req.params.quality = parseInt(match[5], 10) || DEFAULT_QUALITY; + req.params.webp = (match[6] == "webp"); } } next()