o
    t¨Êh~  ã                	   @   s:  d Z ddlZddlZz
ddlZddlZW n ey)   ddlmZ ddlm	Z Y nw ddl
Z
dd„ Zdd„ Zeƒ ZG dd„ dejƒZG d	d
„ d
ejƒZG dd„ dejejƒZedkr›ddlZz	eejd ƒZW n eefyt   dZY nw e ee
jdeƒ¡Ze de !d¡ "¡  #¡  ƒ e de !d¡ "¡  #¡  ƒ dS dS )zù
SocksiPy + urllib2 handler

version: 0.3
author: e<e@tr0ll.in>

This module provides a Handler which you can use with urllib2 to allow it to tunnel your connection through a socks.sockssocket socket, with out monkey patching the original socket...
é    Nc                 C   s   |   ¡ }| |¡ |S ©N)ÚcopyÚupdate)ÚaÚbÚd© r   ú>/var/www/html/env/lib/python3.10/site-packages/sockshandler.pyÚ
merge_dict   s   
r
   c                 C   sH   zd| v rt  t j| ¡ W dS d| v rt  | ¡ W dS W dS    Y dS )Nú:Ú.FT)ÚsocketÚ	inet_ptonÚAF_INET6Ú	inet_aton)Úsr   r   r	   Úis_ip   s   ùür   c                   @   ó   e Zd Zddd„Zdd„ ZdS )ÚSocksiPyConnectionNTc           	      O   ó0   ||||||f| _ tjj| g|¢R i |¤Ž d S r   )Ú	proxyargsÚhttplibÚHTTPConnectionÚ__init__©	ÚselfÚ	proxytypeÚ	proxyaddrÚ	proxyportÚrdnsÚusernameÚpasswordÚargsÚkwargsr   r   r	   r   +   ó   zSocksiPyConnection.__init__c           	      C   s¶   | j \}}}}}}|o|tv}	 zt | j| jf| jd ||||||tjtj	dff¡
}W n* tj
yT } z|rIdt|ƒv rIt| jƒsId}t |¡ n‚ W Y d }~nd }~ww q|| _d S )NTé   Ú0x5bF)r   Úsocks4_no_rdnsÚsocksÚcreate_connectionÚhostÚportÚtimeoutr   ÚIPPROTO_TCPÚTCP_NODELAYÚSOCKS4ErrorÚstrr   ÚaddÚsock)	r   r   r   r   r   r    r!   r2   Úer   r   r	   Úconnect/   s(   ýþ€ûù
zSocksiPyConnection.connect©NTNN©Ú__name__Ú
__module__Ú__qualname__r   r4   r   r   r   r	   r   *   ó    
r   c                   @   r   )ÚSocksiPyConnectionSNTc           	      O   r   r   )r   r   ÚHTTPSConnectionr   r   r   r   r	   r   D   r$   zSocksiPyConnectionS.__init__c                 C   sz   t  | ¡ | jj| j| jd�| _| jjs9| jr;zt 	| j 
¡ | j¡ W d S  ty8   | j tj¡ | j ¡  ‚ w d S d S )N)Úserver_hostname)r   r4   Ú_contextÚwrap_socketr2   r*   Úcheck_hostnameÚ_check_hostnameÚsslÚmatch_hostnameÚgetpeercertÚ	ExceptionÚshutdownr   Ú	SHUT_RDWRÚclose©r   r   r   r	   r4   H   s   

ýýzSocksiPyConnectionS.connectr5   r6   r   r   r   r	   r;   C   r:   r;   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚSocksiPyHandlerc                 O   s   || _ || _tj | ¡ d S r   )r"   ÚkwÚurllib2ÚHTTPHandlerr   )r   r"   r#   r   r   r	   r   T   s   zSocksiPyHandler.__init__c                    ó   d‡ fdd„	}ˆ   ||¡S )Nr   c                    ó(   t ˆ j|ƒ}tˆ j| ||dœ|¤Ž}|S ©N)r*   r+   r,   )r
   rK   r   r"   ©r*   r+   r,   r#   rK   ÚconnrI   r   r	   ÚbuildZ   ó   z(SocksiPyHandler.http_open.<locals>.build©Nr   ©Údo_open©r   ÚreqrS   r   rI   r	   Ú	http_openY   ó   zSocksiPyHandler.http_openc                    rN   )Nr   c                    rO   rP   )r
   rK   r;   r"   rQ   rI   r   r	   rS   a   rT   z)SocksiPyHandler.https_open.<locals>.buildrU   rV   rX   r   rI   r	   Ú
https_open`   r[   zSocksiPyHandler.https_openN)r7   r8   r9   r   rZ   r\   r   r   r   r	   rJ   S   s    rJ   Ú__main__r%   iZ#  Ú	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip)$Ú__doc__r   rB   rL   r   ÚImportErrorÚurllib.requestÚrequestÚhttp.clientÚclientr(   r
   r   Úsetr'   r   r   r<   r;   rM   ÚHTTPSHandlerrJ   r7   ÚsysÚintÚargvr+   Ú
ValueErrorÚ
IndexErrorÚbuild_openerÚPROXY_TYPE_SOCKS5ÚopenerÚprintÚopenÚreadÚdecoder   r   r   r	   Ú<module>   s8   þÿø