Class: Devproxy::Connection
- Inherits:
-
Object
- Object
- Devproxy::Connection
- Defined in:
- lib/devproxy/connection.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Error
Constant Summary collapse
- HEARTBEAT =
"HEARTBEAT"- MAX_DOTS =
60
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#ssh ⇒ Object
readonly
Returns the value of attribute ssh.
Class Method Summary collapse
Instance Method Summary collapse
- #halt? ⇒ Boolean
-
#initialize(options, ssh) ⇒ Connection
constructor
A new instance of Connection.
- #loop! ⇒ Object
- #on_close ⇒ Object
- #on_connected ⇒ Object
- #on_heartbeat(data) ⇒ Object
- #on_stderr(data) ⇒ Object
- #on_stdout(data) ⇒ Object
- #stop! ⇒ Object
Constructor Details
#initialize(options, ssh) ⇒ Connection
14 15 16 17 |
# File 'lib/devproxy/connection.rb', line 14 def initialize , ssh , @ssh, @halt = , ssh, false reset_dots! end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/devproxy/connection.rb', line 10 def end |
#ssh ⇒ Object (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() ssh = open_ssh() connection = new(,ssh) ssh.forward.remote(.port,.listen,0,'0.0.0.0') channel = ssh.exec(.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!() create().loop! end |
.open_ssh(options) ⇒ Object
92 93 94 95 96 97 |
# File 'lib/devproxy/connection.rb', line 92 def self.open_ssh() Net::SSH.start(.host, .username,{ :port => .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_close ⇒ Object
56 57 58 |
# File 'lib/devproxy/connection.rb', line 56 def on_close stop! end |
#on_connected ⇒ Object
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 .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 .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 .verbose $stdout.puts(data) reset_dots! end |
#stop! ⇒ Object
25 26 27 |
# File 'lib/devproxy/connection.rb', line 25 def stop! @halt = true end |