ProtonVPN / linux-cli

Official ProtonVPN Linux app (CLI)
https://protonvpn.com/download-linux
GNU General Public License v3.0
332 stars 42 forks source link

How do I set up a cron job to connect to fastest server? #72

Closed ReaderGuy42 closed 2 years ago

ReaderGuy42 commented 2 years ago

We are happy to answer your questions about the code or discuss technical ideas.

Please complete the following checklist (by adding [x]):


I have a .sh script that automatically connects to the fastest server:

#!/bin/bash

/usr/bin/protonvpn-cli c -f

(I've made it executable) and when I trigger it manually or in terminal it works perfectly. But in the cron tab it doesn't work:

01,31 * * * * /home/ME/Scripts/Proton/ProtonVPN-Fastest.sh

Any ideas? Thanks :)

MuhammedZakir commented 2 years ago

You will need to run protonvpn-cli as non-root user and it also needs DBUS_SESSION_BUS_ADDRESS env var. If the cron job is being executed as root, you can use runuser to run it as specified user (see -w, -u & -g options). For that env var, you can hardcode it if you only have a single user, or you can write that var to a file at login and then read that value from your script.

There maybe a better way, but this is the only solution I know of.

ReaderGuy42 commented 2 years ago

I don't really understand that sorry. I can't find anything regarding the options e.g. -u for protonvpn-cli? Or was that for runuser?

MuhammedZakir commented 2 years ago

I don't really understand that sorry.

What exactly do you not understand?

I can't find anything regarding the options e.g. -u for protonvpn-cli? Or was that for runuser?

It's for runuser.

MuhammedZakir commented 2 years ago

Before you do what I said, you should first log the output of that cron job including errors into a file. It should show why the job failed. Post it here [1] if you couldn't understand.

[1] If you are going to post the entire file, instead of that, upload it into a pastebin site or similiar and post the link here.

ReaderGuy42 commented 2 years ago

OK, so I have thes line in crontab:

*/5 * * * * /home/ReaderGuy42/Scripts/Cron/BackupDocuments.sh 
*/5 * * * * /home/ReaderGuy42/Scripts/Proton/ProtonVPN-Fastest.sh

The Backup thing works, I think (at least it did yesterday, it only backs up when an external drive is connected). But the Proton-CLI still doesn't. As in, it doesn't connect to a VPN through cron.

And this is the last few minutes of the var/log/syslog:

Apr 23 18:59:22 ReaderGuy42 rtkit-daemon[879]: Supervising 5 threads of 3 processes of 1 users.
Apr 23 18:59:22 ReaderGuy42 rtkit-daemon[879]: Supervising 5 threads of 3 processes of 1 users.
Apr 23 19:00:00 ReaderGuy42 wpa_supplicant[715]: wlp3s0: WPA: Group rekeying completed with 3c:37:12:49:d3:87 [GTK=CCMP]
Apr 23 19:00:02 ReaderGuy42 CRON[33726]: (ReaderGuy42) CMD (/home/ReaderGuy42/Scripts/Proton/ProtonVPN-Fastest.sh)
Apr 23 19:00:02 ReaderGuy42 CRON[33727]: (ReaderGuy42) CMD (/home/ReaderGuy42/Scripts/Cron/BackupDocuments.sh)
Apr 23 19:00:02 ReaderGuy42 CRON[33724]: (CRON) info (No MTA installed, discarding output)
Apr 23 19:00:02 ReaderGuy42 CRON[33723]: (CRON) info (No MTA installed, discarding output)
Apr 23 19:00:23 ReaderGuy42 rtkit-daemon[879]: Supervising 5 threads of 3 processes of 1 users.
Apr 23 19:01:19 ReaderGuy42 kernel: [11628.512898] [UFW BLOCK] IN=wlp3s0 OUT= MAC=MacAddress SRC=IPaddress DST=IPaddress LEN=36 TOS=0x00 PREC=0xC0 TTL=1 ID=49414 DF PROTO=2 
Apr 23 19:02:33 ReaderGuy42 kernel: [11703.261243] [UFW BLOCK] IN=wlp3s0 OUT= MAC=MacAddress SRC=IPaddress DST=IPaddress LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=40266 DF PROTO=TCP SPT=46093 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 
Apr 23 19:02:34 ReaderGuy42 kernel: [11704.285749] [UFW BLOCK] IN=wlp3s0 OUT= MAC=MacAddress SRC=IPaddress DST=IPaddress LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=40267 DF PROTO=TCP SPT=46093 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 
Apr 23 19:02:36 ReaderGuy42 kernel: [11706.128926] [UFW BLOCK] IN=wlp3s0 OUT= MAC=MacAddress SRC=IPaddress DST=IPaddress LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=40268 DF PROTO=TCP SPT=46093 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 
Apr 23 19:02:40 ReaderGuy42 kernel: [11710.224086] [UFW BLOCK] IN=wlp3s0 OUT= MAC=MacAddress SRC=IPaddress DST=IPaddress LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=40269 DF PROTO=TCP SPT=46093 DPT=80 WINDOW=29200 RES=0x00 SYN URGP=0 
MuhammedZakir commented 2 years ago

I tested it and what I said above was right. You will need to export DBUS_SESSION_BUS_ADDRESS env variable.

Note: don't forget to change the dbus session address to match yours.

E.g.

01,31 * * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus /usr/bin/protonvpn-cli c -f >>/tmp/pvpn.out 2>&1

OR

DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

01,31 * * * * /usr/bin/protonvpn-cli c -f >>/tmp/pvpn.out 2>&1

If you don't want to add env vars in crontab, you can move that to your shell script. Don't forget to export them if you do so.

ReaderGuy42 commented 2 years ago

OK, so first of all, thank you very much for your effort! Second, I'm not sure I've figured out exporting the env variable (if you haven't figure it out, I'm new to this). I used this command: eval dbus-launch --auto-syntax from this website (https://localcoder.org/how-to-export-dbus-session-bus-address) and got the DBUS_SESSION_BUS_ADDRESS and the BUS_PID and BUS_WINDOWID. I put that whole block in my .sh file. Should I only have put in the BUS_ADDRESS?

And then I get this in var/log/syslog

Apr 24 18:48:01 ReaderGuy42 CRON[20702]: (ReaderGuy42) CMD (/home/ReaderGuy42/Scripts/Proton/ProtonVPN-Fastest.sh)
Apr 24 18:48:01 ReaderGuy42 dbus-daemon[20449]: [session uid=1000 pid=20447] Activating service name='org.freedesktop.secrets' requested by ':1.0' (uid=1000 pid=20704 comm="/usr/bin/python3 /usr/bin/protonvpn-cli c -f " label="unconfined")
Apr 24 18:48:01 ReaderGuy42 org.freedesktop.secrets[20718]: SSH_AUTH_SOCK=/run/user/1000/keyring/ssh
Apr 24 18:48:43 ReaderGuy42 CRON[20402]: (CRON) info (No MTA installed, discarding output)

But I can't actually tell if it worked, as I still don't get a pop up that I do when I manually trigger that script. Is this good? Thanks!! (I switched everything over to a script that chooses a random server, and from this I can tell that the cron is NOT doing anything with it. Or at least it's not switching servers.)

MuhammedZakir commented 2 years ago
  1. Is yours single- or multi-user setup?
  2. If it multi-user setup, which DE do you use? KDE, GNOME, or something else?

If you only use a single user, just hardcode the session address in your script. That's the easiest way and is what I do.

  1. Open a terminal and execute echo $DBUS_SESSION_BUS_ADDRESS.
  2. Put the value you got after =.
    
    #!/bin/bash

export DBUS_SESSION_BUS_ADDRESS= /bin/protonvpn-cli c -f >>/tmp/pvpn.out 2>&1


Edit: You can use redirect the output to a different file if you want.

---

> OK, so first of all, thank you very much for your effort!

You're welcome! :-)

> Second, I'm not sure I've figured out exporting the env variable (if you haven't figure it out, I'm new to this). I used this command: `eval dbus-launch --auto-syntax` from this website (https://localcoder.org/how-to-export-dbus-session-bus-address) and got the `DBUS_SESSION_BUS_ADDRESS` and the `BUS_PID` and `BUS_WINDOWID`. I put that whole block in my `.sh` file. Should I only have put in the `BUS_ADDRESS`?

This wouldn't work. The value you get will be different from your running session's. To verify that, just open a terminal and run these:
```sh
# what you actually want
echo $DBUS_SESSION_BUS_ADDRESS

# what you instead get
dbus-launch --auto-syntax

FYI, you will have to use the second method in the above link, but replacing gnome-session (if you don't use Gnome) with the one you use.

But I can't actually tell if it worked, as I still don't get a pop up that I do when I manually trigger that script. Is this good? Thanks!! (I switched everything over to a script that chooses a random server, and from this I can tell that the cron is NOT doing anything with it. Or at least it's not switching servers.)

In the command I used in crontab, >>/tmp/pvpn.out 2>&1 at the end redirects output of the command including errors to /tmp/pvpn.out. This way you can check /tmp/pvpn.out to see what protonvpn-cli prints when running it. syslog will only show about cron itself and won't contain what happened with the command you run. In your case, you can move that to inside the file:

/usr/bin/protonvpn-cli c -f >>/tmp/pvpn.out 2>&1

FYI, this is actually "shell redirection". Search that to know more about it.

ReaderGuy42 commented 2 years ago

I only have a single user on Linux Mint with DE Cinnamon. Following your instructions I now have this in crontab: */30 * * * * /home/ReaderGuy42/Scripts/Proton/ProtonVPN-Fastest.sh and this in that .sh:

#!/bin/bash

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
/usr/bin/protonvpn-cli c -f >>/home/Readerguy42/Scripts/Proton/log.out 2>&1

but I'm still not getting any change in server. I once again tested with an identical setup but with a random server instead of fastest, but it has no effect. Now what? Thanks!!

Edit: I get this in the log.out file:

Setting up ProtonVPN.

An unknown error has occured. Please ensure that you have internet connectivity.
If the issue persists, please contact support.

but I don't think that's especially helpful...

MuhammedZakir commented 2 years ago

Testing it again gave me the same error you're getting. Using journalctl, I found that the error is due to insufficient permission. I even tried to source the current env, but it still didn't work.

Convoluted, but this works:

$ cat ~/.config/systemd/user/start_vpn.service:

[Unit]
Description=Connect to the fastest ProtonVPN server

[Service]
ExecStart=/usr/bin/protonvpn-cli c -f

[Install]
WantedBy=default.target

$ crontab -l:

01,31 * * * * DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus systemctl --user start start_vpn.service

I honestly don't know why we're getting "insufficent permission" issue when executing directly via a cronjob, but not via terminal or systemd service. What's weird is that it did worked for me when I tested yesterday [1]. :confused:

[1] https://github.com/ProtonVPN/linux-cli/issues/72#issuecomment-1107874042

ReaderGuy42 commented 2 years ago

HEY, now it works!! awesome :) Thank you!!