| block_tor_ips.py | fix: use /128 for IPv6 instead of /32 | |
| LICENSE | first commit | |
| README.md | Updated | |
| requirements.txt | first commit | |
Block Tor Exit Nodes & Mullvad VPN for Mastodon
A Python script that automatically downloads and blocks Tor exit nodes and Mullvad VPN IP ranges in your Mastodon server via the admin API, avoiding sign ups from them.
Features
- Automatically blocks Tor exit nodes from multiple trusted sources
- Blocks known Mullvad VPN IP ranges (ASN AS39351)
- Configurable severity levels (
sign_up_block,sign_up_requires_approval,no_access) - Dry-run mode to test without making changes
- Respects Mastodon API rate limits automatically
- Handles pagination for existing blocks
- Detailed logging with Loguru
- Asynchronous operations for better performance
- Automatic retry with exponential backoff
Prerequisites
- Python 3.7+
- Mastodon instance with admin access
- Admin API token
Installation
- Clone the repository or download the script:
git clone <repository-url>
cd <repository-directory>
- Create the Python Virtual Environment:
python -m venv .
- Activate it:
source bin/activate
- Install the required dependencies:
pip install -r requirements.txt
Configuration
Environment Variables Create a .env file in the script directory and fill it with your data:
MASTODON_URL=https://your-mastodon-instance.com
MASTODON_TOKEN=your_admin_api_token_here
Getting a Mastodon Admin Token
- Log into your Mastodon instance as an admin user
- Go to Preferences → Development
- Click "New Application"
- Fill in the application name (e.g., "IP Blocker")
- Grant the following scopes:
admin:read (to view existing blocks) admin:write (to create new blocks)
- Click "Submit"
- Copy the access token shown
Configuration Variables in Script You can modify these variables directly in the script:
# Block severity level
SEVERITY = "sign_up_block" # Options: "sign_up_block", "sign_up_requires_approval", "no_access"
# Custom comment for blocks
COMMENT = "Tor exit node / Mullvad VPN - bloc automàtic"
# Dry run mode (can also use --dry-run flag)
DRY_RUN = "--dry-run" in sys.argv
Severity Levels Explained
sign_up_block: Completely blocks sign-ups from these IPs sign_up_requires_approval: Allows sign-ups but requires admin approval no_access: Blocks all access (including browsing)
Usage
Basic Execution
python3 block_tor_ips.py
Dry Run (Test Mode)
python3 block_tor_ips.py --dry-run
This will simulate the blocking process without actually creating any blocks, showing you what would be blocked.
Setting Up as a Cron Job (Automated) To run the script automatically every day:
crontab -e
Add the following line to run daily at 2 AM:
0 2 * * * cd /path/to/script && /usr/bin/python3 block_tor_ips.py >> /var/log/tor_blocker.log 2>&1