You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.2 KiB

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