Skip to content

Commit f4a9898

Browse files
committed
Refactor passthrough HTTP+WS conn options into a shared type
1 parent b2b7328 commit f4a9898

File tree

6 files changed

+127
-160
lines changed

6 files changed

+127
-160
lines changed

src/main.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export type {
5656
ProxySettingCallback,
5757
ProxySettingCallbackParams
5858
} from './rules/proxy-config';
59+
export type {
60+
ForwardingOptions,
61+
PassThroughLookupOptions,
62+
PassThroughHandlerConnectionOptions
63+
} from './rules/passthrough-handling-definitions';
5964

6065
export type { RequestRuleBuilder } from "./rules/requests/request-rule-builder";
6166
export type { WebSocketRuleBuilder } from "./rules/websockets/websocket-rule-builder";
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { ProxyConfig } from "./proxy-config";
2+
3+
export interface ForwardingOptions {
4+
targetHost: string,
5+
// Should the host (H1) or :authority (H2) header be updated to match?
6+
updateHostHeader?: true | false | string // Change automatically/ignore/change to custom value
7+
}
8+
9+
export interface PassThroughLookupOptions {
10+
/**
11+
* The maximum time to cache a DNS response. Up to this limit,
12+
* responses will be cached according to their own TTL. Defaults
13+
* to Infinity.
14+
*/
15+
maxTtl?: number;
16+
/**
17+
* How long to cache a DNS ENODATA or ENOTFOUND response. Defaults
18+
* to 0.15.
19+
*/
20+
errorTtl?: number;
21+
/**
22+
* The primary servers to use. DNS queries will be resolved against
23+
* these servers first. If no data is available, queries will fall
24+
* back to dns.lookup, and use the OS's default DNS servers.
25+
*
26+
* This defaults to dns.getServers().
27+
*/
28+
servers?: string[];
29+
}
30+
31+
/**
32+
* This defines the upstream connection parameters. These passthrough parameters
33+
* are shared between both WebSocket & Request passthrough rules.
34+
*/
35+
export interface PassThroughHandlerConnectionOptions {
36+
/**
37+
* The forwarding configuration for the passthrough rule.
38+
* This generally shouldn't be used explicitly unless you're
39+
* building rule data by hand. Instead, call `thenPassThrough`
40+
* to send data directly or `thenForwardTo` with options to
41+
* configure traffic forwarding.
42+
*/
43+
forwarding?: ForwardingOptions,
44+
45+
/**
46+
* A list of hostnames for which server certificate and TLS version errors
47+
* should be ignored (none, by default).
48+
*
49+
* If set to 'true', HTTPS errors will be ignored for all hosts. WARNING:
50+
* Use this at your own risk. Setting this to `true` can open your
51+
* application to MITM attacks and should never be used over any network
52+
* that is not completed trusted end-to-end.
53+
*/
54+
ignoreHostHttpsErrors?: string[] | boolean;
55+
56+
/**
57+
* An array of additional certificates, which should be trusted as certificate
58+
* authorities for upstream hosts, in addition to Node.js's built-in certificate
59+
* authorities.
60+
*
61+
* Each certificate should be an object with either a `cert` key and a string
62+
* or buffer value containing the PEM certificate, or a `certPath` key and a
63+
* string value containing the local path to the PEM certificate.
64+
*/
65+
trustAdditionalCAs?: Array<{ cert: string | Buffer } | { certPath: string }>;
66+
67+
/**
68+
* A mapping of hosts to client certificates to use, in the form of
69+
* `{ key, cert }` objects (none, by default)
70+
*/
71+
clientCertificateHostMap?: {
72+
[host: string]: { pfx: Buffer, passphrase?: string }
73+
};
74+
75+
/**
76+
* Upstream proxy configuration: pass through requests via this proxy.
77+
*
78+
* If this is undefined, no proxy will be used. To configure a proxy
79+
* provide either:
80+
* - a ProxySettings object
81+
* - a callback which will be called with an object containing the
82+
* hostname, and must return a ProxySettings object or undefined.
83+
* - an array of ProxySettings or callbacks. The array will be
84+
* processed in order, and the first not-undefined ProxySettings
85+
* found will be used.
86+
*
87+
* When using a remote client, this parameter or individual array
88+
* values may be passed by reference, using the name of a rule
89+
* parameter configured in the admin server.
90+
*/
91+
proxyConfig?: ProxyConfig;
92+
93+
/**
94+
* Custom DNS options, to allow configuration of the resolver used
95+
* when forwarding requests upstream. Passing any option switches
96+
* from using node's default dns.lookup function to using the
97+
* cacheable-lookup module, which will cache responses.
98+
*/
99+
lookupOptions?: PassThroughLookupOptions;
100+
}

src/rules/passthrough-handling.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import { areFFDHECurvesSupported } from '../util/openssl-compat';
1414

1515
import {
1616
CallbackRequestResult,
17-
CallbackResponseMessageResult,
18-
PassThroughLookupOptions
17+
CallbackResponseMessageResult
1918
} from './requests/request-handler-definitions';
19+
import {
20+
PassThroughLookupOptions
21+
} from './passthrough-handling-definitions';
2022

2123
// TLS settings for proxied connections, intended to avoid TLS fingerprint blocking
2224
// issues so far as possible, by closely emulating a Firefox Client Hello:

src/rules/requests/request-handler-definitions.ts

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ import {
2828
withSerializedCallbackBuffers
2929
} from '../../serialization/body-serialization';
3030
import { ProxyConfig } from '../proxy-config';
31+
import {
32+
ForwardingOptions,
33+
PassThroughHandlerConnectionOptions,
34+
PassThroughLookupOptions
35+
} from '../passthrough-handling-definitions';
3136

3237
/*
3338
This file defines request handler *definitions*, which includes everything necessary to define
@@ -446,100 +451,7 @@ export interface PassThroughResponse {
446451
body: CompletedBody;
447452
}
448453

449-
export interface ForwardingOptions {
450-
targetHost: string,
451-
// Should the host (H1) or :authority (H2) header be updated to match?
452-
updateHostHeader?: true | false | string // Change automatically/ignore/change to custom value
453-
}
454-
455-
export interface PassThroughLookupOptions {
456-
/**
457-
* The maximum time to cache a DNS response. Up to this limit,
458-
* responses will be cached according to their own TTL. Defaults
459-
* to Infinity.
460-
*/
461-
maxTtl?: number;
462-
/**
463-
* How long to cache a DNS ENODATA or ENOTFOUND response. Defaults
464-
* to 0.15.
465-
*/
466-
errorTtl?: number;
467-
/**
468-
* The primary servers to use. DNS queries will be resolved against
469-
* these servers first. If no data is available, queries will fall
470-
* back to dns.lookup, and use the OS's default DNS servers.
471-
*
472-
* This defaults to dns.getServers().
473-
*/
474-
servers?: string[];
475-
}
476-
477-
export interface PassThroughHandlerOptions {
478-
/**
479-
* The forwarding configuration for the passthrough rule.
480-
* This generally shouldn't be used explicitly unless you're
481-
* building rule data by hand. Instead, call `thenPassThrough`
482-
* to send data directly or `thenForwardTo` with options to
483-
* configure traffic forwarding.
484-
*/
485-
forwarding?: ForwardingOptions,
486-
487-
/**
488-
* A list of hostnames for which server certificate and TLS version errors
489-
* should be ignored (none, by default).
490-
*
491-
* If set to 'true', HTTPS errors will be ignored for all hosts. WARNING:
492-
* Use this at your own risk. Setting this to `true` can open your
493-
* application to MITM attacks and should never be used over any network
494-
* that is not completed trusted end-to-end.
495-
*/
496-
ignoreHostHttpsErrors?: string[] | boolean;
497-
498-
/**
499-
* An array of additional certificates, which should be trusted as certificate
500-
* authorities for upstream hosts, in addition to Node.js's built-in certificate
501-
* authorities.
502-
*
503-
* Each certificate should be an object with either a `cert` key and a string
504-
* or buffer value containing the PEM certificate, or a `certPath` key and a
505-
* string value containing the local path to the PEM certificate.
506-
*/
507-
trustAdditionalCAs?: Array<{ cert: string | Buffer } | { certPath: string }>;
508-
509-
/**
510-
* A mapping of hosts to client certificates to use, in the form of
511-
* `{ key, cert }` objects (none, by default)
512-
*/
513-
clientCertificateHostMap?: {
514-
[host: string]: { pfx: Buffer, passphrase?: string }
515-
};
516-
517-
/**
518-
* Upstream proxy configuration: pass through requests via this proxy.
519-
*
520-
* If this is undefined, no proxy will be used. To configure a proxy
521-
* provide either:
522-
* - a ProxySettings object
523-
* - a callback which will be called with an object containing the
524-
* hostname, and must return a ProxySettings object or undefined.
525-
* - an array of ProxySettings or callbacks. The array will be
526-
* processed in order, and the first not-undefined ProxySettings
527-
* found will be used.
528-
*
529-
* When using a remote client, this parameter or individual array
530-
* values may be passed by reference, using the name of a rule
531-
* parameter configured in the admin server.
532-
*/
533-
proxyConfig?: ProxyConfig;
534-
535-
/**
536-
* Custom DNS options, to allow configuration of the resolver used
537-
* when forwarding requests upstream. Passing any option switches
538-
* from using node's default dns.lookup function to using the
539-
* cacheable-lookup module, which will cache responses.
540-
*/
541-
lookupOptions?: PassThroughLookupOptions;
542-
454+
export interface PassThroughHandlerOptions extends PassThroughHandlerConnectionOptions {
543455
/**
544456
* Whether to simulate connection errors back to the client.
545457
*

src/rules/requests/request-handlers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ import { assertParamDereferenced, RuleParameters } from '../rule-parameters';
6767

6868
import { getAgent } from '../http-agents';
6969
import { ProxySettingSource } from '../proxy-config';
70+
import {
71+
ForwardingOptions,
72+
PassThroughLookupOptions,
73+
} from '../passthrough-handling-definitions';
7074
import {
7175
getContentLengthAfterModification,
7276
getHostAfterModification,
@@ -89,12 +93,10 @@ import {
8993
CallbackResponseResult,
9094
CloseConnectionHandlerDefinition,
9195
FileHandlerDefinition,
92-
ForwardingOptions,
9396
HandlerDefinitionLookup,
9497
JsonRpcResponseHandlerDefinition,
9598
PassThroughHandlerDefinition,
9699
PassThroughHandlerOptions,
97-
PassThroughLookupOptions,
98100
PassThroughResponse,
99101
RequestHandlerDefinition,
100102
RequestTransform,

src/rules/websockets/websocket-handler-definitions.ts

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import {
1111

1212
import { Explainable, Headers } from "../../types";
1313

14+
import { ProxyConfig } from '../proxy-config';
1415
import {
15-
CloseConnectionHandlerDefinition,
16-
ResetConnectionHandlerDefinition,
17-
TimeoutHandlerDefinition,
16+
PassThroughHandlerConnectionOptions,
1817
ForwardingOptions,
1918
PassThroughLookupOptions
19+
} from '../passthrough-handling-definitions';
20+
import {
21+
CloseConnectionHandlerDefinition,
22+
ResetConnectionHandlerDefinition,
23+
TimeoutHandlerDefinition
2024
} from '../requests/request-handler-definitions';
21-
import { ProxyConfig } from '../proxy-config';
2225

2326
/*
2427
This file defines websocket handler *definitions*, which includes everything necessary to define
@@ -37,64 +40,7 @@ export interface WebSocketHandlerDefinition extends Explainable, Serializable {
3740
type: keyof typeof WsHandlerDefinitionLookup;
3841
}
3942

40-
export interface PassThroughWebSocketHandlerOptions {
41-
/**
42-
* The forwarding configuration for the passthrough rule.
43-
* This generally shouldn't be used explicitly unless you're
44-
* building rule data by hand. Instead, call `thenPassThrough`
45-
* to send data directly or `thenForwardTo` with options to
46-
* configure traffic forwarding.
47-
*/
48-
forwarding?: ForwardingOptions,
49-
50-
/**
51-
* A list of hostnames for which server certificate and TLS version errors
52-
* should be ignored (none, by default).
53-
*
54-
* If set to 'true', HTTPS errors will be ignored for all hosts. WARNING:
55-
* Use this at your own risk. Setting this to `true` can open your
56-
* application to MITM attacks and should never be used over any network
57-
* that is not completed trusted end-to-end.
58-
*/
59-
ignoreHostHttpsErrors?: string[] | boolean;
60-
61-
/**
62-
* An array of additional certificates, which should be trusted as certificate
63-
* authorities for upstream hosts, in addition to Node.js's built-in certificate
64-
* authorities.
65-
*
66-
* Each certificate should be an object with either a `cert` key and a string
67-
* or buffer value containing the PEM certificate, or a `certPath` key and a
68-
* string value containing the local path to the PEM certificate.
69-
*/
70-
trustAdditionalCAs?: Array<{ cert: string | Buffer } | { certPath: string }>;
71-
72-
/**
73-
* Upstream proxy configuration: pass through websockets via this proxy.
74-
*
75-
* If this is undefined, no proxy will be used. To configure a proxy
76-
* provide either:
77-
* - a ProxySettings object
78-
* - a callback which will be called with an object containing the
79-
* hostname, and must return a ProxySettings object or undefined.
80-
* - an array of ProxySettings or callbacks. The array will be
81-
* processed in order, and the first not-undefined ProxySettings
82-
* found will be used.
83-
*
84-
* When using a remote client, this parameter or individual array
85-
* values may be passed by reference, using the name of a rule
86-
* parameter configured in the admin server.
87-
*/
88-
proxyConfig?: ProxyConfig;
89-
90-
/**
91-
* Custom DNS options, to allow configuration of the resolver used
92-
* when forwarding requests upstream. Passing any option switches
93-
* from using node's default dns.lookup function to using the
94-
* cacheable-lookup module, which will cache responses.
95-
*/
96-
lookupOptions?: PassThroughLookupOptions;
97-
}
43+
export type PassThroughWebSocketHandlerOptions = PassThroughHandlerConnectionOptions;
9844

9945
/**
10046
* @internal

0 commit comments

Comments
 (0)