Skip to content

Commit 02feb6d

Browse files
committed
Simplify SOCKS proxy implementation.
1 parent 1e8c2cc commit 02feb6d

File tree

2 files changed

+31
-41
lines changed

2 files changed

+31
-41
lines changed

src/websockets/asyncio/client.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -383,16 +383,28 @@ def factory() -> ClientConnection:
383383
if kwargs.pop("unix", False):
384384
_, connection = await loop.create_unix_connection(factory, **kwargs)
385385
elif proxy is not None:
386-
kwargs["sock"] = await connect_proxy(
387-
parse_proxy(proxy),
388-
ws_uri,
389-
local_addr=kwargs.pop("local_addr", None),
390-
)
391-
_, connection = await loop.create_connection(factory, **kwargs)
386+
proxy_parsed = parse_proxy(proxy)
387+
if proxy_parsed.scheme[:5] == "socks":
388+
# Connect to the server through the proxy.
389+
sock = await connect_socks_proxy(
390+
proxy_parsed,
391+
ws_uri,
392+
local_addr=kwargs.pop("local_addr", None),
393+
)
394+
# Initialize WebSocket connection via the proxy.
395+
_, connection = await loop.create_connection(
396+
factory,
397+
sock=sock,
398+
**kwargs,
399+
)
400+
else:
401+
raise AssertionError("unsupported proxy")
392402
else:
403+
# Connect to the server directly.
393404
if kwargs.get("sock") is None:
394405
kwargs.setdefault("host", ws_uri.host)
395406
kwargs.setdefault("port", ws_uri.port)
407+
# Initialize WebSocket connection.
396408
_, connection = await loop.create_connection(factory, **kwargs)
397409
return connection
398410

@@ -643,16 +655,3 @@ async def connect_socks_proxy(
643655
**kwargs: Any,
644656
) -> socket.socket:
645657
raise ImportError("python-socks is required to use a SOCKS proxy")
646-
647-
648-
async def connect_proxy(
649-
proxy: Proxy,
650-
ws_uri: WebSocketURI,
651-
**kwargs: Any,
652-
) -> socket.socket:
653-
"""Connect via a proxy and return the socket."""
654-
# parse_proxy() validates proxy.scheme.
655-
if proxy.scheme[:5] == "socks":
656-
return await connect_socks_proxy(proxy, ws_uri, **kwargs)
657-
else:
658-
raise AssertionError("unsupported proxy")

src/websockets/sync/client.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,19 @@ def connect(
284284
assert path is not None # mypy cannot figure this out
285285
sock.connect(path)
286286
elif proxy is not None:
287-
sock = connect_proxy(
288-
parse_proxy(proxy),
289-
ws_uri,
290-
deadline,
291-
# websockets is consistent with the socket module while
292-
# python_socks is consistent across implementations.
293-
local_addr=kwargs.pop("source_address", None),
294-
)
287+
proxy_parsed = parse_proxy(proxy)
288+
if proxy_parsed.scheme[:5] == "socks":
289+
# Connect to the server through the proxy.
290+
sock = connect_socks_proxy(
291+
proxy_parsed,
292+
ws_uri,
293+
deadline,
294+
# websockets is consistent with the socket module while
295+
# python_socks is consistent across implementations.
296+
local_addr=kwargs.pop("source_address", None),
297+
)
298+
else:
299+
raise AssertionError("unsupported proxy")
295300
else:
296301
kwargs.setdefault("timeout", deadline.timeout())
297302
sock = socket.create_connection(
@@ -439,17 +444,3 @@ def connect_socks_proxy(
439444
**kwargs: Any,
440445
) -> socket.socket:
441446
raise ImportError("python-socks is required to use a SOCKS proxy")
442-
443-
444-
def connect_proxy(
445-
proxy: Proxy,
446-
ws_uri: WebSocketURI,
447-
deadline: Deadline,
448-
**kwargs: Any,
449-
) -> socket.socket:
450-
"""Connect via a proxy and return the socket."""
451-
# parse_proxy() validates proxy.scheme.
452-
if proxy.scheme[:5] == "socks":
453-
return connect_socks_proxy(proxy, ws_uri, deadline, **kwargs)
454-
else:
455-
raise AssertionError("unsupported proxy")

0 commit comments

Comments
 (0)