Skip to content

Instantly share code, notes, and snippets.

@rjvdw rjvdw/tcp-proxy.coffee
Last active Aug 29, 2015

Embed
What would you like to do?
A TCP proxy that allows you to dump all communication.
#!/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
You can’t perform that action at this time.