diff --git a/README.md b/README.md index 2618a78..87ac8cf 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,9 @@ overriden using command line options: -e, --expired Use expired cache when npm registry unavailable -f, --friendly-names Use actual file names instead of hashes in the cache -v, --verbose Verbose mode - --help This help + -l, --log-path Log path + -m, --internal-port HTTPs port to use for internal proxying "MITM" server (necessary for running on Windows systems) + --help This help ## Why can't I use the built-in npm cache? diff --git a/bin/npm-proxy-cache b/bin/npm-proxy-cache index cfb3c99..9075286 100755 --- a/bin/npm-proxy-cache +++ b/bin/npm-proxy-cache @@ -19,7 +19,8 @@ program .option('-f, --friendly-names', 'Use actual file names instead of hashes in the cache') .option('-v, --verbose', 'Verbose mode') .option('-l, --log-path [path]', 'Log path') - .option('-m, --mitmport [port]', 'HTTPs port to use instead of MITM (necessary for running on Windows systems)') + .option('-m, --internal-port [port]', + 'HTTPs port to use for internal proxying "MITM" server (necessary for running on Windows systems)') .parse(process.argv); require('../lib/proxy').powerup(program); diff --git a/lib/proxy.js b/lib/proxy.js index 7e2549d..7cc9419 100644 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -3,8 +3,9 @@ var http = require('http'), https = require('https'), fs = require('fs'), os = require('os'), - request = require('request'), url = require('url'), + path = require('path'), + request = require('request'), log4js = require('log4js'); Cache = require('./cache'); @@ -14,7 +15,7 @@ process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // socket path for system mitm https server -var mitmSocketPath = os.tmpdir() + '/mitm.sock'; +var mitmSocketPath = path.join(os.tmpdir(), 'mitm.sock') exports.log = null; @@ -45,19 +46,23 @@ exports.powerup = function(opts) { log4js.addAppender(log4js.appenders.file(opts.logPath), 'proxy'); } + // Fake https server, MITM if you want + var mitm = https.createServer(options, this.handler); - if(!opts.mitmport){ - // make sure there's no previously created socket - if (fs.existsSync(mitmSocketPath)) - fs.unlinkSync(mitmSocketPath); + // NOTE: for windows platform user has to specify port, because + // windows does not support unix sockets. + if (process.platform.indexOf('win') === 0) { + if (!opts.internalPort) + throw new Error('For Windows platform you have to specify internal port using `--internal-port` option'); + + // Cleanup MITM socket for unix platforms } else { - // replace mitm socket path with port - // this is necessary on Windows, otherwise it will not work (MITM not supported) - mitmSocketPath = opts.mitmport; + if (fs.existsSync(mitmSocketPath)) + fs.unlinkSync(mitmSocketPath); } - - // fake https server, MITM if you want - https.createServer(options, this.handler).listen(mitmSocketPath); + + mitm.listen(opts.internalPort || mitmSocketPath); + // start HTTP server with custom request handler callback function var server = http.createServer(this.handler).listen(opts.port, opts.host, function(err) {