Class: TunnelRb::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tunnel_rb/client.rb

Constant Summary collapse

TCP_KEEPALIVE_IDLE =
60
TCP_KEEPALIVE_INTERVAL =
30
TCP_KEEPALIVE_PROBES =
3
CONNECT_TIMEOUT =
5
RECONNECT_INITIAL_DELAY =
1
RECONNECT_MAX_DELAY =
30
LOCAL_UNREACHABLE_ERRORS =
[
  Errno::ECONNREFUSED,
  Errno::EHOSTUNREACH,
  Errno::ENETUNREACH,
  Errno::ETIMEDOUT,
  SocketError
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(server_host:, server_port:, local_host: "localhost", local_port:, tls: false, tls_ca: nil, tls_verify: false) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tunnel_rb/client.rb', line 25

def initialize(server_host:, server_port:, local_host: "localhost", local_port:, tls: false, tls_ca: nil, tls_verify: false)
  @server_host = server_host
  @server_port = server_port
  @local_host = local_host
  @local_port = local_port
  @tls = tls
  @tls_ca = tls_ca
  @tls_verify = tls_verify
  @token = nil
  @session_mutex = Mutex.new
  @tls_session = nil
  @ssl_context = build_ssl_context if @tls
end

Instance Method Details

#startObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tunnel_rb/client.rb', line 39

def start
  attempt = 0
  loop do
    connect_and_run { attempt = 0 }
  rescue Interrupt
    puts "\n[Tunnel] Shutting down..."
    break
  rescue => e
    delay = [RECONNECT_INITIAL_DELAY * (2**attempt), RECONNECT_MAX_DELAY].min
    attempt += 1
    puts "[Tunnel] Disconnected: #{e.message}. Reconnecting in #{delay}s..."
    sleep delay
  end
ensure
  @control_socket&.close
end