Rev2027, Rollback PySocks as it does not works well with Tor/gevent
This commit is contained in:
parent
3384bd1e58
commit
1144f28982
|
@ -10,7 +10,7 @@ class Config(object):
|
||||||
|
|
||||||
def __init__(self, argv):
|
def __init__(self, argv):
|
||||||
self.version = "0.5.3"
|
self.version = "0.5.3"
|
||||||
self.rev = 2026
|
self.rev = 2027
|
||||||
self.argv = argv
|
self.argv = argv
|
||||||
self.action = None
|
self.action = None
|
||||||
self.config_file = "zeronet.conf"
|
self.config_file = "zeronet.conf"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
PySocks
|
PySocks
|
||||||
=======
|
=======
|
||||||
|
|
||||||
Updated and semi-actively maintained version of [SocksiPy](http://socksipy.sourceforge.net/), with bug fixes and extra features.
|
Updated version of SocksiPy. Many old bugs fixed, and overall code cleanup.
|
||||||
|
|
||||||
Acts as a drop-in replacement to the socket module.
|
Acts as a drop-in replacement to the socket module.
|
||||||
|
|
||||||
|
@ -10,10 +10,23 @@ Acts as a drop-in replacement to the socket module.
|
||||||
Features
|
Features
|
||||||
========
|
========
|
||||||
|
|
||||||
* SOCKS proxy client for Python 2.6 - 3.x
|
* Fully supports Python 2.6 - 3.4
|
||||||
* 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)
|
* UDP support
|
||||||
* urllib2 handler included. `pip install` / `setup.py install` will automatically install the `sockshandler` module.
|
|
||||||
|
* SocksiPyHandler, courtesy e000, was also added as an example of how this module can be used with urllib2. See example code in sockshandler.py. `pip install` and `setup.py install` will automatically install the `sockshandler` module.
|
||||||
|
|
||||||
|
* Bugs in the original SocksiPy were fixed, including two that could lead to infinite hanging when communicating with bad proxy servers.
|
||||||
|
|
||||||
|
* urllib3, which powers the requests module, is working on integrating SOCKS proxy support based on this branch
|
||||||
|
|
||||||
|
* `SOCKS5`, `SOCKS4`, and `HTTP` are now aliases for `PROXY_TYPE_SOCKS5`, `PROXY_TYPE_SOCKS4`, and `PROXY_TYPE_HTTP`
|
||||||
|
|
||||||
|
* Tests added
|
||||||
|
|
||||||
|
* Various style and performance improvements; codebase simplified
|
||||||
|
|
||||||
|
* Actively maintained
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
============
|
============
|
||||||
|
@ -30,18 +43,18 @@ 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.
|
*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 proxies (and 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
|
Usage
|
||||||
=====
|
=====
|
||||||
|
|
||||||
## socks.socksocket ##
|
## Example ##
|
||||||
|
|
||||||
import socks
|
import socks
|
||||||
|
|
||||||
s = socks.socksocket() # Same API as socket.socket in the standard lib
|
s = socks.socksocket()
|
||||||
|
|
||||||
s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default
|
s.set_proxy(socks.SOCKS5, "localhost") # SOCKS4 and SOCKS5 use port 1080 by default
|
||||||
# Or
|
# Or
|
||||||
|
@ -50,35 +63,23 @@ Usage
|
||||||
s.set_proxy(socks.HTTP, "5.5.5.5", 8888)
|
s.set_proxy(socks.HTTP, "5.5.5.5", 8888)
|
||||||
|
|
||||||
# Can be treated identical to a regular socket object
|
# Can be treated identical to a regular socket object
|
||||||
s.connect(("www.somesite.com", 80))
|
s.connect(("www.test.com", 80))
|
||||||
s.sendall("GET / HTTP/1.1 ...")
|
s.sendall("GET / ...")
|
||||||
print s.recv(4096)
|
print s.recv(4096)
|
||||||
|
|
||||||
## Monkeypatching ##
|
|
||||||
|
|
||||||
To monkeypatch the entire standard library with a single default proxy:
|
To monkeypatch the entire standard library with a single default proxy:
|
||||||
|
|
||||||
import urllib2
|
|
||||||
import socket
|
import socket
|
||||||
import socks
|
import socks
|
||||||
|
import urllib2
|
||||||
|
|
||||||
socks.set_default_proxy(socks.SOCKS5, "localhost")
|
socks.set_default_proxy(socks.SOCKS5, "localhost")
|
||||||
socket.socket = socks.socksocket
|
socket.socket = socks.socksocket
|
||||||
|
|
||||||
urllib2.urlopen("http://www.somesite.com/") # All requests will pass through the SOCKS proxy
|
urllib2.urlopen("http://...") # 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.
|
Note that monkeypatching may not work for all standard modules or for all third party modules, and generally isn't recommended.
|
||||||
|
|
||||||
## 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
|
|
||||||
|
|
||||||
--------------------------------------------
|
--------------------------------------------
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ Original SocksiPy README attached below, amended to reflect API changes.
|
||||||
|
|
||||||
--------------------------------------------
|
--------------------------------------------
|
||||||
|
|
||||||
SocksiPy
|
SocksiPy - version 1.5.0
|
||||||
|
|
||||||
A Python SOCKS module.
|
A Python SOCKS module.
|
||||||
|
|
||||||
|
@ -276,7 +277,6 @@ class `HTTPError` - This will be raised for HTTP errors. The message will contai
|
||||||
the HTTP status code and provided error message.
|
the HTTP status code and provided error message.
|
||||||
|
|
||||||
After establishing the connection, the object behaves like a standard socket.
|
After establishing the connection, the object behaves like a standard socket.
|
||||||
|
|
||||||
Methods like `makefile()` and `settimeout()` should behave just like regular sockets.
|
Methods like `makefile()` and `settimeout()` should behave just like regular sockets.
|
||||||
Call the `close()` method to close the connection.
|
Call the `close()` method to close the connection.
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
from setuptools import setup
|
from distutils.core import setup
|
||||||
|
|
||||||
VERSION = "1.6.7"
|
VERSION = "1.5.3"
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name = "PySocks",
|
name = "PySocks",
|
||||||
|
@ -14,3 +14,4 @@ setup(
|
||||||
keywords = ["socks", "proxy"],
|
keywords = ["socks", "proxy"],
|
||||||
py_modules=["socks", "sockshandler"]
|
py_modules=["socks", "sockshandler"]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
"""
|
"""
|
||||||
SocksiPy - Python SOCKS module.
|
SocksiPy - Python SOCKS module.
|
||||||
|
Version 1.5.3
|
||||||
|
|
||||||
Copyright 2006 Dan-Haim. All rights reserved.
|
Copyright 2006 Dan-Haim. All rights reserved.
|
||||||
|
|
||||||
|
@ -51,28 +52,14 @@ Modifications made by Anorov (https://github.com/Anorov)
|
||||||
-Various small bug fixes
|
-Various small bug fixes
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__version__ = "1.6.7"
|
__version__ = "1.5.3"
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
import struct
|
import struct
|
||||||
from errno import EOPNOTSUPP, EINVAL, EAGAIN
|
from errno import EOPNOTSUPP, EINVAL, EAGAIN
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from os import SEEK_CUR
|
from os import SEEK_CUR
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import functools
|
|
||||||
import logging
|
|
||||||
from collections import Callable
|
from collections import Callable
|
||||||
from base64 import b64encode
|
|
||||||
|
|
||||||
|
|
||||||
if os.name == "nt" and sys.version_info < (3, 0):
|
|
||||||
try:
|
|
||||||
import win_inet_pton
|
|
||||||
except ImportError:
|
|
||||||
raise ImportError("To run PySocks on Windows you must install win_inet_pton")
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
PROXY_TYPE_SOCKS4 = SOCKS4 = 1
|
PROXY_TYPE_SOCKS4 = SOCKS4 = 1
|
||||||
PROXY_TYPE_SOCKS5 = SOCKS5 = 2
|
PROXY_TYPE_SOCKS5 = SOCKS5 = 2
|
||||||
|
@ -83,25 +70,6 @@ PRINTABLE_PROXY_TYPES = dict(zip(PROXY_TYPES.values(), PROXY_TYPES.keys()))
|
||||||
|
|
||||||
_orgsocket = _orig_socket = socket.socket
|
_orgsocket = _orig_socket = socket.socket
|
||||||
|
|
||||||
|
|
||||||
def set_self_blocking(function):
|
|
||||||
|
|
||||||
@functools.wraps(function)
|
|
||||||
def wrapper(*args, **kwargs):
|
|
||||||
self = args[0]
|
|
||||||
try:
|
|
||||||
_is_blocking = self.gettimeout()
|
|
||||||
if _is_blocking == 0:
|
|
||||||
self.setblocking(True)
|
|
||||||
return function(*args, **kwargs)
|
|
||||||
except Exception as e:
|
|
||||||
raise
|
|
||||||
finally:
|
|
||||||
# set orgin blcoking
|
|
||||||
if _is_blocking == 0:
|
|
||||||
self.setblocking(False)
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
class ProxyError(IOError):
|
class ProxyError(IOError):
|
||||||
"""
|
"""
|
||||||
socket_err contains original socket.error exception.
|
socket_err contains original socket.error exception.
|
||||||
|
@ -154,10 +122,7 @@ def set_default_proxy(proxy_type=None, addr=None, port=None, rdns=True, username
|
||||||
username.encode() if username else None,
|
username.encode() if username else None,
|
||||||
password.encode() if password else None)
|
password.encode() if password else None)
|
||||||
|
|
||||||
def setdefaultproxy(*args, **kwargs):
|
setdefaultproxy = set_default_proxy
|
||||||
if 'proxytype' in kwargs:
|
|
||||||
kwargs['proxy_type'] = kwargs.pop('proxytype')
|
|
||||||
return set_default_proxy(*args, **kwargs)
|
|
||||||
|
|
||||||
def get_default_proxy():
|
def get_default_proxy():
|
||||||
"""
|
"""
|
||||||
|
@ -182,10 +147,9 @@ def wrap_module(module):
|
||||||
wrapmodule = wrap_module
|
wrapmodule = wrap_module
|
||||||
|
|
||||||
def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
|
def create_connection(dest_pair, proxy_type=None, proxy_addr=None,
|
||||||
proxy_port=None, proxy_rdns=True,
|
proxy_port=None, proxy_username=None,
|
||||||
proxy_username=None, proxy_password=None,
|
proxy_password=None, timeout=None,
|
||||||
timeout=None, source_address=None,
|
source_address=None):
|
||||||
socket_options=None):
|
|
||||||
"""create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object
|
"""create_connection(dest_pair, *[, timeout], **proxy_args) -> socket object
|
||||||
|
|
||||||
Like socket.create_connection(), but connects to proxy
|
Like socket.create_connection(), but connects to proxy
|
||||||
|
@ -197,48 +161,14 @@ 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
|
source_address - tuple (host, port) for the socket to bind to as its source
|
||||||
address before connecting (only for compatibility)
|
address before connecting (only for compatibility)
|
||||||
"""
|
"""
|
||||||
# Remove IPv6 brackets on the remote address and proxy address.
|
sock = socksocket()
|
||||||
remote_host, remote_port = dest_pair
|
if isinstance(timeout, (int, float)):
|
||||||
if remote_host.startswith('['):
|
sock.settimeout(timeout)
|
||||||
remote_host = remote_host.strip('[]')
|
if proxy_type is not None:
|
||||||
if proxy_addr and proxy_addr.startswith('['):
|
sock.set_proxy(proxy_type, proxy_addr, proxy_port,
|
||||||
proxy_addr = proxy_addr.strip('[]')
|
proxy_username, proxy_password)
|
||||||
|
sock.connect(dest_pair)
|
||||||
err = None
|
return sock
|
||||||
|
|
||||||
# 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:
|
|
||||||
for opt in socket_options:
|
|
||||||
sock.setsockopt(*opt)
|
|
||||||
|
|
||||||
if isinstance(timeout, (int, float)):
|
|
||||||
sock.settimeout(timeout)
|
|
||||||
|
|
||||||
if proxy_type:
|
|
||||||
sock.set_proxy(proxy_type, proxy_addr, proxy_port, proxy_rdns,
|
|
||||||
proxy_username, proxy_password)
|
|
||||||
if source_address:
|
|
||||||
sock.bind(source_address)
|
|
||||||
|
|
||||||
sock.connect((remote_host, remote_port))
|
|
||||||
return sock
|
|
||||||
|
|
||||||
except (socket.error, ProxyConnectionError) as e:
|
|
||||||
err = e
|
|
||||||
if sock:
|
|
||||||
sock.close()
|
|
||||||
sock = None
|
|
||||||
|
|
||||||
if err:
|
|
||||||
raise err
|
|
||||||
|
|
||||||
raise socket.error("gai returned empty list.")
|
|
||||||
|
|
||||||
class _BaseSocket(socket.socket):
|
class _BaseSocket(socket.socket):
|
||||||
"""Allows Python 2's "delegated" methods such as send() to be overridden
|
"""Allows Python 2's "delegated" methods such as send() to be overridden
|
||||||
|
@ -277,12 +207,12 @@ class socksocket(_BaseSocket):
|
||||||
|
|
||||||
default_proxy = None
|
default_proxy = None
|
||||||
|
|
||||||
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, *args, **kwargs):
|
def __init__(self, family=socket.AF_INET, type=socket.SOCK_STREAM, proto=0, _sock=None):
|
||||||
if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM):
|
if type not in (socket.SOCK_STREAM, socket.SOCK_DGRAM):
|
||||||
msg = "Socket type must be stream or datagram, not {!r}"
|
msg = "Socket type must be stream or datagram, not {!r}"
|
||||||
raise ValueError(msg.format(type))
|
raise ValueError(msg.format(type))
|
||||||
|
|
||||||
super(socksocket, self).__init__(family, type, proto, *args, **kwargs)
|
_BaseSocket.__init__(self, family, type, proto, _sock)
|
||||||
self._proxyconn = None # TCP connection to keep UDP relay alive
|
self._proxyconn = None # TCP connection to keep UDP relay alive
|
||||||
|
|
||||||
if self.default_proxy:
|
if self.default_proxy:
|
||||||
|
@ -292,8 +222,6 @@ class socksocket(_BaseSocket):
|
||||||
self.proxy_sockname = None
|
self.proxy_sockname = None
|
||||||
self.proxy_peername = None
|
self.proxy_peername = None
|
||||||
|
|
||||||
self._timeout = None
|
|
||||||
|
|
||||||
def _readall(self, file, count):
|
def _readall(self, file, count):
|
||||||
"""
|
"""
|
||||||
Receive EXACTLY the number of bytes requested from the file object.
|
Receive EXACTLY the number of bytes requested from the file object.
|
||||||
|
@ -307,24 +235,6 @@ class socksocket(_BaseSocket):
|
||||||
data += d
|
data += d
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def settimeout(self, timeout):
|
|
||||||
self._timeout = timeout
|
|
||||||
try:
|
|
||||||
# test if we're connected, if so apply timeout
|
|
||||||
peer = self.get_proxy_peername()
|
|
||||||
super(socksocket, self).settimeout(self._timeout)
|
|
||||||
except socket.error:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def gettimeout(self):
|
|
||||||
return self._timeout
|
|
||||||
|
|
||||||
def setblocking(self, v):
|
|
||||||
if v:
|
|
||||||
self.settimeout(None)
|
|
||||||
else:
|
|
||||||
self.settimeout(0.0)
|
|
||||||
|
|
||||||
def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None):
|
def set_proxy(self, proxy_type=None, addr=None, port=None, rdns=True, username=None, password=None):
|
||||||
"""set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])
|
"""set_proxy(proxy_type, addr[, port[, rdns[, username[, password]]]])
|
||||||
Sets the proxy to be used.
|
Sets the proxy to be used.
|
||||||
|
@ -347,10 +257,7 @@ class socksocket(_BaseSocket):
|
||||||
username.encode() if username else None,
|
username.encode() if username else None,
|
||||||
password.encode() if password else None)
|
password.encode() if password else None)
|
||||||
|
|
||||||
def setproxy(self, *args, **kwargs):
|
setproxy = set_proxy
|
||||||
if 'proxytype' in kwargs:
|
|
||||||
kwargs['proxy_type'] = kwargs.pop('proxytype')
|
|
||||||
return self.set_proxy(*args, **kwargs)
|
|
||||||
|
|
||||||
def bind(self, *pos, **kw):
|
def bind(self, *pos, **kw):
|
||||||
"""
|
"""
|
||||||
|
@ -366,7 +273,7 @@ class socksocket(_BaseSocket):
|
||||||
if proxy_type != SOCKS5:
|
if proxy_type != SOCKS5:
|
||||||
msg = "UDP only supported by SOCKS5 proxy type"
|
msg = "UDP only supported by SOCKS5 proxy type"
|
||||||
raise socket.error(EOPNOTSUPP, msg)
|
raise socket.error(EOPNOTSUPP, msg)
|
||||||
super(socksocket, self).bind(*pos, **kw)
|
_BaseSocket.bind(self, *pos, **kw)
|
||||||
|
|
||||||
# Need to specify actual local port because
|
# Need to specify actual local port because
|
||||||
# some relays drop packets if a port of zero is specified.
|
# some relays drop packets if a port of zero is specified.
|
||||||
|
@ -385,13 +292,12 @@ class socksocket(_BaseSocket):
|
||||||
# but some proxies return a private IP address (10.x.y.z)
|
# but some proxies return a private IP address (10.x.y.z)
|
||||||
host, _ = proxy
|
host, _ = proxy
|
||||||
_, port = relay
|
_, port = relay
|
||||||
super(socksocket, self).connect((host, port))
|
_BaseSocket.connect(self, (host, port))
|
||||||
super(socksocket, self).settimeout(self._timeout)
|
|
||||||
self.proxy_sockname = ("0.0.0.0", 0) # Unknown
|
self.proxy_sockname = ("0.0.0.0", 0) # Unknown
|
||||||
|
|
||||||
def sendto(self, bytes, *args, **kwargs):
|
def sendto(self, bytes, *args, **kwargs):
|
||||||
if self.type != socket.SOCK_DGRAM:
|
if self.type != socket.SOCK_DGRAM:
|
||||||
return super(socksocket, self).sendto(bytes, *args, **kwargs)
|
return _BaseSocket.sendto(self, bytes, *args, **kwargs)
|
||||||
if not self._proxyconn:
|
if not self._proxyconn:
|
||||||
self.bind(("", 0))
|
self.bind(("", 0))
|
||||||
|
|
||||||
|
@ -405,23 +311,23 @@ class socksocket(_BaseSocket):
|
||||||
header.write(STANDALONE)
|
header.write(STANDALONE)
|
||||||
self._write_SOCKS5_address(address, header)
|
self._write_SOCKS5_address(address, header)
|
||||||
|
|
||||||
sent = super(socksocket, self).send(header.getvalue() + bytes, *flags, **kwargs)
|
sent = _BaseSocket.send(self, header.getvalue() + bytes, *flags, **kwargs)
|
||||||
return sent - header.tell()
|
return sent - header.tell()
|
||||||
|
|
||||||
def send(self, bytes, flags=0, **kwargs):
|
def send(self, bytes, flags=0, **kwargs):
|
||||||
if self.type == socket.SOCK_DGRAM:
|
if self.type == socket.SOCK_DGRAM:
|
||||||
return self.sendto(bytes, flags, self.proxy_peername, **kwargs)
|
return self.sendto(bytes, flags, self.proxy_peername, **kwargs)
|
||||||
else:
|
else:
|
||||||
return super(socksocket, self).send(bytes, flags, **kwargs)
|
return _BaseSocket.send(self, bytes, flags, **kwargs)
|
||||||
|
|
||||||
def recvfrom(self, bufsize, flags=0):
|
def recvfrom(self, bufsize, flags=0):
|
||||||
if self.type != socket.SOCK_DGRAM:
|
if self.type != socket.SOCK_DGRAM:
|
||||||
return super(socksocket, self).recvfrom(bufsize, flags)
|
return _BaseSocket.recvfrom(self, bufsize, flags)
|
||||||
if not self._proxyconn:
|
if not self._proxyconn:
|
||||||
self.bind(("", 0))
|
self.bind(("", 0))
|
||||||
|
|
||||||
buf = BytesIO(super(socksocket, self).recv(bufsize + 1024, flags))
|
buf = BytesIO(_BaseSocket.recv(self, bufsize, flags))
|
||||||
buf.seek(2, SEEK_CUR)
|
buf.seek(+2, SEEK_CUR)
|
||||||
frag = buf.read(1)
|
frag = buf.read(1)
|
||||||
if ord(frag):
|
if ord(frag):
|
||||||
raise NotImplementedError("Received UDP packet fragment")
|
raise NotImplementedError("Received UDP packet fragment")
|
||||||
|
@ -432,7 +338,7 @@ class socksocket(_BaseSocket):
|
||||||
if fromhost != peerhost or peerport not in (0, fromport):
|
if fromhost != peerhost or peerport not in (0, fromport):
|
||||||
raise socket.error(EAGAIN, "Packet filtered")
|
raise socket.error(EAGAIN, "Packet filtered")
|
||||||
|
|
||||||
return (buf.read(bufsize), (fromhost, fromport))
|
return (buf.read(), (fromhost, fromport))
|
||||||
|
|
||||||
def recv(self, *pos, **kw):
|
def recv(self, *pos, **kw):
|
||||||
bytes, _ = self.recvfrom(*pos, **kw)
|
bytes, _ = self.recvfrom(*pos, **kw)
|
||||||
|
@ -441,7 +347,7 @@ class socksocket(_BaseSocket):
|
||||||
def close(self):
|
def close(self):
|
||||||
if self._proxyconn:
|
if self._proxyconn:
|
||||||
self._proxyconn.close()
|
self._proxyconn.close()
|
||||||
return super(socksocket, self).close()
|
return _BaseSocket.close(self)
|
||||||
|
|
||||||
def get_proxy_sockname(self):
|
def get_proxy_sockname(self):
|
||||||
"""
|
"""
|
||||||
|
@ -455,7 +361,7 @@ class socksocket(_BaseSocket):
|
||||||
"""
|
"""
|
||||||
Returns the IP and port number of the proxy.
|
Returns the IP and port number of the proxy.
|
||||||
"""
|
"""
|
||||||
return super(socksocket, self).getpeername()
|
return _BaseSocket.getpeername(self)
|
||||||
|
|
||||||
getproxypeername = get_proxy_peername
|
getproxypeername = get_proxy_peername
|
||||||
|
|
||||||
|
@ -553,8 +459,6 @@ class socksocket(_BaseSocket):
|
||||||
|
|
||||||
# Get the bound address/port
|
# Get the bound address/port
|
||||||
bnd = self._read_SOCKS5_address(reader)
|
bnd = self._read_SOCKS5_address(reader)
|
||||||
|
|
||||||
super(socksocket, self).settimeout(self._timeout)
|
|
||||||
return (resolved, bnd)
|
return (resolved, bnd)
|
||||||
finally:
|
finally:
|
||||||
reader.close()
|
reader.close()
|
||||||
|
@ -567,38 +471,25 @@ class socksocket(_BaseSocket):
|
||||||
"""
|
"""
|
||||||
host, port = addr
|
host, port = addr
|
||||||
proxy_type, _, _, rdns, username, password = self.proxy
|
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
|
# If the given destination address is an IP address, we'll
|
||||||
# use the IP address request even if remote resolving was specified.
|
# use the IPv4 address request even if remote resolving was specified.
|
||||||
# Detect whether the address is IPv4/6 directly.
|
try:
|
||||||
for family in (socket.AF_INET, socket.AF_INET6):
|
addr_bytes = socket.inet_aton(host)
|
||||||
try:
|
file.write(b"\x01" + addr_bytes)
|
||||||
addr_bytes = socket.inet_pton(family, host)
|
host = socket.inet_ntoa(addr_bytes)
|
||||||
file.write(family_to_byte[family] + addr_bytes)
|
except socket.error:
|
||||||
host = socket.inet_ntop(family, addr_bytes)
|
# Well it's not an IP number, so it's probably a DNS name.
|
||||||
file.write(struct.pack(">H", port))
|
if rdns:
|
||||||
return host, port
|
# Resolve remotely
|
||||||
except socket.error:
|
host_bytes = host.encode('idna')
|
||||||
continue
|
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)
|
||||||
|
|
||||||
# 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))
|
file.write(struct.pack(">H", port))
|
||||||
return host, port
|
return host, port
|
||||||
|
|
||||||
|
@ -609,8 +500,6 @@ class socksocket(_BaseSocket):
|
||||||
elif atyp == b"\x03":
|
elif atyp == b"\x03":
|
||||||
length = self._readall(file, 1)
|
length = self._readall(file, 1)
|
||||||
addr = self._readall(file, ord(length))
|
addr = self._readall(file, ord(length))
|
||||||
elif atyp == b"\x04":
|
|
||||||
addr = socket.inet_ntop(socket.AF_INET6, self._readall(file, 16))
|
|
||||||
else:
|
else:
|
||||||
raise GeneralProxyError("SOCKS5 proxy server sent invalid data")
|
raise GeneralProxyError("SOCKS5 proxy server sent invalid data")
|
||||||
|
|
||||||
|
@ -686,17 +575,8 @@ class socksocket(_BaseSocket):
|
||||||
# If we need to resolve locally, we do this now
|
# If we need to resolve locally, we do this now
|
||||||
addr = dest_addr if rdns else socket.gethostbyname(dest_addr)
|
addr = dest_addr if rdns else socket.gethostbyname(dest_addr)
|
||||||
|
|
||||||
http_headers = [
|
self.sendall(b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() +
|
||||||
b"CONNECT " + addr.encode('idna') + b":" + str(dest_port).encode() + b" HTTP/1.1",
|
b" HTTP/1.1\r\n" + b"Host: " + dest_addr.encode('idna') + b"\r\n\r\n")
|
||||||
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
|
# We just need the first line to check if the connection was successful
|
||||||
fobj = self.makefile()
|
fobj = self.makefile()
|
||||||
|
@ -736,7 +616,7 @@ class socksocket(_BaseSocket):
|
||||||
HTTP: _negotiate_HTTP
|
HTTP: _negotiate_HTTP
|
||||||
}
|
}
|
||||||
|
|
||||||
@set_self_blocking
|
|
||||||
def connect(self, dest_pair):
|
def connect(self, dest_pair):
|
||||||
"""
|
"""
|
||||||
Connects to the specified destination through a proxy.
|
Connects to the specified destination through a proxy.
|
||||||
|
@ -745,12 +625,6 @@ class socksocket(_BaseSocket):
|
||||||
|
|
||||||
dest_pair - 2-tuple of (IP/hostname, port).
|
dest_pair - 2-tuple of (IP/hostname, port).
|
||||||
"""
|
"""
|
||||||
if len(dest_pair) != 2 or dest_pair[0].startswith("["):
|
|
||||||
# Probably IPv6, not supported -- raise an error, and hope
|
|
||||||
# Happy Eyeballs (RFC6555) makes sure at least the IPv4
|
|
||||||
# connection works...
|
|
||||||
raise socket.error("PySocks doesn't support IPv6: %s" % str(dest_pair))
|
|
||||||
|
|
||||||
dest_addr, dest_port = dest_pair
|
dest_addr, dest_port = dest_pair
|
||||||
|
|
||||||
if self.type == socket.SOCK_DGRAM:
|
if self.type == socket.SOCK_DGRAM:
|
||||||
|
@ -776,22 +650,17 @@ class socksocket(_BaseSocket):
|
||||||
raise GeneralProxyError("Invalid destination-connection (host, port) pair")
|
raise GeneralProxyError("Invalid destination-connection (host, port) pair")
|
||||||
|
|
||||||
|
|
||||||
# We set the timeout here so that we don't hang in connection or during
|
|
||||||
# negotiation.
|
|
||||||
super(socksocket, self).settimeout(self._timeout)
|
|
||||||
|
|
||||||
if proxy_type is None:
|
if proxy_type is None:
|
||||||
# Treat like regular socket object
|
# Treat like regular socket object
|
||||||
self.proxy_peername = dest_pair
|
self.proxy_peername = dest_pair
|
||||||
super(socksocket, self).settimeout(self._timeout)
|
_BaseSocket.connect(self, (dest_addr, dest_port))
|
||||||
super(socksocket, self).connect((dest_addr, dest_port))
|
|
||||||
return
|
return
|
||||||
|
|
||||||
proxy_addr = self._proxy_addr()
|
proxy_addr = self._proxy_addr()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Initial connection to proxy server.
|
# Initial connection to proxy server
|
||||||
super(socksocket, self).connect(proxy_addr)
|
_BaseSocket.connect(self, proxy_addr)
|
||||||
|
|
||||||
except socket.error as error:
|
except socket.error as error:
|
||||||
# Error while connecting to proxy
|
# Error while connecting to proxy
|
||||||
|
@ -802,7 +671,6 @@ class socksocket(_BaseSocket):
|
||||||
|
|
||||||
msg = "Error connecting to {0} proxy {1}".format(printable_type,
|
msg = "Error connecting to {0} proxy {1}".format(printable_type,
|
||||||
proxy_server)
|
proxy_server)
|
||||||
log.debug("%s due to: %s", msg, error)
|
|
||||||
raise ProxyConnectionError(msg, error)
|
raise ProxyConnectionError(msg, error)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,4 +0,0 @@
|
||||||
This software released into the public domain. Anyone is free to copy,
|
|
||||||
modify, publish, use, compile, sell, or distribute this software,
|
|
||||||
either in source code form or as a compiled binary, for any purpose,
|
|
||||||
commercial or non-commercial, and by any means.
|
|
|
@ -1,29 +0,0 @@
|
||||||
win_inet_pton
|
|
||||||
=============
|
|
||||||
|
|
||||||
Native inet_pton and inet_ntop implementation for Python on Windows (with ctypes).
|
|
||||||
|
|
||||||
|
|
||||||
Credit Where Credit Is Due
|
|
||||||
--------------------------
|
|
||||||
|
|
||||||
This package is based on code that was originally written by https://github.com/nnemkin here: https://gist.github.com/nnemkin/4966028
|
|
||||||
|
|
||||||
|
|
||||||
Why?
|
|
||||||
----
|
|
||||||
|
|
||||||
I needed this functionality in https://github.com/SerenitySoftwareLLC/cahoots to get full windows support. I figured, since there were other people looking for a solution to this on the net, I should publish it.
|
|
||||||
|
|
||||||
|
|
||||||
Usage
|
|
||||||
-----
|
|
||||||
|
|
||||||
Just import it, and it will auto-add the methods to the socket library.
|
|
||||||
|
|
||||||
|
|
||||||
import win_inet_pton
|
|
||||||
import socket
|
|
||||||
|
|
||||||
socket.inet_pton(...)
|
|
||||||
socket.inet_ntop(...)
|
|
|
@ -1 +0,0 @@
|
||||||
from win_inet_pton import *
|
|
|
@ -1,20 +0,0 @@
|
||||||
from distutils.core import setup
|
|
||||||
|
|
||||||
setup (
|
|
||||||
name = 'win_inet_pton',
|
|
||||||
version = '1.0.1',
|
|
||||||
py_modules = ['win_inet_pton'],
|
|
||||||
url = 'https://github.com/hickeroar/win_inet_pton',
|
|
||||||
author = 'Ryan Vennell',
|
|
||||||
author_email = 'ryan.vennell@gmail.com',
|
|
||||||
description = 'Native inet_pton and inet_ntop implementation for Python on Windows (with ctypes).',
|
|
||||||
license = open('LICENSE', 'r').read(),
|
|
||||||
classifiers = [
|
|
||||||
'Development Status :: 5 - Production/Stable',
|
|
||||||
'Intended Audience :: Developers',
|
|
||||||
'Operating System :: OS Independent',
|
|
||||||
'Operating System :: Microsoft :: Windows',
|
|
||||||
'Programming Language :: Python :: 2.7',
|
|
||||||
'Topic :: Utilities'
|
|
||||||
]
|
|
||||||
)
|
|
|
@ -1,84 +0,0 @@
|
||||||
# This software released into the public domain. Anyone is free to copy,
|
|
||||||
# modify, publish, use, compile, sell, or distribute this software,
|
|
||||||
# either in source code form or as a compiled binary, for any purpose,
|
|
||||||
# commercial or non-commercial, and by any means.
|
|
||||||
|
|
||||||
import socket
|
|
||||||
import ctypes
|
|
||||||
import os
|
|
||||||
|
|
||||||
|
|
||||||
class sockaddr(ctypes.Structure):
|
|
||||||
_fields_ = [("sa_family", ctypes.c_short),
|
|
||||||
("__pad1", ctypes.c_ushort),
|
|
||||||
("ipv4_addr", ctypes.c_byte * 4),
|
|
||||||
("ipv6_addr", ctypes.c_byte * 16),
|
|
||||||
("__pad2", ctypes.c_ulong)]
|
|
||||||
|
|
||||||
if hasattr(ctypes, 'windll'):
|
|
||||||
WSAStringToAddressA = ctypes.windll.ws2_32.WSAStringToAddressA
|
|
||||||
WSAAddressToStringA = ctypes.windll.ws2_32.WSAAddressToStringA
|
|
||||||
else:
|
|
||||||
def not_windows():
|
|
||||||
raise SystemError(
|
|
||||||
"Invalid platform. ctypes.windll must be available."
|
|
||||||
)
|
|
||||||
WSAStringToAddressA = not_windows
|
|
||||||
WSAAddressToStringA = not_windows
|
|
||||||
|
|
||||||
|
|
||||||
def inet_pton(address_family, ip_string):
|
|
||||||
addr = sockaddr()
|
|
||||||
addr.sa_family = address_family
|
|
||||||
addr_size = ctypes.c_int(ctypes.sizeof(addr))
|
|
||||||
|
|
||||||
if WSAStringToAddressA(
|
|
||||||
ip_string,
|
|
||||||
address_family,
|
|
||||||
None,
|
|
||||||
ctypes.byref(addr),
|
|
||||||
ctypes.byref(addr_size)
|
|
||||||
) != 0:
|
|
||||||
raise socket.error(ctypes.FormatError())
|
|
||||||
|
|
||||||
if address_family == socket.AF_INET:
|
|
||||||
return ctypes.string_at(addr.ipv4_addr, 4)
|
|
||||||
if address_family == socket.AF_INET6:
|
|
||||||
return ctypes.string_at(addr.ipv6_addr, 16)
|
|
||||||
|
|
||||||
raise socket.error('unknown address family')
|
|
||||||
|
|
||||||
|
|
||||||
def inet_ntop(address_family, packed_ip):
|
|
||||||
addr = sockaddr()
|
|
||||||
addr.sa_family = address_family
|
|
||||||
addr_size = ctypes.c_int(ctypes.sizeof(addr))
|
|
||||||
ip_string = ctypes.create_string_buffer(128)
|
|
||||||
ip_string_size = ctypes.c_int(ctypes.sizeof(ip_string))
|
|
||||||
|
|
||||||
if address_family == socket.AF_INET:
|
|
||||||
if len(packed_ip) != ctypes.sizeof(addr.ipv4_addr):
|
|
||||||
raise socket.error('packed IP wrong length for inet_ntoa')
|
|
||||||
ctypes.memmove(addr.ipv4_addr, packed_ip, 4)
|
|
||||||
elif address_family == socket.AF_INET6:
|
|
||||||
if len(packed_ip) != ctypes.sizeof(addr.ipv6_addr):
|
|
||||||
raise socket.error('packed IP wrong length for inet_ntoa')
|
|
||||||
ctypes.memmove(addr.ipv6_addr, packed_ip, 16)
|
|
||||||
else:
|
|
||||||
raise socket.error('unknown address family')
|
|
||||||
|
|
||||||
if WSAAddressToStringA(
|
|
||||||
ctypes.byref(addr),
|
|
||||||
addr_size,
|
|
||||||
None,
|
|
||||||
ip_string,
|
|
||||||
ctypes.byref(ip_string_size)
|
|
||||||
) != 0:
|
|
||||||
raise socket.error(ctypes.FormatError())
|
|
||||||
|
|
||||||
return ip_string[:ip_string_size.value - 1]
|
|
||||||
|
|
||||||
# Adding our two functions to the socket library
|
|
||||||
if os.name == 'nt':
|
|
||||||
socket.inet_pton = inet_pton
|
|
||||||
socket.inet_ntop = inet_ntop
|
|
Loading…
Reference in New Issue