Skip to content
Browse files

Add support for proxying to a unix domain socket.

  • Loading branch information...
1 parent b8a75ca commit 00406477b2c7d54dd019da8286e0497a48b72d1c @dblock dblock committed Dec 11, 2012
Showing with 96 additions and 7 deletions.
  1. +2 −0 .rspec
  2. +2 −2 Gemfile.lock
  3. +13 −4 lib/em-proxy/connection.rb
  4. +1 −0 spec/helper.rb
  5. +60 −1 spec/proxy_spec.rb
  6. +18 −0 spec/support/echo_server.rb
View
2 .rspec
@@ -0,0 +1,2 @@
+--format=documentation
+--color
View
4 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- em-proxy (0.1.6)
+ em-proxy (0.1.7)
eventmachine
GEM
@@ -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)
View
17 lib/em-proxy/connection.rb
@@ -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
View
1 spec/helper.rb
@@ -1,5 +1,6 @@
require 'bundler/setup'
require 'em-http'
require 'pp'
+require 'tmpdir'
require 'em-proxy'
View
61 spec/proxy_spec.rb
@@ -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 }
@@ -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
View
18 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.
Something went wrong with that request. Please try again.