-
Notifications
You must be signed in to change notification settings - Fork 101
/
api-model.ts
238 lines (197 loc) · 7.66 KB
/
api-model.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import * as _ from 'lodash';
import * as os from 'os';
import { ErrorLike, delay } from '@httptoolkit/util';
import { generateSPKIFingerprint } from 'mockttp';
import { getSystemProxy } from 'os-proxy-config';
import { SERVER_VERSION } from "../constants";
import { logError, addBreadcrumb } from '../error-tracking';
import { HtkConfig } from "../config";
import { ActivationError, Interceptor } from "../interceptors";
import { getDnsServer } from '../dns-server';
import { getCertExpiry, parseCert } from '../certificates';
import * as Client from '../client/client-types';
import { HttpClient } from '../client/http-client';
const INTERCEPTOR_TIMEOUT = 1000;
export class ApiModel {
constructor(
private config: HtkConfig,
private interceptors: _.Dictionary<Interceptor>,
private getRuleParamKeys: () => string[],
private httpClient: HttpClient,
private callbacks: {
onTriggerUpdate: () => void,
onTriggerShutdown: () => void
}
) {}
getVersion() {
return SERVER_VERSION;
}
updateServer() {
this.callbacks.onTriggerUpdate();
}
// On Windows, there's no clean way to send signals between processes to trigger graceful
// shutdown. To handle that, we use HTTP from the desktop shell, instead of inter-process
// signals. This completely shuts down the server, not just a single proxy endpoint, and
// should only be called once the app is fully exiting.
shutdownServer() {
this.callbacks.onTriggerShutdown();
}
async getConfig(proxyPort?: number) {
// Wait for each async part in parallel:
const [
systemProxy,
dnsServers
] = await Promise.all([
withFallback(
() => getSystemProxy(),
2000,
undefined
),
proxyPort
? await this.getDnsServers(proxyPort)
: []
])
return {
certificatePath: this.config.https.certPath,
certificateContent: this.config.https.certContent,
certificateExpiry: getCertExpiry(
parseCert(this.config.https.certContent)
),
// We could calculate this client side, but it requires node-forge or some
// other heavyweight crypto lib, and we already have that here, so it's
// convenient to do it up front.
certificateFingerprint: generateSPKIFingerprint(this.config.https.certContent),
networkInterfaces: this.getNetworkInterfaces(),
systemProxy,
dnsServers,
ruleParameterKeys: this.getRuleParamKeys()
};
}
// Seperate purely to support the GQL API resolver structure
getDnsServers(proxyPort: number) {
return withFallback(async () => {
const dnsServer = await getDnsServer(proxyPort);
return [`127.0.0.1:${dnsServer.address().port}`];
}, 2000, []);
}
getNetworkInterfaces() {
return os.networkInterfaces();
}
getInterceptors(proxyPort?: number) {
return Promise.all(
Object.keys(this.interceptors).map((key) => {
return this.getInterceptor(key, { metadataType: 'summary', proxyPort });
})
);
}
async getInterceptor(id: string, options: {
metadataType?: 'summary' | 'detailed',
proxyPort?: number
} = {}) {
const interceptor = this.interceptors[id];
// Wait for each async part in parallel:
const [
metadata,
isActivable,
isActive
] = await Promise.all([
options.metadataType
? this.getInterceptorMetadata(id, options.metadataType)
: undefined,
withFallback(
async () => interceptor.isActivable(),
interceptor.activableTimeout || INTERCEPTOR_TIMEOUT,
false
),
options.proxyPort
? this.isInterceptorActive(id, options.proxyPort)
: undefined
])
return {
id: interceptor.id,
version: interceptor.version,
metadata,
isActivable,
isActive
};
}
// Seperate purely to support the GQL API resolver structure
async isInterceptorActive(id: string, proxyPort: number) {
const interceptor = this.interceptors[id];
return await withFallback(
async () => proxyPort
? interceptor.isActive(proxyPort)
: undefined,
INTERCEPTOR_TIMEOUT,
false
);
}
async getInterceptorMetadata(id: string, metadataType: 'summary' | 'detailed', subId?: string) {
const interceptor = this.interceptors[id];
const metadataTimeout = metadataType === 'summary'
? INTERCEPTOR_TIMEOUT
: INTERCEPTOR_TIMEOUT * 10; // Longer timeout for detailed metadata
return withFallback(
async () => subId
? interceptor.getSubMetadata?.(subId)
: interceptor.getMetadata?.(metadataType),
metadataTimeout,
undefined
)
}
async activateInterceptor(id: string, proxyPort: number, options: unknown) {
addBreadcrumb(`Activating ${id}`, { category: 'interceptor', data: { id, options } });
const interceptor = this.interceptors[id];
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
// After 30s, don't stop activating, but report an error if we're not done yet
let activationDone = false;
delay(30000, { unref: true }).then(() => {
if (!activationDone) logError(`Timeout activating ${id}`)
});
try {
const result = await interceptor.activate(proxyPort, options);
activationDone = true;
addBreadcrumb(`Successfully activated ${id}`, { category: 'interceptor' });
return { success: true, metadata: result };
} catch (err: any) {
const activationError = err as ActivationError;
activationDone = true;
if (activationError.reportable !== false) {
addBreadcrumb(`Failed to activate ${id}`, { category: 'interceptor' });
throw err;
}
// Non-reportable errors are friendly ones (like Global Chrome quit confirmation)
// that need to be returned nicely to the UI for further processing.
return {
success: false,
metadata: activationError.metadata
};
}
}
async deactivateInterceptor(id: string, proxyPort: number, options: unknown) {
const interceptor = this.interceptors[id];
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
await interceptor.deactivate(proxyPort, options).catch(logError);
return { success: !interceptor.isActive(proxyPort) };
}
sendRequest(
requestDefinition: Client.RequestDefinition,
requestOptions: Client.RequestOptions
) {
return this.httpClient.sendRequest(requestDefinition, requestOptions);
}
}
const serializeError = (error: ErrorLike): {} => ({
message: error.message,
code: error.code,
cause: error.cause ? serializeError(error.cause) : undefined
});
// Wait for a promise, falling back to defaultValue on error or timeout
const withFallback = <R>(p: () => Promise<R>, timeoutMs: number, defaultValue: R) =>
Promise.race([
p().catch((error) => {
logError(error);
return defaultValue;
}),
delay(timeoutMs).then(() => defaultValue)
]);