a
    /éÐe÷7  ã                   @   sÂ   d dl m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Zd dlZd dlmZ d dlmZ d dlZd dlmZ e e¡Zej d¡ZG dd„ deƒZdS )	é    )ÚLooseVersionN)Úurlopen)Úurlretrieve)ÚLock)ÚdarwinÚcygwinÚlinuxÚlinux2c                   @   s  e Zd Zeƒ ZdZejZe d¡r(dZ	n4de
jv r8dZ	n$e d¡rHdZ	ne d¡rXd	Z	nd
Z	e
j e
j e	¡¡Zd0edœdd„Zdd„ Zd1dd„Zd2eedœdd„Zdd„ Zdd„ Zdd„ Zdd„ Zd d!„ Zd"d#„ Zed$d%„ ƒZed&d'„ ƒZ d3d(d)„Z!d*d+„ Z"d,d-„ Z#d.d/„ Z$dS )4ÚPatcherzchromedriver%sÚwin32z)~/appdata/roaming/undetected_chromedriverZLAMBDA_TASK_ROOTz/tmp/undetected_chromedriver©r   r	   z&~/.local/share/undetected_chromedriverr   z5~/Library/Application Support/undetected_chromedriverz~/.undetected_chromedriverNFr   )Úversion_mainc                 C   sö   || _ d| _d}|| _|o |dk| _|  ¡  tj | j¡sJtj	| jdd |sltj 
| jd 
|| jg¡¡| _tsŒ|rŒ|dd… d	ksŒ|d	7 }tj 
| j|¡| _|sÂ| jsÂtj tj 
d
| j¡¡| _|rÒd| _|| _| jràd| _nd| _|| _d| _dS )aK  
        Args:
            executable_path: None = automatic
                             a full file path to the chromedriver executable
            force: False
                    terminate processes which are holding lock
            version_main: 0 = auto
                specify main chrome version (rounded, ex: 82)
        FZ
undetectedér   T)Úexist_okÚ_éüÿÿÿNú.exeÚ.z+https://chromedriver.storage.googleapis.comz5https://googlechromelabs.github.io/chrome-for-testing)ÚforceÚ_custom_exe_pathÚuser_multi_procsÚis_old_chromedriverÚ_set_platform_nameÚosÚpathÚexistsÚ	data_pathÚmakedirsÚjoinÚexe_nameÚexecutable_pathÚIS_POSIXÚzip_pathÚabspathÚurl_repor   Úversion_full)Úselfr    r   r   r   Úprefix© r(   úZ/var/www/html/efn/efnconsultor/selenium/selenium-4.34.2/undetected_chromedriver/patcher.pyÚ__init__,   s<    ÿÿzPatcher.__init__c                 C   sr   | j  d¡r d| _|  jd;  _| j  d¡r@d| _|  jd;  _| j  d¡rn| jrZd| _nd| _|  jd;  _d	S )
z¢
        Set the platform and exe name based on the platform undetected_chromedriver is running on
        in order to download the correct chromedriver.
        r   r   r   Zlinux64Ú r   Zmac64zmac-x64N)ÚplatformÚendswithÚplatform_namer   r   ©r&   r(   r(   r)   r   g   s    zPatcher._set_platform_namec           
      C   sœ  t  | j¡}| jr–tƒ n t| d¡ƒ}t|dd„ d}| |¡ tt	dd„ |ƒƒ |  
|¡rxt|ƒ| _W d  ƒ dS W d  ƒ n1 sŒ0    Y  |r¦|| _d| _| jrÈ|  
| j¡}|sÄ|  ¡ S dS |rÒ|| _|du rà|| _zt | j¡ W nz tyX   | jr(|  | j¡ | j| j d Y S z|  
¡ r<W Y dS W n tyR   Y n0 Y n tyj   Y n0 |  ¡ }	|	jd	 | _|	| _|  |  ¡ ¡ |  ¡ S )
zu

        Args:
            executable_path:
            force:
            version_main:

        Returns:

        z*chromedriver*c                 S   s
   |   ¡ jS ©N)ÚstatÚst_mtime©Úfr(   r(   r)   Ú<lambda>ˆ   ó    zPatcher.auto.<locals>.<lambda>)Úkeyc                 S   s   |   ¡ S r0   )Úunlinkr3   r(   r(   r)   r5   Š   r6   NT)r   r   )ÚpathlibÚPathr   r   r   ÚlistÚrglobÚmaxÚremoveÚmapÚis_binary_patchedÚstrr    r   Ú	patch_exer   r   r   r8   ÚPermissionErrorÚforce_kill_instancesÚautoÚFileNotFoundErrorÚfetch_release_numberÚversionr%   Úunzip_packageÚfetch_packageÚpatch)
r&   r    r   r   r   ÚpÚfilesZmost_recentZ	ispatchedÚreleaser(   r(   r)   rE   y   sP    


.

zPatcher.auto)r   Úreturnc                 C   s  |s
| j }t |¡}| ¡ s(td| ƒ‚zÊt|ddª}g }z| dd¡ W n. tyz } z| |¡ W Y d}~n
d}~0 0 z| 	¡  W n. ty¶ } z| |¡ W Y d}~n
d}~0 0 |rÎW d  ƒ W dS W d  ƒ W dS 1 sæ0    Y  W n& t
y } zW Y d}~n
d}~0 0 dS )a  
        naive test to check if a found chromedriver binary is
        currently in use

        Args:
            path: a string or PathLike object to the binary to check.
                  if not specified, we check use this object's executable_path
        zfile does not exist: %sza+b©Úmoder   NTF)r    r9   r:   r   ÚOSErrorÚopenÚseekrC   ÚappendÚreadlineÚ	Exception)r&   r   rL   ÚfsÚexcÚer(   r(   r)   Údriver_binary_in_useµ   s*    	
  &zPatcher.driver_binary_in_usec                 C   sB   t  | j¡}t| d¡ƒ}|D ]}z| ¡  W q   Y q0 qd S )Nz*undetected*)r9   r:   r   r;   Úglobr8   )r&   rL   ÚitemsÚitemr(   r(   r)   Úcleanup_unused_filesÛ   s    zPatcher.cleanup_unused_filesc                 C   s   |   ¡  |  ¡ S r0   )rB   r@   r/   r(   r(   r)   rK   ä   s    zPatcher.patchc                 C   s"  | j rBd| j› }| ¡ }t d| ¡ tt| j| ƒ ¡  	¡ ƒS | js²d}t d| ¡ t| j| ƒ}| ¡  	¡ }W d  ƒ n1 sŠ0    Y  t
 |¡}t|d d d ƒS d}t d| ¡ t| j| ƒ}| ¡  	¡ }W d  ƒ n1 sô0    Y  t
 |¡}t|d	 t| jƒ d ƒS )
z½
        Gets the latest major version available, or the latest major version of self.target_version if set explicitly.
        :return: version string
        :rtype: LooseVersion
        z/latest_release_zgetting release number from %sz-/last-known-good-versions-with-downloads.jsonNZchannelsZStablerH   z2/latest-versions-per-milestone-with-downloads.jsonZ
milestones)r   r   ÚupperÚloggerÚdebugr   r   r$   ÚreadÚdecodeÚjsonÚloadsrA   )r&   r   ÚconnÚresponseZlast_versionsZmajor_versionsr(   r(   r)   rG   è   s$    *
*
zPatcher.fetch_release_numberc                    s|   t  | jd¡Z‰ t‡ fdd„dƒD ]6}t d|¡}|r"t|d  ¡ ƒ  W  d   ƒ S q"W d   ƒ n1 sn0    Y  d S )NÚrbc                      s   ˆ   ¡ S r0   )rV   r(   r3   r(   r)   r5     r6   z+Patcher.parse_exe_version.<locals>.<lambda>r6   s'   platform_handle\x00content\x00([0-9.]*)é   )ÚiorS   r    ÚiterÚreÚsearchr   rd   )r&   ÚlineÚmatchr(   r3   r)   Úparse_exe_version	  s
    zPatcher.parse_exe_versionc                 C   sj   d| j › d}| jr*d| j| jj|f }n&| ddd¡}d}|| jj| j |f; }t d| ¡ t|ƒd	 S )
z^
        Downloads ChromeDriver from source

        :return: path to downloaded file
        Zchromedriver_z.zipz%s/%s/%sr   ú-rj   zAhttps://storage.googleapis.com/chrome-for-testing-public/%s/%s/%szdownloading from %sr   )	r.   r   r$   r%   ZvstringÚreplacera   rb   r   )r&   Úzip_nameZdownload_urlr(   r(   r)   rJ     s    zPatcher.fetch_packagec              	   C   sî   | j }| js(d| j› }tj || j ¡}t d| ¡ zt | j	¡ W n t
tfy\   Y n0 tj| j	ddd tj|dd}| | j	¡ W d  ƒ n1 s 0    Y  t tj | j	|¡| j¡ t |¡ t | j	¡ t | jd¡ | jS )	zQ
        Does what it says

        :return: path to unpacked executable
        zchromedriver-zunzipping %sií  T)rQ   r   ÚrrP   N)r   r   r.   r   r   r   ra   rb   r8   r"   rF   rR   r   ÚzipfileÚZipFileÚ
extractallÚrenamer    r>   ÚshutilÚrmtreeÚchmod)r&   ÚfpZexe_pathrt   Úzfr(   r(   r)   rI   !  s"    *
zPatcher.unzip_packagec                 C   s4   t j | ¡} tr t  d|  ¡}nt  d|  ¡}| S )z–
        kills running instances.
        :param: executable name to kill, may be a path as well

        :return: True on success else False
        zkill -f -9 $(pidof %s)ztaskkill /f /im %s)r   r   Úbasenamer!   Úsystem)r   ru   r(   r(   r)   rD   <  s
    zPatcher.force_kill_instancesc                  C   s   t jtjdd} d | ¡ ¡ S )Né   )Úkr+   )ÚrandomÚchoicesÚstringÚascii_lettersr   Úencode)Zcdcr(   r(   r)   Úgen_random_cdcK  s    zPatcher.gen_random_cdcc                 C   sh   |p| j }zDt |d¡$}| ¡  d¡dkW  d   ƒ W S 1 sB0    Y  W n tyb   Y dS 0 d S )Nri   s   undetected chromedriveréÿÿÿÿF)r    rk   rS   rc   ÚfindrF   )r&   r    Úfhr(   r(   r)   r@   P  s    
6zPatcher.is_binary_patchedc                 C   sØ   t  ¡ }t d| j ¡ t | jd¡†}| ¡ }t 	d|¡}|rž|d }d 
t|ƒd¡}| ||¡}||krxt d¡ nt d||f ¡ | d¡ | |¡ W d   ƒ n1 s²0    Y  t d	 t  ¡ | ¡¡ d S )
Nzpatching driver executable %szr+bs   \{window\.cdc.*?;\}r   s.   {console.log("undetected chromedriver 1337!")}ó    zTsomething went wrong patching the driver binary. could not find injection code blockz"found block:
%s
replacing with:
%szpatching took us {:.2f} seconds)ÚtimeÚperf_counterra   Úinfor    rk   rS   rc   rm   rn   ÚljustÚlenrs   Úwarningrb   rT   ÚwriteÚformat)r&   Ústartr‹   ÚcontentZmatch_injected_codeblockZtarget_bytesZnew_target_bytesZnew_contentr(   r(   r)   rB   X  s4    ÿÿÿÿÿ
(ÿzPatcher.patch_exec                 C   s   d  | jj| j¡S )Nz{0:s}({1:s}))r”   Ú	__class__Ú__name__r    r/   r(   r(   r)   Ú__repr__v  s    þzPatcher.__repr__c              
   C   sœ   | j r
d S d}t ¡ }dd„ }|ƒ | |kr˜z.| jr8W q˜t | j¡ t d| j ¡ W q˜W q t	t
tfy€   t d¡ Y qY q ty”   Y q˜Y q0 qd S )Né   c                   S   s   t  ¡ S r0   )r   Ú	monotonicr(   r(   r(   r)   r5   „  r6   z!Patcher.__del__.<locals>.<lambda>zsuccessfully unlinked %sg{®Gáz„?)r   r   r›   r   r   r8   r    ra   rb   rR   ÚRuntimeErrorrC   ÚsleeprF   )r&   ÚtimeoutÚtÚnowr(   r(   r)   Ú__del__|  s"    
zPatcher.__del__)NFr   F)NFNN)N)N)%r˜   Ú
__module__Ú__qualname__r   Úlockr   Úsysr,   r-   Údr   ÚenvironÚ
startswithr   r#   Ú
expanduserr   Úintr*   r   rE   rA   Úboolr[   r_   rK   rG   rq   rJ   rI   ÚstaticmethodrD   rˆ   r@   rB   r™   r¡   r(   r(   r(   r)   r
      sJ   



    ûü;
<&	!


r
   )Zdistutils.versionr   rk   re   Úloggingr   r9   r,   rƒ   rm   rz   r…   r¥   r   Úurllib.requestr   r   rv   Úmultiprocessingr   Ú	getLoggerr˜   ra   r¨   r!   Úobjectr
   r(   r(   r(   r)   Ú<module>   s&   
