diff --git a/nord-checker.py b/nord-checker.py index e9164d6..cac80ca 100644 --- a/nord-checker.py +++ b/nord-checker.py @@ -7,11 +7,11 @@ import re import sys from psql_utils import connect_to_database, update_or_insert_ip 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 CREDENTIAL_FILE = "nord.creds" -OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp" +OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp" LOG_FILE = "/root/nordvpn/openvpn.log" RESULT_FILE = "vpnlist.txt" @@ -43,7 +43,7 @@ if CURRENT_IP is None: else: print(f"{datetime.datetime.now()} [Main Script]: Current IP before VPN connections: {CURRENT_IP}") if not DEBUG_MODE: - log_file.flush() + log_file.flush() # Terminate any existing OpenVPN connections try: @@ -62,7 +62,7 @@ conn, cursor = connect_to_database() # Download and extract OVPN configurations, and get the list of files to check debug_print(f"{datetime.datetime.now()} [Main Script]: Downloading and extracting OVPN configurations...") -ovpn_files_to_check = download_and_extract_ovpn_configs(cursor, conn) +ovpn_files_to_check = download_and_extract_ovpn_configs(cursor, conn) total_files = len(ovpn_files_to_check) @@ -85,22 +85,22 @@ for OVPN_FILE in ovpn_files_to_check: max_retries = 3 # Maximum number of retries retry_count = 0 while retry_count < max_retries: - openvpn_command = ["openvpn", "--config", f"{OVPN_DIR}/{OVPN_FILE}", - "--auth-user-pass", CREDENTIAL_FILE, "--daemon", + openvpn_command = ["openvpn", "--config", f"{OVPN_DIR}/{OVPN_FILE}", + "--auth-user-pass", CREDENTIAL_FILE, "--daemon", "--log-append", LOG_FILE, "--verb", "3"] debug_print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Executing OpenVPN command: {' '.join(openvpn_command)}") - process = subprocess.Popen(openvpn_command, - stdin=subprocess.PIPE, - stdout=subprocess.PIPE, + process = subprocess.Popen(openvpn_command, + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate(input=password.encode()) # Check for errors and print output if process.returncode != 0 or "AUTH_FAILED" in stderr.decode(): print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Error starting OpenVPN connection or authentication failed:") - print(stderr.decode()) - + print(stderr.decode()) + # Explicitly disconnect VPN if there was an error or auth failure disconnect_vpn() print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN disconnected (due to error or auth failure).") @@ -120,11 +120,11 @@ for OVPN_FILE in ovpn_files_to_check: max_attempts = 10 attempt = 0 backoff_time = 0 - with open(LOG_FILE, 'r') as f: + with open(LOG_FILE, 'r') as f: while attempt < max_attempts: line = f.readline() if not line: - time.sleep(1) + time.sleep(1) attempt += 1 continue @@ -135,23 +135,23 @@ for OVPN_FILE in ovpn_files_to_check: backoff_time += 1 if backoff_time >= 30: print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Authentication failed. Skipping this OVPN file.") - break + break - if attempt == max_attempts or backoff_time >= 30: + if attempt == max_attempts or backoff_time >= 30: print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN connection failed to establish (timeout or auth failure).") - # Disconnect VPN + # Disconnect VPN disconnect_vpn() print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN disconnected.") if not DEBUG_MODE: - log_file.flush() + log_file.flush() # Wait for a random time between 1 and 4 seconds - sleep_time = random.randint(1, 4) + sleep_time = random.randint(1, 4) print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Waiting for {sleep_time} seconds before the next connection...") time.sleep(sleep_time) print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Resuming...") if not DEBUG_MODE: - log_file.flush() + log_file.flush() continue # Skip to the next OVPN file # Check if the VPN connection is active (using the function from openvpn_manager.py) @@ -174,13 +174,13 @@ for OVPN_FILE in ovpn_files_to_check: except subprocess.CalledProcessError: print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN connection failed to establish.") - # Disconnect VPN + # Disconnect VPN disconnect_vpn() print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: VPN disconnected.") if not DEBUG_MODE: - log_file.flush() + log_file.flush() # Wait for a random time between 5 and 8 seconds - sleep_time = random.randint(5, 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...") time.sleep(sleep_time) print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Resuming...") diff --git a/ovpn_downloader.py b/ovpn_downloader.py index 4b8a1fd..006ca58 100644 --- a/ovpn_downloader.py +++ b/ovpn_downloader.py @@ -8,7 +8,7 @@ OVPN_ZIP_URL = "https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip" def download_and_extract_ovpn_configs(cursor, conn): """ - Downloads the OVPN ZIP archive, extracts its contents, + Downloads the OVPN ZIP archive, extracts its contents, adds new entries to the database, and cleans up. Returns a list of OVPN filenames that need to be checked. """