Module: Clerk::Tunnel

Defined in:
lib/clerk/tunnel.rb

Class Method Summary collapse

Class Method Details

.acc_pathObject



23
24
25
# File 'lib/clerk/tunnel.rb', line 23

def self.acc_path
  data_dir.join("account.key")
end

.crt_pathObject



7
8
9
# File 'lib/clerk/tunnel.rb', line 7

def self.crt_path
  data_dir.join("dev.crt")
end

.crt_ready?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/clerk/tunnel.rb', line 27

def self.crt_ready?
  File.exist? key_path and File.exist? crt_path
end

.data_dirObject



3
4
5
# File 'lib/clerk/tunnel.rb', line 3

def self.data_dir
  Rails.root.join(".clerk")
end

.key_pathObject



11
12
13
# File 'lib/clerk/tunnel.rb', line 11

def self.key_path
  data_dir.join("dev.key")
end

.ngrok_pathObject



15
16
17
# File 'lib/clerk/tunnel.rb', line 15

def self.ngrok_path
  data_dir.join("clerk_#{executable_type}")
end

.ngrok_ready?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/clerk/tunnel.rb', line 31

def self.ngrok_ready?
  File.exist? ngrok_path
end

.ngrok_zip_pathObject



19
20
21
# File 'lib/clerk/tunnel.rb', line 19

def self.ngrok_zip_path
  data_dir.join("clerk_#{executable_type}.zip")
end

.save_tunnel_cert_locally!Object



72
73
74
75
76
# File 'lib/clerk/tunnel.rb', line 72

def self.save_tunnel_cert_locally!
  Dir.mkdir(data_dir) unless Dir.exist? data_dir
  File.write(crt_path, Clerk.config.tunnel_cert)
  File.write(key_path, Clerk.config.tunnel_key)
end

.setup_ngrok!Object



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
# File 'lib/clerk/tunnel.rb', line 35

def self.setup_ngrok!
  ngrok_paths = {
    darwin_amd64: "/c/4VmDzA7iaHb/ngrok-stable-darwin-amd64.zip",
    darwin_386: "/c/4VmDzA7iaHb/ngrok-stable-darwin-386.zip",
    windows_amd64: "/c/4VmDzA7iaHb/ngrok-stable-windows-amd64.zip",
    windows_386: "/c/4VmDzA7iaHb/ngrok-stable-windows-386.zip",
    freebsd_amd64: "/c/4VmDzA7iaHb/ngrok-stable-freebsd-amd64.zip",
    freebsd_386: "/c/4VmDzA7iaHb/ngrok-stable-freebsd-386.zip",
    linux_amd64: "/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip",
    linux_386: "/c/4VmDzA7iaHb/ngrok-stable-linux-386.zip",
    linux_arm: "/c/4VmDzA7iaHb/ngrok-stable-linux-arm.zip",
    linux_arm64: "/a/nmkK3DkqZEB/ngrok-2.2.8-linux-arm64.zip",
  }

  puts "=> [Clerk] Downloading tunnel executable."
  require 'zip'
  http = Net::HTTP.new("bin.equinox.io", 443)
  http.use_ssl = true
  resp = http.get(ngrok_paths[executable_type])
  open(ngrok_zip_path, "wb") do |file|
    file.write(resp.body)
  end

  puts "=> [Clerk] Unzipping tunnel executable."
  Zip::File.open(ngrok_zip_path) do |zipfile|
    zipfile.each do |file|
      if file.name == "ngrok"
        zipfile.extract(file, ngrok_path)
      end
    end
  end

  File.delete(ngrok_zip_path) 

  puts "=> [Clerk] Setup done."
end

.start!Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/clerk/tunnel.rb', line 78

def self.start!
  if Clerk.config.tunnel_cert == nil || Clerk.config.tunnel_key == nil
    raise "=> [Clerk] Sorry, your dev certificate isn't ready yet.  It may take up to 30 minutes to create."
  end

  save_tunnel_cert_locally!

  setup_ngrok! unless ngrok_ready?

  self.patch_rack_requests

  server = ObjectSpace.each_object(Rails::Server).first
  server_options = server.instance_variable_get(:@options).dup
  if !server.send(:use_puma?)
    raise "Sorry, Clerk currently only supports Rails using the Puma server."
  elsif server_options[:user_supplied_options].include? :Host
    raise "Sorry, Clerk cannot boot with a custom host: #{server_options[:Host]}"
  else
    server_options[:user_supplied_options] << :Host
    server_options[:Host] = "127.0.0.1"
    # server_options[:Host] = "ssl://127.0.0.1:#{server_options[:Port]}?key=.clerk/dev.key&cert=.clerk/dev.crt"
    server.instance_variable_set(:@options, server_options)
  end

  require 'ngrok/tunnel'
  self.patch_ngrok_gem
  puts "=> Booting https://#{Clerk.config.app_host} with Clerk"
  options = {
    addr: server_options[:Port],
    authtoken: Clerk.config.ngrok_authtoken,
    hostname: Clerk.config.app_host,
    region: "us",
    crt: Rails.root.join(".clerk/dev.crt"),
    key: Rails.root.join(".clerk/dev.key")
  }
  Ngrok::Tunnel.start(options)
end