socks.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # -*- coding: utf-8 -*-
  2. """
  3. This module contains provisional support for SOCKS proxies from within
  4. urllib3. This module supports SOCKS4, SOCKS4A (an extension of SOCKS4), and
  5. SOCKS5. To enable its functionality, either install PySocks or install this
  6. module with the ``socks`` extra.
  7. The SOCKS implementation supports the full range of urllib3 features. It also
  8. supports the following SOCKS features:
  9. - SOCKS4A (``proxy_url='socks4a://...``)
  10. - SOCKS4 (``proxy_url='socks4://...``)
  11. - SOCKS5 with remote DNS (``proxy_url='socks5h://...``)
  12. - SOCKS5 with local DNS (``proxy_url='socks5://...``)
  13. - Usernames and passwords for the SOCKS proxy
  14. .. note::
  15. It is recommended to use ``socks5h://`` or ``socks4a://`` schemes in
  16. your ``proxy_url`` to ensure that DNS resolution is done from the remote
  17. server instead of client-side when connecting to a domain name.
  18. SOCKS4 supports IPv4 and domain names with the SOCKS4A extension. SOCKS5
  19. supports IPv4, IPv6, and domain names.
  20. When connecting to a SOCKS4 proxy the ``username`` portion of the ``proxy_url``
  21. will be sent as the ``userid`` section of the SOCKS request::
  22. proxy_url="socks4a://<userid>@proxy-host"
  23. When connecting to a SOCKS5 proxy the ``username`` and ``password`` portion
  24. of the ``proxy_url`` will be sent as the username/password to authenticate
  25. with the proxy::
  26. proxy_url="socks5h://<username>:<password>@proxy-host"
  27. """
  28. from __future__ import absolute_import
  29. try:
  30. import socks
  31. except ImportError:
  32. import warnings
  33. from ..exceptions import DependencyWarning
  34. warnings.warn(
  35. (
  36. "SOCKS support in urllib3 requires the installation of optional "
  37. "dependencies: specifically, PySocks. For more information, see "
  38. "https://urllib3.readthedocs.io/en/latest/contrib.html#socks-proxies"
  39. ),
  40. DependencyWarning,
  41. )
  42. raise
  43. from socket import error as SocketError, timeout as SocketTimeout
  44. from ..connection import HTTPConnection, HTTPSConnection
  45. from ..connectionpool import HTTPConnectionPool, HTTPSConnectionPool
  46. from ..exceptions import ConnectTimeoutError, NewConnectionError
  47. from ..poolmanager import PoolManager
  48. from ..util.url import parse_url
  49. try:
  50. import ssl
  51. except ImportError:
  52. ssl = None
  53. class SOCKSConnection(HTTPConnection):
  54. """
  55. A plain-text HTTP connection that connects via a SOCKS proxy.
  56. """
  57. def __init__(self, *args, **kwargs):
  58. self._socks_options = kwargs.pop("_socks_options")
  59. super(SOCKSConnection, self).__init__(*args, **kwargs)
  60. def _new_conn(self):
  61. """
  62. Establish a new connection via the SOCKS proxy.
  63. """
  64. extra_kw = {}
  65. if self.source_address:
  66. extra_kw["source_address"] = self.source_address
  67. if self.socket_options:
  68. extra_kw["socket_options"] = self.socket_options
  69. try:
  70. conn = socks.create_connection(
  71. (self.host, self.port),
  72. proxy_type=self._socks_options["socks_version"],
  73. proxy_addr=self._socks_options["proxy_host"],
  74. proxy_port=self._socks_options["proxy_port"],
  75. proxy_username=self._socks_options["username"],
  76. proxy_password=self._socks_options["password"],
  77. proxy_rdns=self._socks_options["rdns"],
  78. timeout=self.timeout,
  79. **extra_kw
  80. )
  81. except SocketTimeout:
  82. raise ConnectTimeoutError(
  83. self,
  84. "Connection to %s timed out. (connect timeout=%s)"
  85. % (self.host, self.timeout),
  86. )
  87. except socks.ProxyError as e:
  88. # This is fragile as hell, but it seems to be the only way to raise
  89. # useful errors here.
  90. if e.socket_err:
  91. error = e.socket_err
  92. if isinstance(error, SocketTimeout):
  93. raise ConnectTimeoutError(
  94. self,
  95. "Connection to %s timed out. (connect timeout=%s)"
  96. % (self.host, self.timeout),
  97. )
  98. else:
  99. raise NewConnectionError(
  100. self, "Failed to establish a new connection: %s" % error
  101. )
  102. else:
  103. raise NewConnectionError(
  104. self, "Failed to establish a new connection: %s" % e
  105. )
  106. except SocketError as e: # Defensive: PySocks should catch all these.
  107. raise NewConnectionError(
  108. self, "Failed to establish a new connection: %s" % e
  109. )
  110. return conn
  111. # We don't need to duplicate the Verified/Unverified distinction from
  112. # urllib3/connection.py here because the HTTPSConnection will already have been
  113. # correctly set to either the Verified or Unverified form by that module. This
  114. # means the SOCKSHTTPSConnection will automatically be the correct type.
  115. class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection):
  116. pass
  117. class SOCKSHTTPConnectionPool(HTTPConnectionPool):
  118. ConnectionCls = SOCKSConnection
  119. class SOCKSHTTPSConnectionPool(HTTPSConnectionPool):
  120. ConnectionCls = SOCKSHTTPSConnection
  121. class SOCKSProxyManager(PoolManager):
  122. """
  123. A version of the urllib3 ProxyManager that routes connections via the
  124. defined SOCKS proxy.
  125. """
  126. pool_classes_by_scheme = {
  127. "http": SOCKSHTTPConnectionPool,
  128. "https": SOCKSHTTPSConnectionPool,
  129. }
  130. def __init__(
  131. self,
  132. proxy_url,
  133. username=None,
  134. password=None,
  135. num_pools=10,
  136. headers=None,
  137. **connection_pool_kw
  138. ):
  139. parsed = parse_url(proxy_url)
  140. if username is None and password is None and parsed.auth is not None:
  141. split = parsed.auth.split(":")
  142. if len(split) == 2:
  143. username, password = split
  144. if parsed.scheme == "socks5":
  145. socks_version = socks.PROXY_TYPE_SOCKS5
  146. rdns = False
  147. elif parsed.scheme == "socks5h":
  148. socks_version = socks.PROXY_TYPE_SOCKS5
  149. rdns = True
  150. elif parsed.scheme == "socks4":
  151. socks_version = socks.PROXY_TYPE_SOCKS4
  152. rdns = False
  153. elif parsed.scheme == "socks4a":
  154. socks_version = socks.PROXY_TYPE_SOCKS4
  155. rdns = True
  156. else:
  157. raise ValueError("Unable to determine SOCKS version from %s" % proxy_url)
  158. self.proxy_url = proxy_url
  159. socks_options = {
  160. "socks_version": socks_version,
  161. "proxy_host": parsed.host,
  162. "proxy_port": parsed.port,
  163. "username": username,
  164. "password": password,
  165. "rdns": rdns,
  166. }
  167. connection_pool_kw["_socks_options"] = socks_options
  168. super(SOCKSProxyManager, self).__init__(
  169. num_pools, headers, **connection_pool_kw
  170. )
  171. self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme