Class: RTunnel::Client

Inherits:
Object
  • Object
show all
Includes:
RTunnel, Logging
Defined in:
lib/rtunnel/client.rb

Defined Under Namespace

Classes: ServerConnection, TunnelConnection

Constant Summary

Constants included from RTunnel

DEFAULT_CONTROL_PORT, KEEP_ALIVE_INTERVAL, TUNNEL_TIMEOUT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#D, #E, #F, #I, #W, #init_log

Methods included from RTunnel

resolve_address, run_client, run_server

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
# File 'lib/rtunnel/client.rb', line 17

def initialize(options = {})
  process_options options
  @connections = {}
  @server_connection = nil
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



15
16
17
# File 'lib/rtunnel/client.rb', line 15

def connections
  @connections
end

#control_addressObject (readonly)

Returns the value of attribute control_address.



11
12
13
# File 'lib/rtunnel/client.rb', line 11

def control_address
  @control_address
end

#control_hostObject (readonly)

Returns the value of attribute control_host.



11
12
13
# File 'lib/rtunnel/client.rb', line 11

def control_host
  @control_host
end

#control_portObject (readonly)

Returns the value of attribute control_port.



11
12
13
# File 'lib/rtunnel/client.rb', line 11

def control_port
  @control_port
end

#loggerObject (readonly)

Returns the value of attribute logger.



14
15
16
# File 'lib/rtunnel/client.rb', line 14

def logger
  @logger
end

#private_keyObject (readonly)

Returns the value of attribute private_key.



13
14
15
# File 'lib/rtunnel/client.rb', line 13

def private_key
  @private_key
end

#remote_listen_addressObject (readonly)

Returns the value of attribute remote_listen_address.



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

def remote_listen_address
  @remote_listen_address
end

#server_connectionObject (readonly)

Returns the value of attribute server_connection.



15
16
17
# File 'lib/rtunnel/client.rb', line 15

def server_connection
  @server_connection
end

#tunnel_timeoutObject (readonly)

Returns the value of attribute tunnel_timeout.



13
14
15
# File 'lib/rtunnel/client.rb', line 13

def tunnel_timeout
  @tunnel_timeout
end

#tunnel_to_addressObject (readonly)

Returns the value of attribute tunnel_to_address.



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

def tunnel_to_address
  @tunnel_to_address
end

Class Method Details

.extract_control_address(address) ⇒ Object



57
58
59
60
61
62
# File 'lib/rtunnel/client.rb', line 57

def self.extract_control_address(address)
  unless SocketFactory.port_from_address address
    address = "#{address}:#{RTunnel::DEFAULT_CONTROL_PORT}"
  end
  RTunnel.resolve_address address
end

.extract_private_key(key_file) ⇒ Object



80
81
82
# File 'lib/rtunnel/client.rb', line 80

def self.extract_private_key(key_file)
  key_file and Crypto.read_private_key key_file
end

.extract_remote_listen_address(address) ⇒ Object



64
65
66
67
68
69
# File 'lib/rtunnel/client.rb', line 64

def self.extract_remote_listen_address(address)
  unless SocketFactory.port_from_address address
    address = "0.0.0.0:#{address}"
  end
  RTunnel.resolve_address address
end

.extract_tunnel_timeout(timeout) ⇒ Object



76
77
78
# File 'lib/rtunnel/client.rb', line 76

def self.extract_tunnel_timeout(timeout)
  timeout || RTunnel::TUNNEL_TIMEOUT
end

.extract_tunnel_to_address(address) ⇒ Object



71
72
73
74
# File 'lib/rtunnel/client.rb', line 71

def self.extract_tunnel_to_address(address)
  address = "localhost:#{address}" if address =~ /^\d+$/    
  RTunnel.resolve_address address
end

Instance Method Details

#connect_to_serverObject



30
31
32
33
34
# File 'lib/rtunnel/client.rb', line 30

def connect_to_server
  D "Connecting to #{@control_host} port #{@control_port}"
  @server_connection = EventMachine.connect @control_host, @control_port,
                                             Client::ServerConnection, self
end

#process_options(options) ⇒ Object

option processing



47
48
49
50
51
52
53
54
55
# File 'lib/rtunnel/client.rb', line 47

def process_options(options)
  [:control_address, :remote_listen_address, :tunnel_to_address,
   :tunnel_timeout, :private_key].each do |opt|
    instance_variable_set "@#{opt}".to_sym,
        RTunnel::Client.send("extract_#{opt}".to_sym, options[opt])
  end
  
  init_log :level => options[:log_level]    
end

#startObject



23
24
25
26
27
28
# File 'lib/rtunnel/client.rb', line 23

def start
  return if @server_connection
  @control_host = SocketFactory.host_from_address @control_address
  @control_port = SocketFactory.port_from_address @control_address
  connect_to_server
end

#stopObject



36
37
38
39
40
41
42
43
# File 'lib/rtunnel/client.rb', line 36

def stop
  @connections.each { |connection| connection.close_connection_after_writing }
  
  return unless @server_connection
  @server_connection.close_connection_after_writing
  @server_connection.disable_tunnel_timeouts
  @server_connection = nil
end