Last active
May 3, 2024 03:56
-
-
Save miguelmota/b1dd7fbd81fef0a30fac0c335e18f29e to your computer and use it in GitHub Desktop.
Node.js proxy mitm log example
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
// run: mitmproxy -p 8080 | |
// run: node proxy.js | |
async function setupProxy (proxyUrl) { | |
// require('global-agent/bootstrap') | |
// process.env.GLOBAL_AGENT_HTTP_PROXY = proxyUrl | |
// process.env.GLOBAL_AGENT_HTTPS_PROXY = proxyUrl | |
// console.log('Global proxy routing set up.') | |
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0' // Ignore self-signed certs, use with caution | |
const { HttpsProxyAgent } = require('https-proxy-agent') | |
// Set up the proxy agent for https module | |
const https = require('https') | |
const url = require('url') | |
class LoggingProxyAgentForHttpsModule extends HttpsProxyAgent { | |
constructor(proxyOptions) { | |
super(proxyOptions) | |
} | |
// Override the addRequest method to log URLs | |
addRequest(req, options) { | |
// Log the full URL or just the pathname, depending on your needs | |
if (typeof options === 'string') { | |
options = url.parse(options) | |
} | |
const fullUrl = url.format({ | |
protocol: options.protocol, | |
hostname: options.hostname, | |
port: options.port, | |
pathname: options.path | |
}) | |
console.log('Request URL (https):', fullUrl) | |
// Call the original addRequest method | |
super.addRequest(req, options) | |
} | |
} | |
const originalFetch = global.fetch // Save the original fetch function | |
const { ProxyAgent } = require('undici') | |
class LoggingProxyAgentForFetchModule extends ProxyAgent { | |
constructor(uri, options) { | |
super(uri, options) | |
} | |
dispatch(options, handler) { | |
console.log('Request URL (fetch):', options.origin + options.path) | |
return super.dispatch(options, handler) | |
} | |
} | |
// Use your custom agent | |
const proxyAgentForHttpsModule = new LoggingProxyAgentForHttpsModule(proxyUrl) | |
const proxyAgentForFetchModule = new LoggingProxyAgentForFetchModule(proxyUrl) | |
// Override global https agent | |
https.globalAgent = proxyAgentForHttpsModule | |
// Override global fetch | |
global.fetch = (url, options = {}) => { | |
options.dispatcher = proxyAgentForFetchModule | |
return originalFetch(url, options) | |
} | |
} | |
async function main () { | |
setupProxy('http://localhost:8080') | |
// Example curl request | |
// curl -x http://localhost:8080 -L http://example.com | |
// Example requests | |
await require('axios').get('https://example.com/') // Request URL (https): https://example.com:443/ | |
await require('node-fetch')('https://example.com/') // Request URL (https): https://example.com:443/ | |
await require('isomorphic-fetch')('https://example.com/') // Request URL (https): https://example.com:443/ | |
await fetch('https://example.com/') // Request URL (fetch): https://example.com/ | |
console.log('done') | |
} | |
main().catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment