
    kjn
                         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  G d	 d
e      ZdedefdZ G d d      Zy)    )Sequence)
NamedTuple)ctx)
exceptions)
flowfilter)http)version)NO_RESPONSEc                   6    e Zd ZU ej                  ed<   eed<   y)	BlockSpecmatchesstatus_codeN)__name__
__module____qualname__r   TFilter__annotations__int     c/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/mitmproxy/addons/blocklist.pyr   r      s    r   r   optionreturnc                    | d   | dd }}|j                  |d      }t        |      dk7  rt        d      |\  }}	 t        |      }t	        j
                  |      }t        ||      S # t        $ r t        d|       w xY w)zl
    Parses strings in the following format, enforces number of segments:

        /flow-filter/status

    r      N   z-Invalid number of parameters (2 are expected)zInvalid HTTP status code: )r   r   )splitlen
ValueErrorr   r   parser   )r   sepremparts	flow_pattstatusr   flow_filters           r   
parse_specr'      s     ay&*CIIc1E
5zQHIIIv@&k ""9-K[kBB	  @5fX>??@s   A) )Bc                   D    e Zd ZddZd Zd Zdej                  ddfdZy)		BlockListr   Nc                     g | _         y )N)items)selfs    r   __init__zBlockList.__init__(   s	    &(
r   c                 B    |j                  dt        t           g d       y )N
block_lista  
            Block matching requests and return an empty response with the specified HTTP status.
            Option syntax is "/flow-filter/status-code", where flow-filter describes
            which requests this rule should be applied to and status-code is the HTTP status code to return for
            blocked requests. The separator ("/" in the example) can be any character.
            Setting a non-standard status code of 444 will close the connection without sending a response.
            )
add_optionr   str)r,   loaders     r   loadzBlockList.load+   s"    SM		
r   c                    d|v rNg | _         t        j                  j                  D ])  }	 t	        |      }| j                   j                  |       + y y # t
        $ r!}t        j                  d| d|       |d }~ww xY w)Nr/   zCannot parse block_list option z: )	r+   r   optionsr/   r'   r   r   OptionsErrorappend)r,   updatedr   speces        r   	configurezBlockList.configure9   s    7"DJ++00 (%f-D
 

!!$'( #
 " $119&A3Gs   A	A>A99A>flowc                 z   |j                   s|j                  s|j                  sy | j                  D ]  }|j	                  |      sd|j
                  d<   |j                  t        k(  r|j                          Ht        j                  j                  |j                  dt        j                  i      |_          y )NTblocklistedServer)headers)responseerrorliver+   r   metadatar   r
   killr   Responsemaker	   	MITMPROXY)r,   r<   r9   s      r   requestzBlockList.requestE   s    ==DJJdiiJJ 	D||D!/3m,##{2IIK$(MM$6$6((8W=N=N2O %7 %DM	r   )r   N)	r   r   r   r-   r3   r;   r   HTTPFlowrI   r   r   r   r)   r)   '   s(    )

(DMM d r   r)   N)collections.abcr   typingr   	mitmproxyr   r   r   r   r	   mitmproxy.net.http.status_codesr
   r   r1   r'   r)   r   r   r   <module>rO      sI    $         7
 
Cs Cy C,* *r   