Class: Forward::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/forward/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = {})
  @options = options
  @config  = Config.create_or_load
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/forward/client.rb', line 3

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/forward/client.rb', line 4

def options
  @options
end

#tunnelObject

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!(message = 'exiting...')
  puts message.chomp

  @session.close if @session && !@session.closed?

  Forward.log.debug('Exiting')
  send_debug_log if Forward.debug_remotely?
ensure
  Thread.main.exit
end

.currentObject



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.forwarding_message(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_logObject



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(options = {})
  Forward.log.debug('Starting client')
  trap(:INT) { cleanup_and_exit!('closing tunnel and exiting...') }

  Forward.client = @client = Client.new(options)
  @tunnel        = @client.setup_tunnel
  @session       = Net::SSH.start(@tunnel.tunneler, Forward.ssh_user, @client.ssh_options)

  Forward.log.debug("Starting remote forward at #{@tunnel.subdomain}.fwd.wf")
  puts forwarding_message(@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



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

Returns:

  • (Boolean)


12
13
14
# File 'lib/forward/client.rb', line 12

def basic_auth?
  @options[:username] && @options[:password]
end

#setup_tunnelObject

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.options)
  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_optionsObject

The options Hash used by Net::SSH.

Returns a Hash of options.



33
34
35
36
37
38
39
40
41
# File 'lib/forward/client.rb', line 33

def ssh_options
  {
    :port       => Forward.ssh_port,
    :keys_only  => true,
    :keys       => [],
    :key_data   => [ @config.private_key ],
    :encryption => 'blowfish-cbc'
  }
end