TrapWatch server mode
=====================

Run TrapWatch as a service and open its console from any browser on your network.

The desktop app needs a graphical session. Server mode does not: it receives,
decodes, stores and alerts exactly the same way, keeps running when nobody is
signed in to the server, and shows the same screen in your browser instead of a
window — over HTTPS, behind a username and password.

What a minimal server still needs
---------------------------------
Server mode opens no window and contacts no X server, but the program is one
binary and the system loads every library that binary is linked against before
it starts. So GTK, X11, NSS and ALSA have to be installed even though nothing
draws. The .deb package lists them as dependencies and apt installs them for
you. If they are missing anyway, trapwatch-headless says so and prints the
apt / dnf line rather than leaving you with a bare "error while loading shared
libraries".

Licence agreement
-----------------
TrapWatch does not receive traps until the licence agreement has been
accepted. The agreement is EULA.txt in this folder (also
https://meshwatch.app/terms). Read it, then either

  - start with --accept-eula, which records your acceptance in the data folder
    (eula.json) and prints one line saying so, or
  - start without it and sign in to the console as an administrator: it shows
    the agreement first, and trap reception starts when you select "I agree".

The service unit (trapwatch-headless.service) passes --accept-eula:
installing and enabling it means you accept the agreement. When a later
version changes the agreement, it asks again.

Try it in the foreground first
------------------------------
    /opt/TrapWatch/trapwatch-headless --accept-eula

It prints the console address and the certificate fingerprint:

    console  https://traps01:8162/
             https://192.168.1.30:8162/
    certificate  self-signed (~/.config/TrapWatch/tls/cert.pem)
             SHA-256 3A:1F:...:C4
    accounts NONE YET — create the first administrator: ...

Open https://localhost:8162/ in a browser on the same machine and create the
first administrator there. Send a test trap to see it arrive (snmptrap is in the
net-snmp package: apt-get install snmp / dnf install net-snmp-utils):

    snmptrap -v 2c -c public 127.0.0.1:1162 '' 1.3.6.1.6.3.1.1.5.3 \
        1.3.6.1.2.1.2.2.1.1.1 i 1 1.3.6.1.2.1.2.2.1.2.1 s "eth0"

That is a linkDown for interface 1 — it shows up as "Link down" on eth0.

Run it as a service: see trapwatch-headless.service in this folder — the
commands are at the top of that file. Open TCP 8162 (the console) and UDP 1162
in the server's firewall if it has one (ufw allow 8162/tcp; ufw allow 1162/udp).

Create the first administrator
------------------------------
Nobody can sign in until an administrator exists, and for safety the first one
can only be created from the server itself — otherwise anyone on the network
could claim it before you. Either run, on the server:

    sudo -u trapwatch TRAPWATCH_DATA_DIR=/var/lib/trapwatch/TrapWatch \
        /opt/TrapWatch/trapwatch-headless --create-admin admin

(it asks for the password twice and does not show it; at least 12 characters),
or open https://localhost:8162/ in a browser on the server, or tunnel to it from
your PC and open the same address there:

    ssh -L 8162:127.0.0.1:8162 you@traps01

Open the console from your PC
-----------------------------
Open https://<server>:8162/ in your browser. The certificate is self-signed, so
the browser warns that it does not know it. Open the certificate details, check
that its SHA-256 fingerprint is the one in the service log (the sign-in page
shows it too), then continue and sign in. A plain http:// address on the same
port is redirected to https://.

Accounts and roles
------------------
  Administrator   everything: settings (port, community strings), alert rules,
                  the licence, Central, accounts
  Viewer          sees traps, the summary and anomalies, exports CSV — cannot
                  change anything (the server refuses it and the screen says so).
                  Viewers can see the settings, including community strings.

Add people in the console under Accounts (top right), or on the server:

    /opt/TrapWatch/trapwatch-headless --add-user alice --role viewer
    /opt/TrapWatch/trapwatch-headless --list-users
    /opt/TrapWatch/trapwatch-headless --reset-password alice     # forgotten password; signs her out everywhere

Run these as the service user with the same data folder (as above). Sessions
end after 12 hours without activity, on Sign out, and when the service restarts.
Ten failed sign-ins a minute from one address, or five in a row for one account,
lock further attempts for a while.

HTTPS
-----
  - The self-signed certificate lives in <data folder>/tls (cert.pem, key.pem,
    mode 0600). It is valid for 825 days and is replaced automatically 30 days
    before it expires — the log then says the fingerprint is new.
  - Your own certificate: --cert /path/fullchain.pem --key /path/privkey.pem
    (the service user must be able to read both).
  - --insecure-http serves plain HTTP. Use it only behind an HTTPS reverse proxy
    on the same machine, together with --ui-host 127.0.0.1. Requests that arrive
    through a proxy (X-Forwarded-For) never count as "this machine", so create
    the first administrator with --create-admin.

Options: trapwatch-headless --help
Lost the address or the fingerprint: trapwatch-headless --show-url

Port 162
--------
TrapWatch listens on UDP 1162 by default, which any user may open. Devices
that can only send to 162 need the service to bind a privileged port: change
the port to 162 in Settings, then uncomment

    AmbientCapabilities=CAP_NET_BIND_SERVICE

in trapwatch-headless.service and `systemctl daemon-reload && systemctl
restart trapwatch-headless`. If the port cannot be opened, the reason is in the
journal (journalctl -u trapwatch-headless) as well as at the top of the console.

Upgrading from 0.1.5 (token sign-in)
------------------------------------
0.1.5 opened the screen with a one-time token (ui-token in the data folder). That
token no longer opens anything; the console now uses accounts, and the log says
so on the first start. Create the first administrator as above.

  - Your existing unit file in /etc/systemd/system still says --ui-host 127.0.0.1,
    so the console stays on this machine until you remove that option (the new
    unit file in this folder no longer has it).
  - If you put an HTTPS reverse proxy in front as 0.1.5 suggested, either point it
    at https://127.0.0.1:8162 (the console now speaks HTTPS itself), or add
    --insecure-http to keep plain HTTP behind the proxy.

Secrets on a server
-------------------
There is no keyring on a server, so the licence key, alert rules and the
Central token are stored as plain files readable only by the service user
(mode 0600) under the data folder. settings.json (community strings) is
written with mode 0600 as well. Account passwords are stored only as scrypt
hashes (users.json, mode 0600).

Notifications
-------------
There is no desktop to notify. A rule that would have raised a desktop
notification writes one line to the journal instead (journalctl -u
trapwatch-headless). MeshWatch Central works as on the desktop — it is the
intended alert path for a server.

Same data as the desktop app
----------------------------
Server mode and desktop use the same folder (~/.config/TrapWatch for the user
that runs it, or TRAPWATCH_DATA_DIR), so a licence activated in one is active
in the other. As a service the folder is /var/lib/trapwatch/TrapWatch.

Behind a corporate proxy
------------------------
TrapWatch makes two kinds of outbound request, both to meshwatch.app: the
once-a-day check for a newer version, and up to three one-time anonymous
start-up signals per installation (see https://meshwatch.app/privacy). Receiving
and the console are inbound and need no proxy. If the server reaches the
Internet only through a proxy, give it the usual variables — trapwatch-headless
tells the runtime to honour them:

    Environment=HTTPS_PROXY=http://proxy.example.com:3128
    Environment=NO_PROXY=localhost,127.0.0.1

in the [Service] section of trapwatch-headless.service (there are commented
lines ready for it), then systemctl daemon-reload and restart. Without the
variables nothing changes. To stop both requests altogether, set
MESHWATCH_DISABLE_UPDATE_CHECK=1 the same way, or create an empty file named
no-update-check in the data folder.
