
    @OOf$                       U d 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 ddlZdd
lmZ ddlmZ ej&                  dk  rddlmZ nddlmZ  ed       G d dee             ZdZded<   dZded<   d Z G d de      Z G d de      Z G d de      Z G d d e      Z G d! d"e      Z G d# d$e      Z  G d% d&e      Z! G d' d(e      Z" G d) d*e      Z#y)+a  
This module is responsible for parsing proxy mode specifications such as
`"regular"`, `"reverse:https://example.com"`, or `"socks5@1234"`. The general syntax is

    mode [: mode_configuration] [@ [listen_addr:]listen_port]

For a full example, consider `reverse:https://example.com@127.0.0.1:443`.
This would spawn a reverse proxy on port 443 bound to localhost.
The mode is `reverse`, and the mode data is `https://example.com`.
Examples:

    mode = ProxyMode.parse("regular@1234")
    assert mode.listen_port == 1234
    assert isinstance(mode, RegularMode)

    ProxyMode.parse("reverse:example.com@invalid-port")  # ValueError

    RegularMode.parse("regular")  # ok
    RegularMode.parse("socks5")  # ValueError

    )annotationsN)ABCMeta)abstractmethod)	dataclass)cache)ClassVar)Literal)Serializable)server_spec)      )SelfT)frozenc                     e Zd ZU dZded<   	 ded<   	 ded<   	 ded<   	 d	ed
<   	 i Zded<   d Zd Zedd       Z	e
edd              Ze
dd       Ze
edd              Zeedd              Zdd dZdd!dZed        Zd Zd Zy)"	ProxyModez
    Parsed representation of a proxy mode spec. Subclassed for each specific mode,
    which then does its own data validation.
    str	full_specdata
str | Nonecustom_listen_host
int | Nonecustom_listen_portzClassVar[str]	type_namez$ClassVar[dict[str, type[ProxyMode]]]_ProxyMode__typesc                    | j                   j                  d      j                         | _        | j                  t        j
                  vsJ | t        j
                  | j                  <   y )NMode)__name__removesuffixlowerr   r   r   )clskwargss     [/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/proxy/mode_specs.py__init_subclass__zProxyMode.__init_subclass__C   sM    11&9??A}}I$5$5555+.	#--(    c                "    d| j                   dS )NzProxyMode.parse()r   selfs    r"   __repr__zProxyMode.__repr__H   s    !$..!3155r$   c                     y)z Validation of data happens here.N r(   s    r"   __post_init__zProxyMode.__post_init__K       r$   c                     y)z=The mode description that will be used in server logs and UI.Nr,   r(   s    r"   descriptionzProxyMode.descriptionO   r.   r$   c                     y)z^
        Default listen port of servers for this mode, see `ProxyMode.listen_port()`.
        i  r,   r(   s    r"   default_portzProxyMode.default_portT   s    
 r$   c                     y)z2The transport protocol used by this mode's server.Nr,   r(   s    r"   transport_protocolzProxyMode.transport_protocol[   r.   r$   c                   |j                  d      \  }}}|s|}d}|j                  d      \  }}}|r;d|v r|j                  d      \  }}}nd}|}	 t        |      }	|	dk  sd|	k  rt        nd}d}		 t        j
                  |j                            }
t        |
|       st        |d	| j                   d
       |
||||	      S # t        $ r t        d|       w xY w# t        $ r t        d      w xY w)ze
        Parse a proxy mode specification and return the corresponding `ProxyMode` instance.
        @ :Nr   i  zinvalid port: zunknown modez is not a spec for a z mode)r   r   r   r   )

rpartition	partitionint
ValueErrorr   r   r   KeyError
issubclassr   )r    spechead_	listen_atmoder   hostport_strportmode_clss              r"   parsezProxyMode.parse`   s1    "__S1aDIs+ai$-$8$8$=!a$>8}!8ut|$$  ,
 DD	. ((6H (C(x'<S]]O5QRR$SW
 	
  > >(!<==>  	.|--	.s   C 3!C" C"C7Nc                <    | j                   | j                   S ||S y)a  
        Return the address a server for this mode should listen on. This can be either directly
        specified in the spec or taken from a user-configured global default (`options.listen_host`).
        By default, return an empty string to listen on all hosts.
        r7   )r   r)   defaults     r"   listen_hostzProxyMode.listen_host   s*     "".*** Nr$   c                R    | j                   | j                   S ||S | j                  S )z
        Return the port a server for this mode should listen on. This can be either directly
        specified in the spec, taken from a user-configured global default (`options.listen_port`),
        or from `ProxyMode.default_port`.
        )r   r2   rJ   s     r"   listen_portzProxyMode.listen_port   s3     "".*** N$$$r$   c                ,    t         j                  |      S N)r   rH   )r    states     r"   
from_statezProxyMode.from_state   s    u%%r$   c                    | j                   S rP   r'   r(   s    r"   	get_statezProxyMode.get_state   s    ~~r$   c                L    || j                   k7  rt        j                  d      y )NzProxy modes are immutable.)r   dataclassesFrozenInstanceError)r)   rQ   s     r"   	set_statezProxyMode.set_state   s%    DNN"112NOO #r$   returnNone)rZ   r   rZ   r;   )rZ   zLiteral['tcp', 'udp'] | None)r?   r   rZ   r   rP   )rK   r   rZ   r   )rK   r   rZ   r;   )r   
__module____qualname____doc____annotations__r   r#   r*   r   r-   propertyr0   r2   r4   classmethodr   rH   rL   rN   rR   rT   rX   r,   r$   r"   r   r   -   s    
 N:
IA""9""9  L46G16/
6 / / L  L   A  A 
%
  %
N% & &Pr$   r   )	metaclasstcpzLiteral['tcp', 'udp']TCPudpUDPc                    | rt        d      y )Nzmode takes no argumentsr<   )r   s    r"   _check_emptyrj      s    233 r$   c                       e Zd ZdZdZeZddZy)RegularModezfA regular HTTP(S) proxy that is interfaced with `HTTP CONNECT` calls (or absolute-form HTTP requests).zHTTP(S) proxyc                .    t        | j                         y rP   rj   r   r(   s    r"   r-   zRegularMode.__post_init__       TYYr$   NrY   r   r]   r^   r_   r0   re   r4   r-   r,   r$   r"   rl   rl      s    p!K r$   rl   c                       e Zd ZdZdZeZddZy)TransparentModezJA transparent proxy, see https://docs.mitmproxy.org/dev/howto-transparent/zTransparent Proxyc                .    t        | j                         y rP   rn   r(   s    r"   r-   zTransparentMode.__post_init__   ro   r$   NrY   rp   r,   r$   r"   rr   rr      s    T%K r$   rr   c                  6    e Zd ZU dZdZeZded<   ded<   d	dZy)
UpstreamModez^A regular HTTP(S) proxy, but all connections are forwarded to a second upstream HTTP(S) proxy.zHTTP(S) proxy (upstream mode)zLiteral['http', 'https']schemetuple[str, int]addressc                    t        j                  | j                  d      \  }| _        |dk7  r|dk7  rt	        d      || _        y )Nhttpdefault_schemehttpszinvalid upstream proxy scheme)r   rH   r   rx   r<   rv   )r)   rv   s     r"   r-   zUpstreamMode.__post_init__   sA    *006RV' 1<==r$   NrY   )	r   r]   r^   r_   r0   re   r4   r`   r-   r,   r$   r"   ru   ru      s!    h1K$$r$   ru   c                  T     e Zd ZU dZdZeZded<   ded<   d	dZe	d
 fd       Z
 xZS )ReverseModez^A reverse proxy. This acts like a normal server, but redirects all requests to a fixed target.zreverse proxyzMLiteral['http', 'https', 'http3', 'tls', 'dtls', 'tcp', 'udp', 'dns', 'quic']rv   rw   rx   c                    t        j                  | j                  d      \  | _        | _        | j                  dv rt
        | _        | j                   d| j                   | _        y )Nr}   r{   )http3dtlsrf   dnsquicz to )r   rH   r   rv   rx   rg   r4   r0   r(   s    r"   r-   zReverseMode.__post_init__   sU    $/$5$5diiPW$X!T\;;AA&)D#"../tDII;?r$   c                8    | j                   dk(  ryt        |   S )Nr   5   )rv   superr2   )r)   	__class__s    r"   r2   zReverseMode.default_port   s    ;;%w##r$   rY   r\   )r   r]   r^   r_   r0   re   r4   r`   r-   ra   r2   __classcell__)r   s   @r"   r   r      s=    h!K  @ $ $r$   r   c                  $    e Zd ZdZdZdZeZddZy)
Socks5ModezA SOCKSv5 proxy.zSOCKS v5 proxyi8  c                .    t        | j                         y rP   rn   r(   s    r"   r-   zSocks5Mode.__post_init__   ro   r$   NrY   )	r   r]   r^   r_   r0   r2   re   r4   r-   r,   r$   r"   r   r      s    "KL r$   r   c                  $    e Zd ZdZdZdZeZddZy)DnsModezA DNS server.z
DNS serverr   c                .    t        | j                         y rP   rn   r(   s    r"   r-   zDnsMode.__post_init__  ro   r$   NrY   	r   r]   r^   r_   r0   r2   rg   r4   r-   r,   r$   r"   r   r      s    KL r$   r   c                  $    e Zd ZdZdZdZeZddZy)WireGuardModezProxy Server based on WireGuardzWireGuard serveril  c                     y rP   r,   r(   s    r"   r-   zWireGuardMode.__post_init__  s    r$   NrY   r   r,   r$   r"   r   r     s    )$KLr$   r   c                       e Zd ZdZdZdZddZy)	LocalModezOS-level transparent proxy.zLocal redirectorNc                V    t         j                  j                  | j                         y rP   )mitmproxy_rsLocalRedirectordescribe_specr   r(   s    r"   r-   zLocalMode.__post_init__(  s    $$22499=r$   rY   r   r]   r^   r_   r0   r4   r-   r,   r$   r"   r   r   "  s    %$K>r$   r   c                       e Zd ZdZdZdZddZy)OsProxyModezDeprecated alias for LocalModeNc                    t        d      )NzYosproxy mode has been renamed to local mode. Thanks for trying our experimental features!ri   r(   s    r"   r-   zOsProxyMode.__post_init__3  s    g
 	
r$   rY   r   r,   r$   r"   r   r   -  s    (2K
r$   r   )$r_   
__future__r   rV   sysabcr   r   r   	functoolsr   typingr   r	   r    mitmproxy.coretypes.serializabler
   mitmproxy.netr   version_infotyping_extensionsr   r   re   r`   rg   rj   rl   rr   ru   r   r   r   r   r   r   r,   r$   r"   <module>r      s   , #  
   !     9 %g& $~P ~P ~PB # "" "4
 )   i  9  $) $0    i  0I >	 >	
) 	
r$   