U
    h>                     @   s   d Z ddlZddlmZ ddlmZmZ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  Standalone Configuration Models

This module contains models for managing VPN and FTP configurations
independently of channel-zone relationships. These configurations can be
reused across multiple channels and zones.

Models:
    - StandaloneVPNConfiguration: Reusable VPN configurations
    - StandaloneFTPConfiguration: Reusable FTP configurations
    - ZoneVPNConfiguration: Links zones to VPN configurations
    - ZoneFTPConfiguration: Links zones to FTP configurations
    N)models)RegexValidatorMinValueValidatorMaxValueValidator)gettext_lazy)	BaseModelc                	   @   s  e Zd ZdZdedfdedfdedfgZdd	d
dgZejde	j
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ededdZejdededdZejddededdZejdded ed!dZejded"ed#dZejd$ded%ed&ed'ed(d)gd*Zejd$ded+ed,ed'ed-d)gd*Zejdeded.ed/d0Zejd1dded2ed3d4Zejded5ed6dZejded7ed8dZejded9ed:dZejded;ed<d=Zejd>ded?ed@dZ ejd>dedAedBdZ!ejd>dedCedDdZ"ejddedEedFdZ#ejdedGedHdZ$ej%dde&dIe'dJgedKedLdMZ(ejdedNedOd=Z)G dPdQ dQZ*dRdS Z+dTdU Z,dVS )WStandaloneVPNConfigurationaD  
    Standalone VPN configuration that can be reused across multiple channels and zones.
    
    This model allows creating VPN configurations independently of specific
    channel-zone relationships, enabling better configuration management and reuse.
    
    Attributes:
        name (str): Human-readable name for the configuration
        description (str): Optional description
        vpn_type (str): Type of VPN (IPSec, OpenVPN, WireGuard)
        server_address (str): VPN server address
        username/password (str): Authentication credentials
        
    Type-specific fields:
        IPSec: preshared_key, local_subnet, remote_subnet, encryption_algorithm
        OpenVPN: config_file, ca_cert, client_cert, client_key, compression
        WireGuard: private_key, public_key, peer_public_key, endpoint, allowed_ips
    ipseczIPSec TunnelopenvpnOpenVPN	wireguard	WireGuard)aes128zAES-128)aes192zAES-192)aes256zAES-256)3des3DESTFprimary_keydefaulteditable   Configuration Namez.Human-readable name for this VPN configuration
max_lengthverbose_name	help_textDescriptionz.Optional description of this VPN configurationblankr   r      zVPN TypezType of VPN technology to use)r   choicesr   r   zServer Addressz!VPN server IP address or hostnamed   UsernamezUsername for VPN authenticationr   r   r   r   PasswordzPassword for VPN authenticationzIPSec Pre-shared Keyz'Pre-shared key for IPSec authentication   zLocal Subnetz%Local network subnet in CIDR notationz^(\d{1,3}\.){3}\d{1,3}/\d{1,2}$z<Enter a valid subnet in CIDR notation (e.g., 192.168.1.0/24))regexmessage)r   r   r   r   
validatorszRemote Subnetz&Remote network subnet in CIDR notationz9Enter a valid subnet in CIDR notation (e.g., 10.0.0.0/24)zEncryption Algorithmz%Encryption algorithm for IPSec tunnel)r   r!   r   r   r   zvpn/openvpn/configs/zOpenVPN Config Filez"OpenVPN configuration file (.ovpn))	upload_tor   nullr   r   zCA Certificatez)Certificate Authority certificate contentzClient CertificatezClient certificate contentzClient Private KeyzClient private key contentzEnable Compressionz!Whether to enable LZO compressionr   r   r   ,   zPrivate Keyz&WireGuard private key (base64 encoded)z
Public Keyz%WireGuard public key (base64 encoded)zPeer Public Keyz*WireGuard peer public key (base64 encoded)Endpointz-WireGuard endpoint (IP:port or hostname:port)zAllowed IPsz)Comma-separated list of allowed IP ranges     zPersistent Keepalivez(Keepalive interval in seconds (optional))r+   r   r)   r   r   	Is Activez(Whether this VPN configuration is activec                   @   sD   e Zd ZdZdgZedZedZej	ddgdej	dgdgZ
dS )	zStandaloneVPNConfiguration.MetaZstandalone_vpn_configurationsnamezStandalone VPN ConfigurationzStandalone VPN Configurationsvpn_type	is_activefieldsN__name__
__module____qualname__db_tableordering_r   verbose_name_pluralr   Indexindexes rA   rA   B/var/www/html/Focus/src/apps/channels/models/standalone_configs.pyMeta   s   rC   c                 C   s   | j  d|   dS )N ())r2   get_vpn_type_displayselfrA   rA   rB   __str__   s    z"StandaloneVPNConfiguration.__str__c                 C   sp   | j dkr | j| j| j| jdS | j dkrD| j| j| j| j| j	dS | j dkrl| j
| j| j| j| j| jdS i S )z+Get configuration specific to the VPN type.r	   )preshared_keylocal_subnetremote_subnetencryption_algorithmr
   )config_fileca_certclient_cert
client_keycompressionr   )private_key
public_keypeer_public_keyendpointallowed_ipspersistent_keepalive)r3   ipsec_preshared_keyipsec_local_subnetipsec_remote_subnetipsec_encryption_algorithmopenvpn_config_fileopenvpn_ca_certopenvpn_client_certopenvpn_client_keyopenvpn_compressionwireguard_private_keywireguard_public_keywireguard_peer_public_keywireguard_endpointwireguard_allowed_ipswireguard_persistent_keepaliverG   rA   rA   rB   get_type_specific_config   s,    


z3StandaloneVPNConfiguration.get_type_specific_configN)-r8   r9   r:   __doc__r=   	VPN_TYPESENCRYPTION_ALGORITHMSr   	UUIDFielduuiduuid4id	CharFieldr2   	TextFielddescriptionr3   server_addressusernamepasswordrY   r   rZ   r[   r\   	FileFieldr]   r^   r_   r`   BooleanFieldra   rb   rc   rd   re   rf   PositiveIntegerFieldr   r   rg   r4   rC   rI   rh   rA   rA   rA   rB   r      s2  


	
r   c                   @   s  e Zd ZdZejdej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dZej	de
de
ddZejdededge
de
ddZej	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%ge
d&e
d'dZej	dde
d(e
d)d*Zej	dde
d+e
d,d*Zej	dde
d-e
d.d*Zejde
d/e
d0d ZG d1d2 d2Zd3d4 Zd5d6 Z d7d8 Z!d9S ):StandaloneFTPConfigurationa  
    Standalone FTP configuration that can be reused across multiple channels and zones.
    
    This model allows creating FTP configurations independently of specific
    channel-zone relationships, enabling better configuration management and reuse.
    
    Attributes:
        name (str): Human-readable name for the configuration
        description (str): Optional description
        host (str): FTP server hostname or IP
        username/password (str): FTP credentials
        port (int): FTP server port
        root_directory (str): Root directory path
        passive_mode (bool): Whether to use passive mode
        ssl_enabled (bool): Whether to use FTPS
        timeout (int): Connection timeout in seconds
    TFr   r   r   z.Human-readable name for this FTP configurationr   r   z.Optional description of this FTP configurationr   zFTP Hostz!FTP server hostname or IP addressr"   r#   zUsername for FTP authenticationr%   zPassword for FTP authentication   r/   r0   ZPortz$FTP server port number (default: 21))r   r)   r   r   i  /zRoot Directoryz%Root directory path on the FTP server)r   r   r   r   zPassive Modez/Whether to use passive mode for FTP connectionsr,   zSSL/TLS Enabledz&Whether to use FTPS (FTP over SSL/TLS)      i,  ZTimeoutzConnection timeout in secondsz	Zone Namez8Zone identifier for FTP configuration (e.g., 2005, 2008)r$   zVerification Numberz>Verification number for FTP configuration (e.g., 00001, 00002)zPlatform Namez(Platform identifier (e.g., CJI, TDF, 2M)r1   z(Whether this FTP configuration is activec                   @   sP   e Zd ZdZdgZedZedZej	ddgdej	dgdej	dgdgZ
d	S )
zStandaloneFTPConfiguration.MetaZstandalone_ftp_configurationsr2   zStandalone FTP ConfigurationzStandalone FTP Configurationshostportr5   r4   Nr7   rA   rA   rA   rB   rC   _  s   rC   c                 C   s   | j  d| j d| j dS )NrD   :rE   )r2   r~   r   rG   rA   rA   rB   rI   j  s    z"StandaloneFTPConfiguration.__str__c                 C   s4   | j r
dnd}| d| j d| j d| j | j S )zGet FTP connection string.ftpsftpz://@r   )ssl_enabledrt   r~   r   root_directory)rH   protocolrA   rA   rB   get_connection_stringm  s    z0StandaloneFTPConfiguration.get_connection_stringc                 C   s   dS )z<Test FTP connection (placeholder for actual implementation).TrA   rG   rA   rA   rB   test_connectionr  s    z*StandaloneFTPConfiguration.test_connectionN)"r8   r9   r:   ri   r   rl   rm   rn   ro   rp   r=   r2   rq   rr   r~   rt   ru   rx   r   r   r   r   rw   passive_moder   timeoutZ	zone_nameZverification_numberZplatform_namer4   rC   rI   r   r   rA   rA   rA   rB   ry      s   ry   c                   @   s   e Zd ZdZejdejdededdZeje	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dS )ZoneVPNConfigurationz
    Links ChannelZone to StandaloneVPNConfiguration.
    
    This model allows zones to have default VPN configurations
    that can be inherited by channels in that zone.
    ChannelZoneZvpn_configurationsZonez*The zone this VPN configuration applies to	on_deleterelated_namer   r   zone_assignmentszVPN Configurationz*The VPN configuration to use for this zoneF
Is Defaultz:Whether this is the default VPN configuration for the zoner,   r/   Priority0Priority level (higher number = higher priority)c                   @   sP   e Zd ZdZddgZedZedZddggZe	j
ddgd	e	j
d
gd	gZdS )zZoneVPNConfiguration.MetaZzone_vpn_configurations	-priority
zone__namezZone VPN ConfigurationzZone VPN Configurationszone
vpn_config
is_defaultr5   priorityNr8   r9   r:   r;   r<   r=   r   r>   unique_togetherr   r?   r@   rA   rA   rA   rB   rC     s   
rC   c                 C   s   | j j d| jj S Nz - )r   r2   r   rG   rA   rA   rB   rI     s    zZoneVPNConfiguration.__str__N)r8   r9   r:   ri   r   
ForeignKeyCASCADEr=   r   r   r   rw   r   rx   r   rC   rI   rA   rA   rA   rB   r   x  s6   r   c                   @   s   e Zd ZdZejdejdededdZeje	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dS )ZoneFTPConfigurationz
    Links ChannelZone to StandaloneFTPConfiguration.
    
    This model allows zones to have default FTP configurations
    that can be inherited by channels in that zone.
    r   Zftp_configurationsr   z*The zone this FTP configuration applies tor   r   zFTP Configurationz*The FTP configuration to use for this zoneFr   z:Whether this is the default FTP configuration for the zoner,   r/   r   r   c                   @   sP   e Zd ZdZddgZedZedZddggZe	j
ddgd	e	j
d
gd	gZdS )zZoneFTPConfiguration.MetaZzone_ftp_configurationsr   r   zZone FTP ConfigurationzZone FTP Configurationsr   
ftp_configr   r5   r   Nr   rA   rA   rA   rB   rC     s   
rC   c                 C   s   | j j d| jj S r   )r   r2   r   rG   rA   rA   rB   rI     s    zZoneFTPConfiguration.__str__N)r8   r9   r:   ri   r   r   r   r=   r   ry   r   rw   r   rx   r   rC   rI   rA   rA   rA   rB   r     s6   r   )ri   rm   	django.dbr   django.core.validatorsr   r   r   django.utils.translationr   r=   apps.common.modelsr   r   ry   r   r   rA   rA   rA   rB   <module>   s    b 0