Module: Caldecott::Client

Defined in:
lib/caldecott/client/client.rb,
lib/caldecott/client/tunnel.rb,
lib/caldecott/client/http_tunnel.rb,
lib/caldecott/client/websocket_tunnel.rb

Defined Under Namespace

Modules: Tunnel Classes: HttpTunnel, WebSocketTunnel

Class Method Summary collapse

Class Method Details

.sanitize_url(tun_url) ⇒ Object



7
8
9
# File 'lib/caldecott/client/client.rb', line 7

def self.sanitize_url(tun_url)
  tun_url = tun_url =~ /(http|https|ws).*/i ? tun_url : "https://#{tun_url}"
end

.start(opts) ⇒ Object



11
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/caldecott/client/client.rb', line 11

def self.start(opts)
  local_port = opts[:local_port]
  tun_url    = opts[:tun_url]
  dst_host   = opts[:dst_host]
  dst_port   = opts[:dst_port]
  log_file   = opts[:log_file]
  log_level  = opts[:log_level]
  auth_token = opts[:auth_token]

  @quiet = opts[:quiet]

  trap("TERM") { stop }
  trap("INT") { stop }

  tun_url = sanitize_url(tun_url)

  EM.run do
    unless @quiet
      puts "Starting local server on port #{local_port} to #{tun_url}"
    end

    EM.start_server("0.0.0.0", local_port, TcpConnection) do |conn|
      # avoid races between tunnel setup and incoming local data
      conn.pause

      log = SessionLogger.new("client", log_file)
      log.level = SessionLogger.severity_from_string(log_level)

      tun = nil

      conn.onopen do
        log.debug "local connected"
        tun = Tunnel.start(log, tun_url, dst_host, dst_port, auth_token)
      end

      tun.onopen do
        log.debug "tunnel connected"
        conn.resume
      end

      conn.onreceive do |data|
        log.debug "l -> t #{data.length}"
        tun.send_data(data)
      end

      tun.onreceive do |data|
        log.debug("l <- t #{data.length}")
        conn.send_data(data)
      end

      conn.onclose do
        log.debug "local closed"
        tun.close
      end

      tun.onclose do
        log.debug "tunnel closed"
        conn.close_connection_after_writing
      end
    end
  end
end

.stopObject



74
75
76
77
# File 'lib/caldecott/client/client.rb', line 74

def self.stop
  puts "Caldecott shutting down" unless @quiet
  EM.stop
end