U
    !Xh)                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ G dd de	ZG d	d
 d
eZG dd deZG dd deZdS )a  
VPN Configuration Models

This module contains models for managing VPN configurations specific
to channel-zone relationships. Each VPN technology has its own model
with technology-specific parameters.

Models:
    - VPNConfiguration: Abstract base for VPN configurations
    - IPSecConfiguration: IPSec-specific VPN configuration
    - OpenVPNConfiguration: OpenVPN-specific configuration
    - WireGuardConfiguration: WireGuard-specific configuration
    )models)RegexValidator)gettext_lazy)	BaseModel)ChannelZoneRelationc                   @   sT   e Zd ZdZejeejdededdZ	ej
dededd	ZG d
d dZdS )VPNConfigurationz
    Abstract base for VPN-specific configurations.
    
    Extended by concrete VPN implementation models to store
    technology-specific configuration parameters.
    z%(class)s_configzZone Relationz3The zone relation this VPN configuration applies to)	on_deleterelated_nameverbose_name	help_textTz
Is Enabledz3Whether this VPN configuration is currently enableddefaultr
   r   c                   @   s   e Zd ZdZdS )zVPNConfiguration.MetaTN)__name__
__module____qualname__abstract r   r   ;/var/www/html/Focus/src/apps/channels/models/vpn_configs.pyMeta-   s   r   N)r   r   r   __doc__r   OneToOneFieldr   CASCADE_zone_relationBooleanField
is_enabledr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdZddddgZdddd	d
gZejededdZ	ej
dededededdgdZej
dededededdgdZej
dedededdZej
dedededdZG d d! d!Zd"d# Zd$S )%IPSecConfigurationa  
    IPSec VPN configuration for channel-zone relationships.
    
    Stores IPSec-specific parameters including pre-shared keys,
    subnet configurations, and tunnel settings.
    
    Attributes:
        preshared_key (str): IPSec pre-shared key for authentication
        local_subnet (str): Local network subnet (e.g., 192.168.1.0/24)
        remote_subnet (str): Remote network subnet (e.g., 10.0.0.0/24)
        encryption_algorithm (str): Encryption algorithm to use
        hash_algorithm (str): Hash algorithm for integrity
        
    Relationships:
        - zone_relation: One-to-one with ChannelZoneRelation
        
    Example:
        >>> ipsec_config = IPSecConfiguration.objects.create(
        ...     zone_relation=my_relation,
        ...     preshared_key="secure_key_here",
        ...     local_subnet="192.168.1.0/24",
        ...     remote_subnet="10.0.0.0/24"
        ... )
    )Zaes128zAES-128)Zaes192zAES-192)aes256zAES-256)Z3desZ3DES)sha1zSHA-1)sha256zSHA-256)sha384zSHA-384)sha512zSHA-512)md5MD5zPre-shared Keyz'IPSec pre-shared key for authenticationr
   r      zLocal Subnetz<Local network subnet in CIDR notation (e.g., 192.168.1.0/24)z^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$z0Enter a valid CIDR subnet (e.g., 192.168.1.0/24))regexmessage)
max_lengthr
   r   
validatorszRemote Subnetz:Remote network subnet in CIDR notation (e.g., 10.0.0.0/24)z-Enter a valid CIDR subnet (e.g., 10.0.0.0/24)   r   zEncryption Algorithmz%Encryption algorithm for IPSec tunnelr(   choicesr   r
   r   r   zHash Algorithmz)Hash algorithm for integrity verificationc                   @   s    e Zd ZdZedZedZdS )zIPSecConfiguration.MetaZipsec_configurationszIPSec ConfigurationzIPSec ConfigurationsNr   r   r   db_tabler   r
   verbose_name_pluralr   r   r   r   r      s   r   c                 C   s   d| j  S )NzIPSec: r   selfr   r   r   __str__   s    zIPSecConfiguration.__str__N)r   r   r   r   ZENCRYPTION_ALGORITHMSZHASH_ALGORITHMSr   	TextFieldr   Zpreshared_key	CharFieldr   Zlocal_subnetZremote_subnetZencryption_algorithmhash_algorithmr   r3   r   r   r   r   r   1   sh   r   c                   @   s   e Zd ZdZdddgZddddd	gZejd
ddededdZ	ej
dededdZej
dededdZej
dededdZejdedededdZejdedededdZG dd  d Zd!d" Zd#d$ Zd%S )&OpenVPNConfigurationa  
    OpenVPN configuration for channel-zone relationships.
    
    Stores OpenVPN-specific parameters including certificates,
    configuration files, and connection settings.
    
    Attributes:
        config_file (FileField): OpenVPN configuration file
        ca_cert (str): Certificate Authority certificate
        client_cert (str): Client certificate
        client_key (str): Client private key
        compression (str): Compression algorithm
        cipher (str): Encryption cipher
        
    Relationships:
        - zone_relation: One-to-one with ChannelZoneRelation
        
    Example:
        >>> openvpn_config = OpenVPNConfiguration.objects.create(
        ...     zone_relation=my_relation,
        ...     ca_cert="-----BEGIN CERTIFICATE-----...",
        ...     client_cert="-----BEGIN CERTIFICATE-----...",
        ...     client_key="-----BEGIN PRIVATE KEY-----..."
        ... )
    )nonezNo Compression)lzozLZO Compression)Zlz4zLZ4 Compression)aes-256-cbczAES-256-CBC)zaes-192-cbczAES-192-CBC)zaes-128-cbczAES-128-CBC)zaes-256-gcmzAES-256-GCM)zchacha20-poly1305zChaCha20-Poly1305zvpn_configs/openvpn/TzOpenVPN Config Filez"OpenVPN configuration file (.ovpn))	upload_toblanknullr
   r   zCA Certificatez/Certificate Authority certificate in PEM formatr<   r
   r   zClient Certificatez Client certificate in PEM formatzClient Private Keyz Client private key in PEM formatr*   r9   ZCompressionz(Compression algorithm for OpenVPN tunnelr+      r:   ZCipherz$Encryption cipher for OpenVPN tunnelc                   @   s    e Zd ZdZedZedZdS )zOpenVPNConfiguration.MetaZopenvpn_configurationszOpenVPN ConfigurationzOpenVPN ConfigurationsNr-   r   r   r   r   r      s   r   c                 C   s   d| j  S )Nz	OpenVPN: r0   r1   r   r   r   r3      s    zOpenVPNConfiguration.__str__c                 C   s   t | jo| jo| jS )z
        Check if all required certificates are configured.
        
        Returns:
            bool: True if CA cert, client cert, and key are present
        )boolca_certclient_cert
client_keyr1   r   r   r   has_certificates   s    z%OpenVPNConfiguration.has_certificatesN)r   r   r   r   ZCOMPRESSION_ALGORITHMSZCIPHER_ALGORITHMSr   	FileFieldr   Zconfig_filer4   rA   rB   rC   r5   compressioncipherr   r3   rD   r   r   r   r   r7      sd   r7   c                       s   e Zd ZdZejededdZejdededdZejed	ed
dZ	ej
dededdZejdededdZejdededdZG dd dZdd Z fddZdd Z  ZS )WireGuardConfigurationa  
    WireGuard VPN configuration for channel-zone relationships.
    
    Stores WireGuard-specific parameters including cryptographic keys,
    endpoints, and allowed IP ranges.
    
    Attributes:
        private_key (str): WireGuard private key
        public_key (str): WireGuard public key
        peer_public_key (str): Peer's public key
        endpoint (str): Server endpoint (host:port)
        allowed_ips (str): Comma-separated allowed IP ranges
        persistent_keepalive (int): Keepalive interval in seconds
        
    Relationships:
        - zone_relation: One-to-one with ChannelZoneRelation
        
    Example:
        >>> wireguard_config = WireGuardConfiguration.objects.create(
        ...     zone_relation=my_relation,
        ...     private_key="private_key_here",
        ...     peer_public_key="peer_public_key_here",
        ...     endpoint="vpn.example.com:51820",
        ...     allowed_ips="0.0.0.0/0"
        ... )
    zPrivate Keyz%WireGuard private key for this clientr$   Tz
Public KeyzFWireGuard public key for this client (auto-generated from private key)r>   zPeer Public Keyz&WireGuard public key of the VPN server   ZEndpointzBWireGuard server endpoint (host:port, e.g., vpn.example.com:51820))r(   r
   r   z	0.0.0.0/0zAllowed IPsz9Comma-separated list of allowed IP ranges (CIDR notation)r      zPersistent Keepalivez,Keepalive interval in seconds (0 to disable)c                   @   s    e Zd ZdZedZedZdS )zWireGuardConfiguration.MetaZwireguard_configurationszWireGuard ConfigurationzWireGuard ConfigurationsNr-   r   r   r   r   r   %  s   r   c                 C   s   d| j  S )NzWireGuard: r0   r1   r   r   r   r3   *  s    zWireGuardConfiguration.__str__c                    s   | j r| jst j|| dS )z
        Auto-generate public key from private key if not provided.
        Note: This is a placeholder - actual implementation would require
        WireGuard cryptography library.
        N)private_key
public_keysupersave)r2   argskwargs	__class__r   r   rN   -  s    zWireGuardConfiguration.savec                 C   s    | j s
g S dd | j dD S )z}
        Parse allowed IPs string into a list.
        
        Returns:
            list: List of allowed IP ranges
        c                 S   s   g | ]}|  r|  qS r   )strip).0ipr   r   r   
<listcomp>B  s      z?WireGuardConfiguration.get_allowed_ips_list.<locals>.<listcomp>,)allowed_ipssplitr1   r   r   r   get_allowed_ips_list9  s    z+WireGuardConfiguration.get_allowed_ips_list)r   r   r   r   r   r4   r   rK   rL   Zpeer_public_keyr5   ZendpointrX   PositiveIntegerFieldZpersistent_keepaliver   r3   rN   rZ   __classcell__r   r   rQ   r   rH      sB   rH   N)r   	django.dbr   Zdjango.core.validatorsr   django.utils.translationr   r   apps.common.modelsr   &apps.channels.models.channel_relationsr   r   r   r7   rH   r   r   r   r   <module>   s   [`