o
    NK&hF%                  	   @  sd  d dl mZ d dlZd dlZd dlZd dlmZm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mZmZmZ d d	lm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  d dl!m"Z"m#Z# zd dl$Z$dZ%W n e&e'fy   edZ$dZ%Y nw erd dl(m)Z) g dZ*d)ddZ+d*d!d"Z,G d#d$ d$eZ-G d%d& d&e-Z.G d'd( d(e-Z/dS )+    )annotationsN)ABCabstractmethod)suppress)Path)mkdtemp)
ModuleType)TYPE_CHECKINGOptionalUnioncast)Mode)loading)DEFAULT_LOCAL_TLS_CERTDEFAULT_LOCAL_TLS_KEYLocalCertCreator)SanicException)Default)
CertSimpleSanicSSLContextTtrustmeF)Sanic)zECDHE-ECDSA-CHACHA20-POLY1305zECDHE-ECDSA-AES256-GCM-SHA384zECDHE-ECDSA-AES128-GCM-SHA256zECDHE-RSA-CHACHA20-POLY1305zECDHE-RSA-AES256-GCM-SHA384zECDHE-RSA-AES128-GCM-SHA256
maybe_pathUnion[Path, str]tmpdirOptional[Path]returnr   c                 C  s6   t | tr| S t| }| s|std||  S |S )Nz$Reached an unknown state. No tmpdir.)
isinstancer   existsRuntimeError)r   r   path r!   J/var/www/html/venv/lib/python3.10/site-packages/sanic/http/tls/creators.py
_make_path2   s   
r#   appr   sslOptional[ssl.SSLContext]ssl.SSLContextc                 C  sR   |r|S | j jtju rtdt| tt| j	j
| j	j| j	j}|| j	j}|S )Na?  Cannot run Sanic as an HTTPS server in PRODUCTION mode without passing a TLS certificate. If you are developing locally, please enable DEVELOPMENT mode and Sanic will generate a localhost TLS certificate. For more information please see: https://sanic.dev/en/guide/deployment/development.html#automatic-tls-certificate.)statemoder   
PRODUCTIONr   CertCreatorselectr   r   configLOCAL_CERT_CREATORLOCAL_TLS_KEYLOCAL_TLS_CERTgenerate_cert	LOCALHOST)r$   r%   creatorcontextr!   r!   r"   get_ssl_context?   s   	r5   c                   @  sN   e Zd ZdddZedddZedd
dZedddZe	dddZ
dS )r+   r   Nonec                 C  s   || _ || _|| _d | _t| jtst| jtrtt | _t| jtr&tn| j}t| jtr1t	n| j}t
|| j| _t
|| j| _d S N)r$   keycertr   r   r   r   r   r   r   r#   key_path	cert_path)selfr$   r8   r9   r!   r!   r"   __init__Z   s    

zCertCreator.__init__c                 C     d S r7   r!   r<   r!   r!   r"   check_supportedq      zCertCreator.check_supported	localhoststrr'   c                 C  r>   r7   r!   )r<   rB   r!   r!   r"   r1   u   rA   zCertCreator.generate_certr$   r   cert_creatorr   c           	   
   C  sT   d }t tjfttjff}|D ]\}}| |||||||}|r! nq|s(td|S )NzSanic could not find package to create a TLS certificate. You must have either mkcert or trustme installed. See https://sanic.dev/en/guide/deployment/development.html#automatic-tls-certificate for more details.)MkcertCreatorr   MKCERTTrustmeCreatorTRUSTME_try_selectr   )	clsr$   rD   local_tls_keylocal_tls_certr3   cert_creator_optionscreator_classlocal_creatorr!   r!   r"   r,   y   s.   	zCertCreator.selectr3   Optional[CertCreator]rN   type[CertCreator]creator_requirementcreator_requestedc                 C  sT   |s|t jur||ur|S || ||}z|  W |S  ty)   ||u r& Y d S w r7   )r   AUTOr@   r   )r$   r3   rN   rR   rS   rK   rL   instancer!   r!   r"   rI      s   


zCertCreator._try_selectNr   r6   rB   rC   r   r'   )r$   r   rD   r   r   r+   )
r$   r   r3   rP   rN   rQ   rR   r   rS   r   )__name__
__module____qualname__r=   r   r@   r1   classmethodr,   staticmethodrI   r!   r!   r!   r"   r+   Y   s    
&r+   c                   @      e Zd ZdddZddd	Zd
S )rE   r   r6   c              
   C  sD   zt jddgdt jt jd W d S  ty! } ztd|d }~ww )Nmkcertz-helpT)checkstderrstdouta  Sanic is attempting to use mkcert to generate local TLS certificates since you did not supply a certificate, but one is required. Sanic cannot proceed since mkcert does not appear to be installed. Alternatively, you can use trustme. Please install mkcert, trustme, or supply TLS certificates to proceed. Installation instructions can be found here: https://github.com/FiloSottile/mkcert.
Find out more information about your options here: https://sanic.dev/en/guide/deployment/development.html#automatic-tls-certificate)
subprocessrunDEVNULL	Exceptionr   )r<   er!   r!   r"   r@      s   zMkcertCreator.check_supportedrB   rC   r'   c              
     s   z[ j  sPd}t|" ddt jdt j |g}tj|dtjtjdd}W d    n1 s1w   Y  t	j
ddt|d	    t	j
  t	j
|j
 W  jj fd
d}n jj fd
d}w t j  j}d|jd< ||jd< t| |S )NzGenerating TLS certificater^   z	-key-filez
-cert-fileT)r_   ra   r`   text    c                    sV    j r)tt  j   j  W d    n1 sw   Y   j   d S d S r7   )r   r   FileNotFoundErrorr:   unlinkr;   rmdir)_r?   r!   r"   cleanup   s   

z,MkcertCreator.generate_cert.<locals>.cleanupr3   rB   )r;   r   r   rC   r:   rb   rc   PIPESTDOUTsysra   writelenflushr$   main_process_stopr   sanicr   create_from_ssl_context)r<   rB   messagecmdrespro   r4   r!   r?   r"   r1      s@   





zMkcertCreator.generate_certNrV   rW   rX   rY   rZ   r@   r1   r!   r!   r!   r"   rE      s    
rE   c                   @  r]   )rG   r   r6   c                 C  s   t stdd S )Na	  Sanic is attempting to use trustme to generate local TLS certificates since you did not supply a certificate, but one is required. Sanic cannot proceed since trustme does not appear to be installed. Alternatively, you can use mkcert. Please install mkcert, trustme, or supply TLS certificates to proceed. Installation instructions can be found here: https://github.com/python-trio/trustme.
Find out more information about your options here: https://sanic.dev/en/guide/deployment/development.html#automatic-tls-certificate)TRUSTME_INSTALLEDr   r?   r!   r!   r"   r@      s
   zTrustmeCreator.check_supportedrB   rC   r'   c                 C  s   t ttj}| j | j d|_t	
 }||}|| || |jt| j  |jt| j  d|jd< ||jd< |S )N)r9   r8   r   r3   rB   )r   rx   r%   
SSLContextPROTOCOL_TLS_SERVERr;   absoluter:   rw   r   CA
issue_certconfigure_certconfigure_trustcert_pemwrite_to_pathrC   private_key_and_cert_chain_pem)r<   rB   r4   caserver_certr!   r!   r"   r1     s"   





zTrustmeCreator.generate_certNrV   rW   r|   r!   r!   r!   r"   rG      s    
rG   )r   r   r   r   r   r   )r$   r   r%   r&   r   r'   )0
__future__r   r%   rb   rr   abcr   r   
contextlibr   pathlibr   tempfiler   typesr   typingr	   r
   r   r   sanic.application.constantsr   sanic.application.spinnerr   sanic.constantsr   r   r   sanic.exceptionsr   sanic.helpersr   sanic.http.tls.contextr   r   r   r}   ImportErrorModuleNotFoundErrorrw   r   CIPHERS_TLS12r#   r5   r+   rE   rG   r!   r!   r!   r"   <module>   s>    


cA