update debug log handling, change to stoud instead of openvpn.log

This commit is contained in:
2024-09-08 09:52:19 -05:00
parent 1bf6d4efd8
commit 8fadcf88f2
2 changed files with 60 additions and 86 deletions

View File

@@ -2,8 +2,9 @@ import subprocess
import time
import datetime
import re
import sys
def establish_vpn_connection(ovpn_file, credential_file, log_file):
def establish_vpn_connection(ovpn_file, credential_file, log_file, debug_print=None, timeout=15): # Changed timeout to 15
"""
Establishes a VPN connection using the provided OVPN file and credentials.
Returns True if the connection is successful, False otherwise.
@@ -15,22 +16,56 @@ def establish_vpn_connection(ovpn_file, credential_file, log_file):
password = f.readline().strip()
# Establish VPN connection (capture stdout and stderr)
openvpn_command = ["openvpn", "--config", ovpn_file,
"--auth-user-pass", credential_file, "--daemon",
"--log-append", log_file, "--verb", "0", "--flush-log"] # Added --flush-log
openvpn_command = ["openvpn", "--config", ovpn_file,
"--auth-user-pass", credential_file,
"--verb", "1"]
process = subprocess.Popen(openvpn_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
if debug_print:
debug_print(f"OpenVPN command: {' '.join(openvpn_command)}") # Debug print
sys.stdout.flush() # Flush the output buffer
process = subprocess.Popen(openvpn_command,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate(input=password.encode())
# Send the password to the process
process.stdin.write((password + '\n').encode())
process.stdin.flush()
# Check for errors
if process.returncode != 0:
return False, stderr.decode()
else:
return True, stdout.decode()
start_time = time.time()
while True:
# Check if the process has exited
if process.poll() is not None:
debug_print(f"OpenVPN exited with a non-zero return code: {process.returncode}")
return False, process.stderr.read().decode()
# Read a line from stdout (non-blocking)
line = process.stdout.readline().decode()
if line:
if debug_print:
debug_print(f"OpenVPN output: {line.strip()}")
if "Initialization Sequence Completed" in line:
debug_print("VPN Initialization Sequence Completed found in stdout.")
#print(f"{datetime.datetime.now()} [OpenVPN Manger]: VPN Initialization Sequence Completed found in stdout.")
return True, line
# Check for AUTH_FAILED message
if "AUTH: Received control message: AUTH_FAILED" in line:
debug_print("Authentication failed. Sleeping for 15 seconds.")
#print(f"{datetime.datetime.now()} [OpenVPN Manger]: AUTH: Received control message: AUTH_FAILED, sleeping for 15 seconds")
time.sleep(15)
return False, "AUTH: Received control message: AUTH_FAILED"
break
# Check for timeout
if time.time() - start_time > timeout:
debug_print(f"VPN connection timed out after {timeout} seconds.")
# print(f"{datetime.datetime.now()} [OpenVPN Manger]: VPN connection timed out after {timeout} seconds.")
process.kill()
return False, "VPN connection timed out."
time.sleep(0.1) # Small delay to avoid busy-waiting
def is_vpn_active(ovpn_filename):
"""
Checks if the VPN connection associated with the given OVPN filename is active.
@@ -69,5 +104,5 @@ def get_external_ip():
print(f"{datetime.datetime.now()} [Main Script]: Invalid response from {service}")
except subprocess.CalledProcessError:
print(f"{datetime.datetime.now()} [Main Script]: Error getting external IP from {service}")
return None # Unable to get external IP
return None # Unable to get external IP