中文 | English
A Python tool that dynamically updates qBittorrent's announce IP and port in real-time. This tool is designed to work with NAT traversal solutions like Natter to automatically update the public address that qBittorrent reports to trackers.
- Real-time port and IP address updates for qBittorrent
- Seamless integration with qBittorrent Web API
- Automatic torrent re-announcement after address changes
- Configurable through TOML configuration file
- Comprehensive logging with rotation
- Error handling and graceful shutdown
- Python 3.12+
- qBittorrent with Web UI enabled
- Dependencies:
httpx
,loguru
- Clone the repository:
git clone https://github.com/Illustar0/BTPortRedirector
cd BTPortRedirector
- Install dependencies:
pip install -r requirements.txt
Or using uv:
uv sync
- Create or modify
src/config.toml
:
[Qbittorrent]
api_endpoint = "http://127.0.0.1:8080"
username = "your_username"
password = "your_password"
- Enable qBittorrent Web UI:
- Open qBittorrent
- Go to Tools → Options → Web UI
- Check "Web User Interface (Remote control)"
- Set username and password
- Note the port (default: 8080)
The script is designed to be called by NAT traversal tools or scripts with the following arguments:
python src/agent.py <protocol> <private_ip> <private_port> <public_ip> <public_port>
python src/agent.py tcp 192.168.1.100 45000 203.0.113.1 45000
Add this to your Natter configuration to automatically update qBittorrent when the port mapping changes:
python3 natter.py -m nftables -U -p <Qbittorrent Port> -e /path/to/BTPortRedirector/src/agent.py
- The script receives public IP and port information as command line arguments
- Loads qBittorrent configuration from
config.toml
- Authenticates with qBittorrent Web API
- Updates the announce IP and port settings
- Forces re-announcement of all torrents
- Logs the operation results
The main class that handles qBittorrent API interactions:
login(username, password)
- Authenticate with qBittorrentget_preferences()
- Retrieve current preferencesset_preferences(preferences)
- Update multiple preferencesreannounce(hashes)
- Force torrent re-announcementstop(hashes)
/start(hashes)
- Control torrent statelogout()
- End the session
Logs are automatically saved to agent.log
with:
- 10 MB rotation size
- 10 log file retention
- Color-coded console output
The script handles various error conditions:
- Missing or invalid configuration files
- qBittorrent connection failures
- Authentication errors
- API request failures
This project is open source. See LICENSE file for details.
Contributions are welcome! Please feel free to submit issues and pull requests.