Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const globalTunnel = require('global-tunnel-ng');
const settings = require('balena-settings-client');
let proxy;
try {
proxy = settings.get('proxy') || null;
} catch (error1) {
proxy = null;
}
// Init the tunnel even if the proxy is not configured
// because it can also get the proxy from the http(s)_proxy env var
// If that is not set as well the initialize will do nothing
globalTunnel.initialize(proxy);
// TODO: make this a feature of capitano https://github.com/balena-io/capitano/issues/48
(global as any).PROXY_CONFIG = globalTunnel.proxyConfig;
}
async action(params, options) {
normalizeUuidProp(params);
const proxyConfig = globalTunnel.proxyConfig;
const useProxy = !!proxyConfig && !options.noproxy;
const tunnelViaSocket = (
socket: net.Socket,
host: string,
port: number,
proxyAuth?: Buffer,
): Bluebird =>
new Bluebird((resolve, reject) => {
let tunnelProxyResponse = '';
socket.write(`CONNECT ${host}:${port} HTTP/1.0\r\n`);
if (proxyAuth != null) {
socket.write(
`Proxy-Authorization: Basic ${proxyAuth.toString('base64')}\r\n`,
);
}