First pass at connection counting in nginx.conf

This commit is contained in:
ltning 2024-08-11 13:43:46 +00:00
parent 99ada17af5
commit 4cff849bb3

View file

@ -36,6 +36,11 @@ stream {
server 192.88.99.15:8023 max_conns=2;
}
# Create a shared DICT (an in-memory database) to keep track of active
# users that have passed the challenge. This will be used to give a
# sensible response to connections made when all nodes are busy.
lua_shared_dict users 64k;
# Define our actual listeners, math quiz and all.
server {
# Listen on port 23 (duh); the whole point of this exercise is to be
@ -46,8 +51,27 @@ stream {
# The real magic: A block of LUA code which takes over the
# connection before it gets proxied to the BBS nodes.
# See https://anduin.net/~ltning/bbs/bbs_math.lua ..
# We start by creating an Nginx variable, which will be used from
# the script:
set $bbs_challenge_passed 0;
# Then we call the script:
preread_by_lua_file bbs_math.lua;
# Assuming the user has passed the challenge, we now have an active
# connection, and the counter has presumably been increased (see the
# bbs_math.lua file above). We need to decrease it again once the
# session ends.
# We register a lua function to be run at the end of the connection
# (during the "log" phase), which triggers only if the challenge
# was passed.
log_by_lua_block {
local users = ngx.shared.users
if ngx.var.bbs_challenge_passed == 1 then
users:incr(active, -1, 1)
end
}
# Don't wait too long when we attempt to connect to the BBS. If it
# doesn't want to talk to us (dead?), we drop the connection.
proxy_connect_timeout 10s;