Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
TooTallNate committed May 5, 2023
1 parent 59c6fbe commit f4788c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 60 deletions.
16 changes: 5 additions & 11 deletions packages/http-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ interface HttpProxyAgentClientRequest extends http.ClientRequest {
_implicitHeader(): void;
}

function isHTTPS(protocol?: string | null): boolean {
return typeof protocol === 'string' ? /^https:?$/i.test(protocol) : false;
}

/**
* The `HttpProxyAgent` implements an HTTP Agent subclass that connects
* to the specified "HTTP proxy server" in order to proxy HTTP requests.
Expand All @@ -45,10 +41,6 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
readonly proxy: URL;
connectOpts: net.TcpNetConnectOpts & tls.ConnectionOptions;

get secureProxy() {
return isHTTPS(this.proxy.protocol);
}

constructor(proxy: Uri | URL, opts?: HttpProxyAgentOptions<Uri>) {
super(opts);
this.proxy = typeof proxy === 'string' ? new URL(proxy) : proxy;
Expand All @@ -61,7 +53,7 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
);
const port = this.proxy.port
? parseInt(this.proxy.port, 10)
: this.secureProxy
: this.proxy.protocol === 'https:'
? 443
: 80;
this.connectOpts = {
Expand All @@ -76,6 +68,8 @@ export class HttpProxyAgent<Uri extends string> extends Agent {
opts: AgentConnectOpts
): Promise<net.Socket> {
const { proxy } = this;
// @ts-expect-error asdf
console.log(this.getName(opts));

const protocol = opts.secureEndpoint ? 'https:' : 'http:';
const hostname = req.getHeader('host') || 'localhost';
Expand All @@ -87,7 +81,7 @@ export class HttpProxyAgent<Uri extends string> extends Agent {

// Change the `http.ClientRequest` instance's "path" field
// to the absolute path of the URL that will be requested.
req.path = String(url);
req.path = url.href;

// Inject the `Proxy-Authorization` header if necessary.
req._header = null;
Expand All @@ -110,7 +104,7 @@ export class HttpProxyAgent<Uri extends string> extends Agent {

// Create a socket connection to the proxy server.
let socket: net.Socket;
if (this.secureProxy) {
if (proxy.protocol === 'https:') {
debug('Creating `tls.Socket`: %o', this.connectOpts);
socket = tls.connect(this.connectOpts);
} else {
Expand Down
14 changes: 0 additions & 14 deletions packages/http-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,6 @@ describe('HttpProxyAgent', () => {
const agent = new HttpProxyAgent(proxyUrl);
assert.equal(80, agent.defaultPort);
});
describe('secureProxy', () => {
it('should be `false` when "http:" protocol is used', () => {
const agent = new HttpProxyAgent(
`http://127.0.0.1:${proxyUrl.port}`
);
assert.equal(false, agent.secureProxy);
});
it('should be `true` when "https:" protocol is used', () => {
const agent = new HttpProxyAgent(
`https://127.0.0.1:${proxyUrl.port}`
);
assert.equal(true, agent.secureProxy);
});
});
});

describe('"http" module', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/https-proxy-agent/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ describe('HttpsProxyAgent', () => {
assert.equal(proxyUrl.hostname, agent.proxy.hostname);
assert.equal(proxyUrl.port, agent.proxy.port);
});
describe('secureProxy', () => {
it('should be `false` when "http:" protocol is used', () => {
const agent = new HttpsProxyAgent(proxyUrl);
assert.equal(false, agent.secureProxy);
});
it('should be `true` when "https:" protocol is used', () => {
const agent = new HttpsProxyAgent(sslProxyUrl);
assert.equal(true, agent.secureProxy);
});
});
});

describe('"http" module', () => {
Expand Down
42 changes: 7 additions & 35 deletions packages/pac-proxy-agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as crypto from 'crypto';
import { once } from 'events';
import createDebug from 'debug';
import { Readable } from 'stream';
import { format } from 'url';
import { Agent, AgentConnectOpts, toBuffer } from 'agent-base';
import { HttpProxyAgent, HttpProxyAgentOptions } from 'http-proxy-agent';
import { HttpsProxyAgent, HttpsProxyAgentOptions } from 'https-proxy-agent';
Expand Down Expand Up @@ -177,43 +176,16 @@ export class PacProxyAgent<Uri extends string> extends Agent {
): Promise<http.Agent | net.Socket> {
const { secureEndpoint } = opts;

// Calculate the `url` parameter
const protocol = secureEndpoint ? 'https:' : 'http:';
const host = req.getHeader('host');
const url = new URL(req.path, `${protocol}//${host}`);
debug('url: %o', url.href);

// First, get a generated `FindProxyForURL()` function,
// either cached or retrieved from the source
const resolver = await this.getResolver();

// Calculate the `url` parameter
const defaultPort = secureEndpoint ? 443 : 80;
let path = req.path;
let search: string | null = null;
const firstQuestion = path.indexOf('?');
if (firstQuestion !== -1) {
search = path.substring(firstQuestion);
path = path.substring(0, firstQuestion);
}

const urlOpts = {
...opts,
protocol: secureEndpoint ? 'https:' : 'http:',
pathname: path,
search,

// need to use `hostname` instead of `host` otherwise `port` is ignored
hostname: opts.host,
host: null,
href: null,

// set `port` to null when it is the protocol default port (80 / 443)
port: defaultPort === opts.port ? null : opts.port,
};
const url = format(urlOpts);

debug('url: %o', url);
let result = await resolver(url);

// Default to "DIRECT" if a falsey value was returned (or nothing)
if (!result) {
result = 'DIRECT';
}
const result = (await resolver(url)) || 'DIRECT';

const proxies = String(result)
.trim()
Expand Down

0 comments on commit f4788c2

Please sign in to comment.