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

Add support for custom dns lookup #175

Merged
merged 4 commits into from
May 2, 2022
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
5 changes: 5 additions & 0 deletions src/chain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import http from 'http';
import dns from 'dns';
import { URL } from 'url';
import { EventEmitter } from 'events';
import { Buffer } from 'buffer';
Expand All @@ -22,11 +23,13 @@ interface Options {
headers: string[];
path?: string;
localAddress?: string;
lookup?: typeof dns['lookup'];
}

export interface HandlerOpts {
upstreamProxyUrlParsed: URL;
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
}

interface ChainOpts {
Expand All @@ -37,6 +40,7 @@ interface ChainOpts {
server: EventEmitter & { log: (...args: any[]) => void; };
isPlain: boolean;
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
}

/**
Expand Down Expand Up @@ -70,6 +74,7 @@ export const chain = (
request.url!,
],
localAddress: handlerOpts.localAddress,
lookup: handlerOpts.dnsLookup,
};

if (proxy.username || proxy.password) {
Expand Down
3 changes: 3 additions & 0 deletions src/direct.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net from 'net';
import dns from 'dns';
import { Buffer } from 'buffer';
import { URL } from 'url';
import { EventEmitter } from 'events';
Expand All @@ -7,6 +8,7 @@ import { Socket } from './socket';

export interface HandlerOpts {
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
}

interface DirectOpts {
Expand Down Expand Up @@ -49,6 +51,7 @@ export const direct = (
port: Number(url.port),
host: url.hostname,
localAddress: handlerOpts.localAddress,
lookup: handlerOpts.dnsLookup,
};

if (options.host[0] === '[') {
Expand Down
4 changes: 4 additions & 0 deletions src/forward.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dns from 'dns';
import http from 'http';
import https from 'https';
import stream from 'stream';
Expand All @@ -15,11 +16,13 @@ interface Options {
insecureHTTPParser: boolean;
path?: string;
localAddress?: string;
lookup?: typeof dns['lookup'];
}

export interface HandlerOpts {
upstreamProxyUrlParsed: URL;
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
}

/**
Expand Down Expand Up @@ -53,6 +56,7 @@ export const forward = async (
headers: validHeadersOnly(request.rawHeaders),
insecureHTTPParser: true,
localAddress: handlerOpts.localAddress,
lookup: handlerOpts.dnsLookup,
};

// In case of proxy the path needs to be an absolute URL
Expand Down
4 changes: 4 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import net from 'net';
import dns from 'dns';
import http from 'http';
import util from 'util';
import { URL } from 'url';
Expand Down Expand Up @@ -46,6 +47,7 @@ type HandlerOpts = {
isHttp: boolean;
customResponseFunction: CustomResponseOpts['customResponseFunction'] | null;
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
};

export type PrepareRequestFunctionOpts = {
Expand All @@ -64,6 +66,7 @@ export type PrepareRequestFunctionResult = {
failMsg?: string;
upstreamProxyUrl?: string | null;
localAddress?: string;
dnsLookup?: typeof dns['lookup'];
};

type Promisable<T> = T | Promise<T>;
Expand Down Expand Up @@ -397,6 +400,7 @@ export class Server extends EventEmitter {
const funcResult = await this.callPrepareRequestFunction(request, handlerOpts);

handlerOpts.localAddress = funcResult.localAddress;
handlerOpts.dnsLookup = funcResult.dnsLookup;

// If not authenticated, request client to authenticate
if (funcResult.requestAuthentication) {
Expand Down