Skip to content

Commit 553e3e6

Browse files
committed
Update socks_proxy.py
set timeouts for socks5 connection initialization
1 parent 9820975 commit 553e3e6

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

httpcore/_sync/socks_proxy.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def _init_socks5_connection(
4545
host: bytes,
4646
port: int,
4747
auth: tuple[bytes, bytes] | None = None,
48+
init_timeouts: typing.Optional[typing.Tuple[float, float]] = 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 @@ def _init_socks5_connection(
5658
)
5759
conn.send(socksio.socks5.SOCKS5AuthMethodsRequest([auth_method]))
5860
outgoing_bytes = conn.data_to_send()
59-
stream.write(outgoing_bytes)
61+
stream.write(outgoing_bytes, timeout=write_timeout)
6062

6163
# Auth method response
62-
incoming_bytes = stream.read(max_bytes=4096)
64+
incoming_bytes = 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 @@ def _init_socks5_connection(
7577
username, password = auth
7678
conn.send(socksio.socks5.SOCKS5UsernamePasswordRequest(username, password))
7779
outgoing_bytes = conn.data_to_send()
78-
stream.write(outgoing_bytes)
80+
stream.write(outgoing_bytes, timeout=write_timeout)
7981

8082
# Username/password response
81-
incoming_bytes = stream.read(max_bytes=4096)
83+
incoming_bytes = 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 @@ def _init_socks5_connection(
9193
)
9294
)
9395
outgoing_bytes = conn.data_to_send()
94-
stream.write(outgoing_bytes)
96+
stream.write(outgoing_bytes, timeout=write_timeout)
9597

9698
# Connect response
97-
incoming_bytes = stream.read(max_bytes=4096)
99+
incoming_bytes = 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 @@ def handle_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
with self._connect_lock:
222225
if self._connection is None:
@@ -237,6 +240,7 @@ def handle_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
with Trace(
242246
"setup_socks5_connection", logger, request, kwargs

0 commit comments

Comments
 (0)