
    @OOf0                     4   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	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  ej&                  e      Zd
efdZd Zdedej2                  deez  fdZdedej2                  defdZ G d d      Zy)    N)Sequence)Any)certs)command)
exceptions)flow)http)ALERTspecc                     | j                  d      r| j                  d      st        j                  d| z        | t	        d      d j                         S )Nheader[]zInvalid header spec: %s)
startswithendswithr   CommandErrorlenstrip)r   s    U/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/addons/cut.py
headernamer      sJ    OOI&4==+=%%&?$&FGGI$**,,    c                 B    t        | t              xr t        |       dkD  S )N   )
isinstancetupler   )vs    r   is_addrr      s    a.CFQJ.r   cutfreturnc                    t        |t        j                        r*|j                  r| dv r|j                  j	                         S | j                  d      }|}t        |      D ]  \  }}|j                  d      rt        j                  d|z        t        ||d       }|t        |      dz
  k(  rD|dk(  rt        |      rt        |d         c S |dk(  rt        |      rt        |d         c S |j                  d	      r+|s y
|j                  j                  t!        |      d
      c S t        |t"              r|c S t        |t$              r
|rdc S dc S t        |t&        j(                        r!|j+                         j-                  d      c S t        |t.              rOt        |      dkD  rAt        |d   t&        j(                        r$|d   j+                         j-                  d      c S |} t        |xs d
      S )N)zrequest.contentzresponse.content._z"Can't access internal attribute %sr   porthostr   r    truefalseascii)r   r	   HTTPFlow	websocket_get_formatted_messagessplit	enumerater   r   r   getattrr   r   strheadersgetr   bytesboolr   Certto_pemdecodelist)r   r   pathcurrentir   parts          r   extractr=      s   
 	1dmm$KK::{{224499S>DGT? 4??3))*NQU*UVVwd+D	Av~''"271:&v~''"271:&+**:d+;R@@D%(D$'!%v272D%**-{{}++G444&IMtAw

3 Aw~~'..w7778 w}"r   c                 T    t        | |      }t        |t              rt        |      S |S )N)r=   r   r3   repr)r   r   rets      r   extract_strrA   K   s&    
#q/C#uCy
r   c            	          e Zd Z ej                  d      deej                     dej                  j                  dej                  j                  fd       Z ej                  d      deej                     dej                  j                  dej                  j                  ddfd	       Z ej                  d
      deej                     dej                  j                  ddfd       Zy)Cutr   flowscutsr    c           
      t    g }|D ]+  }|j                  |D cg c]  }t        ||       c}       - |S c c}w )a  
        Cut data from a set of flows. Cut specifications are attribute paths
        from the base of the flow object, with a few conveniences - "port"
        and "host" retrieve parts of an address tuple, ".header[key]"
        retrieves a header value. Return values converted to strings or
        bytes: SSL certificates are converted to PEM format, bools are "true"
        or "false", "bytes" are preserved, and all other values are
        converted to strings.
        )appendr=   )selfrD   rE   r@   r   cs         r   r   zCut.cutT   s?     (* 	6AJJt4!145	6
 5s   5
zcut.saver9   Nc           	         d}|j                  d      rCd}t        j                  j                  |dd       }t        j
                  j                  |      }	 t        |      dk(  rt        |      dk(  rt        ||rdnd      5 }|j                         dkD  r|j                  d	       t        |d   |d         }t        |t              r|j                  |       n |j                  |j                  d
             ddd       t        j!                  t"        d       yt        ||rdnddd
      5 }t%        j&                  |      }	|D ],  }
|D cg c]  }t)        ||
       }}|	j+                  |       . 	 ddd       t        j!                  t"        dt        |      t        |      fz         y# 1 sw Y   xY wc c}w # 1 sw Y   LxY w# t,        $ r(}t        j/                  t1        |             Y d}~yd}~ww xY w)aI  
        Save cuts to file. If there are multiple flows or cuts, the format
        is UTF-8 encoded CSV. If there is exactly one row and one column,
        the data is written to file as-is, with raw bytes preserved. If the
        path is prefixed with a "+", values are appended if there is an
        existing file.
        F+Tr   Nabwbr      
utf8zSaved single cut.awr&   )newlineencodingz#Saved %s cuts over %d flows as CSV.)r   osr9   
expanduser	mitmproxytypesPathr   opentellwriter=   r   r3   encodeloggerlogr
   csvwriterrA   writerowOSErrorerrorr0   )rH   rD   rE   r9   rG   epathfpr   tfpr`   r   rI   valses                 r   savezCut.saveh   s    ??3FGG&&tABx0E??''.D	!4yA~#e*/$D9 3Rwwy1}Qq2A!!U+&!123 

5"56#S"v . ZZ_F" .;?@aAq 1@@-.	. 

9SYE
<SS%3 3   A. .  	!LLQ  	!sa   ,G A9F5="G  G 3GG%G;9G 5F>:G GGG 	HG>>Hzcut.clipc                    t        j                  d      }t        |      dk(  rLt        |      dk(  r>t        |d   |d         }|j	                  |       t
        j                  t        d       nlt        j                  |      }|D ],  }|D cg c]  }t        ||       }}|j                  |       . t
        j                  t        dt        |      z         	 t        j                  |j                                yc c}w # t        j                  $ r(}	t
        j                  t!        |	             Y d}	~	yd}	~	ww xY w)z
        Send cuts to the clipboard. If there are multiple flows or cuts, the
        format is UTF-8 encoded CSV. If there is exactly one row and one
        column, the data is written to file as-is, with raw bytes preserved.
        r&   )rR   r   r   zClipped single cut.zClipped %s cuts as CSV.N)ioStringIOr   rA   r[   r]   r^   r
   r_   r`   ra   	pyperclipcopygetvaluePyperclipExceptionrc   r0   )
rH   rD   rE   re   r   r`   r   rI   rg   rh   s
             r   clipzCut.clip   s     [[$t9>c%jAoDGU1X.AHHQKJJu34ZZ^F &378aAq)88%& JJu7#d)CD	!NN2;;=)	 9
 ++ 	!LLQ  	!s   D#D ED==E)__name__
__module____qualname__r   r   r   FlowrV   rW   CutSpecDatar   rX   ri   rq    r   r   rC   rC   S   s	   W__U		" oo%% 
			 & W__Z +!		"+! oo%%+! oo""	+!
 
+! !+!Z W__Z !		"! oo%%! 
	! !!r   rC   )r_   rk   loggingos.pathrT   collections.abcr   typingr   rm   mitmproxy.typesrV   r   r   r   r   r	   mitmproxy.logr
   	getLoggerrr   r]   r0   r   r   ru   r3   r=   rA   rC   rx   r   r   <module>r      s    
 	   $          			8	$-S -/) ) )sU{ )XS TYY 3 ]! ]!r   