From 687b2a3567a8742235b3b55f81c79905c92ecc29 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 12 Aug 2018 03:45:13 -0600 Subject: [PATCH] v0.20.0-wip: enable direct download of files via serve-index/serve-static --- lib/sorting-hat.js | 34 ++++++++++++++++++++++++++++++---- package.json | 5 +++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/sorting-hat.js b/lib/sorting-hat.js index 78d294f..b7719f0 100644 --- a/lib/sorting-hat.js +++ b/lib/sorting-hat.js @@ -362,21 +362,47 @@ module.exports.assign = function (state, tun, cb) { state._serveIndex = require('serve-index'); var serveIndex; var serveStatic; + var dlStatic; if (isFile) { serveStatic = state._serveStatic(path.dirname(conf.handler), { dotfiles: 'allow', index: [ 'index.html' ] }); + dlStatic = state._serveStatic(path.dirname(conf.handler), { acceptRanges: false, dotfiles: 'allow', index: [ 'index.html' ] }); serveIndex = function (req, res, next) { next(); }; isFile = path.basename(conf.handler); } else { serveStatic = state._serveStatic(conf.handler, { dotfiles: 'allow', index: [ 'index.html' ] }); - serveIndex = state._serveIndex(conf.handler, { hidden: true, icons: true, view: 'tiles' }); + dlStatic = state._serveStatic(conf.handler, { acceptRanges: false, dotfiles: 'allow', index: [ 'index.html' ] }); + serveIndex = state._serveIndex(conf.handler, { + hidden: true, icons: true + , template: require('serve-tpl-attachment')({ privatefiles: 'ignore' }) + }); } handler = function (req, res) { + var qIndex = req.url.indexOf('?'); + var fIndex; + var fname; + if (-1 === qIndex) { + qIndex = req.url.length; + } + req.querystring = req.url.substr(qIndex); + req.url = req.url.substr(0, qIndex); + req.query = require('querystring').parse(req.querystring.substr(1)); if (isFile) { req.url = '/' + isFile; } - serveStatic(req, res, function () { - serveIndex(req, res, state._finalHandler(req, res)); - }); + //console.log('[req.query]', req.url, req.query); + if (req.query.download) { + fIndex = req.url.lastIndexOf('/'); + fname = req.url.substr(fIndex + 1); + res.setHeader('Content-Disposition', 'attachment; filename="'+decodeURIComponent(fname)+'"'); + res.setHeader('Content-Type', 'application/octet-stream'); + dlStatic(req, res, function () { + serveIndex(req, res, state._finalHandler(req, res)); + }); + } else { + serveStatic(req, res, function () { + serveIndex(req, res, state._finalHandler(req, res)); + }); + } }; handlerservers[conf.handler] = http.createServer(handler); handlerservers[conf.handler].emit('connection', tlsSocket); diff --git a/package.json b/package.json index 2a93cbf..b3cabab 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "telebit", - "version": "0.19.28", + "version": "0.20.0-wip", "description": "Break out of localhost. Connect to any device from anywhere over any tcp port or securely in a browser. A secure tunnel. A poor man's reverse VPN.", "main": "lib/remote.js", "files": [ @@ -59,12 +59,13 @@ "js-yaml": "^3.11.0", "jsonwebtoken": "^7.1.9", "mkdirp": "^0.5.1", - "proxy-packer": "^2.0.0", + "proxy-packer": "^2.0.2", "ps-list": "^5.0.0", "recase": "^1.0.4", "redirect-https": "^1.1.5", "serve-index": "^1.9.1", "serve-static": "^1.13.2", + "serve-tpl-attachment": "^1.0.1", "sni": "^1.0.0", "socket-pair": "^1.0.3", "ws": "^2.3.1"