update ovpn downloader to handle missing files.

This commit is contained in:
2024-09-07 17:34:15 -05:00
parent 6dd4e6f755
commit 99b39a53f8
2 changed files with 68 additions and 31 deletions

View File

@@ -2,13 +2,15 @@ import os
import subprocess
import random
import datetime
import requests
OVPN_DIR = "/root/nordvpn/ovpn_configs/ovpn_tcp"
OVPN_DIR = "ovpn_configs"
OVPN_ZIP_URL = "https://downloads.nordcdn.com/configs/archives/servers/ovpn.zip"
OVPN_LEGACY_URL = "https://downloads.nordcdn.com/configs/files/ovpn_legacy/servers/"
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.
"""
@@ -38,4 +40,34 @@ def download_and_extract_ovpn_configs(cursor, conn):
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
return ovpn_files_to_check
def download_ovpn_if_needed(filename, cursor, conn):
"""
Downloads the specified OVPN file from the legacy URL if it doesn't exist in the OVPN_DIR.
Removes the entry from the database if the file returns a 404 error.
Returns True if the file was downloaded or already exists, False if it was not found and removed from the database
"""
filepath = os.path.join(OVPN_DIR+"/ovpn_tcp/", filename)
if os.path.exists(filepath):
print(f"{filename} already exists. Skipping download.")
return True
url = OVPN_LEGACY_URL + filename
print(f"Attempting to download {filename} from legacy URL...")
response = requests.get(url)
if response.status_code == 200:
with open(filepath, 'wb') as f:
f.write(response.content)
print(f"Downloaded {filename} from legacy URL.")
return True
elif response.status_code == 404:
print(f"{filename} not found on legacy URL: {filepath}. Removing from database.") # this is the updated line
cursor.execute("DELETE FROM ovpn_files WHERE file_name = %s", (filename,))
conn.commit()
return False
else:
print(f"Error downloading {filename}: {response.status_code}")
return False