Class: HrrRbSsh::Connection::GlobalRequestHandler

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/hrr_rb_ssh/connection/global_request_handler.rb

Instance Attribute Summary collapse

Attributes included from Loggable

#log_key, #logger

Instance Method Summary collapse

Methods included from Loggable

#log_debug, #log_error, #log_fatal, #log_info, #log_warn

Constructor Details

#initialize(connection, logger: nil) ⇒ GlobalRequestHandler

Returns a new instance of GlobalRequestHandler.



15
16
17
18
19
20
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 15

def initialize connection, logger: nil
  self.logger = logger
  @connection = connection
  @tcpip_forward_servers = Hash.new
  @tcpip_forward_threads = Hash.new
end

Instance Attribute Details

#acceptedObject (readonly)

Returns the value of attribute accepted.



12
13
14
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 12

def accepted
  @accepted
end

Instance Method Details

#cancel_tcpip_forward(message) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 75

def cancel_tcpip_forward message
  log_info { "canceling tcpip-forward" }
  address_to_bind     = message[:'address to bind']
  port_number_to_bind = message[:'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
  log_info { "tcpip-forward canceled" }
end

#closeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 22

def close
  log_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
  log_info { "tcpip-forward closed" }
end

#request(message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 37

def request message
  case message[:'request name']
  when "tcpip-forward"
    tcpip_forward message
  when "cancel-tcpip-forward"
    cancel_tcpip_forward message
  else
    log_warn { "unsupported request name: #{message[:'request name']}" }
    raise
  end
end

#tcpip_forward(message) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/hrr_rb_ssh/connection/global_request_handler.rb', line 49

def tcpip_forward message
  log_info { "starting tcpip-forward" }
  begin
    address_to_bind     = message[:'address to bind']
    port_number_to_bind = message[:'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
        log_error { [e.backtrace[0], ": ", e.message, " (", e.class.to_s, ")\n\t", e.backtrace[1..-1].join("\n\t")].join }
      end
    }
    log_info { "tcpip-forward started" }
  rescue => e
    log_warn { "starting tcpip-forward failed: #{e.message}" }
    raise e
  end
end