o
    2gF                     @   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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"rd dl#m$Z$ d dl#m%Z% d dl#m&Z& dZ'de(de(fddZ)da*e
j+e
j,e(e-f  e.d< de
j+e
j,e(e-f  fddZ/G dd dZ0ddde
j,e
j1e(e(f e
j1d  f fd!d"Z2G d#d$ d$Z3dS )%    N)chain)basename)join   )_log)parse_cookie)gen_salt)Request)Response   )Console)Frame)get_current_traceback)render_console_html)	Traceback)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   f/var/www/mastermindingenieria.com/MONITOR/venv/lib/python3.10/site-packages/werkzeug/debug/__init__.pyhash_pin$   s   $r   _machine_idc                  C   s4   t d urt S dtjtjttf  fdd} |  a t S )Nr   c               	   S   s  d} dD ]1}zt |d}|  }W d    n1 sw   Y  W n	 ty,   Y qw |r5| |7 }  nqz$t dd}| |  dd 7 } W d    n1 sTw   Y  W n	 tyc   Y nw | rh| S z&ddlm}m} |g d	|d
 d }t	
d|}|d ur|dW S W n ttfy   Y nw zdd l}W n
 ty   Y d S w z=||jdd|j|jB %}	||	d\}
}||jkr|
dW  d    W S |
W  d    W S 1 sw   Y  W d S  ty   Y d S w )N    )z/etc/machine-idz/proc/sys/kernel/random/boot_idrbz/proc/self/cgroup   /r   r   )PopenPIPE)ioregz-cIOPlatformExpertDevicez-d2)stdouts   "serial-number" = <([^>]+)r   zSOFTWARE\Microsoft\CryptographyMachineGuidr   )openreadlinestripOSError
rpartition
subprocessr$   r%   communicateresearchgroupImportErrorwinregOpenKeyHKEY_LOCAL_MACHINEKEY_READKEY_WOW64_64KEYQueryValueExREG_SZr   )linuxfilenamefvaluer$   r%   dumpmatchr6   rkguid	guid_typer   r   r   	_generate1   s~   

$z!get_machine_id.<locals>._generate)r    tOptionalUnionstrbytes)rF   r   r   r   get_machine_id+   s
   ErL   c                   @   s*   e Zd ZdZdejeejf f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)selfrN   r   r   r   __init__   s   

z_ConsoleFrame.__init__N)	__name__
__module____qualname____doc__rG   DictrJ   AnyrR   r   r   r   r   rM   z   s    rM   appr   NNc              	      s  t jd}d}d|dkrdS |dur%|dd r%d|v r#|}n|t| dtt| j	j
}zt }W n ttfyD   d}Y nw tj|}||t| dt| jt|d	dg}tt t g}t }t||D ]}	|	ssqnt|	tr}|	d
}	||	 qn|d d| dd  }
du r|d t| dddd |du rdD ]" t  dkrd  fddt!dt D } ||
fS q}||
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.
    WERKZEUG_DEBUG_PINNoffrZ   - rT   rS   __file__r   s
   cookiesalt__wzd   s   pinsalt   09d	   )         r   c                 3   s(    | ]}||     d V  qdS )0N)rjust).0x
group_sizenumr   r   	<genexpr>   s
    
z*get_pin_and_cookie_name.<locals>.<genexpr>)"osenvirongetr   isdigitgetattrrG   castobject	__class__rT   getpassgetuserr5   KeyErrorsysmodulestyperS   rJ   uuidgetnoderL   r   r   r   
isinstancer   updater   intlenr   range)rY   r   rvmodnameusernamemodprobably_public_bitsprivate_bitshbitcookie_namer   rl   r   get_pin_and_cookie_name   sZ   






r   c                   @   s  e Zd ZU dZeed< eed< 							d5d	d
dedededejej	g ej
eejf f  dedededdfddZedeje fddZejdeddfddZedefddZdddddeje fdd Zd!ed"ed#ejeef defd$d%Zd!edefd&d'Zd!ed(edefd)d*Zdddeje fd+d,Zd6d-d.Zd!edefd/d0Zdefd1d2Zdddddej e fd3d4Z!dS )7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 ths
                        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/consoleNTrY   r   evalexrequest_keyconsole_pathconsole_init_funcshow_hidden_framespin_securitypin_loggingr   c	           	      C   s   |sd }|| _ || _i | _i | _|| _|| _|| _|| _td| _	d| _
|| _|rQtjddkrM|rOtdd | jd u rDtdd d S tdd	| j d S d S d S d | _d S )
Nra   r   WERKZEUG_RUN_MAINtruewarningz * Debugger is active!z- * Debugger PIN disabled. DEBUGGER UNSECURED!infoz * Debugger PIN: %s)rY   r   frames
tracebacksr   r   r   r   r   secret_failed_pin_authr   rp   rq   rr   r   r   )	rQ   rY   r   r   r   r   r   r   r   r   r   r   rR      s*   



zDebuggedApplication.__init__c                 C   s&   t | dst| j}|\| _| _| jS )Nr   hasattrr   rY   r   r   rQ   
pin_cookier   r   r   r     s   

zDebuggedApplication.pinr@   c                 C   s
   || _ d S )N)r   )rQ   r@   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_namerq   r   start_responser   c                 c   s   d}z|  ||}|E dH  t|dr|  W dS W dS  ty   t|dr-|  td| jdd}|jD ]}|| j|j< q8|| j|j< z	|dddg W n ty`   |d	 	d
 Y nw t
| |}|j| j|| jdddV  ||d	  Y dS w )z6Run the application and conserve the traceback frames.Ncloser   T)skipr   ignore_system_exceptionsz500 INTERNAL SERVER ERROR)zContent-Typeztext/html; charset=utf-8)zX-XSS-Protectionrh   zwsgi.errorszpDebugging middleware caught exception in streamed response at a point where response headers were already sent.
)r   evalex_trustedr   r   r   )rY   r   r   	Exceptionr   r   r   rP   r   writeboolcheck_pin_trustrender_fullr   r   r   log)rQ   rq   r   app_iter	tracebackframe
is_trustedr   r   r   debug_application/  sN   



	
z%DebuggedApplication.debug_applicationrequestcommandr   c                 C   s   t |j|ddS )zExecute a command in a console.	text/htmlmimetype)r
   rO   eval)rQ   r   r   r   r   r   r   execute_commanda  s   z#DebuggedApplication.execute_commandc                 C   sh   d| j vr!| 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   NrY   )r   r   r   r   )r   r   dict
setdefaultrY   rM   r   r   rq   r
   r   r   )rQ   r   nsr   r   r   r   display_consoleg  s   

z#DebuggedApplication.display_consoler>   c                 C   sh   t dt|}ztt|}W n ty   d}Y nw |dur.t|d p'd}t||dS tdddS )	z0Return a static resource from the shared folder.sharedNr   zapplication/octet-streamr   z	Not Foundi  )status)	r   r   pkgutilget_data__package__r.   	mimetypes
guess_typer
   )rQ   r   r>   datar   r   r   r   get_resourcev  s   z DebuggedApplication.get_resourcec                 C   sp   | j du rdS t|| j}|rd|vrdS |dd\}}| s%d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   rr   r   splitrs   r   timePIN_TIMEr   )rQ   rq   valts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 )Nre   g      @g      ?r   )r   sleepr   rQ   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}n*|r#d}n%| jdkr+d}n|jd }|	 
dd|
ddkrDd| _d}n|   tt||d	d
d}|rm|j| jtt  dt| ddd |S |ru|| j |S )zAuthenticates with the pin.FNT
   r   r]   r^   r   )auth	exhaustedzapplication/jsonr   r   None)httponlysamesite)r   rq   rG   ru   rJ   r   r   r   argsr-   r   r
   jsondumps
set_cookier   r   r   r   delete_cookie)	rQ   r   r   r   trustr   
bad_cookie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rt|jd}|jd}|jd}| j|jjdtd}|dkr:|r:| ||}nM|d	krI|| jkrI| |}n>|d
krW|| jkrW| 	 }n0| j
rs|durs|durs| j|krs| |rs| |||}n| j
r| jdur|j| jkr| |}|||S )zDispatch the requests.__debugger__yescmdr?   sfrm)r}   resourcepinauthprintpinN)r	   r   r   rr   r   r   r   r   r   r   r   r   r   r   pathr   )	rQ   rq   r   r   responser   argr   r   r   r   r   __call__  s8   




zDebuggedApplication.__call__)Fr   r   NFTT)r   N)"rS   rT   rU   rV   rJ   __annotations__r   rG   rH   CallablerW   rX   rR   propertyr   setterr   IteratorrK   r   r	   rI   r   rM   r
   r   r   r   r   r   r   r   Iterabler   r   r   r   r   r      s   
 	

$
2

2	r   )4rx   r   r   r   rp   r   r2   r{   r   typingrG   r~   	itertoolsr   os.pathr   r   	_internalr   httpr   securityr   wrappers.requestr	   wrappers.responser
   rO   r   tbtoolsr   r   r   r   TYPE_CHECKING_typeshed.wsgir   r   r   r   rJ   r   r    rH   rI   rK   r   rL   rM   Tupler   r   r   r   r   r   <module>   sN   
 O

V