Files
nordvpn/ovpn_downloader.py
2024-09-07 13:33:06 +00:00

41 lines
1.6 KiB
Python

import os
import subprocess
import random
import datetime
OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp"
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,
adds new entries to the database, and cleans up.
Returns a list of OVPN filenames that need to be checked.
"""
if not os.path.exists(OVPN_DIR):
os.makedirs(OVPN_DIR)
subprocess.run(["curl", "-s", "-L", "-o", f"{OVPN_DIR}/ovpn.zip", OVPN_ZIP_URL])
subprocess.run(["unzip", "-q", "-o", f"{OVPN_DIR}/ovpn.zip", "-d", OVPN_DIR])
os.remove(f"{OVPN_DIR}/ovpn.zip")
# Find all OVPN files
ovpn_files = [f for f in os.listdir(OVPN_DIR) if f.endswith(".ovpn")]
# Add new entries to the database
for filename in ovpn_files:
cursor.execute("SELECT 1 FROM ovpn_files WHERE file_name = %s", (filename,))
if cursor.fetchone() is None: # If no record found, insert a new one
cursor.execute("""
INSERT INTO ovpn_files (file_name, last_observed)
VALUES (%s, CURRENT_TIMESTAMP)
""", (filename,))
conn.commit()
# Fetch the list of VPN servers that need to be checked (last check older than 3 days)
three_days_ago = datetime.datetime.now() - datetime.timedelta(days=3)
cursor.execute("SELECT file_name FROM ovpn_files WHERE last_exit_ip_check < %s OR last_exit_ip_check IS NULL", (three_days_ago,))
ovpn_files_to_check = [row[0] for row in cursor.fetchall()]
return ovpn_files_to_check