Skip to content

Commit b92d663

Browse files
committed
Update socks_proxy.py
added timeouts for socks5 connection initialization in _async\socks_proxy.py
1 parent aec7644 commit b92d663

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

httpcore/_async/socks_proxy.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ async def _init_socks5_connection(
4545
host: bytes,
4646
port: int,
4747
auth: tuple[bytes, bytes] | None = None,
48+
init_timeouts: tuple[float, float] | None = None
4849
) -> None:
50+
read_timeout, write_timeout = init_timeouts or (None, None)
4951
conn = socksio.socks5.SOCKS5Connection()
5052

5153
# Auth method request
@@ -56,10 +58,10 @@ async def _init_socks5_connection(
5658
)
5759
conn.send(socksio.socks5.SOCKS5AuthMethodsRequest([auth_method]))
5860
outgoing_bytes = conn.data_to_send()
59-
await stream.write(outgoing_bytes)
61+
await stream.write(outgoing_bytes, timeout=write_timeout)
6062

6163
# Auth method response
62-
incoming_bytes = await stream.read(max_bytes=4096)
64+
incoming_bytes = await stream.read(max_bytes=4096, timeout=read_timeout)
6365
response = conn.receive_data(incoming_bytes)
6466
assert isinstance(response, socksio.socks5.SOCKS5AuthReply)
6567
if response.method != auth_method:
@@ -75,10 +77,10 @@ async def _init_socks5_connection(
7577
username, password = auth
7678
conn.send(socksio.socks5.SOCKS5UsernamePasswordRequest(username, password))
7779
outgoing_bytes = conn.data_to_send()
78-
await stream.write(outgoing_bytes)
80+
await stream.write(outgoing_bytes, timeout=write_timeout)
7981

8082
# Username/password response
81-
incoming_bytes = await stream.read(max_bytes=4096)
83+
incoming_bytes = await stream.read(max_bytes=4096, timeout=read_timeout)
8284
response = conn.receive_data(incoming_bytes)
8385
assert isinstance(response, socksio.socks5.SOCKS5UsernamePasswordReply)
8486
if not response.success:
@@ -91,10 +93,10 @@ async def _init_socks5_connection(
9193
)
9294
)
9395
outgoing_bytes = conn.data_to_send()
94-
await stream.write(outgoing_bytes)
96+
await stream.write(outgoing_bytes, timeout=write_timeout)
9597

9698
# Connect response
97-
incoming_bytes = await stream.read(max_bytes=4096)
99+
incoming_bytes = await stream.read(max_bytes=4096, timeout=read_timeout)
98100
response = conn.receive_data(incoming_bytes)
99101
assert isinstance(response, socksio.socks5.SOCKS5Reply)
100102
if response.reply_code != socksio.socks5.SOCKS5ReplyCode.SUCCEEDED:
@@ -217,6 +219,7 @@ async def handle_async_request(self, request: Request) -> Response:
217219
timeouts = request.extensions.get("timeout", {})
218220
sni_hostname = request.extensions.get("sni_hostname", None)
219221
timeout = timeouts.get("connect", None)
222+
init_timeouts = timeouts.get("read", None), timeouts.get("write", None)
220223

221224
async with self._connect_lock:
222225
if self._connection is None:
@@ -237,6 +240,7 @@ async def handle_async_request(self, request: Request) -> Response:
237240
"host": self._remote_origin.host.decode("ascii"),
238241
"port": self._remote_origin.port,
239242
"auth": self._proxy_auth,
243+
"init_timeouts": init_timeouts,
240244
}
241245
async with Trace(
242246
"setup_socks5_connection", logger, request, kwargs

0 commit comments

Comments
 (0)