Skip to content

Instantly share code, notes, and snippets.

@RealBeepMcJeep
RealBeepMcJeep / jsonParser_hijack_diff.js
Last active September 10, 2023 16:58
used for hijacking jsonParser middleware and dumping the request/response bodies to text files; used with https://jvilk.com/MakeTypes/ to RE an API endpoint (black-box) and begin documenting/refactoring. especially helpful for monolith applications.
const _jsonParser = express.json({ limit: '100mb' });
// hijack jsonParser to log JSON bodies
console.log('installing jsonParser hijack')
const jsonParser = (req, res, next) => {
console.log('jsonParser called @', req.method, req.url);
_jsonParser(req, res, (next_retval) => {
// log if JSON request
if (req.headers && req.headers['content-type'] && req.headers['content-type'].startsWith('application/json') && req.body) {
let fname = `${humanizedISO8601DateTime()}-${req.url.replace(/\//g, '')}.json`.replace(/ /g, '_')
let _path = path.join(__dirname, 'debugLogs', fname)
import contextlib
import ctypes
import ctypes.wintypes
import io
import json
import os
import re
import socket
import socketserver
import threading