Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/socks #103

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,23 @@ dist
node_modules
coverage
package-lock.json

# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
Sessionx.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"mimic-response": "^3.1.0",
"ow": "^0.28.1",
"quick-lru": "^5.1.1",
"socks": "^2.7.1",
"socks-proxy-agent": "^8.0.2",
"tslib": "^2.4.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/agent/h1-proxy-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import http, { ClientRequest, ClientRequestArgs } from 'http';
import https from 'https';

interface AgentOptions extends http.AgentOptions {
export interface AgentOptions extends http.AgentOptions {
proxy: string | URL;
disableConnect?: boolean;
}

const initialize = (self: http.Agent & { proxy: URL }, options: AgentOptions) => {
export const initialize = (self: { proxy: URL }, options: AgentOptions) => {

Check warning on line 13 in src/agent/h1-proxy-agent.ts

View workflow job for this annotation

GitHub Actions / Lint

Missing return type on function
self.proxy = typeof options.proxy === 'string' ? new URL(options.proxy) : options.proxy;
};

Expand Down Expand Up @@ -138,7 +138,7 @@
return;
}

if ((options as any).protocol === 'https:') {

Check warning on line 141 in src/agent/h1-proxy-agent.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
callback(undefined, tls.connect({
...options,
socket,
Expand Down
11 changes: 11 additions & 0 deletions src/agent/socks/http-socks-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SocksProxyAgent } from 'socks-proxy-agent';

export class HttpSocksAgent extends SocksProxyAgent {
override get protocol(): string {
return 'http:';
}

override set protocol(value: string) {
super.protocol = value;
}
}
11 changes: 11 additions & 0 deletions src/agent/socks/https-socks-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SocksProxyAgent } from 'socks-proxy-agent';

export class HttpsSocksAgent extends SocksProxyAgent {
override get protocol(): string {
return 'https:';
}

override set protocol(value: string) {
super.protocol = value;
}
}
46 changes: 46 additions & 0 deletions src/agent/socks/socks-http2-agent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import http2, { Agent } from 'http2-wrapper';
import tls from 'tls';
import { URL } from 'url';

import { SocksClient } from 'socks';

export class SocksHttp2Agent extends Agent {
proxy: URL;

constructor(options: {
proxyOptions: {
url: URL,
rejectUnauthorized: boolean
},
maxFreeSockets: number,
maxEmptySessions: number,
}) {
super(options);
this.proxy = options.proxyOptions.url;
}

override async createConnection(origin: URL, options: http2.SecureClientSessionOptions): Promise<tls.TLSSocket> {
const port = Number(origin.port) || 443;
const host = origin.hostname;
const { socket } = await SocksClient.createConnection({
proxy: {
host: this.proxy.hostname,
port: parseInt(this.proxy.port, 10),
password: decodeURIComponent(this.proxy.password),
// determine type 4 or 5 based on protocol, may be different like socks4, socks5, socks5h, socks
type: this.proxy.protocol.includes('4') ? 4 : 5,
},
command: 'connect',
destination: {
host,
port,
},
existing_socket: options.socket,
timeout: options.timeout,
});
return super.createConnection(origin, {
...options,
socket,
});
}
}
4 changes: 1 addition & 3 deletions src/hooks/fix-decompress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH,
};

const useDecompressor = (decompressor: Transform) => {
delete response.headers['content-encoding'];

Expand Down Expand Up @@ -71,8 +70,8 @@
// @ts-expect-error Looks like a TypeScript bug
result.on('request', (request: ClientRequest) => {
const emit = request.emit.bind(request);

request.emit = (event: string, ...args: unknown[]) => {

Check failure on line 73 in src/hooks/fix-decompress.ts

View workflow job for this annotation

GitHub Actions / Lint

Block must not be padded by blank lines

// It won't double decompress, because Got checks the content-encoding header.
// We delete it if the response is compressed.
if (event === 'response' && options.decompress) {
Expand All @@ -86,7 +85,6 @@

return emitted;
}

return emit(event, ...args);
};
});
Expand Down
1 change: 0 additions & 1 deletion src/hooks/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export function http2Hook(options: Options): void {
if (proxyUrl) {
typedRequestOptions.resolveProtocol = createResolveProtocol(proxyUrl, sessionData as any);
}

return auto(url, typedRequestOptions, callback);
};
} else {
Expand Down
19 changes: 16 additions & 3 deletions src/hooks/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { proxies, auto } from 'http2-wrapper';
import { Agents, Options } from 'got-cjs';
import { HttpsProxyAgent, HttpRegularProxyAgent } from '../agent/h1-proxy-agent';
import { TransformHeadersAgent } from '../agent/transform-headers-agent';
import { SocksHttp2Agent } from '../agent/socks/socks-http2-agent';
import { HttpSocksAgent } from '../agent/socks/http-socks-agent';
import { HttpsSocksAgent } from '../agent/socks/https-socks-agent';

const {
HttpOverHttp2,
Expand All @@ -25,10 +28,10 @@ export async function proxyHook(options: Options): Promise<void> {
}

function validateProxyProtocol(protocol: string) {
const isSupported = protocol === 'http:' || protocol === 'https:';
const isSupported = protocol === 'http:' || protocol === 'https:' || protocol.startsWith('socks');

if (!isSupported) {
throw new Error(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP or HTTPS.`);
throw new Error(`Proxy URL protocol "${protocol}" is not supported. Please use HTTP, HTTPS or SOCKS family.`);
}
}

Expand Down Expand Up @@ -92,13 +95,23 @@ async function getAgents(parsedProxyUrl: URL, rejectUnauthorized: boolean) {
http2: new Http2OverHttps(wrapperOptions),
};
}
} else {
} else if (parsedProxyUrl.protocol === 'http:') {
// Upstream proxies hang up connections on CONNECT + unsecure HTTP
agent = {
http: new TransformHeadersAgent(new HttpRegularProxyAgent(nativeOptions)),
https: new TransformHeadersAgent(new HttpsProxyAgent(nativeOptions)),
http2: new Http2OverHttp(wrapperOptions),
};
} else {
agent = {
http: new TransformHeadersAgent(new HttpSocksAgent(nativeOptions.proxy, {
maxFreeSockets: nativeOptions.maxFreeSockets,
})),
https: new TransformHeadersAgent(new HttpsSocksAgent(nativeOptions.proxy, {
maxFreeSockets: nativeOptions.maxFreeSockets,
})),
http2: new SocksHttp2Agent(wrapperOptions),
};
}

return agent;
Expand Down
28 changes: 26 additions & 2 deletions src/resolve-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,32 @@ import { URL } from 'url';
import { Headers } from 'got-cjs';
import { auto, ResolveProtocolConnectFunction, ResolveProtocolFunction } from 'http2-wrapper';
import QuickLRU from 'quick-lru';
import { SocksClient } from 'socks';

const connectSocks = async (proxyUrl: string, options: tls.ConnectionOptions, callback: () => void) => {
const url = new URL(proxyUrl);
const { socket } = await SocksClient.createConnection({
proxy: {
host: url.hostname,
port: parseInt(url.port, 10),
password: decodeURIComponent(url.password),
type: url.protocol.includes('4') ? 4 : 5,
},
command: 'connect',
destination: {
host: options.host!,
port: options.port!,
},
existing_socket: options.socket,
timeout: options.timeout,
});
return tls.connect({
...options,
socket,
}, callback);
};

const connect = async (proxyUrl: string, options: tls.ConnectionOptions, callback: () => void) => new Promise<TLSSocket>((resolve, reject) => {
const connectHttp = async (proxyUrl: string, options: tls.ConnectionOptions, callback: () => void) => new Promise<TLSSocket>((resolve, reject) => {
let host = `${options.host}:${options.port}`;

if (isIPv6(options.host!)) {
Expand Down Expand Up @@ -85,7 +109,7 @@ export const createResolveProtocol = (proxyUrl: string, sessionData?: ProtocolCa
}

const connectWithProxy: ResolveProtocolConnectFunction = async (pOptions, pCallback) => {
return connect(proxyUrl, pOptions, pCallback);
return (proxyUrl.startsWith('http') ? connectHttp : connectSocks)(proxyUrl, pOptions, pCallback);
};

return auto.createResolveProtocol(
Expand Down
36 changes: 35 additions & 1 deletion test/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URL } from 'url';
import http2 from 'http2-wrapper';
import { SocksProxyAgent } from 'socks-proxy-agent';
import {
HttpsProxyAgent,
HttpRegularProxyAgent,
Expand Down Expand Up @@ -51,7 +52,7 @@ describe('Proxy', () => {

await expect(proxyHook(options))
.rejects
.toThrow(/is not supported. Please use HTTP or HTTPS./);
.toThrow(/is not supported/);
});

describe('agents', () => {
Expand Down Expand Up @@ -160,5 +161,38 @@ describe('Proxy', () => {
const { agent } = options;
expect(agent.http2).toBeInstanceOf(Http2OverHttp2);
});

test('should support http request over socks proxy', async () => {
options.context.proxyUrl = 'socks://localhost:8080';
jest.spyOn(http2.auto, 'resolveProtocol').mockResolvedValue({ alpnProtocol: 'http/1.1' });

await proxyHook(options);

const { agent } = options;
expect(agent.http).toBeInstanceOf(TransformHeadersAgent);
expect((agent.http as any).agent).toBeInstanceOf(SocksProxyAgent);
});

test('should support https request over socks proxy', async () => {
options.context.proxyUrl = 'socks://localhost:8080';
jest.spyOn(http2.auto, 'resolveProtocol').mockResolvedValue({ alpnProtocol: 'http/1.1' });

await proxyHook(options);

const { agent } = options;
expect(agent.http).toBeInstanceOf(TransformHeadersAgent);
expect((agent.http as any).agent).toBeInstanceOf(SocksProxyAgent);
});

test('should support http2 request over socks proxy', async () => {
options.context.proxyUrl = 'socks://localhost:8080';
jest.spyOn(http2.auto, 'resolveProtocol').mockResolvedValue({ alpnProtocol: 'h2' });

await proxyHook(options);

const { agent } = options;
expect(agent.http).toBeInstanceOf(TransformHeadersAgent);
expect((agent.http as any).agent).toBeInstanceOf(SocksProxyAgent);
});
});
});
Loading