import sys import logging from pvpn.server import main # Configure basic logging logging.basicConfig(level=logging.INFO, stream=sys.stdout, format='%(asctime)s - %(levelname)s - %(message)s') def start_ikev2_server_programmatically(): """ Starts a pure Python IKEv2 server using the pvpn library. This is designed to run inside a container where permissions to bind to privileged ports (500/4500) have been pre-granted to the Python executable via setcap. """ # Arguments for the pvpn server original_argv = sys.argv sys.argv = [ "pvpn", "-p", "MySecretPSK123", # The Pre-Shared Key ] print("Starting pure Python IKEv2 VPN server...") print("PSK: MySecretPSK123") print("Listening on UDP ports 500 and 4500.") try: # The main function runs the asyncio loop and the server indefinitely main() except KeyboardInterrupt: print("\nServer stopped by user.") except Exception as e: # Catching any unexpected errors during startup or runtime print(f"An unexpected error occurred: {e}") finally: # Restore sys.argv sys.argv = original_argv if __name__ == '__main__': start_ikev2_server_programmatically()