Skip to content

Commit

Permalink
Added multiple tunnels from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoiani committed May 11, 2015
1 parent 956f01e commit 80ef3d9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 24 deletions.
42 changes: 32 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
23 changes: 15 additions & 8 deletions lib/ngrok/tunnel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -112,4 +119,4 @@ def ensure_binary
init

end
end
end
23 changes: 17 additions & 6 deletions spec/tunnel/tunnel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
end

0 comments on commit 80ef3d9

Please sign in to comment.