Class: Lokal::Tunnel

Inherits:
Object
  • Object
show all
Defined in:
lib/lokal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lokal) ⇒ Tunnel

Returns a new instance of Tunnel.



65
66
67
68
69
70
# File 'lib/lokal.rb', line 65

def initialize(lokal)
  @lokal = lokal
  @options = Options.new
  @ignore_duplicate = false
  @startup_banner = false
end

Instance Attribute Details

#address_mdnsObject

Returns the value of attribute address_mdns.



61
62
63
# File 'lib/lokal.rb', line 61

def address_mdns
  @address_mdns
end

#address_publicObject

Returns the value of attribute address_public.



61
62
63
# File 'lib/lokal.rb', line 61

def address_public
  @address_public
end

#address_tunnelObject

Returns the value of attribute address_tunnel.



61
62
63
# File 'lib/lokal.rb', line 61

def address_tunnel
  @address_tunnel
end

#address_tunnel_portObject

Returns the value of attribute address_tunnel_port.



61
62
63
# File 'lib/lokal.rb', line 61

def address_tunnel_port
  @address_tunnel_port
end

#idObject

Returns the value of attribute id.



61
62
63
# File 'lib/lokal.rb', line 61

def id
  @id
end

#ignore_duplicateObject

Returns the value of attribute ignore_duplicate.



61
62
63
# File 'lib/lokal.rb', line 61

def ignore_duplicate
  @ignore_duplicate
end

#inspectObject

Returns the value of attribute inspect.



61
62
63
# File 'lib/lokal.rb', line 61

def inspect
  @inspect
end

#local_addressObject

Returns the value of attribute local_address.



61
62
63
# File 'lib/lokal.rb', line 61

def local_address
  @local_address
end

#lokalObject

Returns the value of attribute lokal.



61
62
63
# File 'lib/lokal.rb', line 61

def lokal
  @lokal
end

#nameObject

Returns the value of attribute name.



61
62
63
# File 'lib/lokal.rb', line 61

def name
  @name
end

#optionsObject

Returns the value of attribute options.



61
62
63
# File 'lib/lokal.rb', line 61

def options
  @options
end

#server_idObject

Returns the value of attribute server_id.



61
62
63
# File 'lib/lokal.rb', line 61

def server_id
  @server_id
end

#startup_bannerObject

Returns the value of attribute startup_banner.



61
62
63
# File 'lib/lokal.rb', line 61

def startup_banner
  @startup_banner
end

#tunnel_typeObject

Returns the value of attribute tunnel_type.



61
62
63
# File 'lib/lokal.rb', line 61

def tunnel_type
  @tunnel_type
end

Instance Method Details

#createObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lokal.rb', line 112

def create
  raise "Please enable either LAN address or random/custom public URL" if @address_mdns.empty? && @address_public.empty?

  response = @lokal.rest.post('/api/tunnel/start') do |req|
    req.headers['Content-Type'] = 'application/json'
    req.body = to_json
  end

  @lokal.send(:check_server_version, response)

  data = JSON.parse(response.body)
  raise data['message'] unless data['success']

  tunnel_data = data['data'].first
  update_attributes(tunnel_data)

  print_startup_banner if @startup_banner

  self
rescue Faraday::ClientError => e
  handle_duplicate_error(e) if @ignore_duplicate
  raise
end

#get_lan_addressObject



136
137
138
139
# File 'lib/lokal.rb', line 136

def get_lan_address
  raise "LAN address is not being set" if @address_mdns.empty?
  @address_mdns.end_with?('.local') ? @address_mdns : "#{@address_mdns}.local"
end

#get_public_addressObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/lokal.rb', line 141

def get_public_address
  raise "Public address is not requested by client" if @address_public.empty?
  raise "Unable to assign public address" if @address_public.empty?

  if @tunnel_type != 'HTTP' && !@address_public.include?(':')
    update_public_url_port
    raise "Tunnel is using a random port, but it has not been assigned yet. Please try again later"
  end

  @address_public
end

#set_inspection(inspect) ⇒ Object



82
83
84
85
# File 'lib/lokal.rb', line 82

def set_inspection(inspect)
  @inspect = inspect
  self
end

#set_lan_address(lan_address) ⇒ Object



87
88
89
90
# File 'lib/lokal.rb', line 87

def set_lan_address(lan_address)
  @address_mdns = lan_address.chomp('.local')
  self
end

#set_local_address(local_address) ⇒ Object



72
73
74
75
# File 'lib/lokal.rb', line 72

def set_local_address(local_address)
  @local_address = local_address
  self
end

#set_name(name) ⇒ Object



97
98
99
100
# File 'lib/lokal.rb', line 97

def set_name(name)
  @name = name
  self
end

#set_public_address(public_address) ⇒ Object



92
93
94
95
# File 'lib/lokal.rb', line 92

def set_public_address(public_address)
  @address_public = public_address
  self
end

#set_tunnel_type(tunnel_type) ⇒ Object



77
78
79
80
# File 'lib/lokal.rb', line 77

def set_tunnel_type(tunnel_type)
  @tunnel_type = tunnel_type
  self
end

#show_startup_bannerObject



107
108
109
110
# File 'lib/lokal.rb', line 107

def show_startup_banner
  @startup_banner = true
  self
end