Class: HrrRbSsh::Connection::GlobalRequestHandler
- Inherits:
-
Object
- Object
- HrrRbSsh::Connection::GlobalRequestHandler
- Defined in:
- lib/hrr_rb_ssh/connection/global_request_handler.rb
Instance Attribute Summary collapse
-
#accepted ⇒ Object
readonly
Returns the value of attribute accepted.
Instance Method Summary collapse
- #cancel_tcpip_forward(message) ⇒ Object
- #close ⇒ Object
-
#initialize(connection) ⇒ GlobalRequestHandler
constructor
A new instance of GlobalRequestHandler.
- #request(message) ⇒ Object
- #tcpip_forward(message) ⇒ Object
Constructor Details
#initialize(connection) ⇒ GlobalRequestHandler
Returns a new instance of GlobalRequestHandler.
13 14 15 16 17 18 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 13 def initialize connection @logger = Logger.new self.class.name @connection = connection @tcpip_forward_servers = Hash.new @tcpip_forward_threads = Hash.new end |
Instance Attribute Details
#accepted ⇒ Object (readonly)
Returns the value of attribute accepted.
10 11 12 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 10 def accepted @accepted end |
Instance Method Details
#cancel_tcpip_forward(message) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 73 def cancel_tcpip_forward @logger.info { "canceling tcpip-forward" } address_to_bind = [:'address to bind'] port_number_to_bind = [:'port number to bind'] id = "#{address_to_bind}:#{port_number_to_bind}" @tcpip_forward_threads[id].exit begin @tcpip_forward_servers[id].close rescue IOError # for compatibility for Ruby version < 2.3 Thread.pass end @tcpip_forward_threads.delete id @tcpip_forward_servers.delete id @logger.info { "tcpip-forward canceled" } end |
#close ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 20 def close @logger.info { "closing tcpip-forward" } @tcpip_forward_threads.values.each(&:exit) @tcpip_forward_servers.values.each{ |s| begin s.close rescue IOError # for compatibility for Ruby version < 2.3 Thread.pass end } @tcpip_forward_threads.clear @tcpip_forward_servers.clear @logger.info { "tcpip-forward closed" } end |
#request(message) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 35 def request case [:'request name'] when "tcpip-forward" tcpip_forward when "cancel-tcpip-forward" cancel_tcpip_forward else @logger.warn { "unsupported request name: #{[:'request name']}" } raise end end |
#tcpip_forward(message) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 47 def tcpip_forward @logger.info { "starting tcpip-forward" } begin address_to_bind = [:'address to bind'] port_number_to_bind = [:'port number to bind'] id = "#{address_to_bind}:#{port_number_to_bind}" server = TCPServer.new address_to_bind, port_number_to_bind @tcpip_forward_servers[id] = server @tcpip_forward_threads[id] = Thread.new(server){ |server| begin loop do Thread.new(server.accept){ |s| @connection.channel_open_start address_to_bind, port_number_to_bind, s } end rescue => e @logger.error { [e.backtrace[0], ": ", e., " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join } end } @logger.info { "tcpip-forward started" } rescue => e @logger.warn { "starting tcpip-forward failed: #{e.}" } raise e end end |