Class: Tunnlr::Connector

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnlr/connector.rb

Instance Method Summary collapse

Constructor Details

#initializeConnector

Returns a new instance of Connector.



7
8
9
10
# File 'lib/tunnlr/connector.rb', line 7

def initialize
  @config = read_configuration
  @should_disconnect =false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/tunnlr/connector.rb', line 49

def method_missing(name,*args)
  if @config[name.to_s]
    @config[name.to_s]
  else
    super
  end
end

Instance Method Details

#connect!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tunnlr/connector.rb', line 12

def connect!
  @should_disconnect=false
  while true do
    begin
      Net::SSH.start(host,username,:password=>password) do |ssh|
        puts "Connecting to #{username}@#{host} and sending #{remote_port}->#{local_port}"
        ssh.forward.remote_to(local_port,'127.0.0.1',remote_port,'0.0.0.0')
        puts "You can view your tunneled connection at http://web1.tunnlr.com:#{remote_port}/"
        while true
          begin
            ssh.loop {!@should_disconnect}
          rescue Errno::ECONNRESET=>e
            raise
          rescue Interrupt=>e
            raise
          rescue Exception=>e
            puts "Swallowing: #{e.class.to_s} => #{e.to_s}"
          end
        end
      end
    rescue Errno::ECONNRESET=>e
      puts "Your connection was reset, trying to restart ..."
    end
  end
rescue Net::SSH::AuthenticationFailed=>e
  puts "Unable to log in. Please check your password and try again."
end

#disconnect!Object



40
41
42
# File 'lib/tunnlr/connector.rb', line 40

def disconnect!
  @should_disconnect=true
end

#read_configurationObject



44
45
46
47
# File 'lib/tunnlr/connector.rb', line 44

def read_configuration
  path = File.join(Rails.root, "config/tunnlr.yml")
  YAML.load(File.read(path))
end