Class: Devproxy::Connection

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

Direct Known Subclasses

CLI

Defined Under Namespace

Classes: Error

Constant Summary collapse

MAX_DOTS =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, ssh) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
# File 'lib/devproxy/connection.rb', line 13

def initialize options, ssh
  @options, @ssh, @halt = options, ssh, false
  reset_dots!
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/devproxy/connection.rb', line 9

def options
  @options
end

#sshObject (readonly)

Returns the value of attribute ssh.



9
10
11
# File 'lib/devproxy/connection.rb', line 9

def ssh
  @ssh
end

Class Method Details

.create(options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/devproxy/connection.rb', line 56

def self.create(options)
  ssh        = open_ssh(options)
  connection = new(options,ssh)
  ssh.forward.remote(options.port,"localhost",0,'0.0.0.0')
  channel    = ssh.exec(options.proxy) do |ch,stream,data|
    if stream == :stdout
      connection.on_stdout(data)
    else
      connection.on_stderr(data)
    end
  end
  channel.on_close do
    connection.on_close
  end
  connection
rescue Net::SSH::AuthenticationFailed
  raise Error::Authentication, "Authentication Failed: Invalid username or SSH key"
end

.loop!(options) ⇒ Object



52
53
54
# File 'lib/devproxy/connection.rb', line 52

def self.loop!(options)
  create(options).loop!
end

.open_ssh(options) ⇒ Object



74
75
76
77
78
# File 'lib/devproxy/connection.rb', line 74

def self.open_ssh(options)
  Net::SSH.start(options.host, options.username,{
    :port => options.remote_port,
  })
end

Instance Method Details

#halt?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/devproxy/connection.rb', line 27

def halt?
  @halt
end

#loop!Object



18
19
20
21
22
23
# File 'lib/devproxy/connection.rb', line 18

def loop!
  @ssh.loop { !halt? }
rescue Errno::ECONNREFUSED
  on_stderr "CONNECTION REFUSED.  Is there anything listening on port #{options.port}?"
  retry
end

#on_closeObject



43
44
45
# File 'lib/devproxy/connection.rb', line 43

def on_close
  stop!
end

#on_stderr(data) ⇒ Object



38
39
40
41
# File 'lib/devproxy/connection.rb', line 38

def on_stderr data
  $stderr.puts "\nError: #{data}"
  reset_dots!
end

#on_stdout(data) ⇒ Object



31
32
33
34
35
36
# File 'lib/devproxy/connection.rb', line 31

def on_stdout data
  $stdout.write data
  if (@dotno += 1) % MAX_DOTS == 0
    $stdout.write "\n"
  end
end

#stop!Object



24
25
26
# File 'lib/devproxy/connection.rb', line 24

def stop!
  @halt = true
end