mojombo / proxymachine

A simple TCP routing proxy built on EventMachine that lets you configure the routing logic in Ruby.

This URL has Read+Write access

mojombo (author)
Fri Aug 07 01:15:16 -0700 2009
commit  1d5ddca73edd4c9724f9f47edf4544e25065cb84
tree    0dcbba4a61bcadca4521f692ff02aede34edfbc5
parent  4c2094b1bb9c7d9c259ab33a95b9a21cb74db49e
name age message
file .document Fri Apr 24 16:48:18 -0700 2009 Initial commit to proxymachine. [mojombo]
file .gitignore Fri Apr 24 16:48:18 -0700 2009 Initial commit to proxymachine. [mojombo]
file LICENSE Fri Apr 24 16:48:18 -0700 2009 Initial commit to proxymachine. [mojombo]
file README.md Loading commit data...
file Rakefile
file VERSION.yml
directory bin/ Tue Apr 28 12:36:19 -0700 2009 set correct port option [pjhyett]
directory examples/ Wed Jul 15 14:43:51 -0700 2009 allow data to be altered a single time and chan... [mojombo]
directory lib/ Mon Jul 20 15:05:51 -0700 2009 Use EventMachine 0.12.8's proxying facilities. ... [defunkt]
file proxymachine.gemspec
directory test/ Fri Apr 24 16:48:18 -0700 2009 Initial commit to proxymachine. [mojombo]
README.md

ProxyMachine

By Tom Preston-Werner (tom@mojombo.com)

WARNING: This software is alpha and should not be used in production without extensive testing. You should not consider this project production ready until it is released as 1.0.

Description

ProxyMachine is a simple content aware (layer 7) TCP routing proxy built on EventMachine that lets you configure the routing logic in Ruby.

If you need to proxy connections to different backend servers depending on the contents of the transmission, then ProxyMachine will make your life easy!

The idea here is simple. For each client connection, start receiving data chunks and placing them into a buffer. Each time a new chunk arrives, send the buffer to a user specified block. The block's job is to parse the buffer to determine where the connection should proxied. If the buffer contains enough data to make a determination, the block returns the address and port of the correct backend server. If not, it can choose to either do nothing and wait for more data to arrive, or close the connection. Once the block returns an address, a connection to the backend is made, the buffer is replayed to the backend, and the client and backend connections are hooked up to form a straight proxy. This bidirectional proxy continues to exist until either the client or backend close the connection.

Installation

gem install mojombo-proxymachine -s http://gems.github.com

Running

Usage:
  proxymachine -c <config file> [-h <host>] [-p <port>]

Options:
  -c, --config CONFIG              Configuration file
  -h, --host HOST                  Hostname to bind. Default 0.0.0.0
  -p, --port PORT                  Port to listen on. Default 5432

Example routing config file

class GitRouter
  # Look at the routing table and return the correct address for +name+
  # Returns "<host>:<port>" e.g. "ae8f31c.example.com:9418"
  def self.lookup(name)
    ...
  end
end

# Perform content-aware routing based on the stream data. Here, the
# header information from the Git protocol is parsed to find the 
# username and a lookup routine is run on the name to find the correct
# backend server. If no match can be made yet, do nothing with the
# connection yet.
proxy do |data|
  if data =~ %r{^....git-upload-pack /([\w\.\-]+)/[\w\.\-]+\000host=\w+\000}
    name = $1
    { :remote => GitRouter.lookup(name) }
  else
    { :noop => true }
  end
end

Valid return values

{ :remote => String } - String is the host:port of the backend server that will be proxied.
{ :remote => String, :data => String } - Same as above, but send the given data instead.
{ :noop => true } - Do nothing.
{ :close => true } - Close the connection.
{ :close => String } - Close the connection after sending the String.

Contribute

If you'd like to hack on ProxyMachine, start by forking my repo on GitHub:

http://github.com/mojombo/proxymachine

To get all of the dependencies, install the gem first. The best way to get your changes merged back into core is as follows:

  1. Clone down your fork
  2. Create a topic branch to contain your change
  3. Hack away
  4. Add tests and make sure everything still passes by running rake
  5. If you are adding new functionality, document it in the README.md
  6. Do not change the version number, I will do that on my end
  7. If necessary, rebase your commits into logical chunks, without errors
  8. Push the branch up to GitHub
  9. Send me (mojombo) a pull request for your branch

Copyright

Copyright (c) 2009 Tom Preston-Werner. See LICENSE for details.