Nix configurations for my homelab
1#!/bin/sh
2
3QBITTORRENT_URL="http://localhost:8082"
4
5while true
6do
7 tcp_output=$(natpmpc -g 10.2.0.1 -a 0 1 tcp 60 | grep Mapped) || {
8 printf '%s\n' "Error: failed to get TCP port" >&2
9 exit 1
10 }
11
12 udp_output=$(natpmpc -g 10.2.0.1 -a 0 1 udp 60 | grep Mapped) || {
13 printf '%s\n' "Error: failed to get UDP port" >&2
14 exit 1
15 }
16
17 read -r _ _ _ tcp_port _ <<-EOF
18 $tcp_output
19 EOF
20
21 read -r _ _ _ udp_port _ <<-EOF
22 $udp_output
23 EOF
24
25 # NOTE: The port numbers should be the same i'm pretty sure but this is a little sanity check
26 [ "$tcp_port" -eq "$udp_port" ] || {
27 printf '%s\n' "Warning: TCP and UDP ports aren't the same" >&2
28 }
29
30 current_port=$(curl -sf ${QBITTORRENT_URL}/api/v2/app/preferences | jq .listen_port)
31
32 [ "$current_port" -eq "$udp_port" ] || {
33 printf '%s\n' "Port changed from $current_port -> $udp_port"
34 curl -sfd "json={\"listen_port\":$udp_port}" "${QBITTORRENT_URL}/api/v2/app/setPreferences"
35 }
36
37 printf '%s\n' "Current Port: $udp_port"
38
39 sleep 30
40done