File size: 1,288 Bytes
2206dba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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()