B
    2;Za              	   @   s&  d Z ddlZyddlZddlZW n, ek
rL   ddlmZ ddlmZ Y nX ddl	Z	dd 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yeejd ZW n eefk
r   dZY nX eee	jdeZeded    eded    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/python3.7/site-packages/sockshandler.py
merge_dict   s    
r	   c               @   s   e Zd ZdddZdd ZdS )SocksiPyConnectionNTc       	      O   s*   ||||||f| _ tjj| f|| d S )N)	proxyargshttplibHTTPConnection__init__)	self	proxytype	proxyaddr	proxyportrdnsusernamepasswordargskwargsr   r   r   r      s    zSocksiPyConnection.__init__c             C   sP   t  | _| jj| j  t| jttfkr8| j	| j | j
| j| jf d S )N)socks
socksocketsocksetproxyr   typetimeoutintfloat
settimeoutconnecthostport)r   r   r   r   r!      s
    
zSocksiPyConnection.connect)NTNN)__name__
__module____qualname__r   r!   r   r   r   r   r
      s   
r
   c               @   s   e Zd ZdddZdd ZdS )SocksiPyConnectionSNTc       	      O   s*   ||||||f| _ tjj| f|| d S )N)r   r   HTTPSConnectionr   )	r   r   r   r   r   r   r   r   r   r   r   r   r   '   s    zSocksiPyConnectionS.__init__c             C   s\   t  }|j| j  t| jttfkr2|| j |	| j
| jf t|| j| j| _d S )N)r   r   r   r   r   r   r   r   r    r!   r"   r#   sslZwrap_socketZkey_fileZ	cert_filer   )r   r   r   r   r   r!   +   s    zSocksiPyConnectionS.connect)NTNN)r$   r%   r&   r   r!   r   r   r   r   r'   &   s   
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 )N)r   kwurllib2HTTPHandlerr   )r   r   r   r   r   r   r   4   s    zSocksiPyHandler.__init__c                s   d fdd	}  ||S )Nr   c                s(   t  j|}t j| ||d|}|S )N)r"   r#   r   )r	   r+   r
   r   )r"   r#   r   r   r+   conn)r   r   r   build:   s    z(SocksiPyHandler.http_open.<locals>.build)Nr   )do_open)r   reqr/   r   )r   r   	http_open9   s    zSocksiPyHandler.http_openc                s   d fdd	}  ||S )Nr   c                s(   t  j|}t j| ||d|}|S )N)r"   r#   r   )r	   r+   r'   r   )r"   r#   r   r   r+   r.   )r   r   r   r/   A   s    z)SocksiPyHandler.https_open.<locals>.build)Nr   )r0   )r   r1   r/   r   )r   r   
https_open@   s    zSocksiPyHandler.https_openN)r$   r%   r&   r   r2   r3   r   r   r   r   r*   3   s   r*   __main__   iZ#  Z	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip) __doc__r)   r,   r   ImportErrorZurllib.requestZrequestZhttp.clientZclientr   r	   r   r
   r(   r'   r-   ZHTTPSHandlerr*   r$   sysr   argvr#   
ValueError
IndexErrorZbuild_openerZPROXY_TYPE_SOCKS5openerprintopenreaddecoder   r   r   r   <module>	   s*   

