diff --git a/.gitignore b/.gitignore index ae3fdc2..9d3d280 100644 --- a/.gitignore +++ b/.gitignore @@ -1,14 +1,36 @@ -/.bundle/ -/.yardoc -/Gemfile.lock -/_yardoc/ +*.gem +*.rbc +*.log +/.config /coverage/ -/doc/ +/InstalledFiles /pkg/ /spec/reports/ +/test/tmp/ +/test/version_tmp/ /tmp/ -*.bundle -*.so -*.o -*.a -mkmf.log + +## Specific to RubyMotion: +.dat* +.repl_history +build/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalisation: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +Gemfile.lock +.ruby-version +.ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc diff --git a/lib/ngrok/tunnel.rb b/lib/ngrok/tunnel.rb index b160e13..6df0ada 100644 --- a/lib/ngrok/tunnel.rb +++ b/lib/ngrok/tunnel.rb @@ -10,7 +10,7 @@ class Error < StandardError; end class Tunnel class << self - attr_reader :pid, :ngrok_url, :ngrok_url_https, :status + attr_reader :pid, :ngrok_url, :ngrok_url_https, :ngrok_url_tcp, :status def init(params = {}) @params = {port: 3001, timeout: 10, config: '/dev/null'}.merge(params) @@ -23,7 +23,12 @@ def start(params = {}) if stopped? @params[:log] = (@params[:log]) ? File.open(@params[:log], 'w+') : Tempfile.new('ngrok') - @pid = spawn("exec ngrok http " + ngrok_exec_params) + if(@params[:config] == '/dev/null') + command = "exec ngrok http #{@params[:port]} " + ngrok_exec_params + else + command = "exec ngrok start --all " + ngrok_exec_params + end + @pid = spawn(command) at_exit { Ngrok::Tunnel.stop } @status = :running fetch_urls @@ -72,20 +77,22 @@ def inherited(subclass) private def ngrok_exec_params - exec_params = "-log=stdout -log-level=debug " - exec_params << "-authtoken=#{@params[:authtoken]} " if @params[:authtoken] - exec_params << "-subdomain=#{@params[:subdomain]} " if @params[:subdomain] - exec_params << "-config=#{@params[:config]} #{@params[:port].to_i} > #{@params[:log].path}" + exec_params = "-log stdout -log-level debug " + exec_params << "-authtoken #{@params[:authtoken]} " if @params[:authtoken] + exec_params << "-subdomain #{@params[:subdomain]} " if @params[:subdomain] + exec_params << "-config #{@params[:config]} " + exec_params << "> #{@params[:log].path}" end def fetch_urls @params[:timeout].times do log_content = @params[:log].read - result = log_content.scan(/URL:(.+)\sProto:(http|https)\s/) + result = log_content.scan(/URL:(.+)\sProto:(http|https|tcp)\s/) if !result.empty? result = Hash[*result.flatten].invert @ngrok_url = result['http'] @ngrok_url_https = result['https'] + @ngrok_url_tcp = result['tcp'] if result['tcp'] return @ngrok_url if @ngrok_url end @@ -112,4 +119,4 @@ def ensure_binary init end -end \ No newline at end of file +end diff --git a/spec/tunnel/tunnel_spec.rb b/spec/tunnel/tunnel_spec.rb index b3a869b..c8a2e67 100644 --- a/spec/tunnel/tunnel_spec.rb +++ b/spec/tunnel/tunnel_spec.rb @@ -2,7 +2,7 @@ describe Ngrok::Tunnel do describe "Before start" do - + it "should not be running" do expect(Ngrok::Tunnel.running?).to be false end @@ -49,10 +49,25 @@ expect(Ngrok::Tunnel.ngrok_url_https).to be =~ /https:\/\/.*ngrok\.io$/ end + it "ngrok_url_tcp should be nil" do + expect(Ngrok::Tunnel.ngrok_url_tcp).to eq(nil) + end + it "should have pid > 0" do expect(Ngrok::Tunnel.pid).to be > 0 end + end + + describe 'load multiple tunnels from config file' do + before(:all) do + Ngrok::Tunnel.start({ config: '~/.ngrok2/ngrok.yml' }) + end + + after(:all) { Ngrok::Tunnel.stop } + it "should have valid ngrok_url_tcp" do + expect(Ngrok::Tunnel.ngrok_url_tcp).to be =~ /tcp:\/\/.*tcp\.*ngrok\.io:\d{5}$/ + end end describe "Custom log file" do @@ -66,13 +81,9 @@ end describe "Custom subdomain" do - it "should fail without authtoken" do - expect {Ngrok::Tunnel.start(subdomain: 'test-subdomain')}.to raise_error - end - it "should fail with incorrect authtoken" do expect {Ngrok::Tunnel.start(subdomain: 'test-subdomain', authtoken: 'incorrect_token')}.to raise_error end end -end \ No newline at end of file +end