Sw1tch is a FastAPI-based web application designed to enhance the conduwuit Matrix homeserver by addressing key shortcomings for public deployments. It manages account registration with email-based token requests and provides an admin API by relaying HTTP requests to a Matrix admin room, parsing responses for automation. Currently in use for the We2.ee homeserver at join.we2.ee.
This project is specifically built around conduwuit, an excellent Matrix homeserver that lacks native SMTP authentication and a robust admin API—issues sw1tch resolves elegantly.
- Daily rotating registration tokens, emailed upon request
- Email-based registration requiring a valid address
- Rate limiting per email address
- IP, email, and regex-based username banning
- Automatic downtime before token rotation
- Admin API via Matrix room message relaying
- Warrant canary generation and posting (work in progress)
- Gruvbox-themed, responsive UI
-
Clone the Repository:
git clone https://sij.ai/sij/hand_of_morpheus cd hand_of_morpheus -
Install Dependencies:
pip install fastapi uvicorn jinja2 httpx pyyaml python-multipart nio requests feedparser urllib3 smtplib
-
Set Up Configuration:
cp -r ./sw1tch/example-config ./sw1tch/config nano sw1tch/config/config.yaml
config.yaml: Fill in credentials and options for registration, Matrix admin, canary, and SMTP.conduwuit.env: Addconduwuitsettings (see Conduwuit Config Examples).banned_emails.txt: Prefilled with disposable email providers linked to spam/abuse.banned_usernames.txt: Prefilled with regex patterns targeting CSAM-related usernames.banned_ips.txt: Blank; add IPs to block token requests.attestations.txt: Generic statements for the warrant canary; customize as needed.
-
Add Static Assets:
# Add your logo and favicon to the static directory cp your-logo.png sw1tch/static/logo.png cp your-favicon.ico sw1tch/static/favicon.ico -
Generate Initial Registration Token:
openssl rand -hex 16 > sw1tch/data/.registration_token -
Configure
launch.sh:launch.shmanages token rotation,conduwuitcontainer updates, and ensures thesw1tchservice runs:- Updates the
conduwuitDocker image from a Nix-built repository. - Refreshes the registration token and restarts the container.
- Starts or restarts the
sw1tchFastAPI service.
- Updates the
nano launch.sh # Adjust paths (e.g., BASE_PATH, REPO_PATH) for your environment chmod +x launch.sh -
Set Up Cron Jobs:
crontab -e
Add:
# Daily token refresh and container restart at midnight UTC 0 0 * * * cd /home/sij/hand_of_morpheus && ./launch.sh --refresh-token > /home/sij/hand_of_morpheus/logs/token_refresh.log 2>&1 # Weekly conduwuit update (Sundays at 2 AM UTC) 0 2 * * 0 cd /home/sij/hand_of_morpheus && ./launch.sh --update --force-restart > /home/sij/hand_of_morpheus/logs/update.log 2>&1 # Ensure service runs after reboot @reboot cd /home/sij/hand_of_morpheus && ./launch.sh > /home/sij/hand_of_morpheus/logs/reboot.log 2>&1
Run manually:
./launch.sh # --refresh-token, --super-admin, --update, and/or --force-restart-
--refresh-token:- Purpose: Generates a new, random 6-character hexadecimal registration token and writes it to
sw1tch/data/.registration_token. - Behavior: Overwrites the existing token, logs the new value, and exits on failure (e.g., if the file isn’t writable).
- When to Use:
- Daily via cron (e.g., at midnight UTC) to rotate tokens as a security measure.
- Manually if you suspect the current token has been compromised.
- Example:
./launch.sh --refresh-token
- Purpose: Generates a new, random 6-character hexadecimal registration token and writes it to
-
--super-admin:- Purpose: Generates a random 16-character emergency password for the
@conduituser inconduwuitand passes it to the container viaCONDUWUIT_EMERGENCY_PASSWORD. - Behavior: Logs the username (
@conduit:we2.ee) and password, which you can use to log in and regain admin access. - When to Use:
- During initial setup to establish admin access.
- If you lose access to the admin account and need to recover it.
- Example:
./launch.sh --super-admin
- Purpose: Generates a random 16-character emergency password for the
-
--update:- Purpose: Updates the
conduwuitDocker image by pulling the latest source fromREPO_PATH, building it with Nix, and tagging it asconduwuit:custom. - Behavior: Requires Git and Nix; exits on failure (e.g., if the build fails or no image is produced).
- When to Use:
- Weekly via cron to keep
conduwuitup-to-date with the latest features or fixes. - Manually when you want to apply a specific update.
- Weekly via cron to keep
- Example:
./launch.sh --update
- Purpose: Updates the
-
--force-restart:- Purpose: Forces the
sw1tchregistration service to restart by killing any process on the configured port (fromconfig.yaml) and starting a new instance. - Behavior: Removes the PID file, starts
python3 -m sw1tchdetached, and verifies it’s running; logs errors if it fails to start. - When to Use:
- After updating
sw1tchcode or configuration to ensure changes take effect. - If the service is unresponsive or stuck.
- Combined with
--updateto refresh everything.
- After updating
- Example:
./launch.sh --force-restart
- Purpose: Forces the
- Combination: Flags can be combined (e.g.,
./launch.sh --update --force-restart) for comprehensive updates. - Default Behavior: Without flags, the script restarts the
conduwuitcontainer and ensuressw1tchis running (no forced restart). - Cron Integration: The comments align with your crontab (daily
--refresh-token, weekly--update --force-restart, reboot startup).
- IP Banning: Add IPs to
sw1tch/config/banned_ips.txt. - Email Banning: Add emails to
sw1tch/config/banned_emails.txt. - Username Patterns: Add regex to
sw1tch/config/banned_usernames.txt. - Registration Tracking: Logged to
sw1tch/data/registrations.json. - Admin API: Relays HTTP requests to
#adminsroom, parsing responses.
- Use a reverse proxy (e.g., Nginx) with HTTPS.
- Move
.registration_tokenoutside the web root if exposed. - Backup
sw1tch/data/registrations.jsonregularly. - Monitor
sw1tch/logs/registration.logfor abuse.
The warrant canary feature (in progress) generates signed statements posted to a Matrix room, using data from RSS feeds and Bitcoin blocks for freshness. Configure in config.yaml under canary. Current limitations include UI polish and broader testing.
Sw1tch resolves two conduwuit shortcomings:
- Email-Based Registration: Requires a valid email for token requests, enhancing security for public homeservers.
- Admin API: Bridges HTTP requests to Matrix room messages, enabling automation by parsing
@conduitresponses.
Review launch.sh for conduwuit container management settings.