-
-
Save tmm1/762928 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'rubygems' | |
require 'eventmachine' | |
module ProxyConnection | |
def initialize(client) | |
@client = client | |
end | |
def post_init | |
proxy_incoming_to(@client) | |
end | |
def proxy_target_unbound | |
close_connection | |
end | |
def unbind | |
@client.close_connection_after_writing | |
end | |
end | |
module ProxyServer | |
def receive_data(data) | |
(@buf ||= "") << data | |
return if @buf.size < 9 | |
null_index = @buf[8..-1].index("\0") | |
return if null_index.nil? | |
header_request = @buf.slice! 0, 9 + null_index | |
version, cmd, port, host = header_request.unpack("CCnA4") | |
if version != 4 || cmd != 1 | |
send_data [0, 0x5b, 0, 0, 0, 0, 0, 0].pack("C8") | |
close_connection_after_writing | |
else | |
send_data [0, 0x5a, 0, 0, 0, 0, 0, 0].pack("C8") | |
host = host.unpack("C*").join(".") | |
client = EM.connect host, port, ProxyConnection, self | |
client.send_data(@buf) | |
proxy_incoming_to(client) | |
end | |
end | |
end | |
EventMachine::run { | |
EventMachine::start_server "127.0.0.1", 1080, ProxyServer | |
puts "running proxy" | |
} | |
__END__ | |
$ curl --socks4 127.0.0.1:1080 http://google.com/ | |
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8"> | |
<TITLE>301 Moved</TITLE></HEAD><BODY> | |
<H1>301 Moved</H1> | |
The document has moved | |
<A HREF="http://www.google.com/">here</A>. | |
</BODY></HTML> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment