Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ Example routing config file
end
end

Example on multi-homed hosts, use same outgoing IP as inbound
_____________________________________________________________

proxy do |data,local_ip,local_port|
# p datai
{ :remote => "127.0.0.1:31337",
:local_bind =>"#{local_ip}",
}
end


Example SOCKS4 Proxy in 7 Lines
-------------------------------
Expand All @@ -106,6 +116,7 @@ Valid return values
-------------------

`{ :remote => String }` - String is the host:port of the backend server that will be proxied.
`{ :remote => String, :local_bind => String }` - Same as above, but specify local host:port to bind to.
`{ :remote => String, :data => String }` - Same as above, but send the given data instead.
`{ :remote => String, :data => String, :reply => String}` - Same as above, but reply with given data back to the client
`{ :noop => true }` - Do nothing.
Expand Down
8 changes: 6 additions & 2 deletions bin/proxymachine
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ EOF
end

opts.parse!

if options[:config].nil? then
puts opts
exit
end
load(options[:config])
name = options[:config].split('/').last.chomp('.rb')
ProxyMachine.run(name, options[:host], options[:port])
Expand All @@ -42,4 +45,5 @@ rescue Exception => e
LOGGER.info e.message
LOGGER.info e.backtrace.join("\n")
end
end
end

6 changes: 6 additions & 0 deletions examples/bind_same_ip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
proxy do |data,local_ip,local_port|
# p datai
{ :remote => "127.0.0.1:31337",
:local_bind =>"#{local_ip}",
}
end
10 changes: 10 additions & 0 deletions examples/specify_bind.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
proxy do |data|
return if not data.match(/([^:]+):?(\d*)/)
bind_addr=$1.to_s.strip
bind_port=$2.to_s.strip
# p datai
{ :remote => "127.0.0.1:31337",
:local_bind =>"#{bind_addr}:#{bind_port}",
:data => data[data.index("\n")+1..-1]
}
end
19 changes: 17 additions & 2 deletions lib/proxymachine/client_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def post_init
LOGGER.info "Accepted #{peer}"
@buffer = []
@remote = nil
@local_bind = [nil,nil]
@tries = 0
@connected = false
@connect_timeout = nil
Expand Down Expand Up @@ -41,14 +42,27 @@ def receive_data(data)
# attempt is made to connect and proxy to the remote server.
def establish_remote_server
fail "establish_remote_server called with remote established" if @remote
commands = ProxyMachine.router.call(@buffer.join)
#TODO: Add a test case for passing peer name
local_port, local_ip = Socket.unpack_sockaddr_in(get_sockname)
commands = ProxyMachine.router.call(@buffer.join,local_ip,local_port)
LOGGER.info "#{peer} #{commands.inspect}"
close_connection unless commands.instance_of?(Hash)
if remote = commands[:remote]
m, host, port = *remote.match(/^(.+):(.+)$/)
@remote = [host, port]
if local_bind = commands[:local_bind]
m,bind_addr,bind_port=*local_bind.match(/([^:]+):?(\d*)/)
if bind_addr.empty?
bind_addr=nil
end
if bind_port.empty?
bind_port=nil
end
@local_bind=[bind_addr,bind_port]
end
if data = commands[:data]
@buffer = [data]

end
if reply = commands[:reply]
send_data(reply)
Expand All @@ -74,8 +88,9 @@ def establish_remote_server
def connect_to_server
fail "connect_server called without remote established" if @remote.nil?
host, port = @remote
bind_addr,bind_port = @local_bind
LOGGER.info "Establishing new connection with #{host}:#{port}"
@server_side = ServerConnection.request(host, port, self)
@server_side = ServerConnection.request(host, port, self,bind_addr,bind_port)
@server_side.pending_connect_timeout = @connect_timeout
@server_side.comm_inactivity_timeout = @inactivity_timeout
end
Expand Down
12 changes: 10 additions & 2 deletions lib/proxymachine/server_connection.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
#require 'pry'
class ProxyMachine
class ServerConnection < EventMachine::Connection
def self.request(host, port, client_side)
EventMachine.connect(host, port, self, client_side)
def self.request(host, port, client_side,bind_addr=nil,bind_port=nil)
# binding.pry
#LOGGER.debug "Local bind: #{bind_addr} #{bind_port}"
if bind_addr or bind_port then
LOGGER.info "Local bind: #{bind_addr} #{bind_port}"
else
LOGGER.debug "No local binding specified" #FIXME: remove this
end
EventMachine.bind_connect(bind_addr,bind_port,host, port, self, client_side)
end

def initialize(conn)
Expand Down
File renamed without changes.