A high-performance, real-time 4chan clone built from scratch using Rust (2024 Edition), Axum, Tokio, HTTP/2, Server-Sent Events (SSE), PostgreSQL (LISTEN / NOTIFY), and Vanilla JavaScript (strictly following Douglas Crockford's coding standards).
-
HTTP/2 & Real-Time SSE Bus: Multiplexed Server-Sent Events distributed horizontally across instances using PostgreSQL
LISTEN / NOTIFYwith compact JSON descriptors and database hydration fallback. -
Stream Backpressure & Lag Resilience: Resilient broadcast channel handling catching Tokio stream lag to emit
sync-requiredevents, preventing silent socket disconnects on slow connections. -
Declarative AEAD Cookie Sessions: Cryptographically sealed client sessions utilizing
axum-extra'sPrivateCookieJarwith 512-bit SHA-512 master key expansion and zero runtime database lookup overhead. -
Finite Board Capacity & Auto-Pruning: Strictly enforced board limits (100 threads per board) with automated asynchronous cascade deletion of orphaned media files (
$O(1)$ storage ceiling). -
Chronological Bumping & Bump Limit: Real-time thread bumping with
bumped_attimestamps, chronological reply ordering via PostgreSQLjsonb_agg,sagebypass, and an automated 300-reply bump limit. -
Classic & Secure Tripcode Engine: Native parser for traditional (
#password) and salted secure (##password) tripcodes with dedicated styling. -
Pluggable Storage Abstraction: Abstracted
StorageBackendtrait supporting local disk storage and cloud object stores (S3, MinIO, GCS) with non-blocking concurrent file writes and atomic thumbnail rollbacks. -
High-Fidelity Fast Thumbnails: High-performance integer downsampling using the
imagecrate with strict dimension bomb defenses (10000x10000px validation), magic-byte format verification, and EXIF metadata stripping. - Zero-I/O Template Engine: In-memory pre-cached MiniJinja template rendering compiled directly into RAM on startup for sub-millisecond page and item assembly.
-
Transactional Versioned Migrations: Robust, run-once schema migrations (
schema_migrationstracking table) executing within atomic transactions, eliminating boot-timeUPDATEbackfill bottlenecks. -
IPv6 Subnet-Aware Rate Limiting: Token-bucket IP rate limiter (2 req/sec, burst 10) with
/64subnet masking to prevent rate-limit evasion through IPv6 address rotation. -
Double-Submit CSRF Protection: Timing-attack resistant CSRF middleware utilizing
subtle::ConstantTimeEq, non-HttpOnly cookie distribution, HTML meta injection, and client-sideX-CSRF-Tokenvalidation. -
Pure Crockfordian JavaScript: Modular client-side SPA runtime written with zero usage of
this,class,var,new(in application code), orvoidoperators. -
Componentized Frontend Architecture: Decomposed into dedicated ES modules (
post-renderer,tag-hover,reply-box,post-actions,post-form) with explicit lifecycle teardowns to prevent memory leaks. - In-Place Image Expansion: Clickable thumbnail expansion within the feed and thread views, with filename links directly opening raw full-resolution uploads in a new tab.
-
HTML5 History API Routing: Clean URLs (
/g,/g/thread/a1b2c3d4e, static views) with deep-linking support and History API client navigation. -
Graceful Shutdown & Draining: Integrated
SIGINT/SIGTERMsignal listening with active TCP connection draining for both cleartext and ALPN TLS HTTP/2 servers. -
OWASP Hardened: Includes strict Content Security Policy (CSP), Slowloris protection, XSS sanitization, timing-attack resistant password verification (
bcrypt), and defensive security headers (nosniff,DENY,mode=block).
β οΈ Note on User Accounts / Authentication:
The login and registration system (/auth,src/routes/auth.rs) is included strictly as a functional demonstration of the session management, private cookie encryption, and bcrypt capabilities. True to traditional imageboard culture, all board browsing, thread creation, and replying remain completely open, anonymous, and account-free by default.
- Backend: Rust (2024 Edition)
- Web Framework: Axum 0.8 / Axum-Extra 0.12 / Tower / Hyper 1.0
- Async Runtime: Tokio
- Database Driver: SQLx (PostgreSQL 16+)
- Template Engine: MiniJinja (In-Memory Pre-cached)
- Image Processing:
imagecrate (Fast integer downsampling & magic-byte sniffing) - Frontend: Vanilla JavaScript (ES6 Modules, Crockfordian), HTML5, CSS3
- Protocol: HTTP/2 over TLS (ALPN
h2) / Cleartext HTTP / Server-Sent Events (SSE) - Sessions: AES-256-GCM Encrypted
PrivateCookieJar(axum-extra)
- Rust
1.85+(Cargo) - PostgreSQL
16+(or Docker) - OpenSSL (optional, for local HTTP/2 TLS certificates)
git clone https://github.com/joncody/moarchan.git
cd moarchanLog into your local PostgreSQL CLI and create the database:
CREATE DATABASE moarchan;Create a .env file in the root project directory:
PORT=9001
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=moarchan
POSTGRES_SSLMODE=disable
SESSION_HASH_KEY=12345678901234567890123456789012
SESSION_BLOCK_KEY=abcdefghijklmnopqrstuvwx12345678
UPLOAD_PATH=./static/images/uploads
UPLOAD_URL_PREFIX=/static/images/uploads
VIEWS_PATH=./static/views(Optional: For local ALPN HTTP/2 over TLS, generate self-signed certificates:)
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes -subj "/CN=localhost"cargo run --releaseNavigate to http://localhost:9001 (or https://localhost:9001 if TLS certs are present) in your browser.
If you prefer running the application and database together in containerized environments:
docker-compose up --build| Environment Variable | Default Value | Description |
|---|---|---|
PORT |
9001 |
Server HTTP port |
POSTGRES_HOST |
localhost |
PostgreSQL host address |
POSTGRES_PORT |
5432 |
PostgreSQL port |
POSTGRES_USER |
postgres |
PostgreSQL username |
POSTGRES_PASSWORD |
postgres |
PostgreSQL password |
POSTGRES_DB |
moarchan |
Database name |
POSTGRES_SSLMODE |
disable |
SSL mode (disable, require, verify-full) |
SESSION_HASH_KEY |
(32 bytes) | Secret key for deriving session master key |
SESSION_BLOCK_KEY |
(32 bytes) | Secret key for deriving session master key |
UPLOAD_PATH |
./static/images/uploads |
Local filesystem base path for media storage |
UPLOAD_URL_PREFIX |
/static/images/uploads |
Public URL prefix for uploaded media assets |
VIEWS_PATH |
./static/views |
Directory path containing HTML templates |
TLS_CERT_PATH |
(optional) | Path to PEM-encoded TLS certificate file |
TLS_KEY_PATH |
(optional) | Path to PEM-encoded TLS private key file |
.
βββ Cargo.toml # Project dependencies & build manifest
βββ docker-compose.yml # Container orchestration setup
βββ .env # Local environment configuration (git-ignored)
βββ src/
β βββ main.rs # Application bootstrap, graceful shutdown & server launch
β βββ config.rs # Environment variable configuration loader
β βββ state.rs # Thread-safe global AppState & cookie key container
β βββ error.rs # Unified error handling & HTTP response conversion
β βββ db/
β β βββ mod.rs # DB module entrypoint
β β βββ migrations.rs # Versioned transactional schema migrations
β β βββ queries.rs # Domain SQL queries & aggregate builders
β βββ middleware/
β β βββ mod.rs # Middleware module entrypoint
β β βββ csrf.rs # Double-submit cookie CSRF middleware
β β βββ rate_limit.rs # IPv6 /64 subnet-aware token-bucket rate limiter
β β βββ security.rs # Content Security Policy (CSP) & defensive headers
β βββ models/
β β βββ mod.rs # Models module entrypoint
β β βββ auth.rs # User authentication & session models
β β βββ post.rs # Thread, Reply & File models
β β βββ sse.rs # Event envelope models
β βββ routes/
β β βββ mod.rs # Master Axum router builder
β β βββ auth.rs # Login, registration & session handlers (PrivateCookieJar)
β β βββ pages.rs # HTML base shell & SPA dynamic render handlers
β β βββ api/
β β βββ mod.rs # API subrouter
β β βββ threads.rs# Thread creation & auto-pruning endpoint
β β βββ replies.rs# Reply creation & bump limit endpoint
β β βββ delete.rs # Post/file deletion endpoint
β β βββ stream.rs # Real-time SSE stream & lag recovery endpoint
β βββ services/
β β βββ mod.rs # Services module entrypoint
β β βββ auth.rs # Bcrypt password verification & hashing
β β βββ image.rs # Thumbnailing, magic-byte checking & EXIF stripping
β β βββ sanitizer.rs # HTML escaping, tripcode engine & quote parsing
β β βββ sse.rs # Distributed Postgres LISTEN/NOTIFY SSE hub
β βββ storage/
β βββ mod.rs # Pluggable StorageBackend trait
β βββ local.rs # Concurrent local filesystem storage implementation
βββ static/
βββ css/ # Reset, post, thread, reply & screen stylesheet rules
βββ images/ # Application graphics & upload directory
β βββ uploads/ # Image uploads (git-ignored)
βββ js/
β βββ frame.js # Crockfordian SPA runtime (History API + SSE)
β βββ dom.js # Lightweight DOM manipulation library
β βββ components/ # Modular UI components
β β βββ topics-map.js # Board slugs & descriptions map
β β βββ post-renderer.js # JSON-to-DOM HTML builder
β β βββ tag-hover.js # Quote preview tooltips & jump links
β β βββ reply-box.js # Draggable Quick Reply modal
β β βββ post-actions.js # Collapse, hide, & in-place image expansion
β β βββ post-form.js # Form submissions & validation
β βββ controllers/
β βββ auth.js # Auth controller (Demo)
β βββ main.js # Homepage controller
β βββ service.js# Imageboard thread/reply orchestrator
βββ views/ # MiniJinja HTML templates
Distributed under the MIT License. See LICENSE for more information.