Getting the forwarded port number for use in a script after connecting through the official client

edited August 14 in P2P Support Posts: 5
TLDR:
grep "Forwarded port is " ~/.pia_manager/log/pia_nw.log|awk 'END {print $NF}'
And if you have a different solution, please share it below!

Full text:
So you've connected through the official Mac, Windows or Linux client to a server that supports port forwarding, and now you want to run a script that opens the forwarded port in your firewall and bittorrent client. You can see the port number in the tooltip that appears when you hover the mouse cursor over the client's icon, but you don't manually want to pass it to the script. You want to automate all the things dammit!

But how can your script get the port number? You search the forums but there's no thread on this really. Most results are about the PIA port forwarding API that is intended for people who don't use the official client. You already know you can pass your username, password, client ID and local IP to the _old_ API and it will return the port number, but it has been said that this old API will eventually be deactivated. This is what started you on your quest in the first place.

The new API only returns a port number when port forwarding is not yet activated when it is called, so this is not going to work after you've already connected through the client. Bob P. actually has the same problem with the new API as you, and Max P. told him about an undocumented internal API that can be used to get the port number like so:
echo '{"cmd": "status"}' | nc 127.0.0.1 31743
This turns out to be exactly what you need to retreive the forwarded port number locally, and you use a little utility called jq to process the resulting JSON. Now assigning the port number to a variable in your bash script looks like this:
PORT=$(echo '{"cmd":"status"}'|nc 127.0.0.1 31743|jq '.forwarded_port.single')
Works like a charm! Until you update to the latest version of the client application and the internal API stops returning anything... so now what?

...

After racking my brain for a while, I found out the PIA client logs the forwarded port number to ~/.pia_manager/log/pia_nw.log when logging is enabled in the settings dialog. It looks like this:
[2017-08-14T23:19:27.419Z] <info> |main| Forwarded port is 13335
So now we only need to get the last line that contains "Forwarded port is " from this log, extract the port number from this line and assign it to a variable. I use grep and awk for that:
grep "Forwarded port is " ~/.pia_manager/log/pia_nw.log|awk 'END {print $NF}'
Then assigning the port number to a variable in a bash script looks like this:
PORT=$(grep "Forwarded port is " ~/.pia_manager/log/pia_nw.log|awk 'END {print $NF}')
Great, now I can use the port variable to open the port in my firewall and configure my torrent client with it!

The above should work on Linux, and I guess maybe also on MacOS? And if you're on Windows, now you know you should look for the log files to find the forwarded port. Unless the logging is totally different on the other OSes...

I actually like the local undocumented API method better, because it always returns the current forwarded port. When grepping the log file, the returned port number might not be correct in certain situations, e.g. when you're connected to a server that doesn't support port forwarding, it will return the port number of the last server that you were connected to that does support port forwarding.

But what can you do when it stops working ey... if you know how to inspect packets like Max P. you might be able to make this work again. I'd like to hear it!

Anyway, I thought I'd share my findings here, for the benefit of all. If you know of an even better way to retrieve the forwarded port number _locally_, or if this helped you out, please leave a comment!

Thanks.

Post edited by Mushroom on

Comments

  • Posts: 2
    ugh, thank you so much.  I've been searching for this simple line that would tell me what my port forward address was.  I'm quite saddened by how many times I asked support for this and had no answer.  But thankfully you provided the answer I needed and just in time for re-upping my sub tomorrow.
Sign In or Register to comment.