Module: ClerkRails::Tunnel
- Defined in:
- lib/clerk_rails/tunnel.rb
Class Method Summary collapse
- .crt_path ⇒ Object
- .crt_ready? ⇒ Boolean
- .data_dir ⇒ Object
- .key_path ⇒ Object
- .ngrok_path ⇒ Object
- .ngrok_ready? ⇒ Boolean
- .ngrok_zip_path ⇒ Object
- .save_tunnel_cert_locally!(certificate, certificate_key) ⇒ Object
- .setup_ngrok! ⇒ Object
- .start!(certificate:, certificate_key:, authorization:) ⇒ Object
Class Method Details
.crt_path ⇒ Object
7 8 9 |
# File 'lib/clerk_rails/tunnel.rb', line 7 def self.crt_path data_dir.join("dev.crt") end |
.crt_ready? ⇒ 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_dir ⇒ Object
3 4 5 |
# File 'lib/clerk_rails/tunnel.rb', line 3 def self.data_dir Rails.root.join(".clerk") end |
.key_path ⇒ Object
11 12 13 |
# File 'lib/clerk_rails/tunnel.rb', line 11 def self.key_path data_dir.join("dev.key") end |
.ngrok_path ⇒ Object
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
27 28 29 |
# File 'lib/clerk_rails/tunnel.rb', line 27 def self.ngrok_ready? File.exist? ngrok_path end |
.ngrok_zip_path ⇒ Object
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.instance_variable_get(:@options).dup if !server.send(:use_puma?) raise "Sorry, Clerk currently only supports Rails using the Puma server." elsif [:user_supplied_options].include? :Host raise "Sorry, Clerk cannot boot with a custom host: #{[:Host]}" else [:user_supplied_options] << :Host [:Host] = "127.0.0.1" server.instance_variable_set(:@options, ) end require 'ngrok/tunnel' self.patch_ngrok_gem puts "=> Booting https://#{ClerkRails::ENV[:host]} with Clerk" = { addr: [:Port], authtoken: , hostname: "#{ClerkRails::ENV[:host]}", region: "us", crt: Rails.root.join(".clerk/dev.crt"), key: Rails.root.join(".clerk/dev.key") } Ngrok::Tunnel.start() end |