Last active
August 29, 2015 14:07
-
-
Save rjvdw/ec88d152dadd93eabd2a to your computer and use it in GitHub Desktop.
A TCP proxy that allows you to dump all communication.
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
#!/usr/bin/env coffee | |
# vim: set ft=coffee: | |
"use strict" | |
# A TCP proxy that allows you to dump all communication. | |
net = require 'net' | |
[executing, script, source, dest] = process.argv | |
unless source? and dest? | |
console.error 'Invalid number of arguments.' | |
process.exit 1 | |
source_host = 'localhost' | |
source_port = source | |
dest_host = 'localhost' | |
dest_port = dest | |
[source_host, source_port] = source.split ':' if source.match /:/ | |
[dest_host, dest_port] = dest.split ':' if dest.match /:/ | |
console.log "Going to proxy requests to #{source_host}:#{source_port} through #{dest_host}:#{dest_port}." | |
timestamp = -> "[#{(new Date()).toISOString()}]" | |
server = net.createServer (conn) -> | |
client = null | |
client = net.connect {port: source_port, host: source_host}, -> | |
conn.on 'data', (data) -> | |
client.write data | |
console.log '>>', timestamp() | |
console.log data.toString('utf-8') | |
client.on 'data', (data) -> | |
conn.write data | |
console.log '<<', timestamp() | |
console.log data.toString('utf-8') | |
client.on 'end', -> | |
conn.end() | |
client = null | |
conn.on 'end', -> | |
client.end() if client? | |
server.listen dest_port, dest_host, -> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment