socks.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. 'SOCKS support in urllib3 requires the installation of optional '
  36. 'dependencies: specifically, PySocks. For more information, see '
  37. 'https://urllib3.readthedocs.io/en/latest/contrib.html#socks-proxies'
  38. ),
  39. DependencyWarning
  40. )
  41. raise
  42. from socket import error as SocketError, timeout as SocketTimeout
  43. from ..connection import (
  44. HTTPConnection, HTTPSConnection
  45. )
  46. from ..connectionpool import (
  47. HTTPConnectionPool, HTTPSConnectionPool
  48. )
  49. from ..exceptions import ConnectTimeoutError, NewConnectionError
  50. from ..poolmanager import PoolManager
  51. from ..util.url import parse_url
  52. try:
  53. import ssl
  54. except ImportError:
  55. ssl = None
  56. class SOCKSConnection(HTTPConnection):
  57. """
  58. A plain-text HTTP connection that connects via a SOCKS proxy.
  59. """
  60. def __init__(self, *args, **kwargs):
  61. self._socks_options = kwargs.pop('_socks_options')
  62. super(SOCKSConnection, self).__init__(*args, **kwargs)
  63. def _new_conn(self):
  64. """
  65. Establish a new connection via the SOCKS proxy.
  66. """
  67. extra_kw = {}
  68. if self.source_address:
  69. extra_kw['source_address'] = self.source_address
  70. if self.socket_options:
  71. extra_kw['socket_options'] = self.socket_options
  72. try:
  73. conn = socks.create_connection(
  74. (self.host, self.port),
  75. proxy_type=self._socks_options['socks_version'],
  76. proxy_addr=self._socks_options['proxy_host'],
  77. proxy_port=self._socks_options['proxy_port'],
  78. proxy_username=self._socks_options['username'],
  79. proxy_password=self._socks_options['password'],
  80. proxy_rdns=self._socks_options['rdns'],
  81. timeout=self.timeout,
  82. **extra_kw
  83. )
  84. except SocketTimeout:
  85. raise ConnectTimeoutError(
  86. self, "Connection to %s timed out. (connect timeout=%s)" %
  87. (self.host, self.timeout))
  88. except socks.ProxyError as e:
  89. # This is fragile as hell, but it seems to be the only way to raise
  90. # useful errors here.
  91. if e.socket_err:
  92. error = e.socket_err
  93. if isinstance(error, SocketTimeout):
  94. raise ConnectTimeoutError(
  95. self,
  96. "Connection to %s timed out. (connect timeout=%s)" %
  97. (self.host, self.timeout)
  98. )
  99. else:
  100. raise NewConnectionError(
  101. self,
  102. "Failed to establish a new connection: %s" % error
  103. )
  104. else:
  105. raise NewConnectionError(
  106. self,
  107. "Failed to establish a new connection: %s" % e
  108. )
  109. except SocketError as e: # Defensive: PySocks should catch all these.
  110. raise NewConnectionError(
  111. self, "Failed to establish a new connection: %s" % e)
  112. return conn
  113. # We don't need to duplicate the Verified/Unverified distinction from
  114. # urllib3/connection.py here because the HTTPSConnection will already have been
  115. # correctly set to either the Verified or Unverified form by that module. This
  116. # means the SOCKSHTTPSConnection will automatically be the correct type.
  117. class SOCKSHTTPSConnection(SOCKSConnection, HTTPSConnection):
  118. pass
  119. class SOCKSHTTPConnectionPool(HTTPConnectionPool):
  120. ConnectionCls = SOCKSConnection
  121. class SOCKSHTTPSConnectionPool(HTTPSConnectionPool):
  122. ConnectionCls = SOCKSHTTPSConnection
  123. class SOCKSProxyManager(PoolManager):
  124. """
  125. A version of the urllib3 ProxyManager that routes connections via the
  126. defined SOCKS proxy.
  127. """
  128. pool_classes_by_scheme = {
  129. 'http': SOCKSHTTPConnectionPool,
  130. 'https': SOCKSHTTPSConnectionPool,
  131. }
  132. def __init__(self, proxy_url, username=None, password=None,
  133. num_pools=10, headers=None, **connection_pool_kw):
  134. parsed = parse_url(proxy_url)
  135. if username is None and password is None and parsed.auth is not None:
  136. split = parsed.auth.split(':')
  137. if len(split) == 2:
  138. username, password = split
  139. if parsed.scheme == 'socks5':
  140. socks_version = socks.PROXY_TYPE_SOCKS5
  141. rdns = False
  142. elif parsed.scheme == 'socks5h':
  143. socks_version = socks.PROXY_TYPE_SOCKS5
  144. rdns = True
  145. elif parsed.scheme == 'socks4':
  146. socks_version = socks.PROXY_TYPE_SOCKS4
  147. rdns = False
  148. elif parsed.scheme == 'socks4a':
  149. socks_version = socks.PROXY_TYPE_SOCKS4
  150. rdns = True
  151. else:
  152. raise ValueError(
  153. "Unable to determine SOCKS version from %s" % proxy_url
  154. )
  155. self.proxy_url = proxy_url
  156. socks_options = {
  157. 'socks_version': socks_version,
  158. 'proxy_host': parsed.host,
  159. 'proxy_port': parsed.port,
  160. 'username': username,
  161. 'password': password,
  162. 'rdns': rdns
  163. }
  164. connection_pool_kw['_socks_options'] = socks_options
  165. super(SOCKSProxyManager, self).__init__(
  166. num_pools, headers, **connection_pool_kw
  167. )
  168. self.pool_classes_by_scheme = SOCKSProxyManager.pool_classes_by_scheme