lib/ronin/network/mixins/tcp.rb in ronin-support-0.3.0 vs lib/ronin/network/mixins/tcp.rb in ronin-support-0.4.0.rc1

- old
+ new

@@ -15,16 +15,13 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Ronin Support. If not, see <http://www.gnu.org/licenses/>. # +require 'ronin/network/mixins/mixin' require 'ronin/network/tcp' -require 'ronin/ui/output/helpers' -require 'ronin/mixin' -require 'parameters' - module Ronin module Network module Mixins # # Adds TCP convenience methods and connection parameters to a class. @@ -37,40 +34,36 @@ # * `local_port` (`Integer`) - TCP local port. # * `server_host` (`String`) - TCP server host. # * `server_port` (`Integer`) - TCP server port. # module TCP - include Mixin + include Mixin, Network::TCP - mixin UI::Output::Helpers, Parameters + # TCP host + parameter :host, :type => String, + :description => 'TCP host' - mixin do - # TCP host - parameter :host, :type => String, - :description => 'TCP host' + # TCP port + parameter :port, :type => Integer, + :description => 'TCP port' - # TCP port - parameter :port, :type => Integer, - :description => 'TCP port' + # TCP local host + parameter :local_host, :type => String, + :description => 'TCP local host' - # TCP local host - parameter :local_host, :type => String, - :description => 'TCP local host' + # TCP local port + parameter :local_port, :type => Integer, + :description => 'TCP local port' - # TCP local port - parameter :local_port, :type => Integer, - :description => 'TCP local port' + # TCP server host + parameter :server_host, :type => String, + :description => 'TCP server host' - # TCP server host - parameter :server_host, :type => String, - :description => 'TCP server host' + # TCP server port + parameter :server_port, :type => Integer, + :description => 'TCP server port' - # TCP server port - parameter :server_port, :type => Integer, - :description => 'TCP server port' - end - protected # # Opens a TCP connection to the host and port specified by the # `host` and `port` parameters. If the `local_host` and @@ -97,13 +90,13 @@ # end # # @api public # def tcp_connect(&block) - print_info "Connecting to #{self.host}:#{self.port} ..." + print_info "Connecting to #{host_port} ..." - return ::Net.tcp_connect(self.host,self.port,self.local_host,self.local_port,&block) + return super(self.host,self.port,self.local_host,self.local_port,&block) end # # Connects to the host and port specified by the `host` and `port` # parameters, then sends the given data. @@ -121,14 +114,14 @@ # The newly created TCPSocket object. # # @api public # def tcp_connect_and_send(data,&block) - print_info "Connecting to #{self.host}:#{self.port} ..." + print_info "Connecting to #{host_port} ..." print_debug "Sending data: #{data.inspect}" - return ::Net.tcp_connect_and_send(data,self.host,self.port,self.local_host,self.local_port,&block) + return super(data,self.host,self.port,self.local_host,self.local_port,&block) end # # Creates a TCP session to the host and port specified by the # `host` and `port` parameters. @@ -143,15 +136,15 @@ # @return [nil] # # @api public # def tcp_session(&block) - print_info "Connecting to #{self.host}:#{self.port} ..." + print_info "Connecting to #{host_port} ..." Net.tcp_session(self.host,self.port,self.local_host,self.local_port,&block) - print_info "Disconnected from #{self.host}:#{self.port}" + print_info "Disconnected from #{host_port}" return nil end # # Connects to the host and port specified by the `host` and `port` @@ -171,13 +164,13 @@ # # => "220 mx.google.com ESMTP c20sm3096959rvf.1" # # @api public # def tcp_banner(&block) - print_debug "Grabbing banner from #{self.host}:#{self.port}" + print_debug "Grabbing banner from #{host_port}" - return ::Net.tcp_banner(self.host,self.port,self.local_host,self.local_port,&block) + return super(self.host,self.port,self.local_host,self.local_port,&block) end # # Connects to the host and port specified by the `host` and `port` # parameters, sends the given data and then disconnects. @@ -191,16 +184,16 @@ # # => true # # @api public # def tcp_send(data) - print_info "Connecting to #{self.host}:#{self.port} ..." + print_info "Connecting to #{host_port} ..." print_debug "Sending data: #{data.inspect}" - ::Net.tcp_send(data,self.host,self.port,self.local_host,self.local_port) + super(data,self.host,self.port,self.local_host,self.local_port) - print_info "Disconnected from #{self.host}:#{self.port}" + print_info "Disconnected from #{host_port}" return true end # # Creates a new TCPServer object listening on the `server_host` @@ -219,17 +212,13 @@ # tcp_server # # @api public # def tcp_server(&block) - if self.server_host - print_info "Listening on #{self.server_host}:#{self.server_port} ..." - else - print_info "Listening on #{self.server_port} ..." - end + print_info "Listening on #{self.server_host_port} ..." - return ::Net.tcp_server(self.server_port,self.server_host,&block) + return super(self.server_port,self.server_host,&block) end # # Creates a new temporary TCPServer object listening on the # `server_host` and `server_port` parameters. @@ -255,24 +244,15 @@ # end # # @api public # def tcp_server_session(&block) - if self.server_host - print_info "Listening on #{self.server_host}:#{self.server_port} ..." - else - print_info "Listening on #{self.server_port} ..." - end + print_info "Listening on #{server_host_port} ..." - ::Net.tcp_server_session(&block) + super(self.server_port,self.server_host,&block) - if self.server_host - print_info "Closed #{self.server_host}:#{self.server_port}" - else - print_info "Closed #{self.server_port}" - end - + print_info "Closed #{server_host_port}" return nil end # # Creates a new temporary TCPServer object listening on @@ -297,34 +277,41 @@ # end # # @api public # def tcp_single_server(&block) - if self.server_host - print_info "Listening on #{self.server_host}:#{self.server_port} ..." - else - print_info "Listening on #{self.server_port} ..." - end + print_info "Listening on #{server_host_port} ..." - ::Net.tcp_single_server do |client| + super(self.server_port,self.server_host) do |client| client_addr = client.peeraddr client_host = (client_addr[2] || client_addr[3]) client_port = client_addr[1] print_info "Client connected #{client_host}:#{client_port}" yield client if block_given? - print_info "Disconnecting client #{client_host}:#{client_port}" + print_info "Disconnected client #{client_host}:#{client_port}" end - if self.server_host - print_info "Closed #{self.server_host}:#{self.server_port}" - else - print_info "Closed #{self.server_port}" - end - + print_info "Closed #{server_host_port}" return nil + end + + private + + # + # The server host/port parameters. + # + # @return [String] + # The server host/port parameters in String form. + # + # @since 0.4.0 + # + # @api private + # + def server_host_port + "#{self.server_host}:#{self.server_port}" end end end end end