This repository was archived by the owner on Jan 13, 2025. It is now read-only.
forked from httptoolkit/httptoolkit-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-server.ts
More file actions
359 lines (315 loc) · 12.8 KB
/
api-server.ts
File metadata and controls
359 lines (315 loc) · 12.8 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import _ from 'lodash';
import * as os from 'os';
import * as events from 'events';
import express from 'express';
import cors from 'cors';
import corsGate from 'cors-gate';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { GraphQLScalarType } from 'graphql';
import { graphqlHTTP } from 'express-graphql';
import gql from 'graphql-tag';
import { generateSPKIFingerprint } from 'mockttp';
import { getSystemProxy } from 'os-proxy-config';
import { HtkConfig } from './config';
import { reportError, addBreadcrumb } from './error-tracking';
import { buildInterceptors, Interceptor, ActivationError } from './interceptors';
import { ALLOWED_ORIGINS, SERVER_VERSION } from './constants';
import { delay } from './util/promise';
import { getDnsServer } from './dns-server';
import { shutdown } from './shutdown';
const ENABLE_PLAYGROUND = false;
/**
* This file contains the core server API, used by the UI to query
* machine state that isn't easily visible from the web (cert files,
* network interfaces), and to launch intercepted applications
* directly on this machine.
*
* This is a very powerful API! It's not far from remote code
* execution. Because of that, access is tightly controlled:
* - Only listens on 127.0.0.1
* - All requests must include an acceptable Origin header, i.e.
* no browsers requests except from a strict whitelist of valid
* origins. In prod, that's just app.pipe.tech.
* - Optionally (always set in the HTK app) requires an auth
* token with every request, provided by $HTK_SERVER_TOKEN or
* --token at startup.
*/
const typeDefs = gql`
type Query {
version: String!
config: InterceptionConfig!
interceptors: [Interceptor!]!
interceptor(id: ID!): Interceptor!
networkInterfaces: Json
systemProxy: Proxy
dnsServers(proxyPort: Int!): [String!]!
ruleParameterKeys: [String!]!
}
type Mutation {
activateInterceptor(
id: ID!,
proxyPort: Int!,
options: Json
): Json
deactivateInterceptor(
id: ID!,
proxyPort: Int!
): Boolean!
triggerUpdate: Void
shutdown: Void
}
type InterceptionConfig {
certificatePath: String!
certificateContent: String!
certificateFingerprint: String!
}
type Interceptor {
id: ID!
version: String!
metadata(type: MetadataType): Json
isActivable: Boolean!
isActive(proxyPort: Int!): Boolean!
}
type Proxy {
proxyUrl: String!
noProxy: [String!]
}
enum MetadataType {
SUMMARY,
DETAILED
}
scalar Json
scalar Error
scalar Void
`;
// 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) => {
reportError(error);
return defaultValue;
}),
delay(timeoutMs).then(() => defaultValue)
]);
const isActivationError = (value: any): value is ActivationError => _.isError(value);
const INTERCEPTOR_TIMEOUT = 1000;
const buildResolvers = (
config: HtkConfig,
interceptors: _.Dictionary<Interceptor>,
getRuleParamKeys: () => string[],
eventEmitter: events.EventEmitter
) => {
return {
Query: {
version: () => SERVER_VERSION,
interceptors: () => _.values(interceptors),
interceptor: (_: any, { id } : { id: string }) => interceptors[id],
config: () => ({
certificatePath: config.https.certPath,
certificateContent: config.https.certContent,
// We could calculate this client side, but it normally 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(config.https.certContent)
}),
networkInterfaces: () => os.networkInterfaces(),
systemProxy: () => getSystemProxy().catch((e) => {
reportError(e);
return undefined;
}),
dnsServers: async (__: void, { proxyPort }: { proxyPort: number }): Promise<string[]> => {
const dnsServer = await getDnsServer(proxyPort);
return [`127.0.0.1:${dnsServer.address().port}`];
},
ruleParameterKeys: async (): Promise<String[]> => {
return getRuleParamKeys();
}
},
Mutation: {
activateInterceptor: async (__: void, { id, proxyPort, options }: {
id: string,
proxyPort: number,
options: unknown
}) => {
addBreadcrumb(`Activating ${id}`, { category: 'interceptor', data: { id, options } });
const interceptor = 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).then(() => {
if (!activationDone) reportError(`Timeout activating ${id}`)
});
const result = await interceptor.activate(proxyPort, options).catch((e) => e);
activationDone = true;
if (isActivationError(result)) {
if (result.reportable !== false) reportError(result);
return { success: false, metadata: result.metadata };
} else {
addBreadcrumb(`Successfully activated ${id}`, { category: 'interceptor' });
return { success: true, metadata: result };
}
},
deactivateInterceptor: async (__: void, { id, proxyPort, options }: {
id: string,
proxyPort: number,
options: unknown
}) => {
const interceptor = interceptors[id];
if (!interceptor) throw new Error(`Unknown interceptor ${id}`);
await interceptor.deactivate(proxyPort, options).catch(reportError);
return { success: !interceptor.isActive(proxyPort) };
},
triggerUpdate: () => {
eventEmitter.emit('update-requested');
},
// 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.
shutdown: () => {
shutdown('API call');
}
},
Interceptor: {
isActivable: (interceptor: Interceptor) => {
return withFallback(
interceptor.isActivable(),
interceptor.activableTimeout || INTERCEPTOR_TIMEOUT,
false
);
},
isActive: async (interceptor: Interceptor, { proxyPort }: { proxyPort: number }) => {
try {
return await interceptor.isActive(proxyPort);
} catch (e) {
reportError(e);
return false;
}
},
metadata: async function (interceptor: Interceptor, { type }: { type?: 'DETAILED' | 'SUMMARY' }) {
if (!interceptor.getMetadata) return undefined;
const metadataType = type
? type.toLowerCase() as 'summary' | 'detailed'
: 'summary';
const timeout = metadataType === 'summary'
? INTERCEPTOR_TIMEOUT
: INTERCEPTOR_TIMEOUT * 10; // Longer timeout for detailed metadata
try {
return await withFallback(
interceptor.getMetadata(metadataType),
timeout,
undefined
);
} catch (e) {
reportError(e);
return undefined;
}
}
},
Json: new GraphQLScalarType({
name: 'Json',
description: 'A JSON entity, serialized as a raw object',
serialize: (value: any) => value,
parseValue: (input: string): any => input,
parseLiteral: (): any => { throw new Error('JSON literals are not supported') }
}),
Void: new GraphQLScalarType({
name: 'Void',
description: 'Nothing at all',
serialize: (value: any) => null,
parseValue: (input: string): any => null,
parseLiteral: (): any => { throw new Error('Void literals are not supported') }
}),
Error: new GraphQLScalarType({
name: 'Error',
description: 'An error',
serialize: (value: Error) => JSON.stringify({
name: value.name,
message: value.message,
stack: value.stack
}),
parseValue: (input: string): any => {
let data = JSON.parse(input);
let error = new Error();
error.name = data.name;
error.message = data.message;
error.stack = data.stack;
throw error;
},
parseLiteral: (): any => { throw new Error('Error literals are not supported') }
}),
}
};
export class HttpToolkitServerApi extends events.EventEmitter {
private server: express.Application;
constructor(config: HtkConfig, getRuleParamKeys: () => string[]) {
super();
let interceptors = buildInterceptors(config);
const schema = makeExecutableSchema({
typeDefs,
resolvers: buildResolvers(config, interceptors, getRuleParamKeys, this)
});
this.server = express();
this.server.disable('x-powered-by');
// Allow web pages on non-local URLs (app.pipe.tech, not localhost) to
// send requests to this admin server too. Without this, those requests will
// fail after rejected preflights in recent Chrome (from ~v102, ish? Unclear).
this.server.use((req, res, next) => {
if (req.headers["access-control-request-private-network"]) {
res.setHeader("access-control-allow-private-network", "true");
}
next(null);
});
this.server.use(cors({
origin: ALLOWED_ORIGINS,
maxAge: 86400 // Cache this result for as long as possible
}));
this.server.use(corsGate(
ENABLE_PLAYGROUND
// When the debugging playground is enabled, we're slightly more lax
? {
strict: true,
allowSafe: true,
origin: 'http://localhost:45457'
}
: {
strict: true, // MUST send an allowed origin
allowSafe: false, // Even for HEAD/GET requests (should be none anyway)
origin: '' // No origin - we accept *no* same-origin requests
}
));
this.server.use((req, res, next) => {
if (req.method !== 'POST' && !ENABLE_PLAYGROUND) {
// We allow only POST, because that's all we expect for GraphQL queries,
// and this helps derisk some (admittedly unlikely) XSRF possibilities.
res.status(405).send('Only POST requests are supported');
} else {
next();
}
});
if (config.authToken) {
// Optional auth token. This allows us to lock down UI/server communication further
// when started together. The desktop generates a token every run and passes it to both.
this.server.use((req: express.Request, res: express.Response, next: () => void) => {
const authHeader = req.headers['authorization'] || '';
const tokenMatch = authHeader.match(/Bearer (\S+)/) || [];
const token = tokenMatch[1];
if (token !== config.authToken) {
res.status(403).send('Valid token required');
} else {
next();
}
});
}
this.server.use(graphqlHTTP({
schema,
graphiql: ENABLE_PLAYGROUND
}));
}
start() {
return new Promise<void>((resolve, reject) => {
this.server.listen(45457, '127.0.0.1', resolve); // Localhost only
this.server.once('error', reject);
});
}
};