[Python-modules-commits] [python-socksipy] 02/09: Import python-socksipy_1.5.7.orig.tar.gz

Ondřej Nový onovy at moszumanska.debian.org
Sat Aug 6 20:44:43 UTC 2016


This is an automated email from the git hooks/post-receive script.

onovy pushed a commit to branch master
in repository python-socksipy.

commit 39faf6540d13cd6f38c70adf2b534811ade6ddb8
Author: Ondřej Nový <onovy at debian.org>
Date:   Sat Aug 6 22:16:44 2016 +0200

    Import python-socksipy_1.5.7.orig.tar.gz
---
 LICENSE              |  22 ++++
 PKG-INFO             |   2 +-
 README.md            | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++
 setup.py             |   2 +-
 socks.py             | 121 +++++++++++++++------
 test/README          |   5 +
 test/httpproxy.py    | 137 +++++++++++++++++++++++
 test/mocks           | Bin 0 -> 33596 bytes
 test/mocks.conf      | 104 ++++++++++++++++++
 test/socks4server.py |  14 +++
 test/sockstest.py    | 185 +++++++++++++++++++++++++++++++
 test/test.sh         |  25 +++++
 12 files changed, 880 insertions(+), 36 deletions(-)

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..04b6b1f
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,22 @@
+Copyright 2006 Dan-Haim. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without modification,
+are permitted provided that the following conditions are met:
+1. Redistributions of source code must retain the above copyright notice, this
+   list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+3. Neither the name of Dan Haim nor the names of his contributors may be used
+   to endorse or promote products derived from this software without specific
+   prior written permission.
+   
+THIS SOFTWARE IS PROVIDED BY DAN HAIM "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+EVENT SHALL DAN HAIM OR HIS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA
+OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMANGE.
diff --git a/PKG-INFO b/PKG-INFO
index 8c9058e..e70e6ad 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.0
 Name: PySocks
-Version: 1.5.6
+Version: 1.5.7
 Summary: A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information.
 Home-page: https://github.com/Anorov/PySocks
 Author: Anorov
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..f67a026
--- /dev/null
+++ b/README.md
@@ -0,0 +1,299 @@
+PySocks
+=======
+
+Updated and semi-actively maintained version of [SocksiPy](http://socksipy.sourceforge.net/), with bug fixes and extra features.
+
+Acts as a drop-in replacement to the socket module.
+
+----------------
+
+Features
+========
+
+* SOCKS proxy client for Python 2.6 - 3.x
+* TCP and UDP both supported
+* HTTP proxy client included but not supported or recommended (you should use urllib2's or requests' own HTTP proxy interface)
+* urllib2 handler included. `pip install` / `setup.py install` will automatically install the `sockshandler` module.
+
+Installation
+============
+
+    pip install PySocks
+
+Or download the tarball / `git clone` and...
+
+    python setup.py install
+
+These will install both the `socks` and `sockshandler` modules.
+
+Alternatively, include just `socks.py` in your project.
+
+--------------------------------------------
+
+*Warning:* PySocks/SocksiPy only supports HTTP proxies that use CONNECT tunneling. Certain HTTP proxies may not work with this library. If you wish to use HTTP (not SOCKS) proxies, it is recommended that you rely on your HTTP client's native proxy support (`proxies` dict for `requests`, or `urllib2.ProxyHandler` for `urllib2`) instead.
+
+--------------------------------------------
+
+Usage
+=====
+
+## socks.socksocket ##
+
+    import socks
+
+    s = socks.socksocket() # Same API as socket.socket in the standard lib
+
+    s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default
+    # Or
+    s.set_proxy(socks.SOCKS4, "localhost", 4444)
+    # Or
+    s.set_proxy(socks.HTTP, "5.5.5.5", 8888)
+
+    # Can be treated identical to a regular socket object
+    s.connect(("www.somesite.com", 80))
+    s.sendall("GET / HTTP/1.1 ...")
+    print s.recv(4096)
+
+## Monkeypatching ##
+
+To monkeypatch the entire standard library with a single default proxy:
+
+    import urllib2
+    import socket
+    import socks
+
+    socks.set_default_proxy(socks.SOCKS5, "localhost")
+    socket.socket = socks.socksocket
+
+    urllib2.urlopen("http://www.somesite.com/") # All requests will pass through the SOCKS proxy
+
+Note that monkeypatching may not work for all standard modules or for all third party modules, and generally isn't recommended. Monkeypatching is usually an anti-pattern in Python.
+
+## urllib2 Handler ##
+
+Example use case with the `sockshandler` urllib2 handler. Note that you must import both `socks` and `sockshandler`, as the handler is its own module separate from PySocks. The module is included in the PyPI package.
+
+    import urllib2
+    import socks
+    from sockshandler import SocksiPyHandler
+
+    opener = urllib2.build_opener(SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 9050))
+    print opener.open("http://www.somesite.com/") # All requests made by the opener will pass through the SOCKS proxy
+
+--------------------------------------------
+
+Original SocksiPy README attached below, amended to reflect API changes.
+
+--------------------------------------------
+
+SocksiPy
+
+A Python SOCKS module.
+
+(C) 2006 Dan-Haim. All rights reserved.
+
+See LICENSE file for details.
+
+
+*WHAT IS A SOCKS PROXY?*
+
+A SOCKS proxy is a proxy server at the TCP level. In other words, it acts as
+a tunnel, relaying all traffic going through it without modifying it.
+SOCKS proxies can be used to relay traffic using any network protocol that
+uses TCP.
+
+*WHAT IS SOCKSIPY?*
+
+This Python module allows you to create TCP connections through a SOCKS
+proxy without any special effort.
+It also supports relaying UDP packets with a SOCKS5 proxy.
+
+*PROXY COMPATIBILITY*
+
+SocksiPy is compatible with three different types of proxies:
+
+1. SOCKS Version 4 (SOCKS4), including the SOCKS4a extension.
+2. SOCKS Version 5 (SOCKS5).
+3. HTTP Proxies which support tunneling using the CONNECT method.
+
+*SYSTEM REQUIREMENTS*
+
+Being written in Python, SocksiPy can run on any platform that has a Python
+interpreter and TCP/IP support.
+This module has been tested with Python 2.3 and should work with greater versions
+just as well.
+
+
+INSTALLATION
+-------------
+
+Simply copy the file "socks.py" to your Python's `lib/site-packages` directory,
+and you're ready to go. [Editor's note: it is better to use `python setup.py install` for PySocks]
+
+
+USAGE
+------
+
+First load the socks module with the command:
+
+    >>> import socks
+    >>>
+
+The socks module provides a class called `socksocket`, which is the base to all of the module's functionality.
+
+The `socksocket` object has the same initialization parameters as the normal socket
+object to ensure maximal compatibility, however it should be noted that `socksocket` will only function with family being `AF_INET` and
+type being either `SOCK_STREAM` or `SOCK_DGRAM`.
+Generally, it is best to initialize the `socksocket` object with no parameters
+
+    >>> s = socks.socksocket()
+    >>>
+
+The `socksocket` object has an interface which is very similiar to socket's (in fact
+the `socksocket` class is derived from socket) with a few extra methods.
+To select the proxy server you would like to use, use the `set_proxy` method, whose
+syntax is:
+
+    set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])
+
+Explanation of the parameters:
+
+`proxy_type` - The type of the proxy server. This can be one of three possible
+choices: `PROXY_TYPE_SOCKS4`, `PROXY_TYPE_SOCKS5` and `PROXY_TYPE_HTTP` for SOCKS4,
+SOCKS5 and HTTP servers respectively. `SOCKS4`, `SOCKS5`, and `HTTP` are all aliases, respectively.
+
+`addr` - The IP address or DNS name of the proxy server.
+
+`port` - The port of the proxy server. Defaults to 1080 for socks and 8080 for http.
+
+`rdns` - This is a boolean flag than modifies the behavior regarding DNS resolving.
+If it is set to True, DNS resolving will be preformed remotely, on the server.
+If it is set to False, DNS resolving will be preformed locally. Please note that
+setting this to True with SOCKS4 servers actually use an extension to the protocol,
+called SOCKS4a, which may not be supported on all servers (SOCKS5 and http servers
+always support DNS). The default is True.
+
+`username` - For SOCKS5 servers, this allows simple username / password authentication
+with the server. For SOCKS4 servers, this parameter will be sent as the userid.
+This parameter is ignored if an HTTP server is being used. If it is not provided,
+authentication will not be used (servers may accept unauthenticated requests).
+
+`password` - This parameter is valid only for SOCKS5 servers and specifies the
+respective password for the username provided.
+
+Example of usage:
+
+    >>> s.set_proxy(socks.SOCKS5, "socks.example.com") # uses default port 1080
+    >>> s.set_proxy(socks.SOCKS4, "socks.test.com", 1081)
+
+After the set_proxy method has been called, simply call the connect method with the
+traditional parameters to establish a connection through the proxy:
+
+    >>> s.connect(("www.sourceforge.net", 80))
+    >>>
+
+Connection will take a bit longer to allow negotiation with the proxy server.
+Please note that calling connect without calling `set_proxy` earlier will connect
+without a proxy (just like a regular socket).
+
+Errors: Any errors in the connection process will trigger exceptions. The exception
+may either be generated by the underlying socket layer or may be custom module
+exceptions, whose details follow:
+
+class `ProxyError` - This is a base exception class. It is not raised directly but
+rather all other exception classes raised by this module are derived from it.
+This allows an easy way to catch all proxy-related errors. It descends from `IOError`.
+
+All `ProxyError` exceptions have an attribute `socket_err`, which will contain either a
+caught `socket.error` exception, or `None` if there wasn't any.
+
+class `GeneralProxyError` - When thrown, it indicates a problem which does not fall
+into another category.
+
+* `Sent invalid data` - This error means that unexpected data has been received from
+the server. The most common reason is that the server specified as the proxy is
+not really a SOCKS4/SOCKS5/HTTP proxy, or maybe the proxy type specified is wrong.
+
+* `Connection closed unexpectedly` - The proxy server unexpectedly closed the connection.
+This may indicate that the proxy server is experiencing network or software problems.
+
+* `Bad proxy type` - This will be raised if the type of the proxy supplied to the
+set_proxy function was not one of `SOCKS4`/`SOCKS5`/`HTTP`.
+
+* `Bad input` - This will be raised if the `connect()` method is called with bad input
+parameters.
+
+class `SOCKS5AuthError` - This indicates that the connection through a SOCKS5 server
+failed due to an authentication problem.
+
+* `Authentication is required` - This will happen if you use a SOCKS5 server which
+requires authentication without providing a username / password at all.
+
+* `All offered authentication methods were rejected` - This will happen if the proxy
+requires a special authentication method which is not supported by this module.
+
+* `Unknown username or invalid password` - Self descriptive.
+
+class `SOCKS5Error` - This will be raised for SOCKS5 errors which are not related to
+authentication.
+The parameter is a tuple containing a code, as given by the server,
+and a description of the
+error. The possible errors, according to the RFC, are:
+
+* `0x01` - General SOCKS server failure - If for any reason the proxy server is unable to
+fulfill your request (internal server error).
+* `0x02` - connection not allowed by ruleset - If the address you're trying to connect to
+is blacklisted on the server or requires authentication.
+* `0x03` - Network unreachable - The target could not be contacted. A router on the network
+had replied with a destination net unreachable error.
+* `0x04` - Host unreachable - The target could not be contacted. A router on the network
+had replied with a destination host unreachable error.
+* `0x05` - Connection refused - The target server has actively refused the connection
+(the requested port is closed).
+* `0x06` - TTL expired - The TTL value of the SYN packet from the proxy to the target server
+has expired. This usually means that there are network problems causing the packet
+to be caught in a router-to-router "ping-pong".
+* `0x07` - Command not supported - For instance if the server does not support UDP.
+* `0x08` - Address type not supported - The client has provided an invalid address type.
+When using this module, this error should not occur.
+
+class `SOCKS4Error` - This will be raised for SOCKS4 errors. The parameter is a tuple
+containing a code and a description of the error, as given by the server. The
+possible error, according to the specification are:
+
+* `0x5B` - Request rejected or failed - Will be raised in the event of an failure for any
+reason other then the two mentioned next.
+* `0x5C` - request rejected because SOCKS server cannot connect to identd on the client -
+The Socks server had tried an ident lookup on your computer and has failed. In this
+case you should run an identd server and/or configure your firewall to allow incoming
+connections to local port 113 from the remote server.
+* `0x5D` - request rejected because the client program and identd report different user-ids -
+The Socks server had performed an ident lookup on your computer and has received a
+different userid than the one you have provided. Change your userid (through the
+username parameter of the set_proxy method) to match and try again.
+
+class `HTTPError` - This will be raised for HTTP errors. The message will contain
+the HTTP status code and provided error message.
+
+After establishing the connection, the object behaves like a standard socket.
+
+Methods like `makefile()` and `settimeout()` should behave just like regular sockets.
+Call the `close()` method to close the connection.
+
+In addition to the `socksocket` class, an additional function worth mentioning is the
+`set_default_proxy` function. The parameters are the same as the `set_proxy` method.
+This function will set default proxy settings for newly created `socksocket` objects,
+in which the proxy settings haven't been changed via the `set_proxy` method.
+This is quite useful if you wish to force 3rd party modules to use a SOCKS proxy,
+by overriding the socket object.
+For example:
+
+    >>> socks.set_default_proxy(socks.SOCKS5, "socks.example.com")
+    >>> socket.socket = socks.socksocket
+    >>> urllib.urlopen("http://www.sourceforge.net/")
+
+
+PROBLEMS
+---------
+
+Please open a GitHub issue at https://github.com/Anorov/PySocks
diff --git a/setup.py b/setup.py
index 0a1c6cb..fb3ef0a 100755
--- a/setup.py
+++ b/setup.py
@@ -1,7 +1,7 @@
 #!/usr/bin/env python
 from distutils.core import setup
 
-VERSION = "1.5.6"
+VERSION = "1.5.7"
 
 setup(
     name = "PySocks",
diff --git a/socks.py b/socks.py
index 56bfca8..1858d86 100644
--- a/socks.py
+++ b/socks.py
@@ -1,6 +1,6 @@
 """
 SocksiPy - Python SOCKS module.
-Version 1.5.6
+Version 1.5.7
 
 Copyright 2006 Dan-Haim. All rights reserved.
 
@@ -52,7 +52,7 @@ Modifications made by Anorov (https://github.com/Anorov)
 -Various small bug fixes
 """
 
-__version__ = "1.5.6"
+__version__ = "1.5.7"
 
 import socket
 import struct
@@ -60,6 +60,7 @@ from errno import EOPNOTSUPP, EINVAL, EAGAIN
 from io import BytesIO
 from os import SEEK_CUR
 from collections import Callable
+from base64 import b64encode
 
 PROXY_TYPE_SOCKS4 = SOCKS4 = 1
 PROXY_TYPE_SOCKS5 = SOCKS5 = 2
@@ -162,20 +163,48 @@ def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
     source_address - tuple (host, port) for the socket to bind to as its source
     address before connecting (only for compatibility)
     """
-    sock = socksocket()
-    if socket_options is not None:
-        for opt in socket_options:
-            sock.setsockopt(*opt)
-    if isinstance(timeout, (int, float)):
-        sock.settimeout(timeout)
-    if proxy_type is not None:
-        sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns,
-                       proxy_username, proxy_password)
-    if source_address is not None:
-        sock.bind(source_address)
-
-    sock.connect(dest_pair)
-    return sock
+    # Remove IPv6 brackets on the remote address and proxy address.
+    remote_host, remote_port = dest_pair
+    if remote_host.startswith('['):
+        remote_host = remote_host.strip('[]')
+    if proxy_addr and proxy_addr.startswith('['):
+        proxy_addr = proxy_addr.strip('[]')
+
+    err = None
+
+    # Allow the SOCKS proxy to be on IPv4 or IPv6 addresses.
+    for r in socket.getaddrinfo(proxy_addr, proxy_port, 0, socket.SOCK_STREAM):
+        family, socket_type, proto, canonname, sa = r
+        sock = None
+        try:
+            sock = socksocket(family, socket_type, proto)
+
+            if socket_options is not None:
+                for opt in socket_options:
+                    sock.setsockopt(*opt)
+
+            if isinstance(timeout, (int, float)):
+                sock.settimeout(timeout)
+
+            if proxy_type is not None:
+                sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns,
+                               proxy_username, proxy_password)
+            if source_address is not None:
+                sock.bind(source_address)
+
+            sock.connect((remote_host, remote_port))
+            return sock
+
+        except socket.error as e:
+            err = e
+            if sock is not None:
+                sock.close()
+                sock = None
+
+    if err is not None:
+        raise err
+
+    raise socket.error("gai returned empty list.")
 
 class _BaseSocket(socket.socket):
     """Allows Python 2's "delegated" methods such as send() to be overridden
@@ -478,25 +507,38 @@ class socksocket(_BaseSocket):
         """
         host, port = addr
         proxy_type, _, _, rdns, username, password = self.proxy
+        family_to_byte = {socket.AF_INET: b"\x01", socket.AF_INET6: b"\x04"}
 
         # If the given destination address is an IP address, we'll
-        # use the IPv4 address request even if remote resolving was specified.
-        try:
-            addr_bytes = socket.inet_aton(host)
-            file.write(b"\x01" + addr_bytes)
-            host = socket.inet_ntoa(addr_bytes)
-        except socket.error:
-            # Well it's not an IP number, so it's probably a DNS name.
-            if rdns:
-                # Resolve remotely
-                host_bytes = host.encode('idna')
-                file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes)
-            else:
-                # Resolve locally
-                addr_bytes = socket.inet_aton(socket.gethostbyname(host))
-                file.write(b"\x01" + addr_bytes)
-                host = socket.inet_ntoa(addr_bytes)
+        # use the IP address request even if remote resolving was specified.
+        # Detect whether the address is IPv4/6 directly.
+        for family in (socket.AF_INET, socket.AF_INET6):
+            try:
+                addr_bytes = socket.inet_pton(family, host)
+                file.write(family_to_byte[family] + addr_bytes)
+                host = socket.inet_ntop(family, addr_bytes)
+                file.write(struct.pack(">H", port))
+                return host, port
+            except socket.error:
+                continue
 
+        # Well it's not an IP number, so it's probably a DNS name.
+        if rdns:
+            # Resolve remotely
+            host_bytes = host.encode('idna')
+            file.write(b"\x03" + chr(len(host_bytes)).encode() + host_bytes)
+        else:
+            # Resolve locally
+            addresses = socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG)
+            # We can't really work out what IP is reachable, so just pick the
+            # first.
+            target_addr = addresses[0]
+            family = target_addr[0]
+            host = target_addr[4][0]
+
+            addr_bytes = socket.inet_pton(family, host)
+            file.write(family_to_byte[family] + addr_bytes)
+            host = socket.inet_ntop(family, addr_bytes)
         file.write(struct.pack(">H", port))
         return host, port
 
@@ -507,6 +549,8 @@ class socksocket(_BaseSocket):
         elif atyp == b"\x03":
             length = self._readall(file, 1)
             addr = self._readall(file, ord(length))
+        elif atyp == b"\x04":
+            addr = socket.inet_ntop(socket.AF_INET6, self._readall(file, 16))
         else:
             raise GeneralProxyError("SOCKS5 proxy server sent invalid data")
 
@@ -582,8 +626,17 @@ class socksocket(_BaseSocket):
         # If we need to resolve locally, we do this now
         addr = dest_addr if rdns else socket.gethostbyname(dest_addr)
 
-        self.sendall(b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() +
-                     b" HTTP/1.1\r\n" + b"Host: " + dest_addr.encode('idna') + b"\r\n\r\n")
+        http_headers = [
+            b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() + b" HTTP/1.1",
+            b"Host: " + dest_addr.encode('idna')
+        ]
+
+        if username and password:
+            http_headers.append(b"Proxy-Authorization: basic " + b64encode(username + b":" + password))
+
+        http_headers.append(b"\r\n")
+
+        self.sendall(b"\r\n".join(http_headers))
 
         # We just need the first line to check if the connection was successful
         fobj = self.makefile()
diff --git a/test/README b/test/README
new file mode 100644
index 0000000..e08608e
--- /dev/null
+++ b/test/README
@@ -0,0 +1,5 @@
+Very rudimentary tests for Python 2 and Python 3.
+
+Requirements: tornado, twisted (available through pip)
+
+./test.sh
diff --git a/test/httpproxy.py b/test/httpproxy.py
new file mode 100755
index 0000000..df0ad03
--- /dev/null
+++ b/test/httpproxy.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+#
+# Simple asynchronous HTTP proxy with tunnelling (CONNECT).
+#
+# GET/POST proxying based on
+# http://groups.google.com/group/python-tornado/msg/7bea08e7a049cf26
+#
+# Copyright (C) 2012 Senko Rasic <senko.rasic at dobarkod.hr>
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+import sys
+import socket
+
+import tornado.httpserver
+import tornado.ioloop
+import tornado.iostream
+import tornado.web
+import tornado.httpclient
+
+__all__ = ['ProxyHandler', 'run_proxy']
+
+
+class ProxyHandler(tornado.web.RequestHandler):
+    SUPPORTED_METHODS = ['GET', 'POST', 'CONNECT']
+
+    @tornado.web.asynchronous
+    def get(self):
+
+        def handle_response(response):
+            if response.error and not isinstance(response.error,
+                    tornado.httpclient.HTTPError):
+                self.set_status(500)
+                self.write('Internal server error:\n' + str(response.error))
+                self.finish()
+            else:
+                self.set_status(response.code)
+                for header in ('Date', 'Cache-Control', 'Server',
+                        'Content-Type', 'Location'):
+                    v = response.headers.get(header)
+                    if v:
+                        self.set_header(header, v)
+                if response.body:
+                    self.write(response.body)
+                self.finish()
+
+        req = tornado.httpclient.HTTPRequest(url=self.request.uri,
+            method=self.request.method, body=self.request.body,
+            headers=self.request.headers, follow_redirects=False,
+            allow_nonstandard_methods=True)
+
+        client = tornado.httpclient.AsyncHTTPClient()
+        try:
+            client.fetch(req, handle_response)
+        except tornado.httpclient.HTTPError as e:
+            if hasattr(e, 'response') and e.response:
+                self.handle_response(e.response)
+            else:
+                self.set_status(500)
+                self.write('Internal server error:\n' + str(e))
+                self.finish()
+
+    @tornado.web.asynchronous
+    def post(self):
+        return self.get()
+
+    @tornado.web.asynchronous
+    def connect(self):
+        host, port = self.request.uri.split(':')
+        client = self.request.connection.stream
+
+        def read_from_client(data):
+            upstream.write(data)
+
+        def read_from_upstream(data):
+            client.write(data)
+
+        def client_close(data=None):
+            if upstream.closed():
+                return
+            if data:
+                upstream.write(data)
+            upstream.close()
+
+        def upstream_close(data=None):
+            if client.closed():
+                return
+            if data:
+                client.write(data)
+            client.close()
+
+        def start_tunnel():
+            client.read_until_close(client_close, read_from_client)
+            upstream.read_until_close(upstream_close, read_from_upstream)
+            client.write(b'HTTP/1.0 200 Connection established\r\n\r\n')
+
+        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
+        upstream = tornado.iostream.IOStream(s)
+        upstream.connect((host, int(port)), start_tunnel)
+
+
+def run_proxy(port=8080, start_ioloop=True):
+    """
+    Run proxy on the specified port. If start_ioloop is True (default),
+    the tornado IOLoop will be started immediately.
+    """
+    app = tornado.web.Application([
+        (r'.*', ProxyHandler),
+    ])
+    app.listen(port, address="127.0.0.1")
+    ioloop = tornado.ioloop.IOLoop.instance()
+    if start_ioloop:
+        ioloop.start()
+
+if __name__ == '__main__':
+    port = 8081
+    if len(sys.argv) > 1:
+        port = int(sys.argv[1])
+
+    print ("Running HTTP proxy server")
+    run_proxy(port)
diff --git a/test/mocks b/test/mocks
new file mode 100755
index 0000000..5299a3f
Binary files /dev/null and b/test/mocks differ
diff --git a/test/mocks.conf b/test/mocks.conf
new file mode 100644
index 0000000..ab5ef59
--- /dev/null
+++ b/test/mocks.conf
@@ -0,0 +1,104 @@
+#################################################
+#                                               #
+# Sample configuration file for MOCKS 0.0.2     #
+# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     #
+#                                               #
+# I recommend reading the examples in this file #
+# and then extending it to suite your needs.    #
+#                                               #
+#################################################
+
+
+
+#########################
+#                       
+# General daemon config 
+# ~~~~~~~~~~~~~~~~~~~~~ 
+#                       
+#########################
+
+PORT 			= 1081       # Port MOCKS is to listen to
+MOCKS_ADDR              = 127.0.0.1     # IP adress MOCKS is to bind to
+LOG_FILE 		= mocks.log   # MOCKS log file
+PID_FILE 		= mocks.pid   # File holding MOCKS's process ID
+BUFFER_SIZE 		= 65536       # Traffic buffer size in bytes
+BACKLOG     		= 5           # Backlog for listen()
+NEGOTIATION_TIMEOUT 	= 5           
+CONNECTION_IDLE_TIMEOUT = 300
+BIND_TIMEOUT		= 30
+SHUTDOWN_TIMEOUT	= 3
+MAX_CONNECTIONS	        = 50
+
+
+
+##########################################################################
+#
+# Client filter config
+# ~~~~~~~~~~~~~~~~~~~~
+#
+#     Client filtering means sorting out which clients are allowed
+# connection and which are not. This is basically done like this:
+# MOCKS has a default behaviour regarding filtering client
+# connections. This behaviour is called the 'policy' and can either
+# be to ALLOW or to DENY the connection. After setting the policy
+# you can specify a list of exceptions. The action MOCKS takes
+# for a client matching any of these exceptions is the opposite
+# of the policy (that is, if the policy is set to ALLOW the exceptions
+# are denied and if the policy is set to DENY the exceptions are allowed).
+#     An exception is specified in the form ip_address/mask, where mask
+# is optional and is an integer ranging from 0 to 32 identifying the
+# number of common heading bits that ip_address and the client's IP
+# address must have in order to yield a match. If mask is missing,
+# 32 will be assumed. For instance, 192.168.1.0/24 will match any IP
+# ranging from 192.168.1.1 to 192.168.1.255.
+#
+#     Let's take two examples, one for each type of policy. Let's say we
+# only want to allow IPs 10.12.0.0 through 10.12.255.255, 172.23.2.5 and
+# 192.168.52.26 to use MOCKS. What we have to to is this:
+#
+# FILTER_POLICY    = DENY
+# FILTER_EXCEPTION = 10.12.0.0/16
+# FILTER_EXCEPTION = 172.23.2.5     # implied /32
+# FILTER_EXCEPTION = 192.168.52.26  # implied /32
+#
+#     Now, let's say this is a public proxy server, but for some reason
+# you don't want to let any IP ranging from 192.168.1.1 to 192.168.1.255
+# and neither 10.2.5.13 to connect to it:
+#
+# FILTER_POLICY    = ALLOW
+# FILTER_EXCEPTION = 192.168.1.0/24
+# FILTER_EXCEPTION = 10.2.5.13
+#
+###########################################################################
+
+FILTER_POLICY    = ALLOW
+
+
+
+#############################################################################
+#
+# Upstream proxy config
+# ~~~~~~~~~~~~~~~~~~~~~
+# 
+#     You can choose to further relay traffic through another proxy server.
+# MOCKS supports upstream HTTP CONNECT, SOCKS4 and SOCKS5 proxies. You
+# must specify the proxy type (one of HTTPCONNECT, SOCKS4 or SOCKS5), the
+# proxy address and the proxy port. Optionally you can specify an user
+# name and a password used to authenicate to the upstream proxy. This is
+# pretty straight forward, so let's just take an example. Let's say you
+# want to use the HTTP CONNECT server at httpconnectproxy.com, on port 3128,
+# using the username 'foo' and the password 'bar'. You do it like this:
+#
+# UP_PROXY_TYPE   = HTTPCONNECT
+# UP_PROXY_ADDR   = httpconnectproxy.com
+# UP_PROXY_PORT   = 3128
+# UP_PROXY_USER   = foo                   # These two can be missing if you
+# UP_PROXY_PASSWD = bar                   # are not required to authenticate
+#
+#############################################################################
+
+# UP_PROXY_TYPE   = HTTPCONNECT
+# UP_PROXY_ADDR   = 192.168.1.12
+# UP_PROXY_PORT   = 3128
+
+
diff --git a/test/socks4server.py b/test/socks4server.py
new file mode 100755
index 0000000..05a54b9
--- /dev/null
+++ b/test/socks4server.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+from twisted.internet import reactor
+from twisted.protocols.socks import SOCKSv4Factory
+
+def run_proxy():
+    reactor.listenTCP(1080, SOCKSv4Factory("/dev/null"), interface="127.0.0.1")
+    try:
+        reactor.run()
+    except (KeyboardInterrupt, SystemExit):
+        reactor.stop()
+
+if __name__ == "__main__":
+    print "Running SOCKS4 proxy server"
+    run_proxy()
diff --git a/test/sockstest.py b/test/sockstest.py
new file mode 100644
index 0000000..0684b76
--- /dev/null
+++ b/test/sockstest.py
@@ -0,0 +1,185 @@
+import sys
+sys.path.append("..")
+import socks
+import socket
+
+PY3K = sys.version_info[0] == 3
+
+if PY3K:
+    import urllib.request as urllib2
+else:
+    import sockshandler
+    import urllib2
+
+def raw_HTTP_request():
+    req = "GET /ip HTTP/1.1\r\n"
+    req += "Host: ifconfig.me\r\n"
+    req += "User-Agent: Mozilla\r\n"
+    req += "Accept: text/html\r\n"
+    req += "\r\n"
+    return req.encode()
+
+def socket_HTTP_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.HTTP, "127.0.0.1", 8081)
+    s.connect(("ifconfig.me", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def socket_SOCKS4_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080)
+    s.connect(("ifconfig.me", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def socket_SOCKS5_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
+    s.connect(("ifconfig.me", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def SOCKS5_connect_timeout_test():
+    s = socks.socksocket()
+    s.settimeout(0.0001)
+    s.set_proxy(socks.SOCKS5, "8.8.8.8", 80)
+    try:
+        s.connect(("ifconfig.me", 80))
+    except socks.ProxyConnectionError as e:
+        assert str(e.socket_err) == "timed out"
+
+def SOCKS5_timeout_test():
+    s = socks.socksocket()
+    s.settimeout(0.0001)
+    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
+    try:
+        s.connect(("ifconfig.me", 4444))
+    except socks.GeneralProxyError as e:
+        assert str(e.socket_err) == "timed out"
+
+
+def socket_SOCKS5_auth_test():
+    # TODO: add support for this test. Will need a better SOCKS5 server.
+    s = socks.socksocket()
+    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081, username="a", password="b")
+    s.connect(("ifconfig.me", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def socket_HTTP_IP_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.HTTP, "127.0.0.1", 8081)
+    s.connect(("133.242.129.236", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def socket_SOCKS4_IP_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.SOCKS4, "127.0.0.1", 1080)
+    s.connect(("133.242.129.236", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def socket_SOCKS5_IP_test():
+    s = socks.socksocket()
+    s.set_proxy(socks.SOCKS5, "127.0.0.1", 1081)
+    s.connect(("133.242.129.236", 80))
+    s.sendall(raw_HTTP_request())
+    status = s.recv(2048).splitlines()[0]
+    assert status.startswith(b"HTTP/1.1 200")
+
+def urllib2_HTTP_test():
+    socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081)
+    socks.wrap_module(urllib2)
+    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
+    assert status == 200
+
+def urllib2_SOCKS5_test():
+    socks.set_default_proxy(socks.SOCKS5, "127.0.0.1", 1081)
+    socks.wrap_module(urllib2)
+    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
+    assert status == 200
+
+def urllib2_handler_HTTP_test():
+    opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.HTTP, "127.0.0.1", 8081))
+    status = opener.open("http://ifconfig.me/ip").getcode()
+    assert status == 200
+
+def urllib2_handler_SOCKS5_test():
+    opener = urllib2.build_opener(sockshandler.SocksiPyHandler(socks.SOCKS5, "127.0.0.1", 1081))
+    status = opener.open("http://ifconfig.me/ip").getcode()
+    assert status == 200
+
+def global_override_HTTP_test():
+    socks.set_default_proxy(socks.HTTP, "127.0.0.1", 8081)
+    good = socket.socket
+    socket.socket = socks.socksocket
+    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
+    socket.socket = good
+    assert status == 200
+
+def global_override_SOCKS5_test():
+    default_proxy = (socks.SOCKS5, "127.0.0.1", 1081)
+    socks.set_default_proxy(*default_proxy)
+    good = socket.socket
+    socket.socket = socks.socksocket
+    status = urllib2.urlopen("http://ifconfig.me/ip").getcode()
+    socket.socket = good
+    assert status == 200
+    assert socks.get_default_proxy()[1].decode() == default_proxy[1]
+
+def bail_early_with_ipv6_test():
+    sock = socks.socksocket()
+    ipv6_tuple = addr, port, flowinfo, scopeid = "::1", 1234, 0, 0
+    try:
+        sock.connect(ipv6_tuple)
+    except socket.error:
+        return
+    else:
+        assert False, "was expecting"
+
+def main():
+    print("Running tests...")
+    socket_HTTP_test()
+    print("1/13")
+    socket_SOCKS4_test()
... 64 lines suppressed ...

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-socksipy.git



More information about the Python-modules-commits mailing list