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: log CONNECT error response body #139

Merged
merged 3 commits into from
May 22, 2024
Merged
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
214 changes: 108 additions & 106 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"types": "./dist/index.d.ts",
"type": "module",
"dependencies": {
"got": "^13.0.0",
"got": "^14.2.1",
"header-generator": "^2.1.41",
"http2-wrapper": "^2.2.0",
"mimic-response": "^4.0.0",
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ export async function proxyHook(options: Options): Promise<void> {
}
}

export class ProxyError extends Error {}

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

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

Expand Down
5 changes: 4 additions & 1 deletion src/resolve-protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { URL } from 'node:url';
import { type Headers } from 'got';
import { auto, type ResolveProtocolConnectFunction, type ResolveProtocolFunction } from 'http2-wrapper';
import QuickLRU from 'quick-lru';
import { ProxyError } from './hooks/proxy.js';

const connect = async (proxyUrl: string, options: tls.ConnectionOptions, callback: () => void) => new Promise<TLSSocket>((resolve, reject) => {
let host = `${options.host}:${options.port}`;
Expand Down Expand Up @@ -42,8 +43,10 @@ const connect = async (proxyUrl: string, options: tls.ConnectionOptions, callbac

request.once('connect', (response, socket, head) => {
if (response.statusCode !== 200 || head.length > 0) {
reject(new Error(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes`));
reject(new ProxyError(`Proxy responded with ${response.statusCode} ${response.statusMessage}: ${head.length} bytes.

Below is the first 100 bytes of the proxy response body:
${head.toString('utf8', 0, 100)}`, { cause: head.toString('utf8') }));
socket.destroy();
return;
}
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"target": "ES2022",
"verbatimModuleSyntax": true,
"incremental": false,
"lib": ["DOM", "ESNext"],
},
"include": [
"src"
Expand Down
Loading