Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

qBitTorrent automatic update port #23

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions PIAWireguard.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"piaDipToken": "",
"piaPortForward": false,
"piaUseDip": false,
"tunnelGateway": null
}
"tunnelGateway": null,
"qbtUpdate": false,
"qbtURL": "",
"qbtUsername": "",
"qbtPassword": ""
}
81 changes: 80 additions & 1 deletion PIAWireguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"piaDipToken",
"piaPortForward",
"piaUseDip",
"tunnelGateway"
"tunnelGateway",
"qbtUpdate",
"qbtUsername",
"qbtPassword"
]

# Check config contains the right settings
Expand Down Expand Up @@ -212,6 +215,26 @@ def printDebug(text):
print("piaUseDip can only be true or false")
sys.exit(0)

if config['qbtUpdate'] == True and config['piaPortForward'] != True:
print("If you wish to use qbtUpdate, please also enable piaPortForward in the json file")
sys.exit(0)

if config['qbtUpdate'] != True and config['qbtUpdate'] != False:
print("qbtUpdate can only be true or false")
sys.exit(0)

if config['qbtUpdate'] == True and config['qbtURL'] == '':
print("If you wish to use qbtUpdate, please define qbtURL variable in the json file")
sys.exit(0)

if config['qbtUpdate'] == True and config['qbtUsername'] == '':
print("If you wish to use qbtUpdate, please define qbtUsername variable in the json file")
sys.exit(0)

if config['qbtUpdate'] == True and config['qbtPassword'] == '':
print("If you wish to use qbtUpdate, please define qbtPassword variable in the json file")
sys.exit(0)

opnsenseURL = config['opnsenseURL']

# List current wireguard instances looking for PIA one
Expand Down Expand Up @@ -853,6 +876,62 @@ def printDebug(text):
print("reconfigure request failed non 200 status code - apply alias changes")
sys.exit(2)

#
# qBitTorrent update section
# Port forwarding must also be enabled and working
#

# Exit if qbtUpdate isn't enabled otherwise continue
if config['qbtUpdate'] is False:
sys.exit(0)

# Login to qBitTorrent server
login = {
'username': config['qbtUsername'],
'password': config['qbtPassword']
}

referer = {
'Referer': config['qbtURL']
}

r = requests.post(f"{config['qbtURL']}/api/v2/auth/login", data= login, headers= referer)

# Check for errors.
if r.status_code != 200 and r.status_code != 403:
print("qbtUpdate login request failed on invalid status code, verify qbtURL")
sys.exit(2)

if r.status_code == 403:
print("qbtUpdate login request failed, too many unsuccessful login attempts")
sys.exit(2)

if r.content[slice(-1)] != b'Ok':
print("qbtUpdate login request failed, verify qbtUsername and qbtPassword")
sys.exit(2)

# Store authentication cookie data
cookie = {
'SID': r.cookies['SID']
}

# Check current qBitTorrent listen port
r = requests.get(f"{config['qbtURL']}/api/v2/app/preferences", cookies= cookie)
qbtPort = json.loads(r.content)['listen_port']
print(f"qbtUpdate current listen_port is {qbtPort}")
if qbtPort == wireguardSignature['port']:
print("qbtUpdate listen_port change not required")
sys.exit(0)

# Set new qBitTorrent listen port
newPort = {
'listen_port': wireguardSignature['port']
}

r = requests.post(f"{config['qbtURL']}/api/v2/app/setPreferences", cookies= cookie, data= {"json": json.dumps(newPort)}, headers= referer)
if r.status_code == 200:
print(f"qbtUpdate listen_port changed successfully to {wireguardSignature['port']}")

#
# Script finished
#
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ To accommodate this functionality, this is built in to the script. You will need

You'll find your gateway names in "System: Gateways: Single", making sure its the IPv4 one.

***qBitTorrent automatic port update***

This feature can log into any qBitTorrent client that has the webUI enabled and is accessible from OPNsense and change the listen port to the one set by the `piaPortForward` feature. To use qBitTorrent port updating change `qbtUpdate` variable in the json file from `false` to `true`. You must also set the values for `qbtURL` (example: `"http://192.168.1.7:8080"`), `qbtUsername`, and `qbtPassword`.
**Note:** `piaPortForward` must also be set to `true`.

***WireGuard kernel module***

Since OPNsense 21.1.4, they now support the install the new kernel module implementation of WireGuard, its early days for the module and should be considered experimental.
Expand Down