Class: EventMachine::Wssh::Ephemeral

Inherits:
Object
  • Object
show all
Extended by:
Service
Defined in:
lib/em/wssh/ephemeral.rb

Defined Under Namespace

Modules: Parent

Instance Attribute Summary

Attributes included from Service

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Service

daemonize!, daemonize?, getopt, go!, helptions, homebase, log, loop!, mkdir, path, pid

Constructor Details

#initializeEphemeral

Returns a new instance of Ephemeral.



19
20
21
22
# File 'lib/em/wssh/ephemeral.rb', line 19

def initialize
  require 'socket'
  @sock=Addrinfo.tcp('127.0.0.1', 0).listen 1
end

Class Method Details

.allocate(uri) ⇒ Object



42
43
44
# File 'lib/em/wssh/ephemeral.rb', line 42

def self.allocate uri
  new.allocate uri
end

.go!Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/em/wssh/ephemeral.rb', line 58

def self.go!
  help if 2!=ARGV.length
  require_relative 'connect'

  pid

  Connect.options.merge!(
    port: 0,
    uri: ARGV[1],
    ping: 50,
    onlisten: Proc.new{|port| onlisten port},
  )
  Connect.loop!
end

.helpObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/em/wssh/ephemeral.rb', line 46

def self.help
  require_relative 'exe'
  puts <<-EOF
Run HTTP proxy on ephemeral port

#{Exe.usage} <port> <uri>

For internal use.
  EOF
  exit
end

.onlisten(port) ⇒ Object



95
96
97
# File 'lib/em/wssh/ephemeral.rb', line 95

def self.onlisten port
  EM.connect 'localhost', ARGV[0], Parent, port
end

Instance Method Details

#allocate(uri) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/em/wssh/ephemeral.rb', line 34

def allocate uri
  log "Running WSSH proxy..."
  spawn *%w(bundle exec wssh ephemeral), myport.to_s, uri,
    %i(out err)=>File.open(mkdir(:log), 'a')

  rport
end

#myportObject



24
25
26
# File 'lib/em/wssh/ephemeral.rb', line 24

def myport
  Socket.unpack_sockaddr_in(@sock.getsockname).first
end

#rportObject



28
29
30
31
32
# File 'lib/em/wssh/ephemeral.rb', line 28

def rport
  sock=@sock.accept.first
  at_exit{sock}
  sock.gets.to_i
end