
    @OOf                     B   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  ej,                  e      Zdedej4                  dz  fdZdededdfdZdZ G d d      Z G d d      Z y)    N)Sequence)addonmanager)command)ctx)eventsequence)
exceptions)flow)hooks)asyncio_utilspathreturnc                    dj                  t        j                  j                  t        j                  j	                  |             d         }t
        j                  j                  |d        t
        j                  }t
        j                  j                  dt        j                  j                  |              d }	 t        j                  j                  ||       }t        j                  j                  ||      }|sJ t        j                  j                  |      }|j!                  |       t#        |dd       s| |_        |t
        j                  d d  |S # t&        $ r<}t#        t
        dd      r|xj(                  dz  c_        t+        | |       Y d }~Ud }~wt,        $ r}t+        | |       Y d }~rd }~ww xY w# |t
        j                  d d  |c cY S xY w)Nz__mitmproxy_script__.{}r   )loadernamefrozenFa  .
Note that mitmproxy's binaries include their own Python environment. If your addon requires the installation of additional dependencies, please install mitmproxy from PyPI (https://docs.mitmproxy.org/stable/overview-installation/#installation-from-the-python-package-index-pypi).)formatosr   splitextbasenamesysmodulespopinsertdirname	importlib	machinerySourceFileLoaderutilspec_from_loadermodule_from_specexec_modulegetattrr   ImportErrormsgscript_error_handler	Exception)r   fullnameoldpathmr   speces          X/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/mitmproxy/addons/script.pyload_scriptr-      sk   (//
))$/03H
 KKOOHd#hhGHHOOArwwt,-A$$55hE~~..x.GtNN++D11q&$'AF   	&3%(EE~E 	T1%% &T1%%& s=   ?B	E 	F?&2FG F?)F:5G :F??G Gexcc                     |j                   }t        j                  |d      }t        j                  |d      }t        j	                  d|  t        |      ||f       y)z+
    Log errors during script loading.
    invoke_addon_sync_call_with_frames_removedzerror in script )exc_infoN)__traceback__r   cut_tracebackloggererrortype)r   r.   tbacks      r,   r%   r%   :   sb     E&&"E &&*E LL#D6*d3ie5LLM       c                   L    e Zd ZdZdededdfdZd Zd Ze	d	        Z
d
 Zd Zy)Scriptz0
    An addon that manages a single script.
    r   reloadr   Nc                    d|z   | _         || _        t        j                  j                  |j	                  d            | _        d | _        d| _        t        j                  j                  | j
                        s"t        j                  d| j
                         d | _        |r.t        j                  | j                         d|       | _        y | j                          y )Nzscriptmanager:z'" FzNo such script: zscript watcher for )r   )r   r   r   
expanduserstripfullpathns
is_runningisfiler   OptionsError
reloadtaskr   create_taskwatcher
loadscript)selfr   r=   s      r,   __init__zScript.__init__P   s    $t+		**4::f+=>+/ww~~dmm,)),<T]]O*LMM+77*4&1DO
 OOr9   c                     d| _         y NTrC   rJ   s    r,   runningzScript.runningc   	    r9   c                 R    | j                   r| j                   j                          y y N)rF   cancelrO   s    r,   donezScript.donef   s    ??OO""$ r9   c                 8    | j                   r| j                   gS g S rS   )rB   rO   s    r,   addonszScript.addonsj   s     GGy++r9   c                    t         j                  d| j                  z         | j                  r3t        j
                  j                  j                  | j                         d | _        t        j                         5  t        | j                        }t        j
                  j                  j                  |       || _        d d d        | j                  r	 t        j
                  j                  j                  | j                  t        j                  t        j                   j#                                      | j(                  rGt        j
                  j                  j                  | j                  t        j*                                y y y # 1 sw Y   xY w# t$        $ r }t'        | j                  |       Y d }~d }~ww xY w)NzLoading script %s)r5   infor   rB   r   masterrW   remover   safecallr-   rA   registerr0   r
   ConfigureHookoptionskeysr&   r%   rC   RunningHook)rJ   rB   r+   s      r,   rI   zScript.loadscriptn   s3   '$))3477JJ$$TWW-""$ 	T]]+BJJ&&r*DG	 777

!!33GGU001A1A1CD
 

!!33DGGU=N=N=PQ  		 	  7$T]]A667s&   =AFA#F F	G%G  Gc                 6  K   t        j                  d       d {    d}	 	 t        j                  | j                        j
                  }||kD  r| j!                          |}t        j                  t"               d {    c7 j# t        $ r t        j                  d| j                  z         t        t        j                  j                        }|j                  | j                         t        j                  j                  |       Y y w xY w7 w)Nr   g        zRemoving script %s)scripts)asynciosleepr   statrA   st_mtimeFileNotFoundErrorr5   rY   r   listr   r_   rc   r[   updaterI   ReloadInterval)rJ   
last_mtimemtimerc   s       r,   rH   zScript.watcher   s      mmA
.77 z!!"
--///  	
 % 0499<=s{{223tyy)""7"3 0s?   DBD)B 3D DDB	DDDD)__name__
__module____qualname____doc__strboolrK   rP   rU   propertyrW   rI   rH    r9   r,   r<   r<   K   sJ    S $ 4 &% , ,R(0r9   r<   c                       e Zd ZdZd Zd Zd Z ej                  d      dee	j                     dej                  dd	fd
       Zd Zy	)ScriptLoaderz=
    An addon that manages loading scripts from options.
    c                      d| _         g | _        y )NF)rC   rW   rO   s    r,   rK   zScriptLoader.__init__   s    r9   c                 B    |j                  dt        t           g d       y )Nrc   zExecute a script.)
add_optionr   rr   )rJ   r   s     r,   loadzScriptLoader.load   s    )Xc]B8KLr9   c                     d| _         y rM   rN   rO   s    r,   rP   zScriptLoader.running   rQ   r9   z
script.runflowsr   r   Nc                    t         j                  j                  |      st        j	                  d|z         yt        |      }|rt        j                         5  t        j                  j                  j                  |t        j                  t        j                  j                                      t        j                  j                  j                  |t        j                                 |D ]F  }t#        j$                  |      D ],  }t        j                  j                  j                  ||       . H 	 ddd       yy# 1 sw Y   yxY w)z
        Run a script on the specified flows. The script is configured with
        the current options and all lifecycle events for each flow are
        simulated. Note that the load event is not invoked.
        zNo such script: %sN)r   r   rD   r5   r6   r-   r   r\   r   rZ   rW   r0   r
   r^   r_   r`   ra   r   iterate)rJ   r}   r   modfevts         r,   
script_runzScriptLoader.script_run   s    ww~~d#LL-45$&&( F

!!33''(8(8(:; 

!!33C9J9J9LM FA,44Q7 F

));;CEFFF F F Fs   C!EEc                 *   d|v rt         j                  j                  D ]C  }t         j                  j                  j                  |      dkD  s0t	        j
                  d       | j                  d d  D ]  }|j                  t         j                  j                  vs*t        j                  d|j                  z         t         j                  j                  j                  |       | j                  j                  |        i }| j                  D ]  }|||j                  <    g }g }t         j                  j                  D ]I  }||v r|j                  ||          t        |d      }|j                  |       |j                  |       K || _        |D ]t  }t         j                  j                  j                  |       | j                  s9t         j                  j                  j!                  |t#        j$                                v y y )Nrc   r:   zDuplicate scriptzUn-loading script: %sT)r   r_   rc   countr   rE   rW   r   r5   rY   rZ   r[   appendr<   r]   rC   r0   r
   ra   )rJ   updatedsacurrentordered
newscriptsscs           r,   	configurezScriptLoader.configure   s   [[(( F;;&&,,Q/!3$112DEEF [[^ *66!4!44KK 7!&& @AJJ%%,,Q/KK&&q)	* G[[ $"#$ GJ[[(( *<NN71:.4BNN2&%%b)* "DK P

!!**1-?? JJ%%775;L;L;NOPC  r9   )rn   ro   rp   rq   rK   r{   rP   r   r   r	   FlowmtypesPathr   r   ru   r9   r,   rw   rw      sc    M W__\"F 3 F6;; F4 F #F*'Pr9   rw   )!rd   importlib.machineryr   importlib.utilloggingr   r   typescollections.abcr   mitmproxy.typesr   	mitmproxyr   r   r   r   r   r	   r
   mitmproxy.utilsr   	getLoggerrn   r5   rr   
ModuleTyper-   r&   r%   rk   r<   rw   ru   r9   r,   <module>r      s        	 
  $   "   #     )			8	$ c  e..5  FNs N Nt N H0 H0VLP LPr9   