Class: HrrRbSsh::Connection::GlobalRequestHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#acceptedObject (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 message
  @logger.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
  @logger.info { "tcpip-forward canceled" }
end

#closeObject



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 message
  case message[:'request name']
  when "tcpip-forward"
    tcpip_forward message
  when "cancel-tcpip-forward"
    cancel_tcpip_forward message
  else
    @logger.warn { "unsupported request name: #{message[:'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 message
  @logger.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
        @logger.error { [e.backtrace[0], ": ", e.message, " (", 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.message}" }
    raise e
  end
end