From c40bb0e4bd4cbbe41d05845ea574f9ceeb90bb9e Mon Sep 17 00:00:00 2001 From: Fionera Date: Wed, 13 Oct 2021 15:29:17 +0200 Subject: [PATCH] feat: Add support for HTTP-Proxy-Authorization --- index.d.ts | 4 ++-- lib/util/proxy.js | 8 +++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index 331b8f2d..38e0d220 100644 --- a/index.d.ts +++ b/index.d.ts @@ -61,7 +61,7 @@ export interface ProviderOptions { /** * Connect through an HTTP proxy */ - proxy?: { host: string, port: number|string } + proxy?: { host: string, port: number|string, user?: string, pass?: string } } export interface MultiProviderOptions extends ProviderOptions { @@ -154,7 +154,7 @@ export class MultiProvider extends EventEmitter { * Set an info logger, and optionally an errorLogger to separately log errors. * * In order to log, these functions must have a property '.enabled' that is true. - * (The default logger uses the npm 'debug' module which sets '.enabled' + * (The default logger uses the npm 'debug' module which sets '.enabled' * based on the DEBUG environment variable) */ setLogger(logger: (msg: string) => void, errorLogger?: (msg: string) => void): Promise; diff --git a/lib/util/proxy.js b/lib/util/proxy.js index 6fb72d15..ad769087 100644 --- a/lib/util/proxy.js +++ b/lib/util/proxy.js @@ -4,12 +4,18 @@ const http = require("http"); module.exports = function createProxySocket(proxy, target) { return new Promise((resolve, reject) => { + let headers = { Connection: "Keep-Alive" }; + if (proxy.user && proxy.pass) { + const token = Buffer.from(`${proxy.user}:${proxy.pass}`).toString('base64') + headers["Proxy-Authorization"] = `Basic ${token}` + } + const req = http.request({ host: proxy.host, port: proxy.port, method: "connect", path: target.host + ":" + target.port, - headers: { Connection: "Keep-Alive" }, + headers: headers, }); req.on("error", reject); req.on("connect", (res, socket, head) => {