
    wdfC                     <   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 Z#da$d Z% G d de&          Z'd Z( G 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                     t          | t                    r|                     dd          } t          j        | dz                                             d d         S )Nutf-8replaces
   shittysalt   )
isinstancer   encodehashlibmd5	hexdigest)pins    \/var/www/book.euthymeo.com/html/venv/lib/python3.11/site-packages/werkzeug/debug/__init__.pyhash_pinr   '   sQ    #y!! -jj),,;s]*++5577<<    c                  F    t           t           S d }  |             a t           S )Nc                  h   d} dD ]k}	 t          |d          5 }|                                                                }d d d            n# 1 swxY w Y   n# t          $ r Y ^w xY w|r| |z  }  nl	 t          dd          5 }| |                                                                                    d          d         z  } d d d            n# 1 swxY w Y   n# t          $ r Y nw xY w| r| S 	 ddlm}m}  |g d	|
                                          d         }t          j
        d|          }||                    d          S n# t          t          f$ r Y nw xY w	 dd l}n(# t          $ r 	 dd l}n# t          $ r d }Y nw xY wY nw xY w|	 |                    |j        dd|j        |j        z            5 }	|                    |	d          \  }
}||j        k    r!|
                    d          cd d d            S |
cd d d            S # 1 swxY w Y   d S # t.          $ r Y d S w xY wd S )Nr   )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_types               r   	_generatez!get_machine_id.<locals>._generate6   s    M 		 		H(D)) 1QJJLL..00E1 1 1 1 1 1 1 1 1 1 1 1 1 1 1     	)400 BA++--88>>qAAB B B B B B B B B B B B B B B 	 	 	D	  	L	 /.......5DDDT  kmmAD I;TBBE {{1~~% !% 	 	 	D		 	 	 	$$$$$   	 >ZZ)7K""44	   
 &(oob-&H&HOD) BI--#{{733                                                          >s   A'AAA	AA	A
A$#A$2C ACC CC CC 
C*)C*2AE E"!E"&E+ +
F6E;:F;F
F	F

FF*H!  9H9H! HH! HH! HH! !
H/.H/)_machine_id)rH   s    r   get_machine_idrJ   0   s6     C C CJ )++Kr   c                       e Zd ZdZd ZdS )_ConsoleFramez]Helper class so that we can reuse the frame console code for the
    standalone console.
    c                 <    t          |          | _        d| _        d S )Nr   )r   consoleid)self	namespaces     r   __init__z_ConsoleFrame.__init__   s    y))r   N)__name__
__module____qualname____doc__rR    r   r   rL   rL      s-             r   rL   c           
         t           j                            d          }d}d|dk    rdS |1|                    dd                                          r	d|v r|}n|t          | d| j        j                  }	 t          j	                    }n# t          t          f$ r d}Y nw xY wt          j                            |          }||t          | d| j        j                  t          |d	d          g}t          t!          j                              t%                      g}t'          j                    }t+          ||          D ]D}	|	st-          |	t.                    r|	                    d
          }	|                    |	           E|                    d           d|                                dd         z   }
B|                    d           dt7          |                                d          z  dd         |]dD ]Xt9                    z  dk    r@d                    fdt=          dt9                              D                       } nY}||
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- rT   rS   __file__r   s
   cookiesalt__wzd   s   pinsaltz%09d   	   )         r   c              3   Z   K   | ]%}||z                                 d           V  &dS )0N)rjust).0x
group_sizenums     r   	<genexpr>z*get_pin_and_cookie_name.<locals>.<genexpr>   sU         A
N*+11*cBB     r   )osenvirongetr   isdigitgetattr	__class__rT   getpassgetuserr4   KeyErrorsysmodulesrS   struuidgetnoderJ   r   r   r   r   r   r   updater   intlenr   range)appr   rvmodnameusernamemodprobably_public_bitsprivate_bitshbitcookie_namerj   rk   s              @@r   get_pin_and_cookie_namer      s    *..-
.
.C	B
C e||z 3;;sB//7799#::BBCc<)ABBG ?$$"    +//'
"
"C
 	Z!788Z&&	 '')9)9:LA)<88   	c9%% 	&**W%%C	HH]AKKMM#2#..K {	AKKMM2...3 
z! 	 	J3xx*$))XX     "1c#hh
;;      * B{?s   =B B'&B'c                       e Zd ZdZ	 	 	 	 	 	 	 ddZed             Zej        d	             Zed
             Zd Z	d Z
d Zd Zd Zd Zd Zd Zd Z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	                    |sd }|| _         || _        i | _        i | _        || _        || _        || _        || _        t          d          | _	        d| _
        || _        |rlt          j                            d          dk    rE|rEt          dd           | j        t          dd           d S t          dd	| j        z             d S d S d S d | _        d S )
Nr_   r   WERKZEUG_RUN_MAINtruewarningz * Debugger is active!z- * Debugger PIN disabled. DEBUGGER UNSECURED!infoz * Debugger PIN: %s)r   evalexframes
tracebacksrequest_keyconsole_pathconsole_init_funcshow_hidden_framesr
   secret_failed_pin_authpin_loggingrm   rn   ro   r   r   )	rP   r   r   r   r   r   r   pin_securityr   s	            r   rR   zDebuggedApplication.__init__   s     ! 	% $&(!2"4rll !& 		z~~122f<<<Y 89998#$STTTTT!6!ABBBBB =<<< DHHHr   c                 r    t          | d          s!t          | j                  \  | _        | _        | j        S )N_pinhasattrr   r   r   _pin_cookierP   s    r   r   zDebuggedApplication.pin  s6    tV$$ 	L*A$(*K*K'DIt'yr   c                     || _         d S )N)r   )rP   rA   s     r   r   zDebuggedApplication.pin!  s    			r   c                 r    t          | d          s!t          | j                  \  | _        | _        | j        S )zThe name of the pin cookie.r   r   r   s    r   pin_cookie_namez#DebuggedApplication.pin_cookie_name%  s9     t]++ 	L*A$(*K*K'DIt'r   c              #     K   d}	 |                      ||          }|D ]}|V  t          |d          r|                                 dS dS # t          $ r t          |d          r|                                 t	          d| j        d          }|j        D ]}|| j        |j        <   || j        |j        <   	  |dddg           t          | 
                    |                    }|                    | j        || j        	                              d
d          V  n+# t          $ r |d                             d           Y nw xY w|                    |d                    Y dS w xY 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-Protectionrf   )r   evalex_trustedr   r   r   zwsgi.errorszpDebugging middleware caught exception in streamed response at a point where response headers were already sent.
)r   r   r   	Exceptionr   r   r   rO   r   boolcheck_pin_trustrender_fullr   r   r   writelog)rP   rn   start_responseapp_iteritem	tracebackframe
is_trusteds           r   debug_applicationz%DebuggedApplication.debug_application,  s     ,	2xx88H   



x)) !     ! ! &	2 &	2 &	2x)) !   -#'#:)-  I
 #) . .(-EH%%,5DOIL)-/D 2	  ( "$"6"6w"?"?@@
++;z$+ ,  &),,- - - -  	 	 	
 &,,    	  MM'-0111111M&	2s8   AA A.E/<D%
AE/%%E
E/EE/.E/c                 T    t          |j                            |          d          S )zExecute a command in a console.	text/htmlmimetype)ResponserN   eval)rP   requestcommandr   s       r   execute_commandz#DebuggedApplication.execute_command]  s%    **733kJJJJr   c                 f   d| j         vr]| j        i }n!t          |                                           }|                    d| j                   t          |          | j         d<   t          |                     |j                            }t          t          | j        |          d          S )zDisplay a standalone shell.r   Nr   )r   r   r   r   )r   r   dict
setdefaultr   rL   r   r   rn   r   r   r   )rP   r   nsr   s       r   display_consolez#DebuggedApplication.display_consolea  s    DK%-$002233MM%****2..DKN$..w??@@
t{:NNN 
 
 
 	
r   c                 p    |                                 }t          t          j        |          d          S )z/Paste the traceback and return a JSON response.application/jsonr   )paster   jsondumps)rP   r   r   r   s       r   paste_tracebackz#DebuggedApplication.paste_tracebackp  s-    __
21CDDDDr   c                    t          dt          |                    }	 t          j        t          |          }n# t
          $ r d}Y nw xY w|-t          j        |          d         pd}t          ||          S t          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__r3   	mimetypes
guess_typer   )rP   r   r?   datar   s        r   get_resourcez DebuggedApplication.get_resourceu  s    (8"4"455	#K::DD 	 	 	DDD	 +H55a8V<VHD84444C0000s   : A	A	c                 \   | j         dS t          |                              | j                  }|rd|vrdS |                    dd          \  }}|                                sdS |t          | j                   k    rdS t          j                    t          z
  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	   ro   r   splitrp   r   timePIN_TIMEr|   )rP   rn   valtspin_hashs        r   r   z#DebuggedApplication.check_pin_trust  s     847##''(<== 	cnn5yya((Hzz|| 	5x))))4	h&#b''11r   c                 h    t          j        | j        dk    rdnd           | xj        dz  c_        d S )Nrb   g      @g      ?r   )r   sleepr   r   s    r   _fail_pin_authz"DebuggedApplication._fail_pin_auth  s=    
$/!3333==="r   c                    d}d}|                      |j                  }d}||                                  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          j        ||d	          d
          }|rS|                    | j        t          t          j                              dt!          | j                  d           n|r|                    | j                   |S )zAuthenticates with the pin.FNT
   r   r[   r\   r   )auth	exhaustedr   r   r   )httponly)r   rn   r   r   argsro   r+   r   r   r   r   r   
set_cookier   r|   r   r   delete_cookie)rP   r   r   r   trust
bad_cookieentered_pinr   s           r   pin_authzDebuggedApplication.pin_auth  s   	$$W_55 
=!!!JJ  	&DD "R''II ",**511K  ""**333tx7G7GR7P7PPP()%##%%%J9==>>'
 
 
  	3MM$ty{{++++Xdh-?-?-?@     
  	3T1222	r   c                     | j         r/| j        (t          dd           t          dd| j        z             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   s    r   log_pin_requestz#DebuggedApplication.log_pin_request  sS     	@ 4W   3dh>???||r   c                    t          |          }| j        }|j                            d          dk    r|j                            d          }|j                            d          }|j                            d          }| j                            |j                            dt
                              }| j                            |j                            dt
                              }	|d	k    r|r|                     ||          }n|d
k    r$|"|| j        k    r| 	                    ||          }n|dk    r!|| j        k    r| 
                    |          }n|dk    r || j        k    r|                                 }nv| j        r;|9|	7| j        |k    r,|                     |          r|                     |||	          }n3| j        r,| j        %|j        | j        k    r|                     |          } |||          S )zDispatch the requests.__debugger__yescmdr@   stb)typefrmresourcer   Npinauthprintpin)Requestr   r   ro   r   r|   r   r   r   r   r   r   r   r   r   r   pathr   )
rP   rn   r   r   responser   argr   r   r   s
             r   __call__zDebuggedApplication.__call__  s   
 '"")<N++u44,""5))C,""3''C\%%c**F++GL,<,<T,<,L,LMMIKOOGL$4$4U$4$E$EFFEj  S ,,Wc::I$9f>S>S//CC	!!f&;&;==11
""v'<'<//11EO%K6))((11 *  //eDDK	5!- 111++G44Hx000r   )Fr   r   NFTT)rS   rT   rU   rV   rR   propertyr   setterr   r   r   r   r   r   r   r   r   r   r   rW   r   r   r   r      s;        : & " " " "H   X
 	Z  Z     X /2 /2 /2bK K K
 
 
E E E

1 
1 
12 2 2$# # #- - -^  #1 #1 #1 #1 #1r   r   )*rV   rs   r   r   r   rm   r   r0   rv   r   ry   	itertoolsr   os.pathr   r   _compatr   	_internalr   httpr	   securityr
   wrappersr   r   r   r   rN   r   tbtoolsr   r   r   r   rI   rJ   objectrL   r   r   rW   r   r   <module>r     s           				  				 



                                             - - - - - - / / / / / /       * * * * * * ( ( ( ( ( ( = = = L L L^    F   P P PfV1 V1 V1 V1 V1& V1 V1 V1 V1 V1r   