Module: BinProxy::Connection
- Includes:
- Logger, Observable
- Defined in:
- lib/binproxy/connection.rb,
lib/binproxy/connection/filters.rb
Overview
This module is included in an anonymous subclass of EM::Connection; each instance represents a TCP connection between the proxy and the client or server, so each Session has two Connections.
Defined Under Namespace
Modules: Filters
Instance Attribute Summary collapse
-
#filters ⇒ Object
readonly
Returns the value of attribute filters.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#peer ⇒ Object
readonly
Returns the value of attribute peer.
Instance Method Summary collapse
-
#connect(host = nil, port = nil, &cb) ⇒ Object
Used by filters to initiate upstream connection in response to inbound connection.
-
#connection_completed ⇒ Object
EM callback.
- #initialize(opts) ⇒ Object
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
-
#send_message(pm) ⇒ Object
called with a ProxyMessage.
- #unbind(reason) ⇒ Object
-
#upstream_connected(upstream_conn) ⇒ Object
called by session.
Methods included from Logger
Instance Attribute Details
#filters ⇒ Object (readonly)
Returns the value of attribute filters.
19 20 21 |
# File 'lib/binproxy/connection.rb', line 19 def filters @filters end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
19 20 21 |
# File 'lib/binproxy/connection.rb', line 19 def opts @opts end |
#parser ⇒ Object
Returns the value of attribute parser.
18 19 20 |
# File 'lib/binproxy/connection.rb', line 18 def parser @parser end |
#peer ⇒ Object (readonly)
Returns the value of attribute peer.
19 20 21 |
# File 'lib/binproxy/connection.rb', line 19 def peer @peer end |
Instance Method Details
#connect(host = nil, port = nil, &cb) ⇒ Object
Used by filters to initiate upstream connection in response to inbound connection
37 38 39 40 41 42 43 |
# File 'lib/binproxy/connection.rb', line 37 def connect(host=nil, port=nil, &cb) host ||= opts[:upstream_host] || raise('no upstream host') port ||= opts[:upstream_port] || raise('no upstream port') cb ||= lambda { |conn| opts[:session_callback].call(self, conn) } log.debug "Making upstream connection to #{host}:#{port}" EM.connect(host, port, Connection, opts[:upstream_args], &cb) end |
#connection_completed ⇒ Object
EM callback
46 47 48 49 50 |
# File 'lib/binproxy/connection.rb', line 46 def connection_completed log.debug "connection_completed callback" changed notify_observers(:connection_completed, self) end |
#initialize(opts) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/binproxy/connection.rb', line 21 def initialize(opts) @opts = opts @peer = opts[:peer] # :client or :server @buffer = StringIO.new @filters = opts[:filter_classes].map do |c| c.new(self) end end |
#post_init ⇒ Object
28 29 30 31 32 33 |
# File 'lib/binproxy/connection.rb', line 28 def post_init @filters.each do |f| log.debug "initializing filter #{f}" f.init end end |
#receive_data(data) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/binproxy/connection.rb', line 59 def receive_data(data) @filters.each do |f| data = f.read data return if data.nil? or data == '' end @buffer.string << data #does not update @buffer's pos parser.parse @buffer, peer do |pm| log.debug "parsed proxy message: #{pm.inspect}" changed notify_observers(:message_received, pm) end if (pos = @buffer.pos) > 0 @buffer.string = @buffer.string[pos .. -1] #resets pos to 0 end rescue Exception => e puts e, e.backtrace raise e end |
#send_message(pm) ⇒ Object
called with a ProxyMessage
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/binproxy/connection.rb', line 84 def (pm) log.error "OOPS! message going the wrong way (to #{peer})" if pm.dest != peer data = pm.to_binary_s @filters.each do |f| data = f.write data return if data.nil? or data == '' end send_data(data) end |
#unbind(reason) ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/binproxy/connection.rb', line 95 def unbind(reason) log.debug "unbind called" changed notify_observers(:connection_lost, peer, reason) rescue Exception => e puts e, e.backtrace raise e end |
#upstream_connected(upstream_conn) ⇒ Object
called by session
53 54 55 56 57 |
# File 'lib/binproxy/connection.rb', line 53 def upstream_connected(upstream_conn) @filters.each do |f| f.upstream_connected(upstream_conn) end end |