Class: ReverseTunnel::Server::Tunnels

Inherits:
Object
  • Object
show all
Defined in:
lib/reverse-tunnel/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#local_hostObject

Returns the value of attribute local_host.



311
312
313
# File 'lib/reverse-tunnel/server.rb', line 311

def local_host
  @local_host
end

#local_port_rangeObject

Returns the value of attribute local_port_range.



311
312
313
# File 'lib/reverse-tunnel/server.rb', line 311

def local_port_range
  @local_port_range
end

Instance Method Details

#available_local_portObject



354
355
356
357
358
# File 'lib/reverse-tunnel/server.rb', line 354

def available_local_port
  available_local_ports.tap do |ports|
    ports.shuffle if respond_to?(:shuffle)
  end.first
end

#available_local_portsObject



350
351
352
# File 'lib/reverse-tunnel/server.rb', line 350

def available_local_ports
  local_port_range.to_a - used_local_ports
end

#create(attributes = {}) ⇒ Object



321
322
323
324
325
326
327
# File 'lib/reverse-tunnel/server.rb', line 321

def create(attributes = {})
  attributes = default_attributes.merge(attributes).merge(:local_host => local_host)
  Tunnel.new(attributes).tap do |tunnel|
    ReverseTunnel.logger.info "Create tunnel #{tunnel.inspect}"
    tunnels << tunnel
  end
end

#create_tokenObject



342
343
344
# File 'lib/reverse-tunnel/server.rb', line 342

def create_token
  rand(10e32).to_s(16).ljust(28, '0').upcase
end

#default_attributesObject



338
339
340
# File 'lib/reverse-tunnel/server.rb', line 338

def default_attributes
  { "token" => create_token, "local_port" => available_local_port }
end

#destroy(token) ⇒ Object



329
330
331
332
333
334
335
336
# File 'lib/reverse-tunnel/server.rb', line 329

def destroy(token)
  tunnel = find(token)
  if tunnel
    tunnel.close
    tunnels.delete tunnel
    tunnel
  end
end

#find(token) ⇒ Object



317
318
319
# File 'lib/reverse-tunnel/server.rb', line 317

def find(token)
  tunnels.find { |t| t.token == token }
end

#to_json(*args) ⇒ Object



360
361
362
# File 'lib/reverse-tunnel/server.rb', line 360

def to_json(*args)
  tunnels.to_json(*args)
end

#tunnelsObject



307
308
309
# File 'lib/reverse-tunnel/server.rb', line 307

def tunnels
  @tunnels ||= []
end

#used_local_portsObject



346
347
348
# File 'lib/reverse-tunnel/server.rb', line 346

def used_local_ports
  tunnels.map(&:local_port)
end