3 var path = require('path'),
5 util = require('util'),
6 argv = require('optimist').argv,
7 httpProxy = require('../lib/node-http-proxy');
10 "usage: node-http-proxy [options] ",
12 "Starts a node-http-proxy server using the specified command-line options",
15 " --port PORT Port that the proxy server should run on",
16 " --host HOST Host that the proxy server should run on",
17 " --target HOST:PORT Location of the server the proxy will target",
18 " --config OUTFILE Location of the configuration file for the proxy server",
19 " --silent Silence the log output from the proxy server",
20 " --user USER User to drop privileges to once server socket is bound",
21 " -h, --help You're staring at it"
24 if (argv.h || argv.help || Object.keys(argv).length === 2) {
25 return util.puts(help);
28 var location, config = {},
29 port = argv.port || 80,
30 host = argv.host || undefined,
35 // If we were passed a config, parse it
39 var data = fs.readFileSync(argv.config);
40 config = JSON.parse(data.toString());
42 util.puts('Error starting node-http-proxy: ' + ex);
48 // If `config.https` is set, then load the required file contents into the config options.
51 Object.keys(config.https).forEach(function (key) {
52 // If CA certs are specified, load those too.
54 for (var i=0; i < config.https.ca.length; i++) {
55 if (config.https.ca === undefined) {
58 config.https.ca[i] = fs.readFileSync(config.https[key][i], 'utf8');
61 config.https[key] = fs.readFileSync(config.https[key], 'utf8');
67 // Check to see if we should silence the logs
69 config.silent = typeof argv.silent !== 'undefined' ? argv.silent : config.silent;
72 // If we were passed a target, parse the url string
74 if (typeof target === 'string') location = target.split(':');
77 // Create the server with the specified options
81 var targetPort = location.length === 1 ? 80 : parseInt(location[1]);
82 server = httpProxy.createServer(targetPort, location[0], config);
84 else if (config.router) {
85 server = httpProxy.createServer(config);
88 return util.puts(help);
95 server.listen(port, host);
102 // Drop privileges if requested
104 if (typeof user === 'string') {
105 process.setuid(user);
109 // Notify that the server is started
111 if (!config.silent) {
112 util.puts('node-http-proxy server now listening on port: ' + port);