Sunday, March 3, 2024

Learn how to Run Proton VPN in Home windows with Python


On this tutorial, we are going to present you the best way to run Proton VPN in Home windows utilizing Python Code.

Prerequisite

First it is advisable obtain and set up the OpenVPN GUI. OpenVPN GUI is a user-friendly software that lets you simply configure and handle OpenVPN connections in your pc. OpenVPN is a well-liked open-source VPN protocol that gives safe and encrypted connections over public networks.

Observe the steps under to obtain OpenVPN config file together with OpenVPN login credentials.

  1. Log in to your ProtonVPN account at account.protonvpn.com
  2. Go to Downloads tab after which choose Platform: Home windows and Protocol: UDP and Config File: Free server configs
  3. Click on the Motion > Obtain button subsequent to the precise config file you need.
  4. Get your OpenVPN login credentials by visiting Account > OpenVPN / IKEv2 username.
  5. Open the OpenVPN GUI app.
  6. See the icon for OpenVPN GUI showing within the system tray. Proper click on on the icon after which go to Import > Import file.
    OpenVPN
  7. Browse to the situation the place you saved the downloaded OpenVPN config file after which Click on on the Open button.
  8. Proper click on the OpenVPN GUI icon in system tray after which click on on Join.
  9. It can ask you to enter your OpenVPN login credentials. Enter your username and password and examine the Save password field to log in robotically every time.

Python Code

The next code defines a operate that executes a command to work together with the OpenVPN GUI. You’ll be able to join, disconnect or reconnect the VPN utilizing the code under.

import subprocess

def VPN(motion, myvpn=None):
    openvpn_gui_path = r"C:Program FilesOpenVPNbinopenvpn-gui.exe"
    
    valid_actions = ["connect", "disconnect", "reconnect", "status"]
    
    if motion in valid_actions:
        if myvpn:
            command = f'"{openvpn_gui_path}" --command {motion} "{myvpn}"'
        else:
            command = f'"{openvpn_gui_path}" --command {motion}'
        
        subprocess.Popen(command, shell=True)
    else:
        print("Invalid motion:", motion)

myvpn = "us-free-54.protonvpn.web.udp.ovpn"  # Substitute along with your precise config file
VPN("join", myvpn)

To disconnect the VPN, use this command VPN("disconnect", myvpn). We’ve used the subprocess module which supplies the flexibility to spawn new processes, connect with their enter/output/error pipes, and acquire their return codes. Set up this module if it isn’t already put in earlier than loading it.

To disable this pop-up do you wish to enable this app to make modifications to your system? whenever you run the python script, go to Person Account Management Settings and drag the bar slider to “By no means notify

Learn how to Wait until VPN is Linked

Ready till a VPN is related earlier than continuing with sure duties ensures {that a} particular portion of your code or a script is executed solely when a VPN connection is efficiently established. For instance, in case your code entails internet scraping from a region-restricted web site or accessing a safe database, you’ll wish to be sure that the VPN is related earlier than making these requests.

import os
import re
import time

def IPAddress():
    ipconfig = os.popen('ipconfig').learn()
    ipv4 = re.findall(r'IPv4.*?(d+.d+.d+.d+)', ipconfig)
    return ipv4

def WaitUntilVPNConnected():
    s = time.time()
    whereas len(IPAddress()) == 1:
        time.sleep(3)
        if (time.time() - s) > 15:
            print("VPN not related")
            break
    if len(IPAddress()) > 1:
        time.sleep(5)
        print("VPN Linked with", IPAddress()[0])

VPN("join", "us-free-54.protonvpn.web.udp.ovpn")
WaitUntilVPNConnected()
print("testing")
VPN("disconnect", "us-free-54.protonvpn.web.udp.ovpn")

Learn how to Randomly Connect with VPN Areas

The next code introduces randomization when deciding on OpenVPN config recordsdata and incorporates logging of actions and outcomes right into a log file. It’s helpful in eventualities the place you wish to keep away from being simply recognized or tracked primarily based in your IP handle. The script generates log entries containing the present date and time, together with appended command outputs. Moreover, the code verifies the existence of a log file named “proton-log.txt” inside the current working listing. In case the file exists, the log entry is added to it. If the file does not exist, a brand new log file is generated.

import os
import random
from datetime import datetime

def proton_random(motion, connect_list=None):
    
    # Join
    openvpn_gui_path = r"C:Program FilesOpenVPNbinopenvpn-gui.exe"    
    if connect_list shouldn't be None:
        location = random.alternative(connect_list) 
        os.popen(f'"{openvpn_gui_path}" --command {motion} "{location}"').learn()
    
        # Write Log
        log = os.path.be part of(os.getcwd(), "proton-log.txt")
        log_datetime = f"{datetime.now():%Y-%m-%d %H:%M:%S} {location}"
        log_datetime2 = "n".be part of(["", log_datetime])
        if os.path.isfile(log):
            with open(log, "a") as f:
                f.write(log_datetime2)
        else:
            with open(log, "w") as f:
                f.write(log_datetime)
        return(location)
                
    else:
        os.popen(f'"{openvpn_gui_path}" --command {motion}')
            

# Join
connect_list = ["us-free-54.protonvpn.net.udp.ovpn", "us-free-155.protonvpn.net.tcp.ovpn", "vpngate_public-vpn-227.opengw.net_tcp_443.ovpn"]
myvpn = proton_random("join", connect_list)

# Disconnect
VPN("disconnect", myvpn)
About Creator:
Deepanshu Bhalla

Deepanshu based ListenData with a easy goal – Make analytics straightforward to grasp and observe. He has over 10 years of expertise in information science. Throughout his tenure, he labored with international purchasers in numerous domains like Banking, Insurance coverage, Non-public Fairness, Telecom and HR.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles