Class: Ronin::Support::Web::WebSocket::Server

Inherits:
Object
  • Object
show all
Includes:
URLMethods
Defined in:
lib/ronin/support/web/websocket/server.rb

Overview

Represents a WebSocket server.

Defined Under Namespace

Classes: Client

Instance Attribute Summary collapse

Attributes included from URLMethods

#host, #path, #port, #url

Instance Method Summary collapse

Methods included from URLMethods

#ssl?

Constructor Details

#initialize(url, bind_host: nil, bind_port: nil, backlog: 5, ssl: {}) ⇒ Server

Initializes the WebSocket server.

Parameters:

  • url (String, URI::WS, URI::WSS)

    The ws:// or wss:// URL.

  • bind_host (String, nil) (defaults to: nil)

    The optional host to bind the server socket to.

  • bind_port (Integer, nil) (defaults to: nil)

    The optioanl port to bind the server socket to. If not specified, it will default to the port of the URL.

  • backlog (Integer) (defaults to: 5)

    The maximum backlog of pending connections.

  • ssl (Hash{Symbol => Object}) (defaults to: {})

    Additional keyword arguments for Ronin::Support::Network::SSL.server.

Options Hash (ssl:):

  • :version (1, 1.1, 1.2, String, Symbol, nil)

    The SSL version to use.

  • :verify (Symbol, Boolean)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :fail_if_no_peer_cert
    • :client_once
  • :key (Crypto::Key::RSA, OpenSSL::PKey::RSA, nil)

    The RSA key to use for the SSL context.

  • :key_file (String)

    The path to the SSL .key file.

  • :cert (Crypto::Cert, OpenSSL::X509::Certificate, nil)

    The X509 certificate to use for the SSL context.

  • :cert_file (String)

    The path to the SSL .crt file.

  • :ca_bundle (String)

    Path to the CA certificate file or directory.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ronin/support/web/websocket/server.rb', line 92

def initialize(url, bind_host: nil,
                    bind_port: nil,
                    backlog:   5,
                    ssl:       {})
  super(url)

  @bind_host = bind_host
  @bind_port = bind_port || @port

  @socket = case @url.scheme
            when 'ws'
              Support::Network::TCP.server(
                host:    @bind_host,
                port:    @bind_port,
                backlog: backlog
              )
            when 'wss'
              Support::Network::SSL.server(
                host:    @bind_host,
                port:    @bind_port,
                backlog: backlog,
                **ssl
              )
            else
              raise(ArgumentError,"unsupported websocket scheme: #{url}")
            end
end

Instance Attribute Details

#socketTCPServer, OpenSSL::SSL::SSLServer (readonly)

The underlying server socket.

Returns:

  • (TCPServer, OpenSSL::SSL::SSLServer)


43
44
45
# File 'lib/ronin/support/web/websocket/server.rb', line 43

def socket
  @socket
end

Instance Method Details

#acceptClient

Accepts a new WebSocket connection.

Returns:

  • (Client)

    The new WebSocket connection to the server.



136
137
138
# File 'lib/ronin/support/web/websocket/server.rb', line 136

def accept
  Client.new(@url,@socket.accept)
end

#closeObject

Closes the WebSocket server's socket.



145
146
147
# File 'lib/ronin/support/web/websocket/server.rb', line 145

def close
  @socket.close
end

#closed?Boolean

Determines if the WebSocket server is closed?

Returns:

  • (Boolean)


156
157
158
# File 'lib/ronin/support/web/websocket/server.rb', line 156

def closed?
  @socket.closed?
end

#listen(backlog) ⇒ Object

Sets the connection backlog for the server socket.

Parameters:

  • backlog (Integer)

    The number of pending connection to allow.



126
127
128
# File 'lib/ronin/support/web/websocket/server.rb', line 126

def listen(backlog)
  @socket.listen(backlog)
end