Add WebSocket support for exchanging events with external clients.

This exposes Broker's new WebSocket support in Zeek. To enable it,
call `Broker::listen_websocket()`. Zeek will then start listening on
port 9997 for incoming WebSocket connections.

See the Broker documentation for a description of the message format
expected over these WebSocket connections.
This commit is contained in:
Robin Sommer 2022-05-19 07:45:02 +02:00
parent 4b0e1063ed
commit d99f041ac5
No known key found for this signature in database
GPG key ID: 6BEDA4DA6B8B23E3
25 changed files with 248 additions and 26 deletions

View file

@ -3,10 +3,18 @@
module Broker;
export {
## Default port for Broker communication. Where not specified
## Default port for native Broker communication. Where not specified
## otherwise, this is the port to connect to and listen on.
const default_port = 9999/tcp &redef;
## Default port for Broker WebSocket communication. Where not specified
## otherwise, this is the port to connect to and listen on for
## WebSocket connections.
##
## See the Broker documentation for a specification of the message
## format over WebSocket connections.
const default_port_websocket = 9997/tcp &redef;
## Default interval to retry listening on a port if it's currently in
## use already. Use of the ZEEK_DEFAULT_LISTEN_RETRY environment variable
## (set as a number of seconds) will override this option and also
@ -18,6 +26,11 @@ export {
## .. zeek:see:: Broker::listen
const default_listen_address = getenv("ZEEK_DEFAULT_LISTEN_ADDRESS") &redef;
## Default address on which to listen for WebSocket connections.
##
## .. zeek:see:: Broker::listen_websocket
const default_listen_address_websocket = getenv("ZEEK_DEFAULT_LISTEN_ADDRESS") &redef;
## Default interval to retry connecting to a peer if it cannot be made to
## work initially, or if it ever becomes disconnected. Use of the
## ZEEK_DEFAULT_CONNECT_RETRY environment variable (set as number of
@ -267,7 +280,7 @@ export {
val: Broker::Data;
};
## Listen for remote connections.
## Listen for remote connections using the native Broker protocol.
##
## a: an address string on which to accept connections, e.g.
## "127.0.0.1". An empty string refers to INADDR_ANY.
@ -287,6 +300,26 @@ export {
p: port &default = default_port,
retry: interval &default = default_listen_retry): port;
## Listen for remote connections using WebSocket.
##
## a: an address string on which to accept connections, e.g.
## "127.0.0.1". An empty string refers to INADDR_ANY.
##
## p: the TCP port to listen on. The value 0 means that the OS should choose
## the next available free port.
##
## retry: If non-zero, retries listening in regular intervals if the port cannot be
## acquired immediately. 0 disables retries. If the
## ZEEK_DEFAULT_LISTEN_RETRY environment variable is set (as number
## of seconds), it overrides any value given here.
##
## Returns: the bound port or 0/? on failure.
##
## .. zeek:see:: Broker::status
global listen_websocket: function(a: string &default = default_listen_address_websocket,
p: port &default = default_port_websocket,
retry: interval &default = default_listen_retry): port;
## Initiate a remote connection.
##
## a: an address to connect to, e.g. "localhost" or "127.0.0.1".
@ -473,7 +506,7 @@ event retry_listen(a: string, p: port, retry: interval)
function listen(a: string, p: port, retry: interval): port
{
local bound = __listen(a, p);
local bound = __listen(a, p, Broker::NATIVE);
if ( bound == 0/tcp )
{
@ -489,6 +522,29 @@ function listen(a: string, p: port, retry: interval): port
return bound;
}
event retry_listen_websocket(a: string, p: port, retry: interval)
{
listen_websocket(a, p, retry);
}
function listen_websocket(a: string, p: port, retry: interval): port
{
local bound = __listen(a, p, Broker::WEBSOCKET);
if ( bound == 0/tcp )
{
local e = getenv("ZEEK_DEFAULT_LISTEN_RETRY");
if ( e != "" )
retry = double_to_interval(to_double(e));
if ( retry != 0secs )
schedule retry { retry_listen_websocket(a, p, retry) };
}
return bound;
}
function peer(a: string, p: port, retry: interval): bool
{
return __peer(a, p, retry);