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.
 
 
 
 

649 line
22 KiB

  1. from __future__ import annotations
  2. import socket
  3. import ssl as ssl_module
  4. import threading
  5. import warnings
  6. from collections.abc import Sequence
  7. from typing import Any, Callable, Literal, TypeVar, cast
  8. from ..client import ClientProtocol
  9. from ..datastructures import Headers, HeadersLike
  10. from ..exceptions import InvalidProxyMessage, InvalidProxyStatus, ProxyError
  11. from ..extensions.base import ClientExtensionFactory
  12. from ..extensions.permessage_deflate import enable_client_permessage_deflate
  13. from ..headers import build_authorization_basic, build_host, validate_subprotocols
  14. from ..http11 import USER_AGENT, Response
  15. from ..protocol import CONNECTING, Event
  16. from ..streams import StreamReader
  17. from ..typing import LoggerLike, Origin, Subprotocol
  18. from ..uri import Proxy, WebSocketURI, get_proxy, parse_proxy, parse_uri
  19. from .connection import Connection
  20. from .utils import Deadline
  21. __all__ = ["connect", "unix_connect", "ClientConnection"]
  22. class ClientConnection(Connection):
  23. """
  24. :mod:`threading` implementation of a WebSocket client connection.
  25. :class:`ClientConnection` provides :meth:`recv` and :meth:`send` methods for
  26. receiving and sending messages.
  27. It supports iteration to receive messages::
  28. for message in websocket:
  29. process(message)
  30. The iterator exits normally when the connection is closed with close code
  31. 1000 (OK) or 1001 (going away) or without a close code. It raises a
  32. :exc:`~websockets.exceptions.ConnectionClosedError` when the connection is
  33. closed with any other code.
  34. The ``ping_interval``, ``ping_timeout``, ``close_timeout``, and
  35. ``max_queue`` arguments have the same meaning as in :func:`connect`.
  36. Args:
  37. socket: Socket connected to a WebSocket server.
  38. protocol: Sans-I/O connection.
  39. """
  40. def __init__(
  41. self,
  42. socket: socket.socket,
  43. protocol: ClientProtocol,
  44. *,
  45. ping_interval: float | None = 20,
  46. ping_timeout: float | None = 20,
  47. close_timeout: float | None = 10,
  48. max_queue: int | None | tuple[int | None, int | None] = 16,
  49. ) -> None:
  50. self.protocol: ClientProtocol
  51. self.response_rcvd = threading.Event()
  52. super().__init__(
  53. socket,
  54. protocol,
  55. ping_interval=ping_interval,
  56. ping_timeout=ping_timeout,
  57. close_timeout=close_timeout,
  58. max_queue=max_queue,
  59. )
  60. def handshake(
  61. self,
  62. additional_headers: HeadersLike | None = None,
  63. user_agent_header: str | None = USER_AGENT,
  64. timeout: float | None = None,
  65. ) -> None:
  66. """
  67. Perform the opening handshake.
  68. """
  69. with self.send_context(expected_state=CONNECTING):
  70. self.request = self.protocol.connect()
  71. if additional_headers is not None:
  72. self.request.headers.update(additional_headers)
  73. if user_agent_header is not None:
  74. self.request.headers.setdefault("User-Agent", user_agent_header)
  75. self.protocol.send_request(self.request)
  76. if not self.response_rcvd.wait(timeout):
  77. raise TimeoutError("timed out while waiting for handshake response")
  78. # self.protocol.handshake_exc is set when the connection is lost before
  79. # receiving a response, when the response cannot be parsed, or when the
  80. # response fails the handshake.
  81. if self.protocol.handshake_exc is not None:
  82. raise self.protocol.handshake_exc
  83. def process_event(self, event: Event) -> None:
  84. """
  85. Process one incoming event.
  86. """
  87. # First event - handshake response.
  88. if self.response is None:
  89. assert isinstance(event, Response)
  90. self.response = event
  91. self.response_rcvd.set()
  92. # Later events - frames.
  93. else:
  94. super().process_event(event)
  95. def recv_events(self) -> None:
  96. """
  97. Read incoming data from the socket and process events.
  98. """
  99. try:
  100. super().recv_events()
  101. finally:
  102. # If the connection is closed during the handshake, unblock it.
  103. self.response_rcvd.set()
  104. def connect(
  105. uri: str,
  106. *,
  107. # TCP/TLS
  108. sock: socket.socket | None = None,
  109. ssl: ssl_module.SSLContext | None = None,
  110. server_hostname: str | None = None,
  111. # WebSocket
  112. origin: Origin | None = None,
  113. extensions: Sequence[ClientExtensionFactory] | None = None,
  114. subprotocols: Sequence[Subprotocol] | None = None,
  115. compression: str | None = "deflate",
  116. # HTTP
  117. additional_headers: HeadersLike | None = None,
  118. user_agent_header: str | None = USER_AGENT,
  119. proxy: str | Literal[True] | None = True,
  120. proxy_ssl: ssl_module.SSLContext | None = None,
  121. proxy_server_hostname: str | None = None,
  122. # Timeouts
  123. open_timeout: float | None = 10,
  124. ping_interval: float | None = 20,
  125. ping_timeout: float | None = 20,
  126. close_timeout: float | None = 10,
  127. # Limits
  128. max_size: int | None = 2**20,
  129. max_queue: int | None | tuple[int | None, int | None] = 16,
  130. # Logging
  131. logger: LoggerLike | None = None,
  132. # Escape hatch for advanced customization
  133. create_connection: type[ClientConnection] | None = None,
  134. **kwargs: Any,
  135. ) -> ClientConnection:
  136. """
  137. Connect to the WebSocket server at ``uri``.
  138. This function returns a :class:`ClientConnection` instance, which you can
  139. use to send and receive messages.
  140. :func:`connect` may be used as a context manager::
  141. from websockets.sync.client import connect
  142. with connect(...) as websocket:
  143. ...
  144. The connection is closed automatically when exiting the context.
  145. Args:
  146. uri: URI of the WebSocket server.
  147. sock: Preexisting TCP socket. ``sock`` overrides the host and port
  148. from ``uri``. You may call :func:`socket.create_connection` to
  149. create a suitable TCP socket.
  150. ssl: Configuration for enabling TLS on the connection.
  151. server_hostname: Host name for the TLS handshake. ``server_hostname``
  152. overrides the host name from ``uri``.
  153. origin: Value of the ``Origin`` header, for servers that require it.
  154. extensions: List of supported extensions, in order in which they
  155. should be negotiated and run.
  156. subprotocols: List of supported subprotocols, in order of decreasing
  157. preference.
  158. compression: The "permessage-deflate" extension is enabled by default.
  159. Set ``compression`` to :obj:`None` to disable it. See the
  160. :doc:`compression guide <../../topics/compression>` for details.
  161. additional_headers (HeadersLike | None): Arbitrary HTTP headers to add
  162. to the handshake request.
  163. user_agent_header: Value of the ``User-Agent`` request header.
  164. It defaults to ``"Python/x.y.z websockets/X.Y"``.
  165. Setting it to :obj:`None` removes the header.
  166. proxy: If a proxy is configured, it is used by default. Set ``proxy``
  167. to :obj:`None` to disable the proxy or to the address of a proxy
  168. to override the system configuration. See the :doc:`proxy docs
  169. <../../topics/proxies>` for details.
  170. proxy_ssl: Configuration for enabling TLS on the proxy connection.
  171. proxy_server_hostname: Host name for the TLS handshake with the proxy.
  172. ``proxy_server_hostname`` overrides the host name from ``proxy``.
  173. open_timeout: Timeout for opening the connection in seconds.
  174. :obj:`None` disables the timeout.
  175. ping_interval: Interval between keepalive pings in seconds.
  176. :obj:`None` disables keepalive.
  177. ping_timeout: Timeout for keepalive pings in seconds.
  178. :obj:`None` disables timeouts.
  179. close_timeout: Timeout for closing the connection in seconds.
  180. :obj:`None` disables the timeout.
  181. max_size: Maximum size of incoming messages in bytes.
  182. :obj:`None` disables the limit.
  183. max_queue: High-water mark of the buffer where frames are received.
  184. It defaults to 16 frames. The low-water mark defaults to ``max_queue
  185. // 4``. You may pass a ``(high, low)`` tuple to set the high-water
  186. and low-water marks. If you want to disable flow control entirely,
  187. you may set it to ``None``, although that's a bad idea.
  188. logger: Logger for this client.
  189. It defaults to ``logging.getLogger("websockets.client")``.
  190. See the :doc:`logging guide <../../topics/logging>` for details.
  191. create_connection: Factory for the :class:`ClientConnection` managing
  192. the connection. Set it to a wrapper or a subclass to customize
  193. connection handling.
  194. Any other keyword arguments are passed to :func:`~socket.create_connection`.
  195. Raises:
  196. InvalidURI: If ``uri`` isn't a valid WebSocket URI.
  197. OSError: If the TCP connection fails.
  198. InvalidHandshake: If the opening handshake fails.
  199. TimeoutError: If the opening handshake times out.
  200. """
  201. # Process parameters
  202. # Backwards compatibility: ssl used to be called ssl_context.
  203. if ssl is None and "ssl_context" in kwargs:
  204. ssl = kwargs.pop("ssl_context")
  205. warnings.warn( # deprecated in 13.0 - 2024-08-20
  206. "ssl_context was renamed to ssl",
  207. DeprecationWarning,
  208. )
  209. ws_uri = parse_uri(uri)
  210. if not ws_uri.secure and ssl is not None:
  211. raise ValueError("ssl argument is incompatible with a ws:// URI")
  212. # Private APIs for unix_connect()
  213. unix: bool = kwargs.pop("unix", False)
  214. path: str | None = kwargs.pop("path", None)
  215. if unix:
  216. if path is None and sock is None:
  217. raise ValueError("missing path argument")
  218. elif path is not None and sock is not None:
  219. raise ValueError("path and sock arguments are incompatible")
  220. if subprotocols is not None:
  221. validate_subprotocols(subprotocols)
  222. if compression == "deflate":
  223. extensions = enable_client_permessage_deflate(extensions)
  224. elif compression is not None:
  225. raise ValueError(f"unsupported compression: {compression}")
  226. if unix:
  227. proxy = None
  228. if sock is not None:
  229. proxy = None
  230. if proxy is True:
  231. proxy = get_proxy(ws_uri)
  232. # Calculate timeouts on the TCP, TLS, and WebSocket handshakes.
  233. # The TCP and TLS timeouts must be set on the socket, then removed
  234. # to avoid conflicting with the WebSocket timeout in handshake().
  235. deadline = Deadline(open_timeout)
  236. if create_connection is None:
  237. create_connection = ClientConnection
  238. try:
  239. # Connect socket
  240. if sock is None:
  241. if unix:
  242. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  243. sock.settimeout(deadline.timeout())
  244. assert path is not None # mypy cannot figure this out
  245. sock.connect(path)
  246. elif proxy is not None:
  247. proxy_parsed = parse_proxy(proxy)
  248. if proxy_parsed.scheme[:5] == "socks":
  249. # Connect to the server through the proxy.
  250. sock = connect_socks_proxy(
  251. proxy_parsed,
  252. ws_uri,
  253. deadline,
  254. # websockets is consistent with the socket module while
  255. # python_socks is consistent across implementations.
  256. local_addr=kwargs.pop("source_address", None),
  257. )
  258. elif proxy_parsed.scheme[:4] == "http":
  259. # Validate the proxy_ssl argument.
  260. if proxy_parsed.scheme != "https" and proxy_ssl is not None:
  261. raise ValueError(
  262. "proxy_ssl argument is incompatible with an http:// proxy"
  263. )
  264. # Connect to the server through the proxy.
  265. sock = connect_http_proxy(
  266. proxy_parsed,
  267. ws_uri,
  268. deadline,
  269. user_agent_header=user_agent_header,
  270. ssl=proxy_ssl,
  271. server_hostname=proxy_server_hostname,
  272. **kwargs,
  273. )
  274. else:
  275. raise AssertionError("unsupported proxy")
  276. else:
  277. kwargs.setdefault("timeout", deadline.timeout())
  278. sock = socket.create_connection(
  279. (ws_uri.host, ws_uri.port),
  280. **kwargs,
  281. )
  282. sock.settimeout(None)
  283. # Disable Nagle algorithm
  284. if not unix:
  285. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, True)
  286. # Initialize TLS wrapper and perform TLS handshake
  287. if ws_uri.secure:
  288. if ssl is None:
  289. ssl = ssl_module.create_default_context()
  290. if server_hostname is None:
  291. server_hostname = ws_uri.host
  292. sock.settimeout(deadline.timeout())
  293. if proxy_ssl is None:
  294. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  295. else:
  296. sock_2 = SSLSSLSocket(sock, ssl, server_hostname=server_hostname)
  297. # Let's pretend that sock is a socket, even though it isn't.
  298. sock = cast(socket.socket, sock_2)
  299. sock.settimeout(None)
  300. # Initialize WebSocket protocol
  301. protocol = ClientProtocol(
  302. ws_uri,
  303. origin=origin,
  304. extensions=extensions,
  305. subprotocols=subprotocols,
  306. max_size=max_size,
  307. logger=logger,
  308. )
  309. # Initialize WebSocket connection
  310. connection = create_connection(
  311. sock,
  312. protocol,
  313. ping_interval=ping_interval,
  314. ping_timeout=ping_timeout,
  315. close_timeout=close_timeout,
  316. max_queue=max_queue,
  317. )
  318. except Exception:
  319. if sock is not None:
  320. sock.close()
  321. raise
  322. try:
  323. connection.handshake(
  324. additional_headers,
  325. user_agent_header,
  326. deadline.timeout(),
  327. )
  328. except Exception:
  329. connection.close_socket()
  330. connection.recv_events_thread.join()
  331. raise
  332. connection.start_keepalive()
  333. return connection
  334. def unix_connect(
  335. path: str | None = None,
  336. uri: str | None = None,
  337. **kwargs: Any,
  338. ) -> ClientConnection:
  339. """
  340. Connect to a WebSocket server listening on a Unix socket.
  341. This function accepts the same keyword arguments as :func:`connect`.
  342. It's only available on Unix.
  343. It's mainly useful for debugging servers listening on Unix sockets.
  344. Args:
  345. path: File system path to the Unix socket.
  346. uri: URI of the WebSocket server. ``uri`` defaults to
  347. ``ws://localhost/`` or, when a ``ssl`` is provided, to
  348. ``wss://localhost/``.
  349. """
  350. if uri is None:
  351. # Backwards compatibility: ssl used to be called ssl_context.
  352. if kwargs.get("ssl") is None and kwargs.get("ssl_context") is None:
  353. uri = "ws://localhost/"
  354. else:
  355. uri = "wss://localhost/"
  356. return connect(uri=uri, unix=True, path=path, **kwargs)
  357. try:
  358. from python_socks import ProxyType
  359. from python_socks.sync import Proxy as SocksProxy
  360. SOCKS_PROXY_TYPES = {
  361. "socks5h": ProxyType.SOCKS5,
  362. "socks5": ProxyType.SOCKS5,
  363. "socks4a": ProxyType.SOCKS4,
  364. "socks4": ProxyType.SOCKS4,
  365. }
  366. SOCKS_PROXY_RDNS = {
  367. "socks5h": True,
  368. "socks5": False,
  369. "socks4a": True,
  370. "socks4": False,
  371. }
  372. def connect_socks_proxy(
  373. proxy: Proxy,
  374. ws_uri: WebSocketURI,
  375. deadline: Deadline,
  376. **kwargs: Any,
  377. ) -> socket.socket:
  378. """Connect via a SOCKS proxy and return the socket."""
  379. socks_proxy = SocksProxy(
  380. SOCKS_PROXY_TYPES[proxy.scheme],
  381. proxy.host,
  382. proxy.port,
  383. proxy.username,
  384. proxy.password,
  385. SOCKS_PROXY_RDNS[proxy.scheme],
  386. )
  387. kwargs.setdefault("timeout", deadline.timeout())
  388. # connect() is documented to raise OSError and TimeoutError.
  389. # Wrap other exceptions in ProxyError, a subclass of InvalidHandshake.
  390. try:
  391. return socks_proxy.connect(ws_uri.host, ws_uri.port, **kwargs)
  392. except (OSError, TimeoutError, socket.timeout):
  393. raise
  394. except Exception as exc:
  395. raise ProxyError("failed to connect to SOCKS proxy") from exc
  396. except ImportError:
  397. def connect_socks_proxy(
  398. proxy: Proxy,
  399. ws_uri: WebSocketURI,
  400. deadline: Deadline,
  401. **kwargs: Any,
  402. ) -> socket.socket:
  403. raise ImportError("python-socks is required to use a SOCKS proxy")
  404. def prepare_connect_request(
  405. proxy: Proxy,
  406. ws_uri: WebSocketURI,
  407. user_agent_header: str | None = None,
  408. ) -> bytes:
  409. host = build_host(ws_uri.host, ws_uri.port, ws_uri.secure, always_include_port=True)
  410. headers = Headers()
  411. headers["Host"] = build_host(ws_uri.host, ws_uri.port, ws_uri.secure)
  412. if user_agent_header is not None:
  413. headers["User-Agent"] = user_agent_header
  414. if proxy.username is not None:
  415. assert proxy.password is not None # enforced by parse_proxy()
  416. headers["Proxy-Authorization"] = build_authorization_basic(
  417. proxy.username, proxy.password
  418. )
  419. # We cannot use the Request class because it supports only GET requests.
  420. return f"CONNECT {host} HTTP/1.1\r\n".encode() + headers.serialize()
  421. def read_connect_response(sock: socket.socket, deadline: Deadline) -> Response:
  422. reader = StreamReader()
  423. parser = Response.parse(
  424. reader.read_line,
  425. reader.read_exact,
  426. reader.read_to_eof,
  427. include_body=False,
  428. )
  429. try:
  430. while True:
  431. sock.settimeout(deadline.timeout())
  432. data = sock.recv(4096)
  433. if data:
  434. reader.feed_data(data)
  435. else:
  436. reader.feed_eof()
  437. next(parser)
  438. except StopIteration as exc:
  439. assert isinstance(exc.value, Response) # help mypy
  440. response = exc.value
  441. if 200 <= response.status_code < 300:
  442. return response
  443. else:
  444. raise InvalidProxyStatus(response)
  445. except socket.timeout:
  446. raise TimeoutError("timed out while connecting to HTTP proxy")
  447. except Exception as exc:
  448. raise InvalidProxyMessage(
  449. "did not receive a valid HTTP response from proxy"
  450. ) from exc
  451. finally:
  452. sock.settimeout(None)
  453. def connect_http_proxy(
  454. proxy: Proxy,
  455. ws_uri: WebSocketURI,
  456. deadline: Deadline,
  457. *,
  458. user_agent_header: str | None = None,
  459. ssl: ssl_module.SSLContext | None = None,
  460. server_hostname: str | None = None,
  461. **kwargs: Any,
  462. ) -> socket.socket:
  463. # Connect socket
  464. kwargs.setdefault("timeout", deadline.timeout())
  465. sock = socket.create_connection((proxy.host, proxy.port), **kwargs)
  466. # Initialize TLS wrapper and perform TLS handshake
  467. if proxy.scheme == "https":
  468. if ssl is None:
  469. ssl = ssl_module.create_default_context()
  470. if server_hostname is None:
  471. server_hostname = proxy.host
  472. sock.settimeout(deadline.timeout())
  473. sock = ssl.wrap_socket(sock, server_hostname=server_hostname)
  474. sock.settimeout(None)
  475. # Send CONNECT request to the proxy and read response.
  476. sock.sendall(prepare_connect_request(proxy, ws_uri, user_agent_header))
  477. try:
  478. read_connect_response(sock, deadline)
  479. except Exception:
  480. sock.close()
  481. raise
  482. return sock
  483. T = TypeVar("T")
  484. F = TypeVar("F", bound=Callable[..., T])
  485. class SSLSSLSocket:
  486. """
  487. Socket-like object providing TLS-in-TLS.
  488. Only methods that are used by websockets are implemented.
  489. """
  490. recv_bufsize = 65536
  491. def __init__(
  492. self,
  493. sock: socket.socket,
  494. ssl_context: ssl_module.SSLContext,
  495. server_hostname: str | None = None,
  496. ) -> None:
  497. self.incoming = ssl_module.MemoryBIO()
  498. self.outgoing = ssl_module.MemoryBIO()
  499. self.ssl_socket = sock
  500. self.ssl_object = ssl_context.wrap_bio(
  501. self.incoming,
  502. self.outgoing,
  503. server_hostname=server_hostname,
  504. )
  505. self.run_io(self.ssl_object.do_handshake)
  506. def run_io(self, func: Callable[..., T], *args: Any) -> T:
  507. while True:
  508. want_read = False
  509. want_write = False
  510. try:
  511. result = func(*args)
  512. except ssl_module.SSLWantReadError:
  513. want_read = True
  514. except ssl_module.SSLWantWriteError: # pragma: no cover
  515. want_write = True
  516. # Write outgoing data in all cases.
  517. data = self.outgoing.read()
  518. if data:
  519. self.ssl_socket.sendall(data)
  520. # Read incoming data and retry on SSLWantReadError.
  521. if want_read:
  522. data = self.ssl_socket.recv(self.recv_bufsize)
  523. if data:
  524. self.incoming.write(data)
  525. else:
  526. self.incoming.write_eof()
  527. continue
  528. # Retry after writing outgoing data on SSLWantWriteError.
  529. if want_write: # pragma: no cover
  530. continue
  531. # Return result if no error happened.
  532. return result
  533. def recv(self, buflen: int) -> bytes:
  534. try:
  535. return self.run_io(self.ssl_object.read, buflen)
  536. except ssl_module.SSLEOFError:
  537. return b"" # always ignore ragged EOFs
  538. def send(self, data: bytes) -> int:
  539. return self.run_io(self.ssl_object.write, data)
  540. def sendall(self, data: bytes) -> None:
  541. # adapted from ssl_module.SSLSocket.sendall()
  542. count = 0
  543. with memoryview(data) as view, view.cast("B") as byte_view:
  544. amount = len(byte_view)
  545. while count < amount:
  546. count += self.send(byte_view[count:])
  547. # recv_into(), recvfrom(), recvfrom_into(), sendto(), unwrap(), and the
  548. # flags argument aren't implemented because websockets doesn't need them.
  549. def __getattr__(self, name: str) -> Any:
  550. return getattr(self.ssl_socket, name)