o
    US`c~                  	   @   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rd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   7/usr/local/lib/python3.10/dist-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   Z0x5bF)r   socks4_no_rdnssockscreate_connectionhostporttimeoutr   IPPROTO_TCPTCP_NODELAYZSOCKS4Errorstrr   addsock)	r   r   r   r   r   r    r!   r0   er   r   r	   connect/   s(   
zSocksiPyConnection.connectNTNN__name__
__module____qualname__r   r2   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   r2   _contextwrap_socketr0   r)   check_hostname_check_hostnamesslmatch_hostnamegetpeercert	Exceptionshutdownr   	SHUT_RDWRcloser   r   r   r	   r2   H   s   

zSocksiPyConnectionS.connectr3   r4   r   r   r   r	   r9   C   r8   r9   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
   rI   r   r"   r)   r*   r+   r#   rI   connrG   r   r	   buildZ      z(SocksiPyHandler.http_open.<locals>.buildNr   do_openr   reqrQ   r   rG   r	   	http_openY      zSocksiPyHandler.http_openc                    rL   )Nr   c                    rM   rN   )r
   rI   r9   r"   rO   rG   r   r	   rQ   a   rR   z)SocksiPyHandler.https_open.<locals>.buildrS   rT   rV   r   rG   r	   
https_open`   rY   zSocksiPyHandler.https_openN)r5   r6   r7   r   rX   rZ   r   r   r   r	   rH   S   s    rH   __main__r%   iZ#  	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip)$__doc__r   r@   rJ   r   ImportErrorurllib.requestrequesthttp.clientclientr'   r
   r   setr&   r   r   r:   r9   rK   HTTPSHandlerrH   r5   sysintargvr*   
ValueError
IndexErrorbuild_openerZPROXY_TYPE_SOCKS5openerprintopenreaddecoder   r   r   r	   <module>   s8   