Class: Ronin::Support::Network::Proxy
- Inherits:
-
Object
- Object
- Ronin::Support::Network::Proxy
- Defined in:
- lib/ronin/support/network/proxy.rb
Overview
Base class for TCP and UDP Proxies.
Callbacks
The Proxy base class supports several callbacks for proxy events.
client_data
When a client sends data to the proxy.
on_client_data do |client,server,data|
data.gsub!(/foo/,'bar')
end
server_data
When the server sends data to the proxy.
on_server_data do |client,server,data|
data.gsub!(/foo/,'bar')
end
data
Alias for #on_client_data and #on_server_data.
Actions
The Proxy base class also provides methods to change how events are handled.
Direct Known Subclasses
Constant Summary collapse
- DEFAULT_HOST =
Default host to bind to
'0.0.0.0'
- DEFAULT_BUFFER_SIZE =
4096
Instance Attribute Summary collapse
-
#buffer_size ⇒ Integer
The size of read buffer.
-
#connections ⇒ Hash{Socket => Socket}
readonly
The connections maintained by the proxy.
-
#host ⇒ String
readonly
The host the proxy will listen on.
-
#port ⇒ Integer
readonly
The port the proxy will listen on.
-
#server_host ⇒ String
readonly
The remote port the proxy will relay data to.
-
#server_port ⇒ Integer
readonly
The remote host the proxy will relay data to.
Class Method Summary collapse
-
.start(**kwargs, &block) ⇒ Object
Creates a new Proxy and begins relaying data.
Instance Method Summary collapse
-
#callback(event, client_connection, server_connection = nil, data = nil) { ... } ⇒ Object
protected
Triggers the callbacks registered for an event.
-
#client_connection_for(server_connection) ⇒ Socket
Finds the connection from the client, associated with the server connection.
-
#client_connections ⇒ Array<Socket>
Connections from clients.
-
#client_data(client_connection, server_connection, data) ⇒ Object
protected
Triggers the
client_data
event. -
#close ⇒ Object
Closes the proxy.
-
#close! ⇒ Object
Causes the proxy to close a connection.
-
#close_client_connection(connection) ⇒ Object
protected
abstract
Closes a client connection to the proxy.
-
#close_connection(client_connection, server_connection = nil) ⇒ Object
protected
Closes both the client and server connections.
-
#close_connections ⇒ Object
protected
Closes all active connections.
-
#close_proxy ⇒ Object
protected
abstract
Closes the proxy.
-
#close_server_connection(connection) ⇒ Object
protected
abstract
Closes a connection to the server.
-
#ignore! ⇒ Object
Causes the proxy to ignore a message.
-
#initialize(host: DEFAULT_HOST, port:, server:, buffer_size: DEFAULT_BUFFER_SIZE) {|proxy| ... } ⇒ Proxy
constructor
Creates a new Proxy.
-
#inspect ⇒ String
Inspects the proxy.
-
#listen ⇒ Object
Polls the connections for data.
-
#on_client_data {|client, server, data| ... } ⇒ Object
Registers a callback for when a client sends data.
-
#on_data {|client, server, data| ... } ⇒ Object
Registers a callback for when either the client or the server sends data.
-
#on_server_data {|client, server, data| ... } ⇒ Object
Registers a callback for when a server sends data.
-
#open ⇒ Object
abstract
Opens the proxy.
-
#open_server_connection ⇒ Socket
protected
abstract
Creates a new connection to the server.
-
#poll ⇒ Object
abstract
Polls the connections for data or errors.
-
#recv(connection) ⇒ Object
abstract
Receives data from a connection.
-
#reset! ⇒ Object
Causes the proxy to restart a connection.
-
#reset_connection(client_connection, server_connection) ⇒ Object
protected
Resets a server connection.
-
#send(connection, data) ⇒ Object
abstract
Sends data to a connection.
-
#server_connection_for(client_connection) ⇒ Socket
Finds the connection to the server, associated with the client.
-
#server_connections ⇒ Array<Socket>
Connections to the server.
-
#server_data(client_connection, server_connection, data) ⇒ Object
protected
Triggers the
server_data
event. -
#start ⇒ Proxy
Starts the proxy and begins relaying data.
-
#stop ⇒ Object
Stops the proxy from listening.
-
#stop! ⇒ Object
Causes the proxy to stop processing data entirely.
-
#to_s ⇒ String
Converts the proxy to a String.
Constructor Details
#initialize(host: DEFAULT_HOST, port:, server:, buffer_size: DEFAULT_BUFFER_SIZE) {|proxy| ... } ⇒ Proxy
Creates a new Proxy.
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ronin/support/network/proxy.rb', line 125 def initialize(host: DEFAULT_HOST, port: , server: , buffer_size: DEFAULT_BUFFER_SIZE) @host = host @port = port @server_host, @server_port = server @server_port ||= @port @callbacks = {client_data: [], server_data: []} @buffer_size = buffer_size @connections = {} yield self if block_given? end |
Instance Attribute Details
#buffer_size ⇒ Integer
The size of read buffer
90 91 92 |
# File 'lib/ronin/support/network/proxy.rb', line 90 def buffer_size @buffer_size end |
#connections ⇒ Hash{Socket => Socket} (readonly)
The connections maintained by the proxy
95 96 97 |
# File 'lib/ronin/support/network/proxy.rb', line 95 def connections @connections end |
#host ⇒ String (readonly)
The host the proxy will listen on
70 71 72 |
# File 'lib/ronin/support/network/proxy.rb', line 70 def host @host end |
#port ⇒ Integer (readonly)
The port the proxy will listen on
75 76 77 |
# File 'lib/ronin/support/network/proxy.rb', line 75 def port @port end |
#server_host ⇒ String (readonly)
The remote port the proxy will relay data to
80 81 82 |
# File 'lib/ronin/support/network/proxy.rb', line 80 def server_host @server_host end |
#server_port ⇒ Integer (readonly)
The remote host the proxy will relay data to
85 86 87 |
# File 'lib/ronin/support/network/proxy.rb', line 85 def server_port @server_port end |
Class Method Details
.start(**kwargs, &block) ⇒ Object
Creates a new Proxy and begins relaying data.
153 154 155 |
# File 'lib/ronin/support/network/proxy.rb', line 153 def self.start(**kwargs,&block) new(**kwargs,&block).start end |
Instance Method Details
#callback(event, client_connection, server_connection = nil, data = nil) { ... } ⇒ Object (protected)
Triggers the callbacks registered for an event.
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'lib/ronin/support/network/proxy.rb', line 535 def callback(event,client_connection,server_connection=nil,data=nil) action = catch(:action) do @callbacks[event].each do |block| case block.arity when 1 block.call(client_connection) when 2 block.call(client_connection,server_connection) when 3, -1 block.call(client_connection,server_connection,data) end end end case action when :ignore # no-op when :reset reset_connection(client_connection,server_connection) when :close close_connection(client_connection,server_connection) when :stop stop else yield if block_given? end end |
#client_connection_for(server_connection) ⇒ Socket
Finds the connection from the client, associated with the server connection.
406 407 408 |
# File 'lib/ronin/support/network/proxy.rb', line 406 def client_connection_for(server_connection) @connections.key(server_connection) end |
#client_connections ⇒ Array<Socket>
Connections from clients.
369 370 371 |
# File 'lib/ronin/support/network/proxy.rb', line 369 def client_connections @connections.keys end |
#client_data(client_connection, server_connection, data) ⇒ Object (protected)
Triggers the client_data
event.
575 576 577 578 579 |
# File 'lib/ronin/support/network/proxy.rb', line 575 def client_data(client_connection,server_connection,data) callback(:client_data,client_connection,server_connection,data) do send(server_connection,data) end end |
#close ⇒ Object
Closes the proxy.
244 245 246 247 |
# File 'lib/ronin/support/network/proxy.rb', line 244 def close close_connections close_proxy end |
#close! ⇒ Object
Causes the proxy to close a connection.
341 342 343 |
# File 'lib/ronin/support/network/proxy.rb', line 341 def close! throw(:action,:close) end |
#close_client_connection(connection) ⇒ Object (protected)
Closes a client connection to the proxy.
451 452 |
# File 'lib/ronin/support/network/proxy.rb', line 451 def close_client_connection(connection) end |
#close_connection(client_connection, server_connection = nil) ⇒ Object (protected)
Closes both the client and server connections.
497 498 499 500 501 502 |
# File 'lib/ronin/support/network/proxy.rb', line 497 def close_connection(client_connection,server_connection=nil) close_server_connection(server_connection) if server_connection close_client_connection(client_connection) @connections.delete(client_connection) end |
#close_connections ⇒ Object (protected)
Closes all active connections.
507 508 509 510 511 512 513 514 |
# File 'lib/ronin/support/network/proxy.rb', line 507 def close_connections @connections.each do |client_connection,server_connection| close_server_connection(server_connection) close_client_connection(client_connection) end @connections.clear end |
#close_proxy ⇒ Object (protected)
Closes the proxy.
470 471 |
# File 'lib/ronin/support/network/proxy.rb', line 470 def close_proxy end |
#close_server_connection(connection) ⇒ Object (protected)
Closes a connection to the server.
462 463 |
# File 'lib/ronin/support/network/proxy.rb', line 462 def close_server_connection(connection) end |
#ignore! ⇒ Object
Causes the proxy to ignore a message.
332 333 334 |
# File 'lib/ronin/support/network/proxy.rb', line 332 def ignore! throw(:action,:ignore) end |
#inspect ⇒ String
Inspects the proxy.
426 427 428 |
# File 'lib/ronin/support/network/proxy.rb', line 426 def inspect "#<#{self.class}:#{self.object_id}: #{self}>" end |
#listen ⇒ Object
Polls the connections for data.
197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/ronin/support/network/proxy.rb', line 197 def listen @listening = true while @listening begin poll rescue Interrupt @listening = false break end end end |
#on_client_data {|client, server, data| ... } ⇒ Object
Registers a callback for when a client sends data.
277 278 279 |
# File 'lib/ronin/support/network/proxy.rb', line 277 def on_client_data(&block) @callbacks[:client_data] << block end |
#on_data {|client, server, data| ... } ⇒ Object
Registers a callback for when either the client or the server sends data.
322 323 324 325 |
# File 'lib/ronin/support/network/proxy.rb', line 322 def on_data(&block) on_client_data(&block) on_server_data(&block) end |
#on_server_data {|client, server, data| ... } ⇒ Object
Registers a callback for when a server sends data.
299 300 301 |
# File 'lib/ronin/support/network/proxy.rb', line 299 def on_server_data(&block) @callbacks[:server_data] << block end |
#open ⇒ Object
Opens the proxy.
179 180 |
# File 'lib/ronin/support/network/proxy.rb', line 179 def open end |
#open_server_connection ⇒ Socket (protected)
Creates a new connection to the server.
440 441 |
# File 'lib/ronin/support/network/proxy.rb', line 440 def open_server_connection end |
#poll ⇒ Object
Polls the connections for data or errors.
189 190 |
# File 'lib/ronin/support/network/proxy.rb', line 189 def poll end |
#recv(connection) ⇒ Object
Receives data from a connection.
236 237 |
# File 'lib/ronin/support/network/proxy.rb', line 236 def recv(connection) end |
#reset! ⇒ Object
Causes the proxy to restart a connection.
350 351 352 |
# File 'lib/ronin/support/network/proxy.rb', line 350 def reset! throw(:action,:reset) end |
#reset_connection(client_connection, server_connection) ⇒ Object (protected)
Resets a server connection.
482 483 484 485 486 |
# File 'lib/ronin/support/network/proxy.rb', line 482 def reset_connection(client_connection,server_connection) close_server_connection(server_connection) if server_connection @connections[client_connection] = open_server_connection end |
#send(connection, data) ⇒ Object
Sends data to a connection.
223 224 |
# File 'lib/ronin/support/network/proxy.rb', line 223 def send(connection,data) end |
#server_connection_for(client_connection) ⇒ Socket
Finds the connection to the server, associated with the client.
392 393 394 |
# File 'lib/ronin/support/network/proxy.rb', line 392 def server_connection_for(client_connection) @connections[client_connection] end |
#server_connections ⇒ Array<Socket>
Connections to the server.
379 380 381 |
# File 'lib/ronin/support/network/proxy.rb', line 379 def server_connections @connections.values end |
#server_data(client_connection, server_connection, data) ⇒ Object (protected)
Triggers the server_data
event.
593 594 595 596 597 |
# File 'lib/ronin/support/network/proxy.rb', line 593 def server_data(client_connection,server_connection,data) callback(:server_data,client_connection,server_connection,data) do send(client_connection,data) end end |
#start ⇒ Proxy
Starts the proxy and begins relaying data.
165 166 167 168 169 170 |
# File 'lib/ronin/support/network/proxy.rb', line 165 def start open listen close return self end |
#stop ⇒ Object
Stops the proxy from listening.
254 255 256 257 |
# File 'lib/ronin/support/network/proxy.rb', line 254 def stop @listening = false return self end |
#stop! ⇒ Object
Causes the proxy to stop processing data entirely.
359 360 361 |
# File 'lib/ronin/support/network/proxy.rb', line 359 def stop! throw(:action,:stop) end |
#to_s ⇒ String
Converts the proxy to a String.
416 417 418 |
# File 'lib/ronin/support/network/proxy.rb', line 416 def to_s "#{@host}:#{@port} <-> #{@server_host}:#{@server_port}" end |