From 73b98249a2e0e465d416e849422f2f6a6693326c Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Fri, 6 Jul 2018 12:43:21 -0500 Subject: [PATCH 1/2] Allow file urls --- command-start.js | 156 +++++++++++++++++++++-------------------- lib/pac/resolver.js | 164 +++++++++++++++++++++++++------------------- npm-shrinkwrap.json | 2 +- 3 files changed, 175 insertions(+), 147 deletions(-) diff --git a/command-start.js b/command-start.js index 15fe06b..b8475c6 100644 --- a/command-start.js +++ b/command-start.js @@ -1,86 +1,92 @@ -const meta = require('./package') -const debug = require('debug')(meta.name + ':command-start') -const { fork } = require('child_process') -const path = require('path') -const inquirer = require('inquirer') -const server = require('./server') +const meta = require("./package"); +const debug = require("debug")(meta.name + ":command-start"); +const { fork } = require("child_process"); +const path = require("path"); +const inquirer = require("inquirer"); +const server = require("./server"); -exports.command = 'start [options]' +exports.command = "start [options]"; -exports.describe = 'Start the proxy.pac proxy' +exports.describe = "Start the proxy.pac proxy"; exports.builder = { - 'url': { - demandOption: true - }, - 'address': {}, - 'port': {}, - 'authenticate': {}, - 'username': {}, - 'password': {}, - 'foreground': {} -} + url: { + demandOption: true + }, + address: {}, + port: {}, + authenticate: {}, + username: {}, + password: {}, + foreground: {} +}; -exports.handler = async (argv) => { +exports.handler = async argv => { + try { + if (argv.authenticate && !argv.username) { + argv.username = (await inquirer.prompt([ + { + type: "string", + name: "value", + message: "Username:" + } + ])).value; + } - try { - if (argv.authenticate && !argv.username) { - argv.username = (await inquirer.prompt([ - { - type: 'string', - name: 'value', - message: 'Username:' - } - ])).value - } - - if (argv.username && !argv.password) { - argv.password = (await inquirer.prompt([ - { - type: 'password', - name: 'value', - message: `Password for ${argv.username}:` - } - ])).value - } + if (argv.username && !argv.password) { + argv.password = (await inquirer.prompt([ + { + type: "password", + name: "value", + message: `Password for ${argv.username}:` + } + ])).value; + } - if (argv.foreground) { - const daemon = await server(argv) + if (argv.foreground) { + const daemon = await server(argv); - daemon.on('error', (err) => { - console.error('Cannot start proxy.') - console.error(err) - process.exit(1) - }) + daemon.on("error", err => { + console.error("Cannot start proxy."); + console.error(err); + process.exit(1); + }); - daemon.on('listening', () => { - console.log('Proxy succesfully started.') - console.log('You may configure your ' + - 'shell by running the `' + argv['$0'] + ' env` command.') - }) - } else { - const daemon = fork(__dirname + path.sep + 'daemon', { - detached: true, - stdio: 'ignore' - }) + daemon.on("listening", () => { + console.log("Proxy succesfully started."); + console.log( + "You may configure your " + + "shell by running the `" + + argv["$0"] + + " env` command." + ); + }); + } else { + const daemon = fork(__dirname + path.sep + "daemon", { + detached: true, + stdio: "ignore" + }); - daemon.send(argv) - daemon.on('message', (err) => { - if (err) { - console.error('Cannot start proxy.') - console.error(err) - process.exit(1) - } else { - console.log('Proxy succesfully started.') - console.log('You may configure your ' + - 'shell by running the `' + argv['$0'] + ' env` command.') - process.exit(0) - } - }) + daemon.send(argv); + daemon.on("message", err => { + if (err) { + console.error("Cannot start proxy."); + console.error(err); + process.exit(1); + } else { + console.log("Proxy succesfully started."); + console.log( + "You may configure your " + + "shell by running the `" + + argv["$0"] + + " env` command." + ); + process.exit(0); + } + }); + } + } catch (err) { + console.error("Cannot start proxy."); + console.error(err); } - } catch (err) { - console.error('Cannot start proxy.') - console.error(err) - } - -} +}; diff --git a/lib/pac/resolver.js b/lib/pac/resolver.js index 45a221d..d2bf0db 100644 --- a/lib/pac/resolver.js +++ b/lib/pac/resolver.js @@ -1,79 +1,101 @@ -const meta = require('../../package') -const debug = require('debug')(meta.name + ':resolver') -const _ = require('lodash') -const Promise = require('bluebird') -const url = require('url') -const request = require('request') -const pac = require('pac-resolver') -const local = require('./local-address') +const meta = require("../../package"); +const debug = require("debug")(meta.name + ":resolver"); +const _ = require("lodash"); +const Promise = require("bluebird"); +const url = require("url"); +const request = require("request"); +const pac = require("pac-resolver"); +const local = require("./local-address"); +const fs = require("fs"); const defaults = { - url: undefined, - auth: undefined + url: undefined, + auth: undefined +}; + +const fileProtocol = "file://"; + +function retrievePacFile(url) { + return new Promise((resolve, reject) => { + debug("retrieving proxy.pac at %j", url); + + if (!url.startsWith(fileProtocol)) { + request( + { + url: url, + proxy: false + }, + (err, res, body) => { + if (err) { + return reject(err); + } else if (Math.floor(res.statusCode / 100) !== 2) { + return reject(new Error(res.statusMessage)); + } + + debug("proxy.pac retrieved"); + return resolve(body); + } + ); + } else { + fs.readFile(url.substr(fileProtocol.length), (err, contents) => { + if (err) { + return reject(err); + } else { + return resolve(contents); + } + }); + } + }); } -module.exports = (options) => { +module.exports = async options => { + options = _.defaults(options, defaults); - options = _.defaults(options, defaults) - - if (typeof options.url === 'undefined') { - throw new Error('missing url parameter') - } - - return new Promise((resolve, reject) => { + if (typeof options.url === "undefined") { + throw new Error("missing url parameter"); + } - debug('retrieving proxy.pac at %j', options.url) - request({ - url: options.url, - proxy: false - }, (err, res, body) => { - if (err) { - return reject(err) - } else if (Math.floor(res.statusCode / 100) !== 2) { - return reject(new Error(res.statusMessage)) - } - debug('proxy.pac retrieved') + const body = await retrievePacFile(options.url); - try { - const resolver = pac(body, { - sandbox: { - myIpAddress: local.get - } - }) + try { + const resolver = pac(body, { + sandbox: { + myIpAddress: local.get + } + }); - debug('returning resolver for proxy.pac at %j', options.url) - return resolve((uri) => { - return new Promise(async (resolve, reject) => { - debug('resolving proxy for %j', uri) - try { - if (await local.check(url.parse(uri).hostname)) { - throw new Error('trying to resolve local address') - } - const dest = await resolver(uri) - let [verb, proxy] = dest.split(/[\s;]+/) - debug('proxy for %j resolved to %j', uri, proxy) - if (proxy) { - uri = url.parse(`http://${proxy}`) - } else { - uri = url.parse(uri) - } - return resolve({ - connection: verb, - host: uri.hostname, - port: (uri.port ? uri.port : - (uri.protocol === 'https:' ? 443 : 80)), - auth: (proxy ? options.auth : undefined) - }) - } catch (err) { - debug('error resolving %j: %O', uri, err) - return reject(err) - } - }) - }) - } catch (err) { - debug('error creating resolver for proxy.pac at %j', options.url) - return reject(err) - } - }) - }) -} + debug("returning resolver for proxy.pac at %j", options.url); + return async uri => { + debug("resolving proxy for %j", uri); + try { + if (await local.check(url.parse(uri).hostname)) { + throw new Error("trying to resolve local address"); + } + const dest = await resolver(uri); + let [verb, proxy] = dest.split(/[\s;]+/); + debug("proxy for %j resolved to %j", uri, proxy); + if (proxy) { + uri = url.parse(`http://${proxy}`); + } else { + uri = url.parse(uri); + } + return { + connection: verb, + host: uri.hostname, + port: uri.port + ? uri.port + : uri.protocol === "https:" + ? 443 + : 80, + auth: proxy ? options.auth : undefined + }; + } catch (err) { + debug("error resolving %j: %O", uri, err); + throw err; + } + }; + } catch (err) { + debug("error creating resolver for proxy.pac at %j", options.url); + throw err; + } +}; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 9717c9d..6ccff9f 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "proxy-pac-proxy", - "version": "1.0.0", + "version": "1.0.10-dev", "lockfileVersion": 1, "requires": true, "dependencies": { From 45360baf9491805bf0594e38fa0e95e127153362 Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Mon, 9 Jul 2018 09:12:27 -0500 Subject: [PATCH 2/2] Revert some formatting changes. --- command-start.js | 156 ++++++++++++++++++++--------------------- lib/pac/resolver.js | 166 ++++++++++++++++++++++---------------------- npm-shrinkwrap.json | 2 +- 3 files changed, 158 insertions(+), 166 deletions(-) diff --git a/command-start.js b/command-start.js index b8475c6..15fe06b 100644 --- a/command-start.js +++ b/command-start.js @@ -1,92 +1,86 @@ -const meta = require("./package"); -const debug = require("debug")(meta.name + ":command-start"); -const { fork } = require("child_process"); -const path = require("path"); -const inquirer = require("inquirer"); -const server = require("./server"); +const meta = require('./package') +const debug = require('debug')(meta.name + ':command-start') +const { fork } = require('child_process') +const path = require('path') +const inquirer = require('inquirer') +const server = require('./server') -exports.command = "start [options]"; +exports.command = 'start [options]' -exports.describe = "Start the proxy.pac proxy"; +exports.describe = 'Start the proxy.pac proxy' exports.builder = { - url: { - demandOption: true - }, - address: {}, - port: {}, - authenticate: {}, - username: {}, - password: {}, - foreground: {} -}; + 'url': { + demandOption: true + }, + 'address': {}, + 'port': {}, + 'authenticate': {}, + 'username': {}, + 'password': {}, + 'foreground': {} +} -exports.handler = async argv => { - try { - if (argv.authenticate && !argv.username) { - argv.username = (await inquirer.prompt([ - { - type: "string", - name: "value", - message: "Username:" - } - ])).value; - } +exports.handler = async (argv) => { - if (argv.username && !argv.password) { - argv.password = (await inquirer.prompt([ - { - type: "password", - name: "value", - message: `Password for ${argv.username}:` - } - ])).value; - } + try { + if (argv.authenticate && !argv.username) { + argv.username = (await inquirer.prompt([ + { + type: 'string', + name: 'value', + message: 'Username:' + } + ])).value + } + + if (argv.username && !argv.password) { + argv.password = (await inquirer.prompt([ + { + type: 'password', + name: 'value', + message: `Password for ${argv.username}:` + } + ])).value + } - if (argv.foreground) { - const daemon = await server(argv); + if (argv.foreground) { + const daemon = await server(argv) - daemon.on("error", err => { - console.error("Cannot start proxy."); - console.error(err); - process.exit(1); - }); + daemon.on('error', (err) => { + console.error('Cannot start proxy.') + console.error(err) + process.exit(1) + }) - daemon.on("listening", () => { - console.log("Proxy succesfully started."); - console.log( - "You may configure your " + - "shell by running the `" + - argv["$0"] + - " env` command." - ); - }); - } else { - const daemon = fork(__dirname + path.sep + "daemon", { - detached: true, - stdio: "ignore" - }); + daemon.on('listening', () => { + console.log('Proxy succesfully started.') + console.log('You may configure your ' + + 'shell by running the `' + argv['$0'] + ' env` command.') + }) + } else { + const daemon = fork(__dirname + path.sep + 'daemon', { + detached: true, + stdio: 'ignore' + }) - daemon.send(argv); - daemon.on("message", err => { - if (err) { - console.error("Cannot start proxy."); - console.error(err); - process.exit(1); - } else { - console.log("Proxy succesfully started."); - console.log( - "You may configure your " + - "shell by running the `" + - argv["$0"] + - " env` command." - ); - process.exit(0); - } - }); - } - } catch (err) { - console.error("Cannot start proxy."); - console.error(err); + daemon.send(argv) + daemon.on('message', (err) => { + if (err) { + console.error('Cannot start proxy.') + console.error(err) + process.exit(1) + } else { + console.log('Proxy succesfully started.') + console.log('You may configure your ' + + 'shell by running the `' + argv['$0'] + ' env` command.') + process.exit(0) + } + }) } -}; + } catch (err) { + console.error('Cannot start proxy.') + console.error(err) + } + +} diff --git a/lib/pac/resolver.js b/lib/pac/resolver.js index d2bf0db..19b948c 100644 --- a/lib/pac/resolver.js +++ b/lib/pac/resolver.js @@ -1,101 +1,99 @@ -const meta = require("../../package"); -const debug = require("debug")(meta.name + ":resolver"); -const _ = require("lodash"); -const Promise = require("bluebird"); -const url = require("url"); -const request = require("request"); -const pac = require("pac-resolver"); -const local = require("./local-address"); -const fs = require("fs"); +const meta = require('../../package') +const debug = require('debug')(meta.name + ':resolver') +const _ = require('lodash') +const Promise = require('bluebird') +const url = require('url') +const request = require('request') +const pac = require('pac-resolver') +const local = require('./local-address') +const fs = require('fs'); const defaults = { - url: undefined, - auth: undefined -}; + url: undefined, + auth: undefined +} -const fileProtocol = "file://"; +const FILE_PROTOCOL = 'file://'; function retrievePacFile(url) { - return new Promise((resolve, reject) => { - debug("retrieving proxy.pac at %j", url); + return new Promise((resolve, reject) => { + debug('retrieving proxy.pac at %j', url) - if (!url.startsWith(fileProtocol)) { - request( - { - url: url, - proxy: false - }, - (err, res, body) => { - if (err) { - return reject(err); - } else if (Math.floor(res.statusCode / 100) !== 2) { - return reject(new Error(res.statusMessage)); - } + if (!url.startsWith(FILE_PROTOCOL)) { + request( + { + url: url, + proxy: false + }, + (err, res, body) => { + if (err) { + return reject(err); + } else if (Math.floor(res.statusCode / 100) !== 2) { + return reject(new Error(res.statusMessage)) + } - debug("proxy.pac retrieved"); - return resolve(body); - } - ); + debug('proxy.pac retrieved') + return resolve(body) + } + ); + } else { + fs.readFile(url.substr(FILE_PROTOCOL.length), (err, contents) => { + if (err) { + return reject(err) } else { - fs.readFile(url.substr(fileProtocol.length), (err, contents) => { - if (err) { - return reject(err); - } else { - return resolve(contents); - } - }); + return resolve(contents) } - }); + }) + } + }) } -module.exports = async options => { - options = _.defaults(options, defaults); +module.exports = async (options) => { - if (typeof options.url === "undefined") { - throw new Error("missing url parameter"); - } + options = _.defaults(options, defaults) + + if (typeof options.url === 'undefined') { + throw new Error('missing url parameter') + } - const body = await retrievePacFile(options.url); + const body = await retrievePacFile(options.url) - try { - const resolver = pac(body, { - sandbox: { - myIpAddress: local.get - } - }); + try { + const resolver = pac(body, { + sandbox: { + myIpAddress: local.get + } + }) - debug("returning resolver for proxy.pac at %j", options.url); - return async uri => { - debug("resolving proxy for %j", uri); - try { - if (await local.check(url.parse(uri).hostname)) { - throw new Error("trying to resolve local address"); - } - const dest = await resolver(uri); - let [verb, proxy] = dest.split(/[\s;]+/); - debug("proxy for %j resolved to %j", uri, proxy); - if (proxy) { - uri = url.parse(`http://${proxy}`); - } else { - uri = url.parse(uri); - } - return { - connection: verb, - host: uri.hostname, - port: uri.port - ? uri.port - : uri.protocol === "https:" - ? 443 - : 80, - auth: proxy ? options.auth : undefined - }; - } catch (err) { - debug("error resolving %j: %O", uri, err); - throw err; - } - }; + debug('returning resolver for proxy.pac at %j', options.url) + return async (uri) => { + debug('resolving proxy for %j', uri) + try { + if (await local.check(url.parse(uri).hostname)) { + throw new Error('trying to resolve local address') + } + const dest = await resolver(uri) + let [verb, proxy] = dest.split(/[\s;]+/) + debug('proxy for %j resolved to %j', uri, proxy) + if (proxy) { + uri = url.parse(`http://${proxy}`) + } else { + uri = url.parse(uri) + } + return { + connection: verb, + host: uri.hostname, + port: (uri.port ? uri.port : + (uri.protocol === 'https:' ? 443 : 80)), + auth: (proxy ? options.auth : undefined) + } + } catch (err) { + debug('error resolving %j: %O', uri, err) + throw err + } + } } catch (err) { - debug("error creating resolver for proxy.pac at %j", options.url); - throw err; + debug('error creating resolver for proxy.pac at %j', options.url) + throw err } -}; +} diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 6ccff9f..9717c9d 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -1,6 +1,6 @@ { "name": "proxy-pac-proxy", - "version": "1.0.10-dev", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": {