161 lines
5.4 KiB
Markdown
161 lines
5.4 KiB
Markdown
# yourchat2
|
|
|
|
Rust-basierter Chat-Service/Daemon zur Entlastung des Backends von `YourPart3`.
|
|
Die Kommunikation erfolgt ueber WebSocket (Frontend) und optional TCP/Unix-Socket (Bridge/Backend).
|
|
|
|
## Features
|
|
|
|
- Asynchroner WebSocket-Server fuer `MultiChatDialog` (Standard `0.0.0.0:1235`)
|
|
- Zusaetzlicher TCP-Server fuer `chatTcpBridge.js` (Standard `127.0.0.1:1236`)
|
|
- Optional zusaetzlicher Unix Domain Socket
|
|
- In-Memory-Room-Management
|
|
- Token-basierte Session-Absicherung pro Verbindung
|
|
- Username-Pruefung (3-32 Zeichen, `[a-zA-Z0-9_]`)
|
|
- Verhindert Mehrfach-Login mit identischem Namen (`loggedin`)
|
|
- Optionale Allowlist fuer User via `CHAT_ALLOWED_USERS`
|
|
- Optionale DB-basierte User-Pruefung via Postgres (`CHAT_DB_URL`)
|
|
- Kompatible Antworttypen fuer die bestehende `chatTcpBridge.js`
|
|
|
|
## Start
|
|
|
|
```bash
|
|
cd ~/Programs/yourchat2
|
|
cargo run --release
|
|
```
|
|
|
|
Der Server lauscht danach standardmaessig auf:
|
|
|
|
- `ws://0.0.0.0:1235` (Frontend `MultiChatDialog`)
|
|
- `tcp://127.0.0.1:1236` (Backend-Bridge)
|
|
|
|
## Konfiguration (Umgebungsvariablen)
|
|
|
|
- `CHAT_WS_ADDR` (default: `0.0.0.0:1235`)
|
|
- `CHAT_TCP_ADDR` (default: `127.0.0.1:1236`)
|
|
- `CHAT_UNIX_SOCKET` (optional, z. B. `/run/yourchat2/yourchat2.sock`)
|
|
- `CHAT_WS_TLS` (optional, `true` fuer direktes WSS auf `CHAT_WS_ADDR`)
|
|
- `CHAT_TLS_CERT_PATH` (Pfad zum PEM-Zertifikat fuer WSS)
|
|
- `CHAT_TLS_KEY_PATH` (Pfad zum PEM-Private-Key fuer WSS)
|
|
- `CHAT_ALLOWED_USERS` (optional, CSV-Liste erlaubter Usernamen, z. B. `alice,bob,carol`)
|
|
- `CHAT_DB_URL` (optional, PostgreSQL-Connection-String fuer Community-/Chat-User-Checks)
|
|
- `SECRET_KEY` (benoetigt fuer Entschluesselung verschluesselter Birthdate-Werte aus der DB)
|
|
|
|
Beispiel:
|
|
|
|
```bash
|
|
CHAT_WS_ADDR=0.0.0.0:1235 CHAT_TCP_ADDR=127.0.0.1:1236 CHAT_UNIX_SOCKET=/tmp/yourchat2.sock cargo run --release
|
|
```
|
|
|
|
## Protokoll (Kurzfassung)
|
|
|
|
Alle Requests/Responses sind JSON-Objekte.
|
|
- WebSocket: ein JSON pro Frame (Text)
|
|
- TCP/Unix-Socket: newline-delimited JSON
|
|
|
|
### Eingehende Commands
|
|
|
|
- `{"type":"init","name":"alice","room":"lobby"}`
|
|
- `{"type":"join","room":"sports","token":"..."}`
|
|
- `{"type":"message","message":"Hi","token":"..."}`
|
|
- `{"type":"scream","message":"Hallo alle","token":"..."}`
|
|
- `{"type":"do","message":"winkt","token":"..."}`
|
|
- `{"type":"dice","message":"1d6","token":"..."}`
|
|
- `{"type":"roll","value":4,"token":"..."}`
|
|
- `{"type":"color","value":"#33aaee","token":"..."}`
|
|
- `{"type":"rooms","token":"..."}`
|
|
- `{"type":"userlist","token":"..."}`
|
|
- `{"type":"start_dice_game","rounds":3,"token":"..."}`
|
|
- `{"type":"end_dice_game","token":"..."}`
|
|
- `{"type":"reload_rooms","token":"..."}`
|
|
- `{"type":"ping"}`
|
|
|
|
Zusatz: Bei `{"type":"message", ...}` werden Slash-Commands erkannt:
|
|
- `/join <room> [password]`
|
|
- `/color <hex>`
|
|
- `/dice [expr]` oder `/roll [expr]`
|
|
- `/start_dice_game <runden 1..10>`
|
|
- `/end_dice_game`
|
|
- `/reload_rooms`
|
|
- `/do <aktion>`
|
|
- `/scream <text>`
|
|
- `/rooms` und `/userlist`
|
|
|
|
Wenn `CHAT_DB_URL` gesetzt ist, prueft `init` den User gegen `community."user"`.
|
|
Falls fuer den Community-User noch kein Eintrag in `chat."user"` existiert, wird er automatisch angelegt.
|
|
Farbe und Rechte werden aus der DB geladen.
|
|
Fuer Alterspruefungen in Raeumen wird `community.user_param.value` (birthdate) per AES-256-ECB
|
|
mit einem via scrypt aus `SECRET_KEY` abgeleiteten Schluessel entschluesselt (kompatibel zur Alt-App).
|
|
|
|
### Wichtige Antworten
|
|
|
|
- Token: `{"type":1,"message":"<token>"}`
|
|
- Roomliste: `{"type":3,"message":[{"name":"lobby","users":2}]}`
|
|
- Room betreten: `{"type":5,"message":"room_entered","to":"lobby"}`
|
|
- Farbaenderung: `{"type":5,"message":"color_changed","userName":"alice","color":"#33aaee"}`
|
|
- Normale Message: `{"type":"message","userName":"alice","message":"Hi","color":"#33aaee"}`
|
|
- Scream: `{"type":6,"userName":"alice","message":"Hallo alle","color":"#33aaee"}`
|
|
|
|
## Integration mit `YourPart3`
|
|
|
|
In `YourPart3/backend/config/chatBridge.json` sollte stehen:
|
|
|
|
```json
|
|
{
|
|
"host": "127.0.0.1",
|
|
"port": 1236
|
|
}
|
|
```
|
|
|
|
Dann verbindet sich die bestehende Bridge (`chatTcpBridge.js`) direkt mit `yourchat2`.
|
|
|
|
## Raeume per CLI pruefen
|
|
|
|
Die Raumdefinitionen kommen aus `chat.room` (bei gesetztem `CHAT_DB_URL`).
|
|
Ohne DB-Verbindung gibt es einen Fallback mit `lobby`.
|
|
|
|
Pruefen auf Kommandozeile:
|
|
|
|
```bash
|
|
cd /home/tsschulz/Programs/yourchat2
|
|
CHAT_DB_URL='postgres://user:pass@host:5432/dbname' ./target/release/yourchat2 --list-rooms
|
|
```
|
|
|
|
Wenn der Service ueber systemd laeuft und die Variablen in `/etc/yourchat2/yourchat2.env` stehen:
|
|
|
|
```bash
|
|
sudo -u tsschulz bash -lc 'set -a; source /etc/yourchat2/yourchat2.env; set +a; /usr/local/bin/yourchat2 --list-rooms'
|
|
```
|
|
|
|
## Systemd (optional)
|
|
|
|
Es liegt eine Produktions-nahe Unit-Datei `yourchat2.service` im Projekt.
|
|
Zusaetzlich gibt es ein Installationsskript `install-systemd.sh`, das:
|
|
|
|
- Release-Binary baut
|
|
- Binary nach `/usr/local/bin/yourchat2` installiert
|
|
- Unit nach `/etc/systemd/system/yourchat2.service` installiert
|
|
- Arbeitsverzeichnis `/var/lib/yourchat2` erstellt
|
|
- Environment-Datei unter `/etc/yourchat2/yourchat2.env` anlegt (falls nicht vorhanden)
|
|
- Service aktiviert und startet
|
|
- Standard-Config aus `config/yourchat2.env.example` nach `/etc/yourchat2/yourchat2.env` uebernimmt (falls noch nicht vorhanden)
|
|
|
|
Installation:
|
|
|
|
```bash
|
|
cd /home/tsschulz/Programs/yourchat2
|
|
sudo ./install-systemd.sh
|
|
```
|
|
|
|
Danach Konfiguration ueber:
|
|
|
|
- `/etc/yourchat2/yourchat2.env`
|
|
|
|
Wichtige Variablen fuer produktiven Betrieb:
|
|
|
|
- `CHAT_DB_URL`
|
|
- `SECRET_KEY`
|
|
|
|
Standard-Config-Template im Projekt:
|
|
|
|
- `config/yourchat2.env.example`
|