class Forward::Tunnel

Attributes

cname[R]

The CNAME for the Tunnel

host[R]

The host to forward requests to

id[R]

The Tunnel resource ID

last_active_at[RW]
no_auth[R]
password[R]
port[R]

The port to forward requests to

requests[R]
socket[R]
socket_url[R]
subdomain[R]

The domain for the Tunnel

timeout[R]

The timeout

tunneler[R]

The tunneler host

url[R]

The public hostname/subdomain url for the tunnel

username[R]

Authentication for tunnel

Public Class Methods

new(attributes = {}) click to toggle source

Initializes a Tunnel instance for the Client and requests a tunnel from API.

client - The Client instance.

# File lib/forward/tunnel.rb, line 36
def initialize(attributes = {})
  logger.debug "[tunnel] initializing with: #{attributes.inspect}"
  @attributes       = attributes
  @id               = attributes[:id]
  @host             = attributes[:vhost]
  @port             = attributes[:hostport]
  @subdomain_prefix = attributes[:subdomain_prefix]
  @username         = attributes[:username]
  @password         = attributes[:password]
  @no_auth          = attributes[:no_auth]
  @static_path      = attributes[:static_path]
  @cname            = attributes[:cname]
  @timeout          = attributes[:timeout]
  @tunneler         = attributes[:tunneler]
  @url              = attributes[:url]
  @requests         = {}
  @open             = false
  @socket           = Socket.new(self)
end

Public Instance Methods

authority() click to toggle source
# File lib/forward/tunnel.rb, line 73
def authority
  @authority ||= begin
    _authority = "#{host}"
    _authority << ":#{port}" unless port == 80

    _authority
  end
end
destroy(&block) click to toggle source
# File lib/forward/tunnel.rb, line 69
def destroy(&block)
  API::Tunnel.destroy(id, &block)
end
open?() click to toggle source
# File lib/forward/tunnel.rb, line 65
def open?
  @open
end
ready!() click to toggle source
# File lib/forward/tunnel.rb, line 56
def ready!
  logger.debug '[tunnel] opened successfully'
  @open = true

  copy_url_to_clipboard
  open_url_in_browser
  display_ready_message
end
track_activity!() click to toggle source
# File lib/forward/tunnel.rb, line 82
def track_activity!
  self.last_active_at = Time.now.to_i
end

Private Instance Methods

copy_url_to_clipboard() click to toggle source
# File lib/forward/tunnel.rb, line 97
def copy_url_to_clipboard
  return unless config.auto_copy?

  if windows?
    begin
      require 'ffi'
    rescue LoadError
      puts "The FFI gem is required to copy your url to the clipboard, you can install it with `gem install ffi'"
      return
    end
  end

  Clipboard.copy(url)
end
display_ready_message() click to toggle source
# File lib/forward/tunnel.rb, line 88
def display_ready_message
  source  = static? ? "Directory `#{File.expand_path(@static_path).split('/').last}'" : authority
  message = "#{source} is now available at: #{HighLine.color(url, :underline)}"

  message << " and #{HighLine.color("http://#{cname}", :underline)}" if cname

  puts "#{message}\n\nCtrl-C to stop forwarding"
end
open_url_in_browser() click to toggle source
# File lib/forward/tunnel.rb, line 112
def open_url_in_browser
  return unless config.auto_open?

  case os
  when :windows
    %x[start #{url}]
  when :osx
    %x[open #{url}]
  when :unix
    %x[xdg-open #{url}]
  end
end
static?() click to toggle source
# File lib/forward/tunnel.rb, line 125
def static?
  !@static_path.nil?
end