o
    ¾ÙøiN  ã                   @   sö   d dl Z d dlZd dlZd dlmZ d dlmZ zd dlmZ W n ey9   ej	dkr1d dl
Z
nd dlmZ Y nw z
d dlmZmZ W n eyU   d dlmZmZ Y nw ddlmZmZmZmZmZmZ dd	lmZ dd
lmZ G dd„ dƒZdS )é    N)Údeepcopy)ÚOrderedDict)Ú
HTMLParser©é   é   )ÚurlparseÚurljoiné   )ÚCloudflareCode1020ÚCloudflareIUAMErrorÚCloudflareSolveErrorÚCloudflareChallengeErrorÚCloudflareCaptchaErrorÚCloudflareCaptchaProvider)ÚCaptcha)ÚJavaScriptInterpreterc                   @   st   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
Cloudflarec                 C   s
   || _ d S ©N)Úcloudscraper)Úselfr   © r   úd/home/develop/edictos_py/central_edictos/env/lib/python3.10/site-packages/cloudscraper/cloudflare.pyÚ__init__.   s   
zCloudflare.__init__c                 C   s6   t jdkrt jdkrt | ¡S tƒ  | ¡S tƒ  | ¡S )N)r   r   r   )ÚsysÚversion_infoÚhtmlÚunescaper   )Ú	html_textr   r   r   r   5   s
   


zCloudflare.unescapec                 C   sf   z(| j  dd¡ d¡o'| jdv o't d| jtjtjB ¡o't d| jtjtjB ¡W S  t	y2   Y dS w )NÚServerÚ Ú
cloudflare)i­  i÷  z/cdn-cgi/images/trace/jsch/ú5<form .*?="challenge-form" action="/\S+__cf_chl_f_tk=F©
ÚheadersÚgetÚ
startswithÚstatus_codeÚreÚsearchÚtextÚMÚSÚAttributeError©Úrespr   r   r   Úis_IUAM_ChallengeC   ó    ÿþ
ýü
ýzCloudflare.is_IUAM_Challengec                 C   ó:   z|   |¡ot d|jtjtjB ¡W S  ty   Y dS w )NzDcpo.src\s*=\s*['"]/cdn-cgi/challenge-platform/\S+orchestrate/jsch/v1F)r0   r(   r)   r*   r+   r,   r-   ©r   r/   r   r   r   Úis_New_IUAM_ChallengeY   ó   

ýþýz Cloudflare.is_New_IUAM_Challengec                 C   r2   )NzQcpo.src\s*=\s*['"]/cdn-cgi/challenge-platform/\S+orchestrate/(captcha|managed)/v1F)Úis_Captcha_Challenger(   r)   r*   r+   r,   r-   r3   r   r   r   Úis_New_Captcha_Challengel   r5   z#Cloudflare.is_New_Captcha_Challengec                 C   sf   z(| j  dd¡ d¡o'| jdko't d| jtjtjB ¡o't d| jtjtjB ¡W S  t	y2   Y dS w )Nr   r    r!   é“  z(/cdn-cgi/images/trace/(captcha|managed)/r"   Fr#   r.   r   r   r   r6      r1   zCloudflare.is_Captcha_Challengec                 C   sN   z| j  dd¡ d¡o| jdkot d| jtjtjB ¡W S  t	y&   Y dS w )Nr   r    r!   r8   z'<span class="cf-error-code">1020</span>F)
r$   r%   r&   r'   r(   r)   r*   r+   ÚDOTALLr-   r.   r   r   r   Úis_Firewall_Blocked•   s   ÿ
ýý	ýzCloudflare.is_Firewall_Blockedc                 C   st   |   |¡r| j td¡ |  |¡r| j td¡ |  |¡r$| j td¡ |  |¡s.|  |¡r8| jj	r6t
dƒ dS dS )Nz9Cloudflare has blocked this request (Code 1020 Detected).zrDetected a Cloudflare version 2 Captcha challenge, This feature is not available in the opensource (free) version.zjDetected a Cloudflare version 2 challenge, This feature is not available in the opensource (free) version.z*Detected a Cloudflare version 1 challenge.TF)r:   r   ÚsimpleExceptionr   r7   r   r4   r6   r0   ÚdebugÚprintr3   r   r   r   Úis_Challenge_Requestª   s(   
þ
þ
þzCloudflare.is_Challenge_Requestc           	         sJ  zNt  d|t jt jB ¡ ¡ ‰ t‡ fdd„dD ƒƒs | j td¡ t	ƒ }t  
dˆ d t jt jB ¡D ]}tt  
d|¡ƒ}| d	¡d
v rL| |d	 |d i¡ q0W n ty^   | j td¡ Y nw t|ƒ}zt |¡ ||j¡|d< W n  ty‘ } z| j tdt|d|ƒ› �¡ W Y d }~nd }~ww |j› d|j› |  ˆ d ¡› �|dœS )Nza<form (?P<form>.*?="challenge-form" action="(?P<challengeUUID>.*?__cf_chl_f_tk=\S+)"(.*?)</form>)c                 3   ó   � | ]}|ˆ v V  qd S r   r   ©Ú.0Úkey©ÚformPayloadr   r   Ú	<genexpr>Ò   ó   € z5Cloudflare.IUAM_Challenge_Response.<locals>.<genexpr>©ÚformÚchallengeUUIDzRCloudflare IUAM detected, unfortunately we can't extract the parameters correctly.z^\s*<input\s(.*?)/>rH   z(\S+)="(\S+)"Úname)ÚrÚjschl_vcÚpassÚvalueÚjschl_answerz+Unable to parse Cloudflare anti-bots page: Úmessageú://rI   ©ÚurlÚdata)r(   r)   r+   r9   Ú	groupdictÚallr   r;   r   r   Úfindallr,   Údictr%   Úupdater-   r   r   ÚdynamicImportÚsolveChallengeÚnetlocÚ	ExceptionÚgetattrÚschemer   )	r   ÚbodyrS   ÚinterpreterÚpayloadÚchallengeParamÚinputPayloadÚ
hostParsedÚer   rC   r   ÚIUAM_Challenge_ResponseÈ   sV   
ûúþ€ýþÿÿ

þþ€ÿþz"Cloudflare.IUAM_Challenge_Responsec           
   	      sd  z5t  d|t jt jB ¡ ¡ ‰ t‡ fdd„dD ƒƒs | j td¡ t	t  
dˆ d ¡ƒ}|d d	kr2d
nd}W n ttfyG   | j td¡ Y nw | jjr^| jj| jj d¡kr^| j| jjd< | jjd | jjd< t | ¡ ¡ |||d |¡}t	d| dd¡fd|d fd| d¡fd|fgƒ}|dkr›| d|i¡ t|ƒ}	|	j› d|	j› |  ˆ d ¡› �|dœS )Nzi<form (?P<form>.*?="challenge-form" action="(?P<challengeUUID>.*?__cf_chl_captcha_tk__=\S+)"(.*?)</form>)c                 3   r?   r   r   r@   rC   r   r   rE     rF   z8Cloudflare.captcha_Challenge_Response.<locals>.<genexpr>rG   zUCloudflare Captcha detected, unfortunately we can't extract the parameters correctly.zM(name="r"\svalue|data-ray|data-sitekey|name="cf_captcha_kind"\svalue)="(.*?)"rH   zname="cf_captcha_kind" valuer(   Ú	reCaptchaÚhCaptchaÚproxyz
User-Agentzdata-sitekeyrK   zname="r" valuer    Úcf_captcha_kindÚidzdata-rayzg-recaptcha-responsezh-captcha-responserQ   rI   rR   )r(   r)   r+   r9   rU   rV   r   r;   r   r   rW   r-   ÚKeyErrorÚproxiesÚcaptchar%   r$   r   rZ   ÚlowerÚsolveCaptcharY   r   r_   r\   r   )
r   ÚproviderÚprovider_paramsr`   rS   rb   ÚcaptchaTypeÚcaptchaResponseÚdataPayloadre   r   rC   r   Úcaptcha_Challenge_Responseù   sf   
üûþþÿþÿ
ÿüþ
üþz%Cloudflare.captcha_Challenge_Responsec           
   	   K   s^  |   |¡rX| jjr| j | jj|jj|jfi |¤Ž¡}|   |¡s"|S | jjr4t	| jjt
ƒr4| jj d¡s;| j td¡ | jj d¡dkrF|S |  | jj d¡| jj|j|j¡}nF| jjsŒztt d|j¡ d¡ƒtdƒ }t	|ttfƒrx|| j_W n ttfy‹   | j td¡ Y nw t | jj¡ |  |j|j| jj¡}|�r!dd	„ }t|ƒ}d
|d< ||d|d ƒ|d< t|jƒ}||d|j› d|j › �|jdœƒ|d< | jjd|d fi |¤Ž}|j!dkrè| j t"d¡ |j#sí|S t|ƒ}||dd|jiƒ|d< t|j$d ƒj �st%|j|j$d ƒ}	n|j$d }	| jj|jj|	fi |¤ŽS | jj|jj|jfi |¤ŽS )Nrr   z}Cloudflare Captcha detected, unfortunately you haven't loaded an anti Captcha provider correctly via the 'captcha' parameter.Úreturn_responsez submit\(\);\r?\n\s*},\s*([0-9]+)r
   iè  zCCloudflare IUAM possibility malformed, issue extracing delay value.c              	   S   sN   z| |   |¡ | | W S  ttfy&   i | |< | |   |¡ | |  Y S w r   )rY   r-   rm   )ÚobjrJ   ÚnewValuer   r   r   Ú
updateAttrž  s   
ýz1Cloudflare.Challenge_Response.<locals>.updateAttrFÚallow_redirectsrT   r$   rQ   )ÚOriginÚRefererÚPOSTrS   i�  z5Invalid challenge answer detected, Cloudflare broken?r~   ÚLocation)&r6   r   Ú
doubleDownÚdecodeBrotliÚperform_requestÚrequestÚmethodrS   ro   Ú
isinstancerX   r%   r;   r   rw   r*   ÚdelayÚfloatr(   r)   ÚgroupÚintr-   Ú
ValueErrorr   ÚtimeÚsleeprg   ra   r   r   r_   r\   r'   r   Úis_redirectr$   r	   )
r   r/   ÚkwargsÚ
submit_urlr‡   r{   Úcloudflare_kwargsÚ	urlParsedÚchallengeSubmitResponseÚredirect_locationr   r   r   ÚChallenge_ResponseJ  sÈ   
ÿ
ÿþýþ
üþüû€þÿý
	ý
þý	þý
þ
ýþ
þýzCloudflare.Challenge_ResponseN)Ú__name__Ú
__module__Ú__qualname__r   Ústaticmethodr   r0   r4   r7   r6   r:   r>   rg   rw   r•   r   r   r   r   r   ,   s     



1Qr   )r(   r   rŒ   Úcopyr   Úcollectionsr   r   ÚImportErrorr   r   Úhtml.parserr   r	   Úurllib.parseÚ
exceptionsr   r   r   r   r   r   ro   r   Úinterpretersr   r   r   r   r   r   Ú<module>   s,   

€üÿ 