GsDChat/README.md
2026-10-10 16:40:35 +00:00

217 lines
7.5 KiB
Markdown

# GsDChat
A lightweight, secure, and private community chat platform built in pure C# (.NET 10) without external non-.NET libraries.
GsDChat provides a single-server architecture with SQLite persistence, dynamic DLL modding via `AssemblyLoadContext`, HTTPS/WSS and tunnel-ready HTTP/WS support, authenticated file transfers, and a server-driven screen-swapping Terminal User Interface (TUI).
---
## Key Features
- **Pure .NET Architecture**: Strict adherence to the standard .NET runtime and first-party `Microsoft.Data.Sqlite`. Zero native/unmanaged C/C++ third-party dependencies.
- **Flexible Network & Tunnel Support**: Configurable direct HTTPS/WSS (with custom PFX/PEM certificates or self-signed certs) or plain HTTP/WS for running inside Docker containers behind **Cloudflare Tunnels (`cloudflared`)**, Tailscale, or reverse proxies.
- **Screen-Swapping TUI Client**: Built with ANSI escape codes and `System.Console` (zero third-party UI dependencies). Fast screen-swapping using function keys:
- **F1**: Active Channel Chat (scrollable history, timestamps, role badges, file card indicators, bottom input box).
- **F2**: Channel Browser (channel list, topics, active indicator, join and create channels).
- **F3**: User Directory (member list, role tags, real-time online `●` / offline `○` presence).
- **F4**: Shared Files Browser (table of uploaded files, download to `./downloads/`, upload from local path).
- **F5+**: Mod-defined custom screens rendered dynamically from server layout schemas.
- **Dynamic Modding System**: Place mod `.dll` files in `./mods/` to dynamically extend the server using `AssemblyLoadContext`:
- Intercept and transform chat messages (e.g. shorthand emoji formatting `:shrug:`, word filters).
- Register custom slash commands (e.g. `/roll 2d6`, `/announce`).
- Subscribe to server lifecycle and entity events (`UserConnected`, `ChannelCreated`, `FileUploaded`).
- Inject custom TUI screens or customize server MOTD.
- **Security & Authentication**:
- PBKDF2 with SHA-512 (100,000 iterations) and 256-bit cryptographically secure salt.
- Constant-time verification preventing timing attacks.
- 256-bit secure session tokens.
- Optional server-wide password gate.
- Role-based permissions (`Owner`, `Admin`, `Member`). First registered user automatically becomes the Server Owner.
- **Direct File Transfers**: Authenticated multipart HTTPS uploads (`POST /api/files/upload`) and streaming downloads (`GET /api/files/{id}`) with SHA-256 integrity verification.
---
## Project Structure
```
GsDChat/
├── GsDChat.slnx
├── src/
│ ├── GsDChat.Core/ # Shared models, crypto, and network/UI protocols
│ ├── GsDChat.Modding/ # Mod SDK: IMod, pipelines, command registry, event bus
│ ├── GsDChat.Server/ # ASP.NET Core Kestrel, SQLite DB, WebSockets, File REST
│ ├── GsDChat.Client/ # Pure C# screen-swapping TUI client
│ └── GsDChat.Mods.SampleBot/ # Reference mod: /roll, /announce, emoji transformer, F5 screen
└── tests/
└── GsDChat.Tests/ # Comprehensive xUnit test suite (Unit, Integration, E2E)
```
---
## Quick Start
### 1. Build Solution
```bash
dotnet build GsDChat.slnx
```
### 2. Run All Tests
```bash
dotnet test GsDChat.slnx
```
### 3. Run the Server
```bash
dotnet run --project src/GsDChat.Server
```
By default, the server runs on `http://0.0.0.0:5000` with SQLite database in `./data/chat.db` and loads any mod DLLs in `./mods/`.
#### Server Configuration (`serversettings.json` / CLI flags)
You can edit `src/GsDChat.Server/serversettings.json` or pass command-line arguments:
```json
{
"Host": "0.0.0.0",
"Port": 5000,
"UseTls": false,
"CertificatePath": "",
"CertificatePassword": "",
"ServerWidePassword": "",
"RegistrationMode": "Open",
"DataDirectory": "data",
"MaxFileSizeMb": 50,
"ServerName": "GsDChat Community Server",
"Motd": "Welcome to GsDChat! Enjoy private and modular messaging."
}
```
CLI Overrides:
```bash
dotnet run --project src/GsDChat.Server -- --port 8080 --server-password "MySecretCommunityPassword"
```
---
## Docker & Cloudflare Tunnel Deployment
### Option A: Standard Docker Compose
1. Copy `.env.example` to `.env` (already done by default) and customize variables:
```bash
cp .env.example .env
```
2. Start the container:
```bash
docker compose up -d --build
```
Data (database + files) is persisted in `./data/` and mods in `./mods/`.
### Option B: Built-in Cloudflare Tunnel (`cloudflared`)
This deploys GsDChat in an isolated container with zero open host ports, securely accessible globally via Cloudflare Tunnel.
1. Create a tunnel in **Cloudflare Zero Trust** (`Networks -> Tunnels -> Add a Tunnel`).
2. Copy the tunnel token into `.env`:
```bash
CLOUDFLARE_TUNNEL_TOKEN=eyJh...
```
3. In Cloudflare Zero Trust, configure the Public Hostname:
- **Service**: `HTTP`
- **URL**: `gsdchat:5000`
4. Start both GsDChat and the cloudflared tunnel:
```bash
docker compose -f docker-compose.tunnel.yml up -d --build
```
---
### 4. Run the Client TUI
```bash
dotnet run --project src/GsDChat.Client
```
Or connect directly with credentials:
```bash
dotnet run --project src/GsDChat.Client -- --server http://localhost:5000 --username Alice --password MyPassword123!
```
---
## Client TUI Navigation
| Key | Action |
|---|---|
| **F1** | Switch to **Chat** view |
| **F2** | Switch to **Channels** view |
| **F3** | Switch to **Users** directory view |
| **F4** | Switch to **Files** transfer view |
| **F5** | Switch to **Server Status** (SampleBot mod view) |
| **Enter** (on F1) | Send message or run slash command |
| **PageUp / PageDown** | Scroll message viewport up and down |
| **Ctrl + C** | Exit client |
### Built-in & Mod Slash Commands
Type these directly in the chat input on **F1**:
- `/help` — List available commands.
- `/join <channel>` — Switch to a channel (e.g. `/join general`).
- `/create <name> [topic]` — Create a new channel (Admin/Owner).
- `/upload <local_path>` — Upload a file to the active channel.
- `/download <file_id>` — Download a shared file to `./downloads/`.
- `/roll [NdM]` *(SampleBot)* — Roll dice (e.g. `/roll 2d6`, `/roll 1d20`).
- `/announce <text>` *(SampleBot)* — Post an official broadcast announcement (Admin/Owner).
- Text emoji shortcuts: `:shrug:` converts to `¯\_(ツ)_/¯`, `:tableflip:` converts to `(╯°□°)╯︵ ┻━┻`, `:heart:` to `❤️`.
---
## Modding Guide
To create a custom mod:
1. Create a class library targeting `net10.0`.
2. Reference `GsDChat.Modding.csproj`.
3. Implement `IMod`:
```csharp
using GsDChat.Modding;
public class MyCustomMod : IMod
{
public string Id => "com.example.mycustommod";
public string Name => "My Custom Mod";
public string Version => "1.0.0";
public string Description => "Example custom mod for GsDChat";
public Task InitializeAsync(IModContext context)
{
// Register a custom slash command
context.Commands.RegisterCommand("ping", "Responds with pong", async cmdCtx =>
{
return CommandResult.Ok("🏓 Pong from custom mod!");
});
// Intercept messages
context.Pipeline.Use(async (msgCtx, next) =>
{
// Inspect or modify msgCtx.Message.Content
await next();
});
return Task.CompletedTask;
}
public Task ShutdownAsync() => Task.CompletedTask;
}
```
4. Drop the built `.dll` into the server's `./mods/` directory and restart the server.
---
## License
MIT License.