Skip to content

Commit

Permalink
Add support for proxying to a unix domain socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Dec 11, 2012
1 parent b8a75ca commit 0040647
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--format=documentation
--color
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
em-proxy (0.1.6)
em-proxy (0.1.7)
eventmachine

GEM
Expand All @@ -17,7 +17,7 @@ GEM
http_parser.rb (>= 0.5.2)
em-socksify (0.1.0)
eventmachine
eventmachine (1.0.0.beta.4)
eventmachine (1.0.0)
http_parser.rb (0.5.3)
rake (0.9.2.2)
rspec (2.7.0)
Expand Down
17 changes: 13 additions & 4 deletions lib/em-proxy/connection.rb
Expand Up @@ -43,11 +43,20 @@ def relay_to_servers(processed)
# initialize connections to backend servers
#
def server(name, opts)
srv = EventMachine::bind_connect(opts[:bind_host], opts[:bind_port], opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
c.name = name
c.plexer = self
c.proxy_incoming_to(self, 10240) if opts[:relay_server]
if opts[:socket]
srv = EventMachine::connect_unix_domain(opts[:socket], EventMachine::ProxyServer::Backend, @debug) do |c|
c.name = name
c.plexer = self
c.proxy_incoming_to(self, 10240) if opts[:relay_server]
end
else
srv = EventMachine::bind_connect(opts[:bind_host], opts[:bind_port], opts[:host], opts[:port], EventMachine::ProxyServer::Backend, @debug) do |c|
c.name = name
c.plexer = self
c.proxy_incoming_to(self, 10240) if opts[:relay_server]
end
end

self.proxy_incoming_to(srv, 10240) if opts[:relay_client]

@servers[name] = srv
Expand Down
1 change: 1 addition & 0 deletions spec/helper.rb
@@ -1,5 +1,6 @@
require 'bundler/setup'
require 'em-http'
require 'pp'
require 'tmpdir'

require 'em-proxy'
61 changes: 60 additions & 1 deletion spec/proxy_spec.rb
Expand Up @@ -68,7 +68,7 @@ def failed
end

Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :bing, :host => "google.com", :port => 80
conn.server :goog, :host => "google.com", :port => 80
conn.server :yhoo, :host => "yahoo.com", :port => 80
conn.on_data { |data| data }

Expand Down Expand Up @@ -164,4 +164,63 @@ def failed
end
}.should_not raise_error
end

context "with a server listening on a TCP port" do
before :each do
@host = '127.0.0.1'
@port = 4242
@pid = spawn("ruby spec/support/echo_server.rb #{@host} #{@port}")
sleep 1 # let the server come up
end
after :each do
Process.kill('QUIT', @pid)
end
it "should connect to a unix socket" do
connected = false
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
end
host = @host
port = @port
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :local, :host => host, :port => port
conn.on_connect do |name|
connected = true
EventMachine.stop
end
end
end
connected.should == true
end
end

context "with a server listening on a unix socket" do
before :each do
@socket = File.join(Dir.tmpdir, 'em-proxy.sock')
@pid = spawn("ruby spec/support/echo_server.rb #{@socket}")
sleep 1 # let the server come up
end
after :each do
Process.kill('QUIT', @pid)
end
it "should connect to a unix socket" do
connected = false
EM.run do
EventMachine.add_timer(0.1) do
EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get({:timeout => 1})
end
socket = @socket
Proxy.start(:host => "0.0.0.0", :port => 8080) do |conn|
conn.server :local, :socket => socket
conn.on_connect do |name|
connected = true
EventMachine.stop
end
end
end
connected.should == true
end
end

end
18 changes: 18 additions & 0 deletions spec/support/echo_server.rb
@@ -0,0 +1,18 @@
require 'eventmachine'

module EchoServer
def receive_data data
send_data ">>>you sent: #{data}"
close_connection if data =~ /quit/i
end
end

EventMachine.run do
if ARGV.count == 2
EventMachine.start_server ARGV.first, ARGV.last.to_i, EchoServer
elsif ARGV.count == 1
EventMachine.start_server ARGV.first, EchoServer
else
raise "invalid number of params, expected [server] ([port])"
end
end

0 comments on commit 0040647

Please sign in to comment.