#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const args_1 = __importDefault(require("args"));
const debug_1 = __importDefault(require("debug"));
const child_process_1 = require("child_process");
const events_1 = require("events");
// @ts-expect-error no types for "basic-auth-parser"
const basicAuthParser = require("basic-auth-parser");
const proxy_1 = require("../proxy");
//import pkg from '../pkg';
const debug = (0, debug_1.default)('proxy');
process.title = 'proxy';
args_1.default.option('port', 'Port number to the proxy server should bind to', 3128, parseInt).option('authenticate', '"authenticate" command to run when the "Proxy-Authorization" header is sent', '', String);
//.option(
//	'local-address',
//	'IP address of the network interface to send the outgoing requests through',
//	'',
//	String
//);
//const flags = args.parse(process.argv, { name: pkg.name });
const flags = args_1.default.parse(process.argv);
const { port, authenticate } = flags;
const proxy = (0, proxy_1.createProxy)();
/**
 * Outbound proxy requests will use `agent: false`.
 */
//debug("setting outbound proxy request's `agent` to `false`");
//proxy.agent = false;
/**
 * Proxy outgoing request localAddress parameter
 */
//if (flags.localAddress) {
//	proxy.localAddress = flags.localAddress;
//}
/**
 * Proxy authenticate function.
 */
if (authenticate) {
    debug('setting `authenticate()` function for: "%s"', authenticate);
    proxy.authenticate = async (req) => {
        debug('authenticate(): "%s"', authenticate);
        // parse the "Proxy-Authorization" header
        const auth = req.headers['proxy-authorization'];
        if (!auth) {
            // optimization: don't invoke the child process if no
            // "Proxy-Authorization" header was given
            return false;
        }
        const parsed = basicAuthParser(auth);
        debug('parsed "Proxy-Authorization": %j', parsed);
        // spawn a child process with the user-specified "authenticate" command
        const env = { ...process.env };
        // add "auth" related ENV variables
        for (const [key, value] of Object.entries(parsed)) {
            env['PROXY_AUTH_' + key.toUpperCase()] = value;
        }
        // TODO: add Windows support (use `cross-spawn`?)
        const child = (0, child_process_1.spawn)('/bin/sh', ['-c', authenticate], {
            env,
            stdio: ['ignore', 'inherit', 'inherit'],
        });
        const [code, signal] = await (0, events_1.once)(child, 'exit');
        debug('authentication child process "exit" event: %s %s', code, signal);
        return code === 0;
    };
}
proxy.listen(port, function () {
    console.log('HTTP(s) proxy server listening on port %d', 
    // @ts-expect-error "port" is a number
    proxy.address().port);
});
//# sourceMappingURL=proxy.js.map