Page 1 of 1

Changing port forward through CLI

Posted: Thu Aug 25, 2016 10:01 am
by alleyoopster
Hi,

I am setting up a Raspberry Pi with Raspbian and using PIA VPN with openvpn. I've been using PIA for a while and the port that is used changes frequently, so  I need to update my torrent client to reflect this. Is there a way to start qbittorrent with a port assignment and then periodically update this while the torrent is running.

This was fairly easy in deluge, I just used a script to check the port open on PIA used the deluge command to tell deluge of the changes.

Second question, how do I restrict to TUN0 - there is not an advanced tab in the webgui / nox config.

thanks for qbittorrent  ;D and thanks for any suggestions  8)

Dan

Re: Changing port forward through CLI

Posted: Mon Aug 29, 2016 11:30 pm
by mhertz
For your second question, you manually add the 'Connection\Interface=' setting in your qBittorent.conf file.

For your first question, then please post the script you used for doing this with deluge, so I/we know exactly what you're after, and I will change it to qB-nox if it's possible. It's easy to change the retrieved port in the qB-nox conf, but I guess you need to restart qB-nox for it to pickup the change, and I would guess the same with deluge.

Re: Changing port forward through CLI

Posted: Tue Sep 20, 2016 5:43 am
by alleyoopster
Hi,

So sorry for the gap in replying, I got caught up in house move.

Thanks for the info on connection.

What I would like to be able to do is pass the port number to qbittorrent through a script when it changes. I can use the scripts below to pull the port number from pia and assign it a variable, but I just need to be able to pass that value to qbittorrent.

For example deluge uses deluge-console command config -s listen_ports

I have moved away from the script now and just use a plugin that checks the open port periodically and changes it in deluge to reflect any updates.

The last page of a long thread on pia port forward
https://www.privateinternetaccess.com/forum/discussion/180/port-forwarding-without-the-application-advanced-users/p14

Script for deluge
https://gist.github.com/JustinGrote/9983ad3def74e6e0c5cd9bc91ebdd7b4

Plugin for deluge (no need for script, just enter pia user and pass and it does it all)
https://github.com/SillyGoat/PIAPortForward/releases

thanks

Re: Changing port forward through CLI

Posted: Fri Jan 06, 2017 5:42 pm
by jleiss
I'm assuming you have found what you needed otherwise you would have followed up by now, but I thought I would add what I got working here in case anyone else needed it. This is what I use on Ubuntu 16.04

Code: Select all

#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin

# Private Internet Access Advanced Port Forward Script for pfSense
# v1.0 (21st January 2014)

# Pre-requisites for this script:
# pfSense v2.1 (Port forward NAT return destination broken in earlier versions)
# curl - pkg_add -r curl
# xmlstarlet - pkg_add -r xmlstarlet

# Add your PIA username and password
USERNAME="user"
PASSWORD="password"
PIACLIENTID=/home/jleiss/.pia_client_id

# Check to see if we have a valid PIA Client ID file.
# If not, create one. Linux is included for illustration only.
if [ ! -e $PIACLIENTID ]; then

        # OSX/FreeBSD (pfSense)
        #head -n 100 /dev/urandom | md5 > $PIACLIENTID

        # Linux
        head -n 100 /dev/urandom | md5sum | tr -d " -" > $PIACLIENTID

        logger "pia-port: Created new PIA Client ID."
fi

# Find out the tunnelling device for your VPN and get your IP address.
# There are several options presented here. Personally, I prefer to use
# the interface which I know relates to my VPN tunnel for forwarding.

DEVICE=`ifconfig | grep -o "tun[0-9]"`
LOCAL_IP=`ifconfig $DEVICE | grep -Po "(?<=addr.)[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`

# Get the port number for the forwarded port
PORT=`curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat $PIACLIENTID)&local_ip=$LOCAL_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment`

PORTNUM=`echo $PORT | grep -oE "[0-9]+"` 


logger "pia-port: Port number acquired: $PORTNUM"
echo $PORTNUM > /home/jleiss/VPNport.txt

logger "pia-port: Current port forward: $CURPORT"

#Update port in QBittorent
curl -i -X POST -d "json=%7B%22listen_port%22%3A${PORTNUM}%7D" http://localhost:8080/command/setPreferences
and then I setup an entry in crontab to run the script once an hour and dump that to a log file in case there is error's I need to review.
25      *      *      *      *      /home/jleiss/newport.sh > /home/jleiss/.cronlog 2>&1

Jeff

Re: Changing port forward through CLI

Posted: Sat Jan 07, 2017 4:49 pm
by alleyoopster
I did find a solution I started to use transmission, but this was what I was looking for thank you

Code: Select all

#Update port in QBittorent
curl -i -X POST -d "json=%7B%22listen_port%22%3A${PORTNUM}%7D" http://localhost:8080/command/setPreferences

Re: Changing port forward through CLI

Posted: Fri Jan 05, 2018 11:25 am
by sebstr
[quote="jleiss"]
I'm assuming you have found what you needed otherwise you would have followed up by now, but I thought I would add what I got working here in case anyone else needed it. This is what I use on Ubuntu 16.04

Code: Select all

#!/bin/sh
export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/root/bin

# Private Internet Access Advanced Port Forward Script for pfSense
# v1.0 (21st January 2014)

# Pre-requisites for this script:
# pfSense v2.1 (Port forward NAT return destination broken in earlier versions)
# curl - pkg_add -r curl
# xmlstarlet - pkg_add -r xmlstarlet

# Add your PIA username and password
USERNAME="user"
PASSWORD="password"
PIACLIENTID=/home/jleiss/.pia_client_id

# Check to see if we have a valid PIA Client ID file.
# If not, create one. Linux is included for illustration only.
if [ ! -e $PIACLIENTID ]; then

        # OSX/FreeBSD (pfSense)
        #head -n 100 /dev/urandom | md5 > $PIACLIENTID

        # Linux
        head -n 100 /dev/urandom | md5sum | tr -d " -" > $PIACLIENTID

        logger "pia-port: Created new PIA Client ID."
fi

# Find out the tunnelling device for your VPN and get your IP address.
# There are several options presented here. Personally, I prefer to use
# the interface which I know relates to my VPN tunnel for forwarding.

DEVICE=`ifconfig | grep -o "tun[0-9]"`
LOCAL_IP=`ifconfig $DEVICE | grep -Po "(?<=addr.)[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*"`

# Get the port number for the forwarded port
PORT=`curl -d "user=$USERNAME&pass=$PASSWORD&client_id=$(cat $PIACLIENTID)&local_ip=$LOCAL_IP" https://www.privateinternetaccess.com/vpninfo/port_forward_assignment`

PORTNUM=`echo $PORT | grep -oE "[0-9]+"` 


logger "pia-port: Port number acquired: $PORTNUM"
echo $PORTNUM > /home/jleiss/VPNport.txt

logger "pia-port: Current port forward: $CURPORT"

#Update port in QBittorent
curl -i -X POST -d "json=%7B%22listen_port%22%3A${PORTNUM}%7D" http://localhost:8080/command/setPreferences
and then I setup an entry in crontab to run the script once an hour and dump that to a log file in case there is error's I need to review.
25      *      *      *      *      /home/jleiss/newport.sh > /home/jleiss/.cronlog 2>&1

Jeff
[/quote]

Hi Jeff,

Thanks for providing your solution. I found your solution just yesterday and the script seems to work fine. Although I'm getting a weird error which I, who lack any experience in working with web APIs, really can't figure out. I assume it's related to the script not being able to authenticate correctly to my qbt server, see below:

Code: Select all

HTTP/1.1 403 Forbidden
content-length: 0
content-security-policy: default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src 'self' 'unsafe-inline'; object-src 'none';
date: Fri, 05 Jan 2018 11:22:15 GMT
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-xss-protection: 1; mode=block
I tired to add "--data "username=XXX&password=XXX"" to the string in the script, but also a cookie in my 2nd attempt with "--cookie "SID=XXX"" without any luck.

Also, when I run the initial curl string towards PIA I get another port number than the one that is being logged in the logger log file from the script, and the port provided in that logfile stops my current active torrents. Any idea why this might happen? I've made sure that all the commands actually works manually and gives me the right data.

Appreciate any insight you or anyone else might be able to give me,

Cheers,
Seb