Skip to content
teeworldsPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

teeworlds.com

The website and forum of Teeworlds, a free online multiplayer game. This is a rewrite of the old PHP site and PunBB 1.4 forum with Next.js 16 (App Router, server-rendered), PostgreSQL and Drizzle ORM.

What's in here

  • Website: front page, news, blog (developer journals), downloads, docs and contact.
  • Forum: categories and forums with permissions per user group, BBCode posts with preview and quoting, moderation (stick, close, hide, move, delete), profiles with avatars and signatures, the user list, and full-text search.
  • Admin panel at /admin: write news, blog posts, releases and docs, and manage users, groups, forums and settings.
  • Accounts: sign in with a username and password. Sessions are stored in the database and sent as an httpOnly cookie.
  • Responsive design: on desktop it keeps the look of the original site. Below 760 px it switches to a mobile layout with a bottom tab bar.

One design choice runs through everything: website content is forum content, as it was on the old site. A news item or blog entry is a forum topic: its first post is the article and the replies are its comments. Each release on the downloads page is a topic in the downloads forum, with the download links before a [---] line and the release notes after it. Which forums feed news, blog and downloads is a setting.

Getting started

You need Node.js 20.9 or newer (the container uses Node 26), pnpm 10 (corepack enable gives you the pinned version), and Docker for the development database.

pnpm install
pnpm start:dev-db            # PostgreSQL 18 in Docker on 127.0.0.1:55433
cp .env.example .env.local   # then set APP_SECRET (openssl rand -hex 32)
pnpm db:migrate              # schema, default groups and forums, docs pages
pnpm db:seed                 # optional sample content and two accounts
pnpm dev                     # http://localhost:3000

The seed creates the accounts admin (administrator) and player (member), both with the password teeworlds-dev.

The server applies migrations and the bootstrap (src/lib/bootstrap.ts) itself at startup, so pnpm db:migrate is only needed before scripts such as the seed.

Checks

pnpm test        # unit tests (vitest)
pnpm typecheck
pnpm lint
pnpm build       # production build; doesn't need a database

Project layout

src/
  app/
    (site)/           front page, news, blog, downloads, docs, contact
    (feeds)/          RSS feeds
    forum/            the forum, plus login, registration and profiles
    admin/            the admin panel
    img/              proxy for external images in posts (see below)
    livez, readyz     health endpoints
    sitemap.ts, robots.ts
  components/         React components (site/, forum/, admin/)
  content/pages.json  initial docs pages (Markdown)
  db/                 Drizzle schema, client and migration runner
  lib/                auth, permissions, BBCode, forum and content queries
  proxy.ts            permanent redirects from old URLs
drizzle/              generated SQL migrations (committed)
scripts/              import, seed, maintenance (see below)

How things work

Rendering and caching

cacheComponents is enabled. Each page is a static shell (the frame and menus) with request-time content streamed into it through <Suspense>. Any component that reads the database, cookies or route params is an async component inside a <Suspense> boundary, and it starts with await connection(). Keep it that way: next build must never need a database.

This version of Next.js differs from older ones in several places. node_modules/next/dist/docs/ holds the documentation for the installed version (see AGENTS.md).

Database and migrations

The schema is in src/db/schema.ts. After changing it, run pnpm db:generate and commit the new SQL file in drizzle/. Migrations run automatically when the server starts. For data changes, write a custom migration with pnpm exec drizzle-kit generate --custom --name <name>.

BBCode

Posts are stored as BBCode, as in PunBB, and rendered by src/lib/bbcode.ts. The renderer is a tokenizer and stack parser: it escapes all text and attributes, shows unclosed or wrongly nested tags as plain text, and always produces well-formed HTML. If you add a tag, add tests next to it (bbcode.test.ts).

Images in posts and the CSP

The site is meant to run with a strict Content-Security-Policy (img-src 'self'), so external images in posts are served through /img/<signature>/<url>:

  • The server signs these URLs with APP_SECRET, so the proxy can't be used as an open proxy.
  • It refuses private and loopback addresses, which prevents SSRF.
  • It accepts raster images only.

The trailer on the front page is a click-to-play youtube-nocookie.com embed, so the CSP needs frame-src 'self' https://www.youtube-nocookie.com.

Accounts and passwords

New passwords are hashed with argon2id (src/lib/password.ts). Accounts imported from PunBB keep working with their old passwords:

  • The importer stores their SHA-1 based hashes wrapped in argon2id, so the database never holds a fast hash.
  • At the next sign-in, the stored hash is replaced by a plain argon2id one.

Password resets by email don't exist yet.

Sign-in and registration are throttled (src/lib/throttle-policy.ts):

  • Per username and IP: a delay that grows after 3 failed sign-ins (2 s, 4 s, … up to 15 minutes).
  • Per IP: at most 30 failures in 15 minutes.
  • Per username, from any IP: after 20 failures in an hour, attempts must be 10 s apart. This is deliberately mild, so nobody can lock someone else out.
  • Registration, per IP: at most 20 attempts an hour, and 3 new accounts an hour or 10 a day.

The client IP comes from the X-Real-IP header, which the reverse proxy must always set. Without a proxy, as in development, every request counts as local.

New usernames are NFKC-normalized. Invisible characters and mixed alphabets are refused, and so are names that look like an existing one (accents, Cyrillic or Greek look-alike letters, case; see src/lib/username.ts).

Permissions

Users belong to a group. Each group has flags such as administrator, moderator, "manage content" (may use the content parts of the admin panel), and default posting rights. Individual forums can override read, reply and new-topic rights per group (src/lib/perms.ts).

URLs

Forum URLs use slugs: /forum/f/support, /forum/t/security-update-0-7-5-released.

  • Slugs are unique. A clash gets a short random suffix (help-k3x9), and a slug is never only digits.
  • A slug doesn't change when the title is edited.
  • Numeric URLs (/forum/t/14785) and all of the old site's URL schemes (/?page=news&id=…, /forum/viewtopic.php?id=…) answer with one permanent redirect to the current URL (src/proxy.ts and /forum/resolve/…).

Configuration

Environment variables:

Variable
DATABASE_URL PostgreSQL connection string (required)
APP_SECRET at least 32 random characters; signs image-proxy URLs (required)
SITE_URL public base URL for canonical links, the sitemap and RSS (default https://www.teeworlds.com)
DISPLAY_TIMEZONE time zone dates are shown in (default Europe/Berlin, as on the old forum)
BOOTSTRAP_ADMIN_USERNAME, BOOTSTRAP_ADMIN_PASSWORD create a first administrator when the database has no users
LOG_LEVEL, LOG_FORMAT debug/info/warn/error; json (default in production) or pretty
SKIP_MIGRATIONS=1 don't migrate at startup

Other settings live in the database and are edited at /admin/settings: whether registration is open, the forum announcement, page sizes, and which forums feed news, blog and downloads.

Running in production

Dockerfile builds a small image (standalone output, non-root user, no package manager) that listens on port 3000 and logs JSON lines to stdout. It needs a PostgreSQL database (DATABASE_URL) and APP_SECRET. Put a reverse proxy in front for TLS and security headers (HSTS, CSP).

  • GET /livez answers as long as the process is alive.
  • GET /readyz also checks the database and answers 503 while it's unreachable.
  • The proxy must set X-Real-IP to the client's address, replacing any value the client sent. Throttling and logging rely on it. It's meant for healthchecks; there's no reason to expose either endpoint publicly.
  • All data, including avatars, is in PostgreSQL. Back it up with pg_dump.

Importing the old forum

These scripts migrate the old PunBB 1.4 database. They're only useful with access to that database dump, which isn't public.

MYSQL_URL=mysql://user:pass@host:3306/teeworlds pnpm import:punbb [--replace]
pnpm prepare:staff     # make sure the news and blog authors have accounts
pnpm check:render      # render every post once, to catch problems
  • The import keeps all ids, so old links still work, and it recomputes every counter.
  • Imported password hashes are wrapped in argon2id at the end. That takes a few minutes for 32k users. pnpm passwords:wrap does the same for an existing database.
  • For a shareable copy, use --demo. It leaves out emails, IPs, password hashes, forums that guests can't read, moderator notes, ban messages and last-visit times. The flags --no-emails, --no-ips, --no-passwords and --public-only do parts of that individually.
  • pnpm prepare:staff --set-passwords FILE gives authors without a password a random one and writes the list to FILE.
  • Avatars were files on the old server, not in the database.
    • pnpm avatars:fetch recovers what the Internet Archive has, into the gitignored .data/avatars.
    • pnpm avatars:fetch --load stores the cached files in the database.
    • --cache DIR points at another folder, for example the original files named <id>.png|jpg|gif.
    • --sql FILE writes UPDATE statements instead of touching a database.
  • Not imported yet: private messages, polls, karma, attachments, reports, subscriptions, posts waiting for approval, and IP and email bans.

Contributing

Issues and pull requests are welcome. Before opening a pull request, run pnpm test, pnpm typecheck and pnpm lint, and check your change in a narrow window as well as a wide one. Please keep the desktop design close to the original site.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages