Class: ReverseTunnel::Server::TunnelConnection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server) ⇒ TunnelConnection

Returns a new instance of TunnelConnection.



186
187
188
# File 'lib/reverse-tunnel/server.rb', line 186

def initialize(server)
  @server = server
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



184
185
186
# File 'lib/reverse-tunnel/server.rb', line 184

def created_at
  @created_at
end

#serverObject

Returns the value of attribute server.



184
185
186
# File 'lib/reverse-tunnel/server.rb', line 184

def server
  @server
end

#tunnelObject

Returns the value of attribute tunnel.



220
221
222
# File 'lib/reverse-tunnel/server.rb', line 220

def tunnel
  @tunnel
end

Instance Method Details

#as_json(*args) ⇒ Object



258
259
260
# File 'lib/reverse-tunnel/server.rb', line 258

def as_json(*args)
  { :peer => peer, :created_at => created_at }
end

#close_connection(after_writing = false) ⇒ Object



230
231
232
233
# File 'lib/reverse-tunnel/server.rb', line 230

def close_connection(after_writing = false)
  super
  @closed = true
end

#closed?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/reverse-tunnel/server.rb', line 226

def closed?
  @closed ||= false
end

#message_unpackerObject



202
203
204
# File 'lib/reverse-tunnel/server.rb', line 202

def message_unpacker
  @message_unpacker ||= Message::Unpacker.new
end

#open?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/reverse-tunnel/server.rb', line 222

def open?
  !!tunnel
end

#open_tunnel(token) ⇒ Object



235
236
237
238
239
240
241
242
243
244
# File 'lib/reverse-tunnel/server.rb', line 235

def open_tunnel(token)
  self.tunnel = server.tunnels.find token
  if tunnel
    ReverseTunnel.logger.info "Open tunnel #{token}"
    tunnel.connection = self
  else
    ReverseTunnel.logger.warn "Refuse tunnel connection #{token}"
    close_connection
  end
end

#peerObject



250
251
252
253
254
255
256
# File 'lib/reverse-tunnel/server.rb', line 250

def peer
  @peer ||= 
    begin
      port, ip = Socket.unpack_sockaddr_in(get_peername)
      "#{ip}:#{port}"
    end
end

#post_initObject



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/reverse-tunnel/server.rb', line 190

def post_init
  ReverseTunnel.logger.info "New tunnel connection from #{peer}"
  self.created_at = Time.now

  EventMachine.add_timer(10) do  
    unless open? or closed?
      ReverseTunnel.logger.info "Force close of unopened tunnel connection from #{peer}"
      close_connection 
    end
  end
end

#receive_data(data) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/reverse-tunnel/server.rb', line 206

def receive_data(data)
  message_unpacker.feed data

  message_unpacker.each do |message|
    if message.data?
      tunnel.receive_data message.session_id, message.data
    elsif message.open_tunnel?
      open_tunnel message.token
    elsif message.ping?
      tunnel.ping_received message
    end
  end
end

#unbindObject



246
247
248
# File 'lib/reverse-tunnel/server.rb', line 246

def unbind
  tunnel.connection_closed self if tunnel
end