Module: ClerkRails::Tunnel

Defined in:
lib/clerk_rails/tunnel.rb

Class Method Summary collapse

Class Method Details

.crt_pathObject



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

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

.crt_ready?Boolean

Returns:

  • (Boolean)


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

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

.data_dirObject



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

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

.key_pathObject



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

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

.ngrok_pathObject



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

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

.ngrok_ready?Boolean

Returns:

  • (Boolean)


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

def self.ngrok_ready?
  File.exist? ngrok_path
end

.ngrok_zip_pathObject



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

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

.save_tunnel_cert_locally!(certificate, certificate_key) ⇒ Object



68
69
70
71
72
# File 'lib/clerk_rails/tunnel.rb', line 68

def self.save_tunnel_cert_locally!(certificate, certificate_key)
  Dir.mkdir(data_dir) unless Dir.exist? data_dir
  File.write(crt_path, certificate)
  File.write(key_path, certificate_key)
end

.setup_ngrok!Object



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

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!(certificate:, certificate_key:, authorization:) ⇒ Object



74
75
76
77
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
# File 'lib/clerk_rails/tunnel.rb', line 74

def self.start!(certificate:, certificate_key:, authorization:)
  save_tunnel_cert_locally!(certificate, certificate_key)

  setup_ngrok! unless ngrok_ready?

  # Ngrok currently does not send an X-Forwarded-Proto header with requests,
  # which causes Rack to interpret them as HTTP instead of HTTPS.  This patches
  # Rack so it treats everthing as HTTPS
  self.patch_rack_requests

  # Ngrok only worked properly if the host was specified as 127.0.0.1, but
  # the default was 0.0.0.0.  This changes the host to 127.0.0.1
  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.instance_variable_set(:@options, server_options)
  end

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