A helper class for managing ports forwarded from a remote host to the local host.
Methods
Public Class methods
Instantiate a new RemoteNetworkHandler instance that will forward data to the given local port and host address.
[ show source ]
# File lib/net/ssh/service/forward/remote-network-handler.rb, line 30
30: def initialize( log, block_size, local_port, local_host='127.0.0.1' )
31: @log = log
32: @block_size = block_size
33: @local_port = local_port
34: @local_host = local_host
35: end
Public Instance methods
Invoked when the channel is closed.
[ show source ]
# File lib/net/ssh/service/forward/remote-network-handler.rb, line 69
69: def on_close( channel )
70: @client.shutdown
71: end
Invoked when the channel indicates that the end is near.
[ show source ]
# File lib/net/ssh/service/forward/remote-network-handler.rb, line 64
64: def on_eof( channel )
65: channel[:eof] = true
66: end
Opens a new socket to the local host and port given when the handler was created, and forwards data from the channel to that connection.
[ show source ]
# File lib/net/ssh/service/forward/remote-network-handler.rb, line 40
40: def on_open( channel, c_addr, c_port, o_addr, o_port )
41: @client = TCPSocket.new( @local_host, @local_port )
42:
43: Thread.new do
44: begin
45: loop do
46: break if channel[:eof]
47: data = ""
48: while IO.select([@client],nil,nil,0.01)
49: data << @client.recv(@block_size)
50: end
51: channel.send_data data unless data.empty?
52: end
53: rescue Exception => e
54: @log.error "error while forwarding remote port: " +
55: "#{e.class}: #{e.message}\n " +
56: e.backtrace.join( "\n " )
57: ensure
58: channel.close
59: end
60: end
61: end
Invoked when data is received over the channel.
[ show source ]
# File lib/net/ssh/service/forward/remote-network-handler.rb, line 74
74: def on_receive( channel, data )
75: @client.send data, 0
76: end