This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: record browser applications with man-in-the-middle proxy
- Loading branch information
Showing
38 changed files
with
3,461 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ recorder-standalone | |
receptor | ||
command | ||
client | ||
mitm | ||
server | ||
questionnaire | ||
setup | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
test | ||
node |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
util.mjs | ||
stream.mjs | ||
html.mjs | ||
js.mjs | ||
forward.mjs | ||
intercept.mjs | ||
forge.mjs | ||
proxy.mjs | ||
index.mjs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Buffer } from "node:buffer"; | ||
import { Socket as NetSocket } from "node:net"; | ||
import { request as requestHttp } from "node:http"; | ||
import { logWarning } from "../../log/index.mjs"; | ||
|
||
const { from: toBuffer } = Buffer; | ||
|
||
export const forwardRequest = (host, inc_req, callback) => { | ||
let done = false; | ||
/* c8 ignore start */ | ||
inc_req.on("error", (error) => { | ||
if (!done) { | ||
done = true; | ||
callback(error, null); | ||
} | ||
}); | ||
/* c8 ignore stop */ | ||
const out_req = requestHttp({ | ||
host: host.name, | ||
port: host.port, | ||
method: inc_req.method, | ||
path: inc_req.url, | ||
headers: inc_req.headers, | ||
}); | ||
/* c8 ignore start */ | ||
out_req.on("error", (error) => { | ||
if (!done) { | ||
done = true; | ||
callback(error, null); | ||
} | ||
}); | ||
/* c8 ignore stop */ | ||
out_req.on("response", (res) => { | ||
if (!done) { | ||
done = true; | ||
callback(null, res); | ||
} | ||
}); | ||
inc_req.pipe(out_req); | ||
}; | ||
|
||
export const forwardResponse = (inc_res, out_res) => { | ||
out_res.writeHead(inc_res.statusCode, inc_res.statusMessage, inc_res.headers); | ||
inc_res.pipe(out_res); | ||
}; | ||
|
||
const stringifyHead = ({ | ||
method, | ||
url, | ||
httpVersion: version, | ||
rawHeaders: headers, | ||
}) => { | ||
const chunks = []; | ||
chunks.push(`${method} ${url} HTTP/${version}`); | ||
for (let index = 0; index < headers.length; index += 2) { | ||
chunks.push(`${headers[index]}: ${headers[index + 1]}`); | ||
} | ||
chunks.push(""); | ||
chunks.push(""); | ||
return chunks.join("\r\n"); | ||
}; | ||
|
||
const forwardSpecial = (host, req, socket, head) => { | ||
const forward_socket = new NetSocket(); | ||
/* c8 ignore start */ | ||
socket.on("error", (error) => { | ||
logWarning( | ||
"error on socket %j >> %s %s %j >> %o", | ||
host, | ||
req.method, | ||
req.url, | ||
req.headers, | ||
error, | ||
); | ||
forward_socket.destroy(); | ||
}); | ||
forward_socket.on("error", (error) => { | ||
logWarning( | ||
"error on forward socket %j >> %s %s %j >> %o", | ||
host, | ||
req.method, | ||
req.url, | ||
req.headers, | ||
error, | ||
); | ||
socket.destroy(); | ||
}); | ||
/* c8 ignore stop */ | ||
forward_socket.connect(host.port, host.name); | ||
forward_socket.on("connect", () => { | ||
forward_socket.write(toBuffer(stringifyHead(req), "utf8")); | ||
forward_socket.write(head); | ||
socket.pipe(forward_socket); | ||
forward_socket.pipe(socket); | ||
}); | ||
}; | ||
|
||
export const forwardUpgrade = forwardSpecial; | ||
|
||
export const forwardConnect = forwardSpecial; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
import { | ||
createServer as createHttpServer, | ||
request as requestHttp, | ||
} from "node:http"; | ||
import { Buffer } from "node:buffer"; | ||
import { default as WebSocket, WebSocketServer } from "ws"; | ||
import { assertEqual, assertDeepEqual } from "../../__fixture__.mjs"; | ||
import { bufferReadable } from "./stream.mjs"; | ||
import { | ||
forwardRequest, | ||
forwardResponse, | ||
forwardUpgrade, | ||
forwardConnect, | ||
} from "./forward.mjs"; | ||
|
||
const { | ||
String, | ||
Promise, | ||
JSON: { stringify: stringifyJSON, parse: parseJSON }, | ||
} = globalThis; | ||
|
||
const { from: toBuffer } = Buffer; | ||
|
||
const wss = new WebSocketServer({ noServer: true }); | ||
|
||
////////////////// | ||
// Setup Server // | ||
////////////////// | ||
|
||
const server = createHttpServer(); | ||
|
||
server.on("request", (req, res) => { | ||
bufferReadable(req, (buffer) => { | ||
const body = toBuffer( | ||
stringifyJSON({ | ||
method: req.method, | ||
path: req.url, | ||
version: req.httpVersion, | ||
body: buffer.toString("utf8"), | ||
}), | ||
"utf8", | ||
); | ||
res.writeHead(200, { | ||
"content-type": "application/json", | ||
"content-length": body.length, | ||
}); | ||
res.end(body); | ||
}); | ||
}); | ||
|
||
server.on("upgrade", (req, socket, head) => { | ||
wss.handleUpgrade(req, socket, head, (ws) => { | ||
ws.on("message", (buffer) => { | ||
ws.send(buffer); | ||
}); | ||
}); | ||
}); | ||
|
||
server.listen(0); | ||
|
||
await new Promise((resolve, reject) => { | ||
server.on("listening", resolve); | ||
server.on("error", reject); | ||
}); | ||
|
||
const host = { name: "localhost", port: server.address().port }; | ||
|
||
///////////////// | ||
// Setup Forge // | ||
///////////////// | ||
|
||
const forge = createHttpServer(); | ||
|
||
forge.on("request", (inc_req, out_res) => { | ||
forwardRequest(host, inc_req, (error, inc_res) => { | ||
assertEqual(error, null); | ||
forwardResponse(inc_res, out_res); | ||
}); | ||
}); | ||
|
||
forge.on("upgrade", (req, socket, head) => { | ||
forwardUpgrade(host, req, socket, head); | ||
}); | ||
|
||
forge.on("connect", (req, socket, head) => { | ||
forwardConnect(host, req, socket, head); | ||
}); | ||
|
||
forge.listen(0); | ||
|
||
await new Promise((resolve, reject) => { | ||
forge.on("listening", resolve); | ||
forge.on("error", reject); | ||
}); | ||
|
||
///////////// | ||
// Request // | ||
///////////// | ||
|
||
{ | ||
const req = requestHttp({ | ||
host: "localhost", | ||
port: forge.address().port, | ||
method: "POST", | ||
path: "/path", | ||
version: "1.1", | ||
}); | ||
req.end("body"); | ||
const res = await new Promise((resolve, reject) => { | ||
req.on("response", resolve); | ||
req.on("error", reject); | ||
}); | ||
assertDeepEqual( | ||
parseJSON( | ||
( | ||
await new Promise((resolve) => { | ||
bufferReadable(res, resolve); | ||
}) | ||
).toString("utf8"), | ||
), | ||
{ | ||
method: "POST", | ||
path: "/path", | ||
version: "1.1", | ||
body: "body", | ||
}, | ||
); | ||
} | ||
|
||
///////////// | ||
// Upgrade // | ||
///////////// | ||
|
||
{ | ||
const ws = new WebSocket(`ws://localhost:${String(forge.address().port)}`); | ||
await new Promise((resolve, reject) => { | ||
ws.on("open", resolve); | ||
ws.on("error", reject); | ||
}); | ||
ws.send(toBuffer("message", "utf8")); | ||
assertEqual( | ||
( | ||
await new Promise((resolve, reject) => { | ||
ws.on("message", resolve); | ||
ws.on("error", reject); | ||
}) | ||
).toString("utf8"), | ||
"message", | ||
); | ||
ws.close(); | ||
await new Promise((resolve, reject) => { | ||
ws.on("close", resolve); | ||
ws.on("error", reject); | ||
}); | ||
} | ||
|
||
////////////// | ||
// Teardown // | ||
////////////// | ||
|
||
forge.close(); | ||
|
||
await new Promise((resolve, reject) => { | ||
forge.on("error", reject); | ||
forge.on("close", resolve); | ||
}); | ||
|
||
server.close(); | ||
|
||
await new Promise((resolve, reject) => { | ||
server.on("error", reject); | ||
server.on("close", resolve); | ||
}); |
Oops, something went wrong.