Class: ReverseTunnel::Server::TunnelConnection
- Inherits:
-
EventMachine::Connection
- Object
- EventMachine::Connection
- ReverseTunnel::Server::TunnelConnection
- Defined in:
- lib/reverse-tunnel/server.rb
Instance Attribute Summary collapse
-
#created_at ⇒ Object
Returns the value of attribute created_at.
-
#server ⇒ Object
Returns the value of attribute server.
-
#tunnel ⇒ Object
Returns the value of attribute tunnel.
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #close_connection(after_writing = false) ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(server) ⇒ TunnelConnection
constructor
A new instance of TunnelConnection.
- #message_unpacker ⇒ Object
- #open? ⇒ Boolean
- #open_tunnel(token) ⇒ Object
- #peer ⇒ Object
- #post_init ⇒ Object
- #receive_data(data) ⇒ Object
- #unbind ⇒ Object
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_at ⇒ Object
Returns the value of attribute created_at.
184 185 186 |
# File 'lib/reverse-tunnel/server.rb', line 184 def created_at @created_at end |
#server ⇒ Object
Returns the value of attribute server.
184 185 186 |
# File 'lib/reverse-tunnel/server.rb', line 184 def server @server end |
#tunnel ⇒ Object
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
226 227 228 |
# File 'lib/reverse-tunnel/server.rb', line 226 def closed? @closed ||= false end |
#message_unpacker ⇒ Object
202 203 204 |
# File 'lib/reverse-tunnel/server.rb', line 202 def @message_unpacker ||= Message::Unpacker.new end |
#open? ⇒ 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 |
#peer ⇒ Object
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_init ⇒ Object
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) .feed data .each do || if .data? tunnel.receive_data .session_id, .data elsif .open_tunnel? open_tunnel .token elsif .ping? tunnel.ping_received end end end |
#unbind ⇒ Object
246 247 248 |
# File 'lib/reverse-tunnel/server.rb', line 246 def unbind tunnel.connection_closed self if tunnel end |