Skip to content

Commit 498d027

Browse files
committed
Handle 'local_addr' arg of the 'connect' method
1 parent 227ae9f commit 498d027

File tree

9 files changed

+44
-5
lines changed

9 files changed

+44
-5
lines changed

python_socks/async_/anyio/_proxy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ async def connect(
6060
stacklevel=2,
6161
)
6262

63+
local_host = kwargs.get('local_host')
6364
try:
6465
with anyio.fail_after(timeout):
6566
if _stream is None:
@@ -68,6 +69,7 @@ async def connect(
6869
await connect_tcp(
6970
host=self._proxy_host,
7071
port=self._proxy_port,
72+
local_host=local_host,
7173
)
7274
)
7375
except OSError as e:

python_socks/async_/anyio/v2/_proxy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ssl
2-
from typing import Optional
2+
from typing import Any, Optional
33

44
import anyio
55

@@ -47,16 +47,19 @@ async def connect(
4747
dest_port: int,
4848
dest_ssl: Optional[ssl.SSLContext] = None,
4949
timeout: Optional[float] = None,
50+
**kwargs: Any,
5051
) -> AnyioSocketStream:
5152
if timeout is None:
5253
timeout = DEFAULT_TIMEOUT
5354

55+
local_host = kwargs.get('local_host')
5456
try:
5557
with anyio.fail_after(timeout):
5658
return await self._connect(
5759
dest_host=dest_host,
5860
dest_port=dest_port,
5961
dest_ssl=dest_ssl,
62+
local_host=local_host,
6063
)
6164
except TimeoutError as e:
6265
raise ProxyTimeoutError('Proxy connection timed out: {}'.format(timeout)) from e
@@ -66,12 +69,14 @@ async def _connect(
6669
dest_host: str,
6770
dest_port: int,
6871
dest_ssl: Optional[ssl.SSLContext] = None,
72+
local_host: Optional[str] = None,
6973
) -> AnyioSocketStream:
7074
if self._forward is None:
7175
try:
7276
stream = await connect_tcp(
7377
host=self._proxy_host,
7478
port=self._proxy_port,
79+
local_host=local_host,
7580
)
7681
except OSError as e:
7782
raise ProxyConnectionError(

python_socks/async_/asyncio/_proxy.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,32 @@ async def connect(
6666
stacklevel=2,
6767
)
6868

69+
local_addr = kwargs.get('local_addr')
6970
try:
7071
async with async_timeout.timeout(timeout):
7172
return await self._connect(
7273
dest_host=dest_host,
7374
dest_port=dest_port,
7475
_socket=_socket,
76+
local_addr=local_addr,
7577
)
7678
except asyncio.TimeoutError as e:
7779
raise ProxyTimeoutError(f'Proxy connection timed out: {timeout}') from e
7880

79-
async def _connect(self, dest_host, dest_port, _socket=None) -> socket.socket:
81+
async def _connect(
82+
self,
83+
dest_host,
84+
dest_port,
85+
_socket=None,
86+
local_addr=None,
87+
) -> socket.socket:
8088
if _socket is None:
8189
try:
8290
_socket = await connect_tcp(
8391
host=self._proxy_host,
8492
port=self._proxy_port,
8593
loop=self._loop,
94+
local_addr=local_addr,
8695
)
8796
except OSError as e:
8897
msg = 'Could not connect to proxy {}:{} [{}]'.format(

python_socks/async_/asyncio/v2/_proxy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import asyncio
22
import ssl
3-
from typing import Optional
3+
from typing import Any, Optional, Tuple
44
import warnings
55
import sys
66

@@ -68,16 +68,19 @@ async def connect(
6868
dest_port: int,
6969
dest_ssl: Optional[ssl.SSLContext] = None,
7070
timeout: Optional[float] = None,
71+
**kwargs: Any,
7172
) -> AsyncioSocketStream:
7273
if timeout is None:
7374
timeout = DEFAULT_TIMEOUT
7475

76+
local_addr = kwargs.get('local_addr')
7577
try:
7678
async with async_timeout.timeout(timeout):
7779
return await self._connect(
7880
dest_host=dest_host,
7981
dest_port=dest_port,
8082
dest_ssl=dest_ssl,
83+
local_addr=local_addr,
8184
)
8285
except asyncio.TimeoutError as e:
8386
raise ProxyTimeoutError('Proxy connection timed out: {}'.format(timeout)) from e
@@ -87,13 +90,15 @@ async def _connect(
8790
dest_host: str,
8891
dest_port: int,
8992
dest_ssl: Optional[ssl.SSLContext] = None,
93+
local_addr: Optional[Tuple[str, int]] = None,
9094
) -> AsyncioSocketStream:
9195
if self._forward is None:
9296
try:
9397
stream = await connect_tcp(
9498
host=self._proxy_host,
9599
port=self._proxy_port,
96100
loop=self._loop,
101+
local_addr=local_addr,
97102
)
98103
except OSError as e:
99104
raise ProxyConnectionError(

python_socks/async_/curio/_proxy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ async def connect(
5555
stacklevel=2,
5656
)
5757

58+
local_addr = kwargs.get('local_addr')
5859
try:
5960
return await curio.timeout_after(
6061
timeout,
6162
self._connect,
6263
dest_host,
6364
dest_port,
6465
_socket,
66+
local_addr,
6567
)
6668
except curio.TaskTimeout as e:
6769
raise ProxyTimeoutError(f'Proxy connection timed out: {timeout}') from e
@@ -71,12 +73,14 @@ async def _connect(
7173
dest_host: str,
7274
dest_port: int,
7375
_socket=None,
76+
local_addr=None,
7477
):
7578
if _socket is None:
7679
try:
7780
_socket = await connect_tcp(
7881
host=self._proxy_host,
7982
port=self._proxy_port,
83+
local_addr=local_addr,
8084
)
8185
except OSError as e:
8286
msg = 'Could not connect to proxy {}:{} [{}]'.format(

python_socks/async_/trio/_proxy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ async def connect(
5454
stacklevel=2,
5555
)
5656

57+
local_addr = kwargs.get('local_addr')
5758
try:
5859
with trio.fail_after(timeout):
5960
return await self._connect(
6061
dest_host=dest_host,
6162
dest_port=dest_port,
6263
_socket=_socket,
64+
local_addr=local_addr,
6365
)
6466
except trio.TooSlowError as e:
6567
raise ProxyTimeoutError('Proxy connection timed out: {}'.format(timeout)) from e
@@ -69,12 +71,14 @@ async def _connect(
6971
dest_host: str,
7072
dest_port: int,
7173
_socket=None,
74+
local_addr=None,
7275
) -> trio.socket.SocketType:
7376
if _socket is None:
7477
try:
7578
_socket = await connect_tcp(
7679
host=self._proxy_host,
7780
port=self._proxy_port,
81+
local_addr=local_addr,
7882
)
7983
except OSError as e:
8084
msg = 'Could not connect to proxy {}:{} [{}]'.format(

python_socks/async_/trio/v2/_proxy.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import ssl
2-
from typing import Optional
2+
from typing import Any, Optional
33

44
import trio
55

@@ -47,16 +47,19 @@ async def connect(
4747
dest_port: int,
4848
dest_ssl: Optional[ssl.SSLContext] = None,
4949
timeout: Optional[float] = None,
50+
**kwargs: Any,
5051
) -> TrioSocketStream:
5152
if timeout is None:
5253
timeout = DEFAULT_TIMEOUT
5354

55+
local_addr = kwargs.get('local_addr')
5456
try:
5557
with trio.fail_after(timeout):
5658
return await self._connect(
5759
dest_host=dest_host,
5860
dest_port=dest_port,
5961
dest_ssl=dest_ssl,
62+
local_addr=local_addr,
6063
)
6164
except trio.TooSlowError as e:
6265
raise ProxyTimeoutError(f'Proxy connection timed out: {timeout}') from e
@@ -66,12 +69,14 @@ async def _connect(
6669
dest_host: str,
6770
dest_port: int,
6871
dest_ssl: Optional[ssl.SSLContext] = None,
72+
local_addr: Optional[str] = None,
6973
) -> TrioSocketStream:
7074
if self._forward is None:
7175
try:
7276
stream = await connect_tcp(
7377
host=self._proxy_host,
7478
port=self._proxy_port,
79+
local_addr=local_addr,
7580
)
7681
except OSError as e:
7782
raise ProxyConnectionError(

python_socks/sync/_proxy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ def connect(
5555
)
5656

5757
if _socket is None:
58+
local_addr = kwargs.get('local_addr')
5859
try:
5960
_socket = connect_tcp(
6061
host=self._proxy_host,
6162
port=self._proxy_port,
6263
timeout=timeout,
64+
local_addr=local_addr,
6365
)
6466
except OSError as e:
6567
msg = 'Could not connect to proxy {}:{} [{}]'.format(

python_socks/sync/v2/_proxy.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import socket
22
import ssl
3-
from typing import Optional
3+
from typing import Any, Optional
44

55
from ._connect import connect_tcp
66
from ._stream import SyncSocketStream
@@ -45,16 +45,19 @@ def connect(
4545
dest_port: int,
4646
dest_ssl: Optional[ssl.SSLContext] = None,
4747
timeout: Optional[float] = None,
48+
**kwargs: Any,
4849
) -> SyncSocketStream:
4950
if timeout is None:
5051
timeout = DEFAULT_TIMEOUT
5152

5253
if self._forward is None:
54+
local_addr = kwargs.get('local_addr')
5355
try:
5456
stream = connect_tcp(
5557
host=self._proxy_host,
5658
port=self._proxy_port,
5759
timeout=timeout,
60+
local_addr=local_addr,
5861
)
5962
except OSError as e:
6063
msg = 'Could not connect to proxy {}:{} [{}]'.format(

0 commit comments

Comments
 (0)