Add support for non-web WebSocket connections.

Closes #2066.
This commit is contained in:
Sadie Powell 2024-05-23 14:22:11 +01:00
parent 09e8670459
commit 07d7535281
2 changed files with 20 additions and 2 deletions

View File

@ -2640,15 +2640,24 @@
# messages as raw binary frames, or "reject" to close
# connections which do not request a subprotocol. Defaults
# to "text".
#
# proxyranges: A space-delimited list of glob or CIDR matches to trust
# the X-Real-IP or X-Forwarded-For headers from. If enabled
# the server will use the IP address specified by those HTTP
# headers. You should NOT enable this unless you are using
# a HTTP proxy like nginx as it will allow IP spoofing.
# nativeping: Whether to check client connectivity using WebSocket ping
# messages instead of IRC ping messages. Defaults to yes.
#
# allowmissingorigin: Whether to allow connections from clients that
# don't send an origin header. These are probably
# not web clients so it probably safe to allow this.
# Defaults to yes.
#
# nativeping: Whether to check client connectivity using WebSocket ping
# messages instead of IRC ping messages. Defaults to yes.
#
#<websocket defaultmode="text"
# proxyranges="192.0.2.0/24 198.51.100.*"
# allowmissingorigin="yes"
# nativeping="yes">
#
# If you use the websocket module you MUST specify one or more origins

View File

@ -58,6 +58,9 @@ struct WebSocketConfig final
// The IP ranges which send trustworthy X-Real-IP or X-Forwarded-For headers.
ProxyRanges proxyranges;
// Whether to allow connections from clients that do not send an Origin header.
bool allowmissingorigin;
// Whether to send WebSocket ping messages instead of IRC ping messages.
bool nativeping;
};
@ -400,6 +403,11 @@ class WebSocketHook final
}
}
}
else if (config.allowmissingorigin)
{
// This is a non-web WebSocket connection.
allowedorigin = true;
}
else
{
FailHandshake(sock, "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n", "WebSocket: Received HTTP request that did not send the Origin header");
@ -662,6 +670,7 @@ public:
for (std::string proxyrange; proxyranges.GetToken(proxyrange); )
config.proxyranges.push_back(proxyrange);
config.allowmissingorigin = tag->getBool("allowmissingorigin", true);
config.nativeping = tag->getBool("nativeping", true);
// Everything is okay; apply the new config.