forked from auth-xyz/PortFoward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrent.sh
66 lines (53 loc) · 1.97 KB
/
torrent.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env bash
# qBittorrent Web UI API settings
QBITTORRENT_HOST="localhost"
QBITTORRENT_PORT="8080"
QBITTORRENT_API_URL="http://$QBITTORRENT_HOST:$QBITTORRENT_PORT/api/v2"
# Function to set qBittorrent listen port via Web UI API
set_qbittorrent_port() {
local port="$1"
local payload="json={\"listen_port\": $port}"
# Send the request to update the listen port
curl -s -X POST -d "$payload" "$QBITTORRENT_API_URL/app/setPreferences"
if [[ $? -eq 0 ]]; then
echo "Successfully updated qBittorrent listen port to $port."
else
echo "ERROR: Failed to update qBittorrent listen port."
fi
}
previous_mapped_port=""
while true; do
date
# Run natpmpc for UDP and TCP port mapping
udp_mapping_info=$(natpmpc -a 1 0 udp 60 -g 10.2.0.1 2>&1)
tcp_mapping_info=$(natpmpc -a 1 0 tcp 60 -g 10.2.0.1 2>&1)
# Check if natpmpc succeeded
if [[ $? -ne 0 ]]; then
echo "ERROR: natpmpc command failed."
echo "UDP Output: $udp_mapping_info"
echo "TCP Output: $tcp_mapping_info"
sleep 45
continue
fi
# Extract the mapped port (use TCP output as it's more reliable)
mapped_port=$(echo "$tcp_mapping_info" | grep -oP 'Mapped\ public\ port\ \K[0-9]+')
if [[ -z "$mapped_port" ]]; then
echo "ERROR: Failed to extract Mapped public port from natpmpc output."
echo "UDP Output: $udp_mapping_info"
echo "TCP Output: $tcp_mapping_info"
sleep 45
continue
fi
echo "Mapped public port: $mapped_port"
# Check if the mapped port has changed
if [[ "$mapped_port" != "$previous_mapped_port" ]]; then
echo "Changing qBittorrent listen port to $mapped_port"
# Update qBittorrent listen port via Web UI API
set_qbittorrent_port "$mapped_port"
# Update the previous mapped port
previous_mapped_port="$mapped_port"
else
echo "Mapped port not changed. Skipping qBittorrent port update."
fi
sleep 45
done