o
    Ka~                  	   @   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   !Lib\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)socketZ	inet_ptonZAF_INET6Z	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 qV 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socksZcreate_connectionhostporttimeoutr   ZIPPROTO_TCPZTCP_NODELAYZSOCKS4Errorstrr   addsock)	r   r   r   r   r   r   r   r*   er   r   r	   connect/   s(   
zSocksiPyConnection.connectNTNN__name__
__module____qualname__r   r,   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)Zserver_hostname)r   r,   Z_contextZwrap_socketr*   r%   Zcheck_hostnameZ_check_hostnamesslZmatch_hostnameZgetpeercert	ExceptionZshutdownr   Z	SHUT_RDWRcloser   r   r   r	   r,   H   s   

zSocksiPyConnectionS.connectr-   r.   r   r   r   r	   r3   C   r2   r3   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
   r:   r   r   r%   r&   r'   r    r:   Zconnr8   r   r	   buildZ      z(SocksiPyHandler.http_open.<locals>.buildNr   Zdo_openr   ZreqrA   r   r8   r	   	http_openY      zSocksiPyHandler.http_openc                    r=   )Nr   c                    r>   r?   )r
   r:   r3   r   r@   r8   r   r	   rA   a   rB   z)SocksiPyHandler.https_open.<locals>.buildrC   rD   rE   r   r8   r	   
https_open`   rG   zSocksiPyHandler.https_openN)r/   r0   r1   r   rF   rH   r   r   r   r	   r9   S   s    r9   __main__r"   iZ#  Z	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip)$__doc__r   r5   r;   r   ImportErrorZurllib.requestZrequestZhttp.clientZclientr$   r
   r   setr#   r   r   r4   r3   r<   ZHTTPSHandlerr9   r/   sysintargvr&   
ValueError
IndexErrorZbuild_openerZPROXY_TYPE_SOCKS5Zopenerprintopenreaddecoder   r   r   r	   <module>   s8   