o
    iC                     @   s(  d 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Z"dd Z#da$dd Z%G dd de&Z'dd Z(G dd de&Z)dS )z
    werkzeug.debug
    ~~~~~~~~~~~~~~

    WSGI application traceback debugger.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
    N)chain)basename)join   )	text_type)_log)parse_cookie)gen_salt)BaseRequest)BaseResponse   )Console)get_current_traceback)render_console_htmli:	 c                 C   s0   t | tr| dd} t| d  d d S )Nutf-8replaces
   shittysalt   )
isinstancer   encodehashlibmd5	hexdigest)pin r   R/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/werkzeug/debug/__init__.pyhash_pin'   s   
r   c                  C   s   t d urt S dd } |  a t S )Nc                  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   zdd l}W n ty   d }Y nw Y nw |d ur
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 d S )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IOError
rpartition
subprocessr   r    communicateresearchgroupOSErrorImportErrorwinreg_winregOpenKeyHKEY_LOCAL_MACHINEKEY_READKEY_WOW64_64KEYQueryValueExREG_SZr   WindowsError)linuxfilenamefvaluer   r    dumpmatchwrrkguid	guid_typer   r   r   	_generate6   s   


(z!get_machine_id.<locals>._generate)_machine_id)rE   r   r   r   get_machine_id0   s
   ErG   c                   @   s   e Zd ZdZdd ZdS )_ConsoleFramez]Helper class so that we can reuse the frame console code for the
    standalone console.
    c                 C   s   t || _d| _d S )Nr   )r   consoleid)self	namespacer   r   r   __init__   s   

z_ConsoleFrame.__init__N)__name__
__module____qualname____doc__rM   r   r   r   r   rH      s    rH   c              	      s  t jd}d}d|dkrdS |dur%|dd r%d|v r#|}n|t| d| jj}zt	 }W n t
tfy@   d}Y nw tj|}||t| d| jjt|d	dg}tt t g}t }t||D ]}	|	snqit|	trx|	d
}	||	 qi|d d| dd  }
du r|d dt| 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off)NN- rO   rN   __file__r   s
   cookiesalt__wzd   s   pinsaltz%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getattr	__class__rO   getpassgetuserr1   KeyErrorsysmodulesrN   struuidgetnoderG   r   r   r   r   r   r   updater   intlenr   range)appr   rvmodnameusernamemodprobably_public_bitsprivate_bitshbitcookie_namer   rb   r   get_pin_and_cookie_name   sZ   





r   c                   @   s   e Zd ZdZ							d"ddZed	d
 Zejdd
 Zedd Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Z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 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.
    Fwerkzeug.request/consoleNTc	           	      C   s   |sd }|| _ || _i | _i | _|| _|| _|| _|| _td| _	d| _
|| _|rRtjddkrN|rPtdd | jd u rDtdd d S tdd	| j  d S d S d S d | _d S )
NrX   r   WERKZEUG_RUN_MAINtruewarningz * Debugger is active!z- * Debugger PIN disabled. DEBUGGER UNSECURED!infoz * Debugger PIN: %s)rx   evalexframes
tracebacksrequest_keyconsole_pathconsole_init_funcshow_hidden_framesr	   secret_failed_pin_authpin_loggingrf   rg   rh   r   r   )	rK   rx   r   r   r   r   r   pin_securityr   r   r   r   rM      s*   



zDebuggedApplication.__init__c                 C   s"   t | dst| j\| _| _| jS )N_pinhasattrr   rx   r   _pin_cookierK   r   r   r   r     s   
zDebuggedApplication.pinc                 C   s
   || _ d S )N)r   )rK   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_namec                 c   s
   d}z|  ||}|D ]}|V  qt|dr|  W dS W dS  ty   t|dr0|  td| jdd}|jD ]}|| j|j< q;|| j|j< z	|dddg W n tyc   |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-Protectionr^   zwsgi.errorszpDebugging middleware caught exception in streamed response at a point where response headers were already sent.
)r   evalex_trustedr   r   r   )rx   r   r   	Exceptionr   r   r   rJ   r   writeboolcheck_pin_trustrender_fullr   r   r   log)rK   rg   start_responseapp_iteritem	tracebackframe
is_trustedr   r   r   debug_application,  sP   


	
z%DebuggedApplication.debug_applicationc                 C   s   t |j|ddS )zExecute a command in a console.	text/htmlmimetype)ResponserI   eval)rK   requestcommandr   r   r   r   execute_command]  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   Nrx   )r   r   r   r   )r   r   dict
setdefaultrx   rH   r   r   rg   r   r   r   )rK   r   nsr   r   r   r   display_consolea  s   

z#DebuggedApplication.display_consolec                 C   s   |  }tt|ddS )z/Paste the traceback and return a JSON response.application/jsonr   )paster   jsondumps)rK   r   r   ry   r   r   r   paste_tracebackp  s   z#DebuggedApplication.paste_tracebackc                 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__r0   	mimetypes
guess_typer   )rK   r   r<   datar   r   r   r   get_resourceu  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   rh   r   splitri   r   timePIN_TIMEru   )rK   rg   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 )Nr[   g      @g      ?r   )r   sleepr   r   r   r   r   _fail_pin_auth  s   z"DebuggedApplication._fail_pin_authc                 C   s   d}d}|  |j}d}|du r|   d}n,|rd}n'| jdkr$d}n|jd}| dd| jddkr?d| _d}n|   t	t
||d	d
d}|rg|j| jdtt t| jf dd |S |ro|| j |S )zAuthenticates with the pin.FNT
   r   rT   rU   r   )auth	exhaustedr   r   z%s|%s)httponly)r   rg   r   r   argsrh   r(   r   r   r   r   r   
set_cookier   ru   r   r   delete_cookie)rK   r   r   r   trust
bad_cookieentered_pinry   r   r   r   pin_auth  s<   
zDebuggedApplication.pin_authc                 C   s2   | 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: %srU   )r   r   r   r   r   r   r   r   log_pin_request  s   z#DebuggedApplication.log_pin_requestc           
      C   sX  t |}| j}|jddkr|jd}|jd}|jd}| j|jjdtd}| j|jjdtd}	|d	krF|rF| ||}na|d
krZ|durZ|| jkrZ| 	||}nM|dkri|| jkri| 
|}n>|dkrw|| jkrw|  }n0| 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.__debugger__yescmdr=   stb)typefrmresourcer   Npinauthprintpin)Requestr   r   rh   r   ru   r   r   r   r   r   r   r   r   r   r   pathr   )
rK   rg   r   r   responser   argr   r   r   r   r   r   __call__  s>   




zDebuggedApplication.__call__)Fr   r   NFTT)rN   rO   rP   rQ   rM   propertyr   setterr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s4    
$


1/	r   )*rQ   rl   r   r   r   rf   r   r-   ro   r   rr   	itertoolsr   os.pathr   r   _compatr   	_internalr   httpr   securityr	   wrappersr
   r   r   r   rI   r   tbtoolsr   r   r   r   rF   rG   objectrH   r   r   r   r   r   r   <module>   s<   	O
S