This repository has been archived by the owner on Feb 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (120 loc) · 4.12 KB
/
index.js
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
"use strict";
const http = require("http");
const https = require("https");
const { Transform, pipeline } = require("stream");
const Mitm = require("mitm");
const uuid = require("uuid");
const debug = require("debug")("mitm-exp");
const YESNO_INTERNAL_HTTP_HEADER = "x-yesno-internal-header-id";
// We need console output delayed as it prevents seeing the issue.
const DEBUG_DELAY = 1000;
const debugLater = (...args) => {
setTimeout(() => {
debug(...args);
}, DEBUG_DELAY);
};
class DebugTransform extends Transform {
constructor({ id }) {
super();
this.id = id;
}
_transform(chunk, encoding, callback) {
debugLater(`DebugTransform: ${this.id}`, chunk.toString());
callback(null, chunk);
}
}
class Interceptor {
constructor() {
this.clientRequests = {};
}
enable() {
const self = this;
this.mitm = Mitm(); // eslint-disable-line new-cap
// Monkey-patch client requests to track options.
const onSocket = http.ClientRequest.prototype.onSocket;
this._origOnSocket = onSocket;
http.ClientRequest.prototype.onSocket = function (socket) {
if (socket.__yesno_req_id !== undefined) {
self.clientRequests[socket.__yesno_req_id].clientRequest = this;
this.setHeader(YESNO_INTERNAL_HTTP_HEADER, socket.__yesno_req_id);
}
onSocket.call(this, socket);
};
this.mitm.on("connect", this.mitmOnConnect.bind(this));
this.mitm.on("request", this.mitmOnRequest.bind(this));
this.mitm.on("connection", (server) => {
server.on("error", (err) => debug("Server error:", err));
});
}
disable() {
if (this.mitm) {
this.mitm.disable();
this.mitm = undefined;
}
}
mitmOnConnect(socket, clientOptions) {
// Short-circuit: passthrough real requests.
if (clientOptions.proxying) { return void socket.bypass(); }
// Mutate socket and track options for later proxying.
socket.__yesno_req_id = uuid.v4();
this.clientRequests[socket.__yesno_req_id] = { clientOptions };
}
mitmOnRequest(
interceptedRequest,
interceptedResponse
) {
// Re-associate id.
const id = interceptedRequest.headers[YESNO_INTERNAL_HTTP_HEADER];
if (!id) {
throw new Error(`No internal id found: ${JSON.stringify({
headers: interceptedRequest.headers
})}`);
}
// Infer proxy request info.
const { clientOptions, clientRequest } = this.clientRequests[id];
const isHttps = interceptedRequest.connection.encrypted;
const request = isHttps ? https.request : http.request;
// Create request and proxy to _real_ destination.
const proxiedRequest = request({
...clientOptions,
path: clientRequest.path,
// Add in headers, omitting our special one.
headers: Object
.entries(clientOptions.headers)
.filter(([k]) => k !== YESNO_INTERNAL_HTTP_HEADER)
.reduce((m, [k, v]) => Object.assign(m, { [k]: v }), {}),
// Skip MITM to do a _real_ request.
proxying: true
});
// Start bindings.
interceptedRequest.on("error", (e) => debug("Error on intercepted request:", e));
interceptedRequest.on("aborted", () => {
debug("Intercepted request aborted");
proxiedRequest.abort();
});
proxiedRequest.on("timeout", (e) => debug("Proxied request timeout", e));
proxiedRequest.on("error", (e) => debug("Proxied request error", e));
proxiedRequest.on("aborted", () => debug("Proxied request aborted"));
proxiedRequest.on("response", (proxiedResponse) => {
debugLater("proxied response (%d)", proxiedResponse.statusCode);
if (proxiedResponse.statusCode) {
interceptedResponse.writeHead(proxiedResponse.statusCode, proxiedResponse.headers);
}
pipeline(
proxiedResponse,
new DebugTransform({ id: "response" }),
interceptedResponse,
(err) => debug(`Response pipeline ${err ? "failed" : "passed"}`, err || "")
);
});
pipeline(
interceptedRequest,
new DebugTransform({ id: "request" }),
proxiedRequest,
(err) => debugLater(`Request pipeline ${err ? "failed" : "passed"}`, err || "")
);
}
}
module.exports = {
Interceptor
};