Metadata-Version: 2.4
Name: shinqlx
Version: 0.9.1
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Requires-Dist: setuptools>=61.2
Requires-Dist: redis>=5.0
Requires-Dist: sphinx>=7 ; extra == 'docs'
Requires-Dist: sphinx-autoapi>=3 ; extra == 'docs'
Requires-Dist: hiredis>=3.0 ; extra == 'hiredis'
Requires-Dist: mypy>=0.990 ; extra == 'mypy'
Requires-Dist: types-redis>=4.3 ; extra == 'mypy'
Requires-Dist: ruff>=0.17 ; extra == 'ruff'
Requires-Dist: ty>=0.0.91 ; extra == 'ty'
Provides-Extra: docs
Provides-Extra: hiredis
Provides-Extra: mypy
Provides-Extra: ruff
Provides-Extra: ty
License-File: LICENSE
Summary: ShiN0's QuakeLive extension in Rust
License-Expression: BSD-3-Clause
Requires-Python: >=3.9
Description-Content-Type: text/markdown
Project-URL: Changelog, https://github.com/mgaertne/shinqlx/blob/main/CHANGELOG.md
Project-URL: Documentation, https://shinqlx.readthedocs.io/
Project-URL: Homepage, https://github.com/mgaertne/shinqlx
Project-URL: Issues, https://github.com/mgaertne/shinqlx/issues
Project-URL: Source, https://github.com/mgaertne/shinqlx

# shinqlx

![python](https://img.shields.io/badge/python-3.9%7C3.10%7C3.11%7C3.12%7C3.13%7C3.14%7C3.15-blue.svg)
![Tests](https://github.com/mgaertne/shinqlx/actions/workflows/ci.yml/badge.svg)
[![Documentation](https://app.readthedocs.org/projects/shinqlx/badge/?version=latest)](https://shinqlx.readthedocs.io/)
[![codecov](https://codecov.io/gh/mgaertne/shinqlx/branch/main/graph/badge.svg?token=VK9QI52BZX)](https://codecov.io/gh/mgaertne/shinqlx)

ShiN0's Quake Live eXtension, implemented in Rust. Most functionality
from [minqlx](https://raw.githubusercontent.com/MinoMino/minqlx) should work. Support for Python 3.9 and above should
work out of the box.

Some limitations apply for certain minqlx functions maybe used in plugins.

* 32-bit is unsupported. Earlier versions included support for it, but after too long of being untests, we dropped
  support for that older CPU types.
* Some compatibility might not work, as this implementation is not yet fully tested.
* plugins that use db.zincrby, db.lrem or db.setex need to provide the value and amount parameters through keyword
  arguments. This might be a breaking change for some plugins, but ensures Python redis 2.0 compatibility for these
  calls to later versions. We stick with redis >= 5.0.

The documentation is at [shinqlx.readthedocs.io](https://shinqlx.readthedocs.io/). It has a page "Migration from
minqlx" that lists all deviations. The tool `plugin_migration` (see
[plugin_migration/README.md](plugin_migration/README.md)) migrates a whole folder of minqlx plugins. The tool `ql_demo`
(see [ql_demo/README.md](ql_demo/README.md)) converts the match recordings of shinqlx to Quake Live demos. The console
tool `ql_rcon` (see [ql_rcon/README.md](ql_rcon/README.md)) and the stats viewer `ql_stats` (see
[ql_stats/README.md](ql_stats/README.md)) talk to the ZMQ interfaces of a server.

# Compilation and installation

- Install rust with the default profile and the nightly toolchain and Rust's build tool cargo, and make sure to add the
  rust-src component:

```shell
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup install nightly --profile default
rustup +nightly component add rust-src
rustup default nightly
```

- Install Python 3.

```shell
sudo apt-get update
sudo apt-get -y install python3 python3-dev python3-venv python3-pip
```

- Make sure, that you have installed Python 3.9 or later:

```shell
python3 --version
```

- Now you should get Redis, Git and build utils which will be used by minqlx's plugins and for compiling shinqlx:

```shell
sudo apt-get -y install redis-server pkg-config libssl-dev git build-essential
```

  Valkey, KeyDB and Dragonfly work in place of Redis; see the `Redis` class in the database documentation.

- To avoid interference with operating system packages (and future adaptions from Debian v12 onwards for example), I
  recommend installing a python virtual environment to install the packges that shinqlx/minqlx or your plugins need:

```shell
python3 -m venv ~/qlds/.venv
source ~/qlds/.venv/bin/activate
```

If you need to install additional packages later, you can activate the virtual python environment
with `source ~/qlds/.venv/bin/activate` anytime, and then run your `pip install <library>` as you like.

- Install maturin into the virtual python environment:

```shell
pip install maturin
```

- Install shinqlx from pypi source distribution into the virtual python environment (this may take a while):

```shell
pip install shinqlx
```

- Since this may take a while, if you want to see the current progress use run in verbose mode:

```shell
pip install -v shinqlx
```

- Copy the convenience script `run_server_shinqlx.sh` into `~/qlds`, or whatever other directory you might have
  installed the quake live dedicated server files in. (Note: The remaining sections assume you installed the dedicated
  server into ~/qlds)

```shell
cp run_server_shinqlx.sh ~/qlds/
```

Note: In your server startup script, you will have to also activate the the python virtual environment where the
different python libraries are installed. I recommend writing a custom script that can be started with `supervisord`
later on, that calls run_server_shinqlx.sh, i.e.:

```shell
#!/bin/bash
basepath="$(dirname $0)"
gameport=`expr $1 + 27960`
rconport=`expr $1 + 28960`

source ~/.quakelive/$gameport/.venv/bin/activate

exec $basepath/run_server_shinqlx.sh \
+set fs_basepath $basepath \
+set net_port $gameport \
+set fs_homepath ~/.quakelive/$gameport \
+set zmq_rcon_password "<a super secret rcon password that no one will ever guess>" \
+set zmq_rcon_port $rconport \
+set zmq_stats_password "<a super secret stats password that sites like qlstats.net will need to know to gather stats from your server>" \
+set zmq_stats_port $gameport
```

`run_server.sh 0` will start a server on port 27960, `run_server.sh 1` on port 27961, and so on.

# Cargo features

| Feature             | Default | Purpose                                                                           |
|---------------------|---------|-----------------------------------------------------------------------------------|
| `alloc`             | yes     | jemalloc (mimalloc on Windows) as global allocator                                |
| `steam-query-limit` | yes     | rate limit for server queries against reflection and floods                       |
| `patches`           | no      | enables `clientkick-fix`, `zmq-rcon-fix` and `steam-query-limit`                |
| `race-hooks`        | no      | race maps: touching a start point again keeps the run time, `target_init` triggers honour their KEEP* spawnflags, experimental |
| `clientkick-fix`    | no      | patch for the callvote clientkick lookup in `qagame`, experimental                |
| `fps-patch`         | no      | `sv_fps` can be set from 10 to 250, default 125, see [Server frame rate](#server-frame-rate), experimental |
| `zmq-stats`         | no      | reads the stats from the ZMQ socket (`zmq_stats_enable`) with the `StatsListener`; the stats events work without it, through a hook |
| `zmq-rcon-fix`      | yes     | protects against dead RCON clients: drops the engine's "zmq RCON client disconnected" report and limits how long the engine waits for a client (`qlx_zmqRconSendTimeout`) |
| `demo-recording`    | no      | records matches with the view of every player, to be converted to demos with `ql_demo`, experimental, see [Recording matches](https://shinqlx.readthedocs.io/en/latest/demos.html) |
| `bench-support`     | no      | exposes internals to the benchmarks, not for production builds                    |

Enable or disable features when building, e.g. `maturin build --release --features patches`, or without the query
limit: `maturin build --release --no-default-features --features alloc`.

The build uses the baseline of the target architecture, so that the result runs on any CPU of it. To optimize for the
machine that builds and runs the server, set `RUSTFLAGS="-C target-cpu=native"` or the same flag in your own
`~/.cargo/config.toml`; do not copy such a build to another machine.

# Protection against query floods

Quake Live servers can be abused as reflectors by forged server queries. shinqlx contains a rate limit for these
queries (Cargo feature `steam-query-limit`). How it works, how to configure it, and how to combine it with nftables and
XDP is described in [DDOS-PROTECTION.md](DDOS-PROTECTION.md).

# Large matches

With about 20 or more players the end of a match can overrun the 64 reliable commands the engine keeps per client
(*a reliable command was cycled out*), and an overlong team scoreboard is thrown away by the engine. shinqlx spreads
the burst of `castats`, prints and chat over several frames and cuts overlong scoreboards back, both on by default. The
page "Reliable commands and scoreboards" of the documentation describes the cvars and the console commands `reliable`
and `scoreboard`.

# Server frame rate

With the Cargo feature `fps-patch` the server runs with `sv_fps` 125 instead of the read-only 40, and other rates from
10 to 250 can be set in `server.cfg`, with `+set sv_fps` on the command line (which wins over `server.cfg`) or in the
console. Experimental.

* The `server.cfg` that comes with the server contains `set sv_fps "40"`. Remove or change that line, or the server
  keeps running at 40.
* A change takes effect with the next map or `map_restart`, never in the middle of a match. Only a plugin that forces
  the value (`shinqlx.set_cvar` with the flags `-1`) changes it at once.
* Other values are rejected, because the engine does not check them and crashes with 0. A rejected value at the start
  leaves the server at 125.
* The engine counts whole milliseconds for each frame, so the rate is 1000 divided by those: 40, 50, 100, 125, 200 and
  250 are exact, 60 runs at 62.5 and 80 at 83.3. The log shows the effective rate.
* The limits of the reliable guard count frames and pass faster at higher rates.

# Supervisor configuration

I recommend running your ShiNQlx server through a process monitor like `supervisor`. Unfortunately, due to the nature
Rust handles crashes with its own panic system, some crashes may not result in the server exiting properly for the
supervisor daemon to proper restart your server. Until I can come up with a proper way to solve this, I recommend to add
an event listener configuration to your supervisor configuration to automatically check whether your server port is
still reachable, and restart the server automatically if it's not.

A crash with `SIGSEGV` ends the server by itself: the handler of the engine runs first, and if it hangs, the process
is ended with the signal (and a core dump) after 10 seconds. `SIGTERM` and `SIGINT` make the server execute `quit`
at the next frame (the engine alone does not end on them while the ZMQ rcon is enabled), and the process is ended after
30 seconds if the server is still there. The check below remains useful for other kinds of hangs.

The [`supervisor_checks` package](https://github.com/vovanec/supervisor_checks) provides the main means for this.
However, you will have to install the package to your OS pip environment, like so:

```shell
sudo pip3 install --break-system-packages supervisor supervisor_checks
```

After installation, you can configure your server in the supervisor configuration. Here is an
example `/etc/supervisor/conf.d/quakelive.conf`, running two servers on the same host, and the supervisor_checks
checking every 60 seconds for connectivity to the respective port (27960 and 27961) and restarting the server in case it
stopped responding to TCP connection attempts on the server port:

```ini
[program:quakelive]
command = /home/steam/bin/run_server.sh %(process_num)s
user = steam
process_name = qzeroded_%(process_num)s
numprocs = 2
autorestart = true

[eventlistener:ql_heartbeat_0]
command = /usr/local/bin/supervisor_tcp_check -N "quakelive:qzeroded_0" -n ql_heartbeat_0 -r 1 -p 27960
events = TICK_60

[eventlistener:ql_heartbeat_1]
command = /usr/local/bin/supervisor_tcp_check -N "quakelive:qzeroded_1" -n ql_heartbeat_1 -r 1 -p 27961
events = TICK_60
```

