Module: Ronin::Support::Network::TCP
- Defined in:
- lib/ronin/support/network/tcp.rb,
lib/ronin/support/network/tcp/mixin.rb,
lib/ronin/support/network/tcp/proxy.rb
Overview
Defined Under Namespace
Class Method Summary collapse
-
.accept(backlog: 1, **kwargs) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepts only one client and then stops listening.
-
.banner(host, port, **kwargs) {|banner| ... } ⇒ String
Reads the banner from the service running on the given host and port.
-
.connect(host, port, bind_host: nil, bind_port: nil) {|socket| ... } ⇒ TCPSocket?
Creates a new TCP socket connected to a given host and port.
-
.connect_and_send(data, host, port, **kwargs) {|socket| ... } ⇒ TCPSocket
Creates a new TCPSocket object, connected to a given host and port.
-
.open?(host, port, timeout: 5, **kwargs) ⇒ Boolean?
Tests whether a remote TCP port is open.
-
.send(data, host, port, **kwargs) ⇒ true
Connects to a specified host and port, sends the given data and then closes the connection.
-
.server(port: 0, host: nil, backlog: 5) {|server| ... } ⇒ TCPServer
Creates a new TCPServer listening on a given host and port.
-
.server_loop(**kwargs) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepting clients in a loop.
-
.server_session(**kwargs) {|server| ... } ⇒ nil
Creates a new temporary TCPServer listening on a host and port.
Class Method Details
.accept(backlog: 1, **kwargs) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepts only one client and then stops listening.
433 434 435 436 437 438 439 440 |
# File 'lib/ronin/support/network/tcp.rb', line 433 def self.accept(backlog: 1, **kwargs) server_session(backlog: backlog, **kwargs) do |server| client = server.accept yield client if block_given? client.close end end |
.banner(host, port, **kwargs) {|banner| ... } ⇒ String
Reads the banner from the service running on the given host and port.
219 220 221 222 223 224 225 226 227 228 |
# File 'lib/ronin/support/network/tcp.rb', line 219 def self.(host,port,**kwargs) = nil connect(host,port,**kwargs) do |socket| = socket.readline.strip end yield if block_given? return end |
.connect(host, port, bind_host: nil, bind_port: nil) {|socket| ... } ⇒ TCPSocket?
Creates a new TCP socket connected to a given host and port.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/ronin/support/network/tcp.rb', line 123 def self.connect(host,port, bind_host: nil, bind_port: nil) host = DNS::IDN.to_ascii(host) port = port.to_i socket = if bind_host || bind_port TCPSocket.new(host,port,bind_host.to_s,bind_port.to_i) else TCPSocket.new(host,port) end if block_given? begin yield socket ensure socket.close end else return socket end end |
.connect_and_send(data, host, port, **kwargs) {|socket| ... } ⇒ TCPSocket
Creates a new TCPSocket object, connected to a given host and port. The given data will then be written to the newly created TCPSocket.
177 178 179 180 181 182 183 |
# File 'lib/ronin/support/network/tcp.rb', line 177 def self.connect_and_send(data,host,port,**kwargs) socket = connect(host,port,**kwargs) socket.write(data) yield socket if block_given? return socket end |
.open?(host, port, timeout: 5, **kwargs) ⇒ Boolean?
Tests whether a remote TCP port is open.
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ronin/support/network/tcp.rb', line 69 def self.open?(host,port, timeout: 5, **kwargs) Timeout.timeout(timeout) do socket = connect(host,port,**kwargs) socket.close end return true rescue Timeout::Error return nil rescue SocketError, SystemCallError return false end |
.send(data, host, port, **kwargs) ⇒ true
Connects to a specified host and port, sends the given data and then closes the connection.
262 263 264 265 266 267 268 |
# File 'lib/ronin/support/network/tcp.rb', line 262 def self.send(data,host,port,**kwargs) connect(host,port,**kwargs) do |socket| socket.write(data) end return true end |
.server(port: 0, host: nil, backlog: 5) {|server| ... } ⇒ TCPServer
Creates a new TCPServer listening on a given host and port.
298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/ronin/support/network/tcp.rb', line 298 def self.server(port: 0, host: nil, backlog: 5) server = if host TCPServer.new(host.to_s,port.to_i) else TCPServer.new(port.to_i) end server.listen(backlog) yield server if block_given? return server end |
.server_loop(**kwargs) {|client| ... } ⇒ nil
Creates a new TCPServer listening on a given host and port, accepting clients in a loop.
387 388 389 390 391 392 393 394 395 396 |
# File 'lib/ronin/support/network/tcp.rb', line 387 def self.server_loop(**kwargs) server_session(**kwargs) do |server| loop do client = server.accept yield client if block_given? client.close end end end |
.server_session(**kwargs) {|server| ... } ⇒ nil
Creates a new temporary TCPServer listening on a host and port.
347 348 349 350 351 |
# File 'lib/ronin/support/network/tcp.rb', line 347 def self.server_session(**kwargs,&block) server = server(**kwargs,&block) server.close return nil end |