-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.ts
47 lines (36 loc) · 1.02 KB
/
index.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
import Mitm from 'mitm';
export interface HTTPAttackOptions {
ignoreUrlPatterns?: string[];
}
export default class HTTPAttack {
static configure(opts: HTTPAttackOptions): () => HTTPAttack {
return () => {
const attack = new HTTPAttack(opts);
return attack;
};
}
ignoreUrlPatterns: string[];
stopped = false;
constructor({ ignoreUrlPatterns = [] }: HTTPAttackOptions = {}) {
this.ignoreUrlPatterns = ignoreUrlPatterns;
}
start(): void {
const mitm = Mitm();
mitm.on('connect', (socket, opts) => {
// https://github.com/moll/node-mitm/issues/16
// eslint-disable-next-line
const requestedURL: string = opts?.host || '';
const shouldIgnore = this.ignoreUrlPatterns.some((needle: string) =>
requestedURL.includes(needle)
);
if (this.stopped || shouldIgnore) socket.bypass();
});
mitm.on('request', (_, res) => {
res.statusCode = 500;
res.end('Request failed');
});
}
stop(): void {
this.stopped = true;
}
}