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( = {}) @options = @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 @options 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
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/forward/client.rb', line 91 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
43 44 45 |
# File 'lib/forward/client.rb', line 43 def self.current @client end |
.forwarding_message(tunnel) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/forward/client.rb', line 52 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
102 103 104 105 |
# File 'lib/forward/client.rb', line 102 def self.send_debug_log log = Forward.stringio_log.string Forward::Api::ClientLog.create(log) end |
.start(options = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/forward/client.rb', line 69 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.}\n#{e.backtrace.join("\n")}") cleanup_and_exit!("You've been disconnected...") end |
.watch_session(session) ⇒ Object
47 48 49 50 |
# File 'lib/forward/client.rb', line 47 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? @options[:username] && @options[: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 |