Class: Forward::Client
- Inherits:
-
Object
- Object
- Forward::Client
- Defined in:
- lib/forward/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#tunnel ⇒ Object
Returns the value of attribute tunnel.
Class Method Summary collapse
- .cleanup_and_exit!(message = 'exiting...') ⇒ Object
- .current ⇒ Object
- .forwarding_message(tunnel) ⇒ Object
- .send_debug_log ⇒ Object
- .start(options = {}) ⇒ Object
- .watch_session(session) ⇒ Object
Instance Method Summary collapse
- #basic_auth? ⇒ Boolean
-
#initialize(options = {}) ⇒ Client
constructor
A new instance of Client.
-
#setup_tunnel ⇒ Object
Sets up a Tunnel instance and adds it to the Client.
-
#ssh_options ⇒ Object
The options Hash used by Net::SSH.
Constructor Details
#initialize(options = {}) ⇒ Client
Returns a new instance of Client.
7 8 9 10 |
# File 'lib/forward/client.rb', line 7 def initialize( = {}) = @config = Config.create_or_load end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
3 4 5 |
# File 'lib/forward/client.rb', line 3 def config @config end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/forward/client.rb', line 4 def end |
#tunnel ⇒ Object
Returns the value of attribute tunnel.
5 6 7 |
# File 'lib/forward/client.rb', line 5 def tunnel @tunnel end |
Class Method Details
.cleanup_and_exit!(message = 'exiting...') ⇒ Object
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/forward/client.rb', line 94 def self.cleanup_and_exit!( = 'exiting...') puts .chomp @session.close if @session && !@session.closed? Forward.log.debug('Exiting') send_debug_log if Forward.debug_remotely? ensure Thread.main.exit end |
.current ⇒ Object
46 47 48 |
# File 'lib/forward/client.rb', line 46 def self.current @client end |
.forwarding_message(tunnel) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/forward/client.rb', line 55 def self.(tunnel) remote = HighLine.color("https://#{@tunnel.subdomain}.fwd.wf", :underline) unless tunnel.cname.nil? || tunnel.cname.empty? remote << ' and '<< HighLine.color("http://#{@tunnel.cname}", :underline) end if !tunnel.vhost.nil? && tunnel.vhost !~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/ local = tunnel.vhost local << " port #{tunnel.hostport}" unless tunnel.hostport.to_i == 80 else local = "port #{tunnel.hostport}" end "Forwarding #{local} to #{remote}\nCtrl-C to stop forwarding" end |
.send_debug_log ⇒ Object
105 106 107 108 |
# File 'lib/forward/client.rb', line 105 def self.send_debug_log log = Forward.stringio_log.string Forward::Api::ClientLog.create(log) end |
.start(options = {}) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/forward/client.rb', line 72 def self.start( = {}) Forward.log.debug('Starting client') trap(:INT) { cleanup_and_exit!('closing tunnel and exiting...') } Forward.client = @client = Client.new() @tunnel = @client.setup_tunnel @session = Net::SSH.start(@tunnel.tunneler, Forward.ssh_user, @client.) Forward.log.debug("Starting remote forward at #{@tunnel.subdomain}.fwd.wf") puts (@tunnel) @session.forward.remote(@tunnel.hostport, @tunnel.host, @tunnel.port) @session.loop { watch_session(@session) } rescue Net::SSH::AuthenticationFailed => e Forward.log.fatal("SSH Auth failed `#{e}'") cleanup_and_exit!("Authentication failed, try deleting `#{Forward::Config.config_path}' and giving it another go. If the problem continues, contact [email protected]") rescue => e Forward.log.fatal("#{e.message}\n#{e.backtrace.join("\n")}") cleanup_and_exit!("You've been disconnected...") end |
.watch_session(session) ⇒ Object
50 51 52 53 |
# File 'lib/forward/client.rb', line 50 def self.watch_session(session) @client.tunnel.inactive_for = 0 if session.busy?(true) true end |
Instance Method Details
#basic_auth? ⇒ Boolean
12 13 14 |
# File 'lib/forward/client.rb', line 12 def basic_auth? [:username] && [:password] end |
#setup_tunnel ⇒ Object
Sets up a Tunnel instance and adds it to the Client.
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/forward/client.rb', line 17 def setup_tunnel Forward.log.debug('Setting up tunnel') @tunnel = Forward::Tunnel.new(self.) if @tunnel.id @tunnel.poll_status else Forward::Client.cleanup_and_exit!('Unable to create a tunnel. If this continues contact [email protected]') end Forward.log.debug("Tunnel setup: #{@tunnel.inspect}") @tunnel end |
#ssh_options ⇒ Object
The options Hash used by Net::SSH.
Returns a Hash of options.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/forward/client.rb', line 33 def { :config => false, :port => Forward.ssh_port, :keys_only => true, :keys => [], :key_data => [ @config.private_key ], :encryption => 'blowfish-cbc', :logger => Forward.logger, :verbose => Forward.debug? ? :debug : :warn } end |