-
Notifications
You must be signed in to change notification settings - Fork 86
/
em-proxy
executable file
·48 lines (35 loc) · 1.25 KB
/
em-proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env ruby
lib = File.expand_path(File.dirname(__FILE__) + '/../lib')
$LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
require 'em-proxy'
require 'optparse'
ARGV << '--help' if ARGV.empty?
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: em-proxy [options]"
opts.on("-l", "--listen [PORT]", Integer, "Port to listen on") do |v|
options[:listen] = v
end
opts.on("-d", "--duplex [host:port, ...]", Array, "List of backends to duplex data to") do |v|
options[:duplex] = v
end
opts.on("-r", "--relay [hostname:port]", String, "Relay endpoint: hostname:port") do |v|
options[:relay] = v.split(":")
end
opts.on("-v", "--verbose", "Run in debug mode") do |v|
options[:verbose] = v
end
end.parse!
Proxy.start(:host => "0.0.0.0", :port => options[:listen] , :debug => options[:verbose]) do |conn|
conn.server :relay, :host => options[:relay].first, :port => options[:relay].last.to_i
options[:duplex].each_with_index do |backend,i|
hostname, port = backend.split(":")
conn.server "backend_#{i}".intern, :host => hostname, :port => port.to_i
end if options[:duplex]
conn.on_data do |data|
data
end
conn.on_response do |server, resp|
resp if server == :relay
end
end