diff --git a/update-rbl.py b/update-rbl.py index 4b3c778..8320be0 100644 --- a/update-rbl.py +++ b/update-rbl.py @@ -2,17 +2,62 @@ import shutil import subprocess import signal import os +import psycopg2 +import datetime + +# PostgreSQL database connection details (assuming you have psql.creds file) +with open('psql.creds', 'r') as f: + config = {} + for line in f: + key, value = line.strip().split(' = ') + config[key] = value def update_and_reload(): - """Copies the VPN list, restarts rbldnsd (finding PID using ps aux), and logs the process.""" + """ + Retrieves VPN list from the database, restarts rbldnsd, and logs the process. + Also reports the number of VPN servers with exit IPs updated in the last 7 days. + """ - # Copy the VPN list + # Connect to PostgreSQL database + conn = psycopg2.connect( + dbname=config['db_name'], + user=config['db_user'], + password=config['db_password'], + host=config['db_host'], + port=config['db_port'] + ) + cursor = conn.cursor() + + # Fetch VPN servers with exit IPs updated in the last 7 days + seven_days_ago = datetime.datetime.now() - datetime.timedelta(days=7) + cursor.execute(""" + SELECT COUNT(*) FROM ovpn_files + WHERE last_exit_ip_check >= %s + AND exit_ip IS NOT NULL + AND exit_ip ~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' + """, (seven_days_ago,)) + updated_servers_count = cursor.fetchone()[0] + print(f"VPN servers with exit IPs updated in the last 7 days: {updated_servers_count}") + + # Fetch the list of VPN servers from the database (filter for valid IPs and not NULL) + cursor.execute(""" + SELECT exit_ip, file_name FROM ovpn_files + WHERE exit_ip IS NOT NULL + AND exit_ip ~ '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' + """) + vpn_list = cursor.fetchall() + + # Write the VPN list to /etc/vpnlist.txt try: - shutil.copy('/root/vpnlist.txt', '/etc/vpnlist.txt') - print("VPN list copied successfully.") - except shutil.Error as e: - print(f"Error copying VPN list: {e}") - return # Exit if copy fails + with open('/etc/vpnlist.txt', 'w') as f: + for ip, filename in vpn_list: + f.write(f"{ip} # {filename}\n") + print("VPN list written to /etc/vpnlist.txt successfully.") + except IOError as e: + print(f"Error writing VPN list to /etc/vpnlist.txt: {e}") + cursor.close() + conn.close() + return # Exit if write fails # Find the PID of rbldnsd using ps aux try: @@ -26,6 +71,8 @@ def update_and_reload(): print(f"Found rbldnsd process with PID: {pid}") except (subprocess.CalledProcessError, ValueError) as e: print(f"Error finding rbldnsd PID: {e}") + cursor.close() + conn.close() return # Stop rbldnsd @@ -34,6 +81,8 @@ def update_and_reload(): print("Sent termination signal to rbldnsd.") except ProcessLookupError: print("Error: rbldnsd process not found (might have already stopped).") + cursor.close() + conn.close() return # Start rbldnsd (make sure this matches your actual rbldnsd command) @@ -45,6 +94,9 @@ def update_and_reload(): except subprocess.CalledProcessError as e: print(f"Error restarting rbldnsd: {e}") + cursor.close() + conn.close() + print("VPN list updated and rbldnsd restarted successfully.") if __name__ == "__main__":