a
    MbBE                     @   s  U d dl Z d dlZd dlZd dlZd dlZd dlZ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 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 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# ddl$m%Z% ddl$m&Z& ddl$m'Z' e	j(r>d dl)m*Z* d dl)m+Z+ d dl)m,Z, dZ-e.e.dddZ/da0e	j1e	j2e.e3f  e4d< e	j1e	j2e.e3f  dddZ5G dd  d Z6d!e	j2e	j7e.e.f e	j7d" f d#d$d%Z8G d&d' d'Z9dS )(    N)BytesIO)chain)basename)join)adler32   )_log)NotFound)parse_cookie)gen_salt)	send_file)Request)Response   )Console)DebugFrameSummary)DebugTraceback)render_console_html)StartResponse)WSGIApplication)WSGIEnvironmenti:	 )pinreturnc                 C   s$   t |  ddd d d S )Nz added saltutf-8replace   )hashlibsha1encode	hexdigest)r    r    T/var/www/html/diaryflask/venv/lib/python3.9/site-packages/werkzeug/debug/__init__.pyhash_pin&   s    r"   _machine_idr   c                  C   s4   t d urt S tjtjttf  ddd} |  a t S )Nr$   c               	   S   s  d} dD ]f}z:t |d}|  }W d    n1 s:0    Y  W n ty\   Y qY n0 |r| |7 }  qpqzHt dd*}| |  dd 7 } W d    n1 s0    Y  W n ty   Y n0 | r| S zNddlm}m} |g d	|d
 d }t	
d|}|d ur |dW S W n ttfy:   Y n0 tjdkrdd l}z|||jdd|j|jB L}	||	d\}
}||jkr|
dW  d    W S |
W  d    W S 1 s0    Y  W n ty   Y n0 d S )N    )z/etc/machine-idz/proc/sys/kernel/random/boot_idrbz/proc/self/cgroup   /r   r   )PopenPIPE)Zioregz-cZIOPlatformExpertDevicez-d2)stdouts   "serial-number" = <([^>]+)r   win32zSOFTWARE\Microsoft\CryptographyZMachineGuidr   )openreadlinestripOSError
rpartition
subprocessr(   r)   communicateresearchgroupImportErrorsysplatformwinregOpenKeyHKEY_LOCAL_MACHINEZKEY_READZKEY_WOW64_64KEYQueryValueExREG_SZr   )linuxfilenamefvaluer(   r)   dumpmatchr:   ZrkZguidZ	guid_typer    r    r!   	_generate3   s\    .
<

*z!get_machine_id.<locals>._generate)r#   tOptionalUnionstrbytes)rE   r    r    r!   get_machine_id-   s
    CrK   c                   @   s<   e Zd ZdZejeejf dddZeejdddZ	dS )	_ConsoleFramez]Helper class so that we can reuse the frame console code for the
    standalone console.
    )	namespacec                 C   s   t || _d| _d S )Nr   )r   consoleid)selfrM   r    r    r!   __init__   s    
z_ConsoleFrame.__init__)coder   c                 C   s   | j |S N)rN   eval)rP   rR   r    r    r!   rT      s    z_ConsoleFrame.evalN)
__name__
__module____qualname____doc__rF   DictrI   AnyrQ   rT   r    r    r    r!   rL   z   s   rL   r   NN)appr   c              	      s  t jd}d}d|dkr dS |durJ|dd rJd|v rF|}n|t| dtt| j	j
}zt }W n ttfy   d}Y n0 tj|}||t| dt| jt|d	dg}tt t g}t }t||D ](}	|	sqt|	tr|	d
}	||	 q|d d| dd  }
du rT|d t| dddd |du rdD ]D t  dkrbd  fddt!dt D } qqb}||
fS )aQ  Given an application object this returns a semi-stable 9 digit pin
    code and a random key.  The hope is that this is stable between
    restarts to not make debugging particularly frustrating.  If the pin
    was forcefully disabled this returns `None`.

    Second item in the resulting tuple is the cookie name for remembering.
    ZWERKZEUG_DEBUG_PINNoffr[   - rV   rU   __file__r   s
   cookiesaltZ__wzd   s   pinsalt   Z09d	   )         r   c                 3   s&   | ]}||     d V  qdS )0N)rjust).0xZ
group_sizenumr    r!   	<genexpr>   s   z*get_pin_and_cookie_name.<locals>.<genexpr>)"osenvirongetr   isdigitgetattrrF   castobject	__class__rV   getpassgetuserr7   KeyErrorr8   modulestyperU   rI   uuidgetnoderK   r   r   r   
isinstancer   updater   intlenr   range)r\   r   rvmodnameusernamemodZprobably_public_bitsZprivate_bitshbitZcookie_namer    rk   r!   get_pin_and_cookie_name   sT    










r   c                   @   sP  e Zd ZU dZeed< eed< d-d	eeeejej	g ej
eejf f  eeedd
	ddZeeje dddZejeddddZeedddZddeje dddZeeejeef edddZeedddZeeedd d!Zdeje d"d#d$Zddd%d&Zeedd'd(Zedd)d*Zddej e dd+d,Z!dS ).DebuggedApplicationa  Enables debugging support for a given application::

        from werkzeug.debug import DebuggedApplication
        from myapp import app
        app = DebuggedApplication(app, evalex=True)

    The `evalex` keyword argument allows evaluating expressions in a
    traceback's frame context.

    :param app: the WSGI application to run debugged.
    :param evalex: enable exception evaluation feature (interactive
                   debugging).  This requires a non-forking server.
    :param request_key: The key that points to the request object in this
                        environment.  This parameter is ignored in current
                        versions.
    :param console_path: the URL for a general purpose console.
    :param console_init_func: the function that is executed before starting
                              the general purpose console.  The return value
                              is used as initial namespace.
    :param show_hidden_frames: by default hidden traceback frames are skipped.
                               You can show them by setting this parameter
                               to `True`.
    :param pin_security: can be used to disable the pin based security system.
    :param pin_logging: enables the logging of the pin system.
    _pin_pin_cookieFwerkzeug.request/consoleNTr   )	r\   evalexrequest_keyconsole_pathconsole_init_funcshow_hidden_framespin_securitypin_loggingr   c	           	      C   s   |sd }|| _ || _i | _|| _|| _|| _|| _td| _d| _	|| _
|rtjddkr|rtdd | jd u rtdd qtdd	| j nd | _d S )
Nra   r   ZWERKZEUG_RUN_MAINtruewarningz * Debugger is active!z- * Debugger PIN disabled. DEBUGGER UNSECURED!infoz * Debugger PIN: %s)r\   r   framesr   r   r   r   r   secret_failed_pin_authr   rn   ro   rp   r   r   )	rP   r\   r   r   r   r   r   r   r   r    r    r!   rQ      s&    


zDebuggedApplication.__init__r$   c                 C   s&   t | ds t| j}|\| _| _| jS )Nr   hasattrr   r\   r   r   rP   Z
pin_cookier    r    r!   r     s    

zDebuggedApplication.pin)rB   r   c                 C   s
   || _ d S rS   )r   )rP   rB   r    r    r!   r   %  s    c                 C   s&   t | ds t| j}|\| _| _| jS )zThe name of the pin cookie.r   r   r   r    r    r!   pin_cookie_name)  s    

z#DebuggedApplication.pin_cookie_namer   r   )ro   start_responser   c           
      c   s  d}z,|  ||}|E dH  t|dr.|  W n ty } zt|drT|  t|d| j d}|jD ]}|| jt|< qlt	| 
|}|j| j| j|d}t|ddd}	z|	||E dH  W n  ty   |d	 d
 Y n0 |d	 d|  W Y d}~n
d}~0 0 dS )z6Run the application and conserve the traceback frames.Ncloser   )skiphide)r   r   evalex_trustedi  	text/html)statusmimetypezwsgi.errorszpDebugging middleware caught exception in streamed response at a point where response headers were already sent.
r_   )r\   r   r   	Exceptionr   r   Z
all_framesr   rO   boolcheck_pin_trustZrender_debugger_htmlr   r   r   writer   Zrender_traceback_text)
rP   ro   r   Zapp_iteretbframe
is_trustedhtmlresponser    r    r!   debug_application1  s4    




z%DebuggedApplication.debug_application)requestcommandr   r   c                 C   s   t ||ddS )zExecute a command in a console.r   r   )r   rT   )rP   r   r   r   r    r    r!   execute_command[  s    z#DebuggedApplication.execute_command)r   r   c                 C   sh   d| j vrB| jdu ri }nt|  }|d| j t|| j d< t| |j}t	t
| j|dddS )zDisplay a standalone shell.r   Nr\   )r   r   r   r   )r   r   dict
setdefaultr\   rL   r   r   ro   r   r   r   )rP   r   nsr   r    r    r!   display_consoled  s    

z#DebuggedApplication.display_console)r   r@   r   c                 C   sp   t dt|}ztt|}W n ty6   t  Y S 0 |du rFt S tt|d@ }t	t
||j||dS dS )z0Return a static resource from the shared folder.ZsharedNl    )Zdownload_nameetag)r   r   pkgutilget_data__package__r0   r	   rI   r   r   r   ro   )rP   r   r@   pathdatar   r    r    r!   get_resources  s    z DebuggedApplication.get_resource)ro   r   c                 C   sp   | j du rdS t|| j}|r*d|vr.dS |dd\}}| sJdS |t| j kr\dS t t t	|k S )a!  Checks if the request passed the pin test.  This returns `True` if the
        request is trusted on a pin/cookie basis and returns `False` if not.
        Additionally if the cookie's stored pin hash is wrong it will return
        `None` so that appropriate action can be taken.
        NT|Fr   )
r   r
   rp   r   splitrq   r"   timePIN_TIMEr   )rP   ro   valtsZpin_hashr    r    r!   r     s    
z#DebuggedApplication.check_pin_trustc                 C   s*   t | jdkrdnd |  jd7  _d S )Nrd   g      @g      ?r   )r   sleepr   rP   r    r    r!   _fail_pin_auth  s    z"DebuggedApplication._fail_pin_authc           	      C   s   d}d}|  |j}tt| j}d}|du r<|   d}nT|rFd}nJ| jdkrVd}n:|jd }|	 
dd|
ddkrd| _d}n|   tt||d	d
d}|r|j| jtt  dt| dd|jd n|r|| j |S )zAuthenticates with the pin.FNT
   r   r^   r_   r   )auth	exhaustedzapplication/jsonr   r   ZStrict)httponlysamesitesecure)r   ro   rF   rs   rI   r   r   r   argsr/   r   r   jsondumps
set_cookier   r   r   r"   Z	is_secureZdelete_cookie)	rP   r   r   r   trustr   
bad_cookieZentered_pinr   r    r    r!   pin_auth  s@    

zDebuggedApplication.pin_authc                 C   s0   | j r(| jdur(tdd tdd| j tdS )zLog the pin if needed.Nr   z= * To enable the debugger you need to enter the security pin:z * Debugger pin code: %sr_   )r   r   r   r   r   r    r    r!   log_pin_request  s    z#DebuggedApplication.log_pin_requestc           	      C   s  t |}| j}|jddkr|jd}|jd}|jd}| j|jjdtd}|dkrt|rt| ||}nr|d	kr|| jkr| |}nT|d
kr|| jkr| 	 }n8| j
r|dur|dur| j|kr| |r| |||}n,| j
r| jdur|j| jkr| |}|||S )zDispatch the requests.Z__debugger__yescmdrA   sfrm)rz   resourceZpinauthZprintpinN)r   r   r   rp   r   r   r   r   r   r   r   r   r   r   r   r   )	rP   ro   r   r   r   r   argr   r   r    r    r!   __call__  s@    


zDebuggedApplication.__call__)Fr   r   NFTT)"rU   rV   rW   rX   rI   __annotations__r   rF   rG   CallablerY   rZ   rQ   propertyr   setterr   IteratorrJ   r   r   rH   r   rL   r   r   r   r   r   r   r   r   Iterabler   r    r    r    r!   r      sX   
       #,	3
r   ):rv   r   r   rn   r   r4   r8   r   typingrF   r{   ior   	itertoolsr   os.pathr   r   zlibr   	_internalr   
exceptionsr	   httpr
   securityr   utilsr   Zwrappers.requestr   Zwrappers.responser   rN   r   Ztbtoolsr   r   r   TYPE_CHECKINGZ_typeshed.wsgir   r   r   r   rI   r"   r#   rG   rH   rJ   r   rK   rL   Tupler   r   r    r    r    r!   <module>   sL   
MV