
    a                     r   d Z ddlZ	 ddlZddlZddl	Z	d Z
 G d dej                        Z G d dej                        Z G d d	ej                  ej                         Zed
k(  rddlZ	  eej*                  d         Z ej2                   ee	j4                  de            Z edej;                  d      j=                         j?                         z           edej;                  d      j=                         j?                         z          yy# e$ r ddlmZ ddlmZ Y w xY w# eef$ r dZY w xY w)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                 H    | j                         }|j                  |       |S N)copyupdate)abds      -platform/bq/third_party/socks/sockshandler.py
merge_dictr      s    	AHHQKH    c                       e Zd ZddZd Zy)SocksiPyConnectionNc                 d    ||||||f| _         t        j                  j                  | g|i | y r   )	proxyargshttplibHTTPConnection__init__	self	proxytype	proxyaddr	proxyportrdnsusernamepasswordargskwargss	            r
   r   zSocksiPyConnection.__init__   s4    #Y	48T''>t>v>r   c                 j   t        j                         | _         | j                  j                  | j                    t        | j                        t        t        fv r%| j                  j                  | j                         | j                  j                  | j                  | j                  f       y r   )socks
socksocketsocksetproxyr   typetimeoutintfloat
settimeoutconnecthostport)r   s    r
   r(   zSocksiPyConnection.connect   st    $$&			DNN+#u-II  .		499dii01r   NTNN__name__
__module____qualname__r   r(    r   r
   r   r      s    ?2r   r   c                       e Zd ZddZd Zy)SocksiPyConnectionSNc                 d    ||||||f| _         t        j                  j                  | g|i | y r   )r   r   HTTPSConnectionr   r   s	            r
   r   zSocksiPyConnectionS.__init__'   s4    #Y	48T((???r   c                    t        j                         } |j                  | j                    t	        | j
                        t        t        fv r|j                  | j
                         |j                  | j                  | j                  f       t        j                  || j                  | j                        | _        y r   )r   r    r"   r   r#   r$   r%   r&   r'   r(   r)   r*   sslwrap_socketkey_file	cert_filer!   )r   r!   s     r
   r(   zSocksiPyConnectionS.connect+   s|    !t~~&#u-OODLL)dii+,OOD$--H	r   r+   r,   r0   r   r
   r2   r2   &   s    @Ir   r2   c                       e Zd Zd Zd Zd Zy)SocksiPyHandlerc                 ^    || _         || _        t        j                  j	                  |        y r   )r   kwurllib2HTTPHandlerr   )r   r   r   s      r
   r   zSocksiPyHandler.__init__4   s$    	$$T*r   c                 4     d fd	} j                  ||      S )Nc                 f    t        j                  |      }t        j                  | ||d|}|S N)r)   r*   r$   )r   r=   r   r   r)   r*   r$   r   r=   connr   s         r
   buildz(SocksiPyHandler.http_open.<locals>.build:   s3    DGGV,B%tyyt$PW^[]^DKr   Nr   do_openr   reqrE   s   `  r
   	http_openzSocksiPyHandler.http_open9       	 ||E3''r   c                 4     d fd	} j                  ||      S )Nc                 f    t        j                  |      }t        j                  | ||d|}|S rB   )r   r=   r2   r   rC   s         r
   rE   z)SocksiPyHandler.https_open.<locals>.buildA   s3    DGGV,B&		4QX_\^_DKr   rF   rG   rI   s   `  r
   
https_openzSocksiPyHandler.https_open@   rL   r   N)r-   r.   r/   r   rK   rO   r0   r   r
   r;   r;   3   s    +
((r   r;   __main__   iZ#  	localhostzHTTP: zhttp://httpbin.org/ipzHTTPS: zhttps://httpbin.org/ip) __doc__r6   r>   r   ImportErrorurllib.requestrequesthttp.clientclientr   r   r   r   r4   r2   r?   HTTPSHandlerr;   r-   sysr%   argvr*   
ValueError
IndexErrorbuild_openerPROXY_TYPE_SOCKS5openerprintopenreaddecoder0   r   r
   <module>re      s9   "
 

2// 
2I'11 I(g))7+?+? (( z388A; "W!!/%2I2I;X\"]^F	(V[[!89>>@GGI
IJ	)fkk":;@@BIIK
KL q  "$!!"x 
# s#   D +D* D'&D'*	D65D6