socks.py 6.1 KB

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