class Ngrok::Tunnel

Constants

VERSION

Attributes

ngrok_url[R]
ngrok_url_https[R]
pid[R]
status[R]

Public Class Methods

addr() click to toggle source
# File lib/ngrok/tunnel.rb, line 55
def addr
  @params[:addr]
end
authtoken() click to toggle source
# File lib/ngrok/tunnel.rb, line 72
def authtoken
  @params[:authtoken]
end
inherited(subclass) click to toggle source
# File lib/ngrok/tunnel.rb, line 76
def inherited(subclass)
  init
end
init(params = {}) click to toggle source
# File lib/ngrok/tunnel.rb, line 15
def init(params = {})
  # map old key 'port' to 'addr' to maintain backwards compatibility with versions 2.0.21 and earlier
  params[:addr] = params.delete(:port) if params.key?(:port)

  @params = {addr: 3001, timeout: 10, config: '/dev/null'}.merge(params)
  @status = :stopped unless @status
end
log() click to toggle source
# File lib/ngrok/tunnel.rb, line 64
def log
  @params[:log]
end
port() click to toggle source
# File lib/ngrok/tunnel.rb, line 59
def port
  return addr if addr.is_a?(Numeric)
  addr.split(":").last.to_i
end
running?() click to toggle source
# File lib/ngrok/tunnel.rb, line 47
def running?
  @status == :running
end
start(params = {}) click to toggle source
# File lib/ngrok/tunnel.rb, line 23
def start(params = {})
  ensure_binary
  init(params)

  if stopped?
    @params[:log] = (@params[:log]) ? File.open(@params[:log], 'w+') : Tempfile.new('ngrok')
    @pid = spawn("exec ngrok http " + ngrok_exec_params)
    at_exit { Ngrok::Tunnel.stop }
    fetch_urls
  end

  @status = :running
  @ngrok_url
end
stop() click to toggle source
# File lib/ngrok/tunnel.rb, line 38
def stop
  if running?
    Process.kill(9, @pid)
    @ngrok_url = @ngrok_url_https = @pid = nil
    @status = :stopped
  end
  @status
end
stopped?() click to toggle source
# File lib/ngrok/tunnel.rb, line 51
def stopped?
  @status == :stopped
end
subdomain() click to toggle source
# File lib/ngrok/tunnel.rb, line 68
def subdomain
  @params[:subdomain]
end

Private Class Methods

ensure_binary() click to toggle source
# File lib/ngrok/tunnel.rb, line 117
def ensure_binary
  `ngrok version`
rescue Errno::ENOENT
  raise Ngrok::NotFound, "Ngrok binary not found"
end
fetch_urls() click to toggle source
# File lib/ngrok/tunnel.rb, line 93
def fetch_urls
  @params[:timeout].times do
    log_content = @params[:log].read
    result = log_content.scan(/URL:(.+)\sProto:(http|https)\s/)
    if !result.empty?
      result = Hash[*result.flatten].invert
      @ngrok_url = result['http']
      @ngrok_url_https = result['https']
      return @ngrok_url if @ngrok_url
    end

    error = log_content.scan(/msg="command failed" err="([^"]+)"/).flatten
    unless error.empty?
      self.stop
      raise Ngrok::Error, error.first
    end

    sleep 1
    @params[:log].rewind
  end
  raise FetchUrlError, "Unable to fetch external url"
  @ngrok_url
end
ngrok_exec_params() click to toggle source
# File lib/ngrok/tunnel.rb, line 82
def ngrok_exec_params
  exec_params = "-log=stdout -log-level=debug "
  exec_params << "-region=#{@params[:region]} " if @params[:region]
  exec_params << "-host-header=#{@params[:host_header]} " if @params[:host_header]
  exec_params << "-authtoken=#{@params[:authtoken]} " if @params[:authtoken]
  exec_params << "-subdomain=#{@params[:subdomain]} " if @params[:subdomain]
  exec_params << "-hostname=#{@params[:hostname]} " if @params[:hostname]
  exec_params << "-inspect=#{@params[:inspect]} " if @params.has_key? :inspect
  exec_params << "-config=#{@params[:config]} #{@params[:addr]} > #{@params[:log].path}"
end