re-work the wait for VPN connection to leverage openvpn.log instead of 10 second sleep.
This commit is contained in:
@@ -9,7 +9,6 @@ from psql_utils import connect_to_database, should_skip_file, update_or_insert_i
|
|||||||
from ovpn_downloader import download_and_extract_ovpn_configs
|
from ovpn_downloader import download_and_extract_ovpn_configs
|
||||||
from openvpn_manager import establish_vpn_connection, is_vpn_active, disconnect_vpn, get_external_ip
|
from openvpn_manager import establish_vpn_connection, is_vpn_active, disconnect_vpn, get_external_ip
|
||||||
|
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
CREDENTIAL_FILE = "nord.creds"
|
CREDENTIAL_FILE = "nord.creds"
|
||||||
OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp"
|
OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp"
|
||||||
@@ -41,7 +40,7 @@ except subprocess.CalledProcessError:
|
|||||||
|
|
||||||
# Download and extract OVPN configurations (using the function from ovpn_downloader.py)
|
# Download and extract OVPN configurations (using the function from ovpn_downloader.py)
|
||||||
debug_print(f"{datetime.datetime.now()} [Main Script]: Downloading and extracting OVPN configurations...")
|
debug_print(f"{datetime.datetime.now()} [Main Script]: Downloading and extracting OVPN configurations...")
|
||||||
ovpn_files = download_and_extract_ovpn_configs() # The function now returns the shuffled list
|
ovpn_files = download_and_extract_ovpn_configs()
|
||||||
|
|
||||||
total_files = len(ovpn_files)
|
total_files = len(ovpn_files)
|
||||||
|
|
||||||
@@ -72,8 +71,6 @@ for OVPN_FILE in ovpn_files:
|
|||||||
username = f.readline().strip()
|
username = f.readline().strip()
|
||||||
password = f.readline().strip()
|
password = f.readline().strip()
|
||||||
|
|
||||||
debug_print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Read credentials: username={username}")
|
|
||||||
|
|
||||||
# Establish VPN connection (capture stdout and stderr)
|
# Establish VPN connection (capture stdout and stderr)
|
||||||
openvpn_command = ["openvpn", "--config", f"{OVPN_DIR}/{OVPN_FILE}",
|
openvpn_command = ["openvpn", "--config", f"{OVPN_DIR}/{OVPN_FILE}",
|
||||||
"--auth-user-pass", CREDENTIAL_FILE, "--daemon",
|
"--auth-user-pass", CREDENTIAL_FILE, "--daemon",
|
||||||
@@ -90,13 +87,30 @@ for OVPN_FILE in ovpn_files:
|
|||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Error starting OpenVPN connection:")
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Error starting OpenVPN connection:")
|
||||||
print(stderr.decode())
|
print(stderr.decode())
|
||||||
else:
|
|
||||||
debug_print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: OpenVPN output:")
|
|
||||||
debug_print(stdout.decode())
|
|
||||||
|
|
||||||
# Wait for connection to establish
|
# Wait for connection to establish (with backoff for auth failure)
|
||||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Waiting for VPN connection to establish...")
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Waiting for VPN connection to establish...")
|
||||||
time.sleep(10)
|
max_attempts = 10
|
||||||
|
attempt = 0
|
||||||
|
backoff_time = 0
|
||||||
|
while attempt < max_attempts:
|
||||||
|
try:
|
||||||
|
last_lines = subprocess.check_output(["tail", "-n", "10", LOG_FILE]).decode().strip()
|
||||||
|
if "Initialization Sequence Completed" in last_lines:
|
||||||
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN connection established.")
|
||||||
|
break
|
||||||
|
elif "AUTH: Received control message: AUTH_FAILED" in last_lines:
|
||||||
|
backoff_time += 1 # Increment backoff time
|
||||||
|
if backoff_time >= 30: # If backoff reaches 30 seconds, skip
|
||||||
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Authentication failed. Skipping this OVPN file.")
|
||||||
|
continue # Skip to the next OVPN file
|
||||||
|
except subprocess.CalledProcessError:
|
||||||
|
pass
|
||||||
|
time.sleep(1)
|
||||||
|
attempt += 1
|
||||||
|
else:
|
||||||
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN connection failed to establish (timeout).")
|
||||||
|
continue
|
||||||
|
|
||||||
# Check if the VPN connection is active
|
# Check if the VPN connection is active
|
||||||
try:
|
try:
|
||||||
@@ -128,8 +142,8 @@ for OVPN_FILE in ovpn_files:
|
|||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: No openvpn process found.")
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: No openvpn process found.")
|
||||||
|
|
||||||
# Wait for a random time between 8 and 30 seconds
|
# Wait for a random time between 5 and 8 seconds
|
||||||
sleep_time = random.randint(6, 8)
|
sleep_time = random.randint(5, 8)
|
||||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Waiting for {sleep_time} seconds before the next connection...")
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Waiting for {sleep_time} seconds before the next connection...")
|
||||||
time.sleep(sleep_time)
|
time.sleep(sleep_time)
|
||||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Resuming...")
|
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Resuming...")
|
||||||
@@ -140,3 +154,6 @@ for OVPN_FILE in ovpn_files:
|
|||||||
# Close the database connection
|
# Close the database connection
|
||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
# Close the log file
|
||||||
|
log_file.close()
|
||||||
Reference in New Issue
Block a user