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

HEARTBEAT =
"HEARTBEAT"
MAX_DOTS =
60

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options, ssh) ⇒ Connection



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#sshObject (readonly)

Returns the value of attribute ssh.



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

def ssh
  @ssh
end

Class Method Details

.create(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/devproxy/connection.rb', line 69

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

.loop!(options) ⇒ Object



65
66
67
# File 'lib/devproxy/connection.rb', line 65

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

.open_ssh(options) ⇒ Object



92
93
94
95
96
97
# File 'lib/devproxy/connection.rb', line 92

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

Instance Method Details

#halt?Boolean



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

def halt?
  @halt
end

#loop!Object



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

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

#on_closeObject



56
57
58
# File 'lib/devproxy/connection.rb', line 56

def on_close
  stop!
end

#on_connectedObject



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

def on_connected
  $stdout.puts "Tunneling requests from https://#{options.proxy}.devproxy.io to #{options.listen} port #{options.port}"
end

#on_heartbeat(data) ⇒ Object



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

def on_heartbeat data
  return unless options.verbose
  $stdout.write "."
  if (@dotno += 1) % MAX_DOTS == 0
    $stdout.write "\n"
  end
end

#on_stderr(data) ⇒ Object



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

def on_stderr data
  $stdout.write("\n") if options.verbose
  $stderr.puts(data)
  reset_dots!
end

#on_stdout(data) ⇒ Object



44
45
46
47
48
# File 'lib/devproxy/connection.rb', line 44

def on_stdout data
  $stdout.write("\n") if options.verbose
  $stdout.puts(data)
  reset_dots!
end

#stop!Object



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

def stop!
  @halt = true
end