Class: EventMachine::Wssh::Server::Req

Inherits:
Object
  • Object
show all
Defined in:
lib/em/wssh/server.rb

Constant Summary collapse

Server =
Module.nesting[1]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws) ⇒ Req

Returns a new instance of Req.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/em/wssh/server.rb', line 68

def initialize ws
  self.ws=ws
  self.buf=[]

  @count=self.class.count

  log "Connect from", Socket.unpack_sockaddr_in(ws.get_peername)[1]

  ws.onopen{|handshake| onopen handshake}
  ws.onbinary{|msg| ondata msg}
  ws.onclose{|code, body| onclose}
  ws.onerror{|err| onerror err}
end

Instance Attribute Details

#bufObject

Returns the value of attribute buf.



59
60
61
# File 'lib/em/wssh/server.rb', line 59

def buf
  @buf
end

#sshObject

Returns the value of attribute ssh.



59
60
61
# File 'lib/em/wssh/server.rb', line 59

def ssh
  @ssh
end

#wsObject

Returns the value of attribute ws.



59
60
61
# File 'lib/em/wssh/server.rb', line 59

def ws
  @ws
end

Class Method Details

.countObject



63
64
65
66
# File 'lib/em/wssh/server.rb', line 63

def self.count
  @n||=0
  @n+=1
end

Instance Method Details

#byeObject



154
155
156
157
158
159
# File 'lib/em/wssh/server.rb', line 154

def bye
  ssh.close_connection if ssh
  ws.close if ws
  @timer.cancel if @timer
  instance_variables.each{|v| remove_instance_variable v if '@count'!=v.to_s}
end

#log(*msg) ⇒ Object



82
83
84
# File 'lib/em/wssh/server.rb', line 82

def log *msg
  Server.log "<#{@count}>", *msg
end

#oncloseObject



113
114
115
116
# File 'lib/em/wssh/server.rb', line 113

def onclose
  log 'Client closed connection'
  bye
end

#ondata(msg) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/em/wssh/server.rb', line 105

def ondata msg
  if buf
    buf << msg
  else
    ssh.send_data msg
  end
end

#onerror(err) ⇒ Object



118
119
120
121
# File 'lib/em/wssh/server.rb', line 118

def onerror err
  log "Websocket error", err
  bye
end

#onopen(handshake) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/em/wssh/server.rb', line 86

def onopen handshake
  xf=handshake.headers_downcased['x-forwarded-for']
  log "Forwarded for", xf if xf
  log "Request", handshake.path
  unless host = resolve(handshake.path) rescue nil
    log "Invalid host"
    bye
    return
  end
  log "Connecting to", host
  EM.connect host, 22, Ssh, self
  pinger
end

#pingerObject



100
101
102
103
# File 'lib/em/wssh/server.rb', line 100

def pinger
  return unless t=Server.options[:ping]
  @timer=EM::PeriodicTimer.new(t){ws.ping if ws}
end

#resolve(path) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/em/wssh/server.rb', line 123

def resolve(path)
  path = path.to_s
  .split(/[^-.\w]+/)
  .select{|s|s.length>0}
  .select{|s|!s.match /^[-_.]|[-_.]$/}
  .last
  yml = YAML.load_file Server.path(:hosts)

  if yml.key? path
    host = yml[path]
    raise 'X' unless host
    host = path if true===host
    host = host.to_s.strip
    raise 'X' if 0==host.length
    return host
  end

  host=nil

  yml.each do |k, v|
    next unless m=/^\/(.*)\/(i?)$/.match(k)
    next unless Regexp.new(m[1], m[2]).match path
    raise 'X' unless v
    host = true===v ? path : v
    host = host.to_s.strip
    raise 'X' if 0==host.length
  end
  raise 'X' unless host
  host
end