Skip to content

Commit

Permalink
Merge pull request #23 from texpert/develop
Browse files Browse the repository at this point in the history
Prepare for different ngrok versions params, still for version 2
  • Loading branch information
texpert authored Nov 19, 2022
2 parents 0b986e9 + 768c313 commit f8aa216
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/ngrok/wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
require 'tempfile'

module Ngrok
OPTIONAL_PARAMS = %i[authtoken bind_tls host_header hostname inspect region subdomain].freeze
VERSION_PARAMS = {
'2' => {
flag_prefix: '-',
http_tunnel_flags: %i[config authtoken bind_tls host_header hostname inspect region subdomain].freeze
}
}.freeze

class NotFound < StandardError; end
class FetchUrlError < StandardError; end
Expand Down Expand Up @@ -152,11 +157,13 @@ def spawn_new_ngrok(persistent_ngrok:)
end

def ngrok_exec_params
exec_params = +'-log=stdout -log-level=debug '
OPTIONAL_PARAMS.each do |opt|
exec_params << "-#{opt.to_s.tr('_', '-')}=#{@params[opt]} " if @params.key?(opt)
current_version_params = VERSION_PARAMS[@major_version]
flag_prefix = current_version_params[:flag_prefix]
exec_params = +"#{flag_prefix}log=stdout #{flag_prefix}log-level=debug "
current_version_params[:http_tunnel_flags].each do |flag|
exec_params << "#{flag_prefix}#{flag.to_s.tr('_', '-')}=#{@params[flag]} " if @params.key?(flag)
end
exec_params << "-config #{@params[:config]} #{@params[:addr]} > #{@params[:log].path}"
exec_params << " #{@params[:addr]} > #{@params[:log].path}"
end

def fetch_urls
Expand Down Expand Up @@ -191,7 +198,7 @@ def scan_log_for_urls
end

def ensure_binary
`ngrok version`
@major_version, * = `ngrok version`.chomp.delete_prefix('ngrok version ').split('.')
rescue Errno::ENOENT
raise Ngrok::NotFound, 'Ngrok binary not found'
end
Expand Down
1 change: 1 addition & 0 deletions spec/ngrok/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
let(:fake_pid) { rand(99_999) }

before do
Ngrok::Wrapper.instance_variable_set(:@major_version, '2')
allow(Ngrok::Wrapper).to receive(:ensure_binary)
allow(Ngrok::Wrapper).to receive(:raise_if_similar_ngroks)
allow(Process).to receive(:spawn).and_return(fake_pid)
Expand Down

0 comments on commit f8aa216

Please sign in to comment.