November 6, 2009

SOCKS 4 server with ProxyMachine

Filed under: network, ruby — Tags: , — coderrr @ 4:15 am

I proxy almost all my web traffic through a SOCKS proxy. After going through a bunch of different servers, each with its own issues, I decided to just write my own. I ended up using ProxyMachine and it only took seven lines!

# socks4.rb
proxy do |data|
  next  if data.size < 9
  v, c, port, o1, o2, o3, o4, user = data.unpack("CCnC4a*")
  return { :close => "\x0\x5b\x0\x0\x0\x0\x0\x0" }  if v != 4 or c != 1
  next  if ! idx = user.index("\x0")
  { :remote => "#{[o1,o2,o3,o4]*'.'}:#{port}", :reply => "\x0\x5a\x0\x0\x0\x0\x0\x0", :data => data[idx+9..-1] }

run it with:

$ proxymachine -h -p 1080 -c socks4.rb

* note this only actually covers half of the SOCKS4 protocol, but it’s the only half that pretty much anyone uses


  1. […] SOCKS 4 server with ProxyMachine « coderrr a few seconds ago from xmpp […]

    Pingback by Postmodern (postmodern) 's status on Friday, 06-Nov-09 08:00:29 UTC - — November 6, 2009 @ 8:00 am

  2. […] a low ping time) and proxy the rest through a SOCKS server. It’s built on top of this 7 line SOCKS 4 server hack which is built on top of ProxyMachine which is built on top of […]

    Pingback by socks_switch: switching between direct connection and SOCKS proxying based on ping time to host « coderrr — September 20, 2010 @ 7:32 pm

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Silver is the New Black Theme. Blog at


Get every new post delivered to your Inbox.

Join 31 other followers

%d bloggers like this: