o
    i                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 e j
eZe j
eZe e dZdZdZdZd	ZeG d
d dZdedefddZdedededB fddZdedejjdedB fddZdd ZdS )    N)Literal)	dataclass)
log_outputz./IPz https://www.hover.com/api/dns/{}z!https://www.hover.com/api/domainszhttps://www.hover.com/api/dnsz,https://www.hover.com/api/control_panel/dns/c                   @   s^   e Zd ZU eed< eed< eed< ed ed< dd Zdd	 Zd
efddZd
efddZ	dS )	DnsRecorddomainhostvalue)AAAAACNAMEMXSRVrecord_typec                 C   
   | j dkS )Nr	   r   self r   3/home/seanl/mnt/public/Repos/hover-dyn-dns/./dns.pyis_ip4      
zDnsRecord.is_ip4c                 C   r   )Nr
   r   r   r   r   r   is_ip6   r   zDnsRecord.is_ip6returnc                 C   s$   |   dkr	| jS | j d| j S )N@.)get_hostr   r   r   r   r   r   get_fqdn"   s   zDnsRecord.get_fqdnc                 C   s   | j d u rdS | j S )Nr   )r   r   r   r   r   r   (   s   
zDnsRecord.get_hostN)
__name__
__module____qualname__str__annotations__r   r   r   r   r   r   r   r   r   r      s   
 r   use_ipv6r   c                 C   s    | rdnd}t j|dd}|jS )z@
    Retrieves the external IP address using the ipify API.
    zhttps://api6.ipify.orgzhttps://api.ipify.orgT)verify)requestsgettext)r"   apiresponser   r   r   get_external_ip/   s   r)   r   c                 C   sf   z't | d|rt jjnt jjD ]}t|d d tr$|d d   W S qW dS  t jy2   Y dS w )z,Retrieves the IP address for a given domain.P      r   N)socketgetaddrinfoAddressFamilyAF_INET6AF_INET
isinstancer    gaierror)r   r"   addrinfor   r   r   
get_dns_ip7   s   "r4   recordcookiesdns_entry_idc              	   C   s   |r"t j}d| j || j| j| jddddgd| jddd}nt j}| j| j| jdd	d| j d
}|t||dd}tt	
| td|j  td|j  |S )z/
    Updates a new or existing DNS record.
    zdomain-900FT)idnametypecontentttl
is_default
can_revert)r9   dns_records)r<   r=   )r   fields)r:   r<   r;   r=   )
dns_recordr9   )urljsonr6   r#   z!DNS update response status code: zDNS update response content: )r$   putr   r   r   r   postDNS_SUBENTRIES_URLr   DNS_UPDATE_URLformatstatus_coder<   )r5   r6   r7   http_method	json_datar(   r   r   r   submit_dns_entry_updateA   s<   


rM   c                 C   sH   t jt| dd}|jdkr| S |d|j d|j | td dS )z4
    Retrieves the DNS entries for the account.
    T)r6   r#      z-Failed to retrieve DNS entries. Status code: z, Response text: r+   N)r$   r%   DNS_ENTRIES_URLrJ   rD   r&   exit)r6   loggingr(   r   r   r   get_dns_entriesn   s
   
rR   )osr,   r$   requests.cookiestypingr   dataclassesr   logging_outr   pathabspath__file__dirnamednamechdirIP_FILErH   DOMAIN_CHECK_URLrO   rG   r   boolr    r)   r4   r6   RequestsCookieJarrM   rR   r   r   r   r   <module>   s*    

-