major refactor, replace ovpn.zip file, leverage API instead.
This commit is contained in:
128
nord-checker.py
128
nord-checker.py
@@ -6,17 +6,20 @@ import datetime
|
||||
import requests
|
||||
import re
|
||||
import sys
|
||||
from psql_utils import connect_to_database, update_or_insert_ip
|
||||
from ovpn_downloader import download_and_extract_ovpn_configs, download_ovpn_if_needed
|
||||
from psql_utils import connect_to_database, update_or_insert_ip, should_skip_file
|
||||
from openvpn_manager import establish_vpn_connection, is_vpn_active, disconnect_vpn, get_external_ip
|
||||
from ovpn_template import DEFAULT_OVPN_CONFIG
|
||||
|
||||
# Variables
|
||||
CREDENTIAL_FILE = "nord.creds"
|
||||
OVPN_DIR = "ovpn_configs/ovpn_tcp"
|
||||
TEMP_OVPN_DIR = "/tmp/ovpn_configs" # Directory for temporary OVPN files
|
||||
LOG_FILE = "openvpn.log" # This is not used anymore
|
||||
RESULT_FILE = "vpnlist.txt"
|
||||
PING_URL = "https://health.ext.ben.io/ping/a1a55915-1051-48ed-bd13-ea3f1717a3ee"
|
||||
|
||||
# NordVPN API endpoint (updated to get all servers)
|
||||
NORDVPN_API_URL = "https://api.nordvpn.com/v1/servers?limit=16384"
|
||||
|
||||
# Define DEBUG_MODE
|
||||
DEBUG_MODE = False
|
||||
if len(sys.argv) > 1 and sys.argv[1] == "-d":
|
||||
@@ -27,7 +30,7 @@ def debug_print(message):
|
||||
print(f"{datetime.datetime.now()} + [Debug Mode]:{message}")
|
||||
|
||||
try:
|
||||
requests.get(PING_URL, timeout=10)
|
||||
requests.get(PING_URL, auth=('local', 'local'), timeout=10)
|
||||
print(f"{datetime.datetime.now()} [Main Script]: Ping sent to: %s" % PING_URL)
|
||||
except requests.RequestException as e:
|
||||
# Log ping failure here...
|
||||
@@ -64,7 +67,17 @@ def is_script_running():
|
||||
print(f"{datetime.datetime.now()} [Main Script]: Error checking for running instances: {e}")
|
||||
return False
|
||||
|
||||
|
||||
# Function to get a list of all NordVPN servers
|
||||
def get_nordvpn_servers():
|
||||
"""Fetches a list of all NordVPN servers from the API."""
|
||||
try:
|
||||
response = requests.get(NORDVPN_API_URL)
|
||||
response.raise_for_status() # Raise an exception for bad status codes
|
||||
servers = response.json()
|
||||
return servers
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"{datetime.datetime.now()} [Main Script]: Error fetching NordVPN servers: {e}")
|
||||
return []
|
||||
|
||||
|
||||
# Get current IP before starting (using get_external_ip)
|
||||
@@ -91,76 +104,85 @@ except subprocess.CalledProcessError:
|
||||
# Connect to PostgreSQL database
|
||||
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, debug_print=debug_print)
|
||||
# Get the list of recommended NordVPN servers
|
||||
servers = get_nordvpn_servers()
|
||||
|
||||
total_files = len(ovpn_files_to_check)
|
||||
debug_print(f"{datetime.datetime.now()} [Main Script]: Found {total_files} OVPN files to check.")
|
||||
print(f"{datetime.datetime.now()} [Main Script]: Found {total_files} OVPN files to check.")
|
||||
# Create the temporary directory if it doesn't exist
|
||||
if not os.path.exists(TEMP_OVPN_DIR):
|
||||
os.makedirs(TEMP_OVPN_DIR)
|
||||
|
||||
# Process each OVPN file
|
||||
FILE_NUM = 1
|
||||
for OVPN_FILE in ovpn_files_to_check:
|
||||
OVPN_FILENAME = os.path.basename(OVPN_FILE)
|
||||
# Process each server
|
||||
server_count = len(servers)
|
||||
for index, server in enumerate(servers):
|
||||
# Check if the server uses OpenVPN TCP Dedicated technology
|
||||
technologies = server.get('technologies', [])
|
||||
skip_server = any(tech.get('identifier') == 'openvpn_dedicated_tcp' for tech in technologies)
|
||||
|
||||
debug_print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Processing OVPN file...")
|
||||
if skip_server:
|
||||
server_name = server.get('hostname')
|
||||
debug_print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Skipping OpenVPN TCP Dedicated server.")
|
||||
continue # Skip to the next server
|
||||
|
||||
# Check if the OVPN file exists, download if needed
|
||||
file_exists = download_ovpn_if_needed(OVPN_FILENAME, cursor, conn, debug_print=debug_print)
|
||||
server_name = server.get('hostname')
|
||||
server_ip = server.get('station') # Get the server IP address
|
||||
server_load = server.get('load', 'N/A') # Get server load, default to 'N/A' if not available
|
||||
server_status = server.get('status', 'N/A') # Get server status, default to 'N/A' if not available
|
||||
|
||||
if not file_exists:
|
||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Skipping, file not found and removed from database.")
|
||||
FILE_NUM += 1
|
||||
debug_print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Load: {server_load}, Status: {server_status} Processing server... ")
|
||||
|
||||
# Construct the temporary OVPN filename
|
||||
ovpn_filename = f"{server_name}.tcp.ovpn"
|
||||
ovpn_filepath = os.path.join(TEMP_OVPN_DIR, ovpn_filename)
|
||||
|
||||
# Check if the server should be skipped based on last check time
|
||||
if should_skip_file(cursor, ovpn_filename, index+1, server_count, debug_print=debug_print):
|
||||
debug_print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Skipping server, checked within the last 3 days.")
|
||||
continue
|
||||
|
||||
# Establish VPN connection
|
||||
connection_successful, connection_message = establish_vpn_connection(f"{OVPN_DIR}/{OVPN_FILE}", CREDENTIAL_FILE, LOG_FILE, debug_print=debug_print)
|
||||
# Create temporary OVPN file from template
|
||||
with open(ovpn_filepath, "w") as f:
|
||||
f.write(DEFAULT_OVPN_CONFIG.format(server_ip=server_ip, server_cn=server_name))
|
||||
|
||||
# Establish VPN connection
|
||||
connection_successful, connection_message = establish_vpn_connection(
|
||||
ovpn_filepath, CREDENTIAL_FILE, LOG_FILE, debug_print=debug_print
|
||||
)
|
||||
if connection_successful:
|
||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: {connection_message}")
|
||||
if not connection_successful:
|
||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Error starting OpenVPN connection:{connection_message}")
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: {connection_message}")
|
||||
|
||||
# Wait for a random time between 1 and 4 seconds
|
||||
sleep_time = random.randint(8, 15)
|
||||
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...")
|
||||
# Check if the VPN connection is active
|
||||
try:
|
||||
subprocess.run(["pgrep", "-f", ovpn_filename], check=True, capture_output=True)
|
||||
|
||||
continue # Skip to the next OVPN file
|
||||
# Check external IP (with validation and retries)
|
||||
external_ip = get_external_ip()
|
||||
|
||||
# Check if the VPN connection is active (using the function from openvpn_manager.py)
|
||||
try:
|
||||
subprocess.run(["pgrep", "-f", OVPN_FILENAME], check=True, capture_output=True)
|
||||
if external_ip is None:
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Unable to get external IP. Skipping this server.")
|
||||
continue
|
||||
|
||||
# Check external IP (with validation and retries)
|
||||
external_ip = get_external_ip()
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: External IP via VPN: {external_ip}")
|
||||
|
||||
if external_ip is None:
|
||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: Unable to get external IP. Skipping this OVPN file.")
|
||||
continue
|
||||
# Update or insert into database
|
||||
update_or_insert_ip(cursor, conn, ovpn_filename, external_ip)
|
||||
|
||||
print(f"{datetime.datetime.now()} [{FILE_NUM}/{total_files}] [{OVPN_FILENAME}]: External IP via VPN: {external_ip}")
|
||||
except subprocess.CalledProcessError:
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: VPN connection failed to establish.")
|
||||
else:
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Error starting OpenVPN connection: {connection_message}")
|
||||
|
||||
# Update or insert into database
|
||||
update_or_insert_ip(cursor, conn, OVPN_FILENAME, external_ip)
|
||||
|
||||
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.")
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: VPN disconnected.")
|
||||
|
||||
# Wait for a random time between 5 and 8 seconds
|
||||
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()} [{index+1}/{server_count}] [{server_name}]: 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...")
|
||||
print(f"{datetime.datetime.now()} [{index+1}/{server_count}] [{server_name}]: Resuming...")
|
||||
|
||||
# Increment file number
|
||||
FILE_NUM += 1
|
||||
# Clean up the temporary OVPN file
|
||||
os.remove(ovpn_filepath)
|
||||
|
||||
# Close the database connection
|
||||
cursor.close()
|
||||
|
||||
Reference in New Issue
Block a user