Skip to content

Commit

Permalink
socket.dup
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed Mar 29, 2023
1 parent 0942dba commit 4d58af2
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 29 deletions.
5 changes: 2 additions & 3 deletions proxy/core/acceptor/acceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
pre
"""
import os
import socket
import logging
import argparse
Expand All @@ -31,7 +30,7 @@
from ...common.types import HostPort
from ...common.logger import Logger
from ...common.backports import NonBlockingQueue
from ...common.constants import IS_WINDOWS, DEFAULT_LOCAL_EXECUTOR
from ...common.constants import DEFAULT_LOCAL_EXECUTOR


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -187,7 +186,7 @@ def _recv_and_setup_socks(self) -> None:
# dynamically accept from new fds.
for _ in range(self.fd_queue.recv()):
fileno = recv_handle(self.fd_queue)
sock = socket.socket(fileno=fileno if IS_WINDOWS else os.dup(fileno))
sock = socket.socket(fileno=socket.dup(fileno)) # type: ignore[attr-defined]
self.socks[fileno] = sock
self.fd_queue.close()

Expand Down
4 changes: 1 addition & 3 deletions proxy/core/work/fd/fd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
:copyright: (c) 2013-present by Abhinav Singh and contributors.
:license: BSD, see LICENSE for more details.
"""
import os
import socket
import logging
from typing import Any, TypeVar, Optional

from ...event import eventNames
from ..threadless import Threadless
from ....common.types import HostPort, TcpOrTlsSocket
from ....common.constants import IS_WINDOWS


T = TypeVar('T')
Expand All @@ -32,7 +30,7 @@ def work(self, *args: Any) -> None:
fileno: int = args[0]
addr: Optional[HostPort] = args[1]
conn: Optional[TcpOrTlsSocket] = args[2]
conn = conn or socket.socket(fileno=fileno if IS_WINDOWS else os.dup(fileno))
conn = conn or socket.socket(fileno=socket.dup(fileno)) # type: ignore[attr-defined]
uid = '%s-%s-%s' % (self.iid, self._total, fileno)
self.works[fileno] = self.create(uid, conn, addr)
self.works[fileno].publish_event(
Expand Down
12 changes: 6 additions & 6 deletions tests/core/test_acceptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ def setUp(self) -> None:

@mock.patch('selectors.DefaultSelector')
@mock.patch('socket.socket')
@mock.patch('os.dup')
@mock.patch('socket.dup')
@mock.patch('proxy.core.acceptor.acceptor.recv_handle')
def test_continues_when_no_events(
self,
mock_recv_handle: mock.Mock,
mock_os_dup: mock.Mock,
mock_socket_dup: mock.Mock,
mock_socket: mock.Mock,
mock_selector: mock.Mock,
) -> None:
fileno = 10
mock_os_dup.side_effect = lambda fd: fd if fd == fileno else mock.DEFAULT
mock_socket_dup.side_effect = lambda fd: fd
conn = mock.MagicMock()
addr = mock.MagicMock()
sock = mock.MagicMock()
Expand All @@ -69,19 +69,19 @@ def test_continues_when_no_events(

@mock.patch('threading.Thread')
@mock.patch('selectors.DefaultSelector')
@mock.patch('os.dup')
@mock.patch('socket.dup')
@mock.patch('socket.socket')
@mock.patch('proxy.core.acceptor.acceptor.recv_handle')
def test_accepts_client_from_server_socket(
self,
mock_recv_handle: mock.Mock,
mock_socket: mock.Mock,
mock_os_dup: mock.Mock,
mock_socket_dup: mock.Mock,
mock_selector: mock.Mock,
mock_thread: mock.Mock,
) -> None:
fileno = 10
mock_os_dup.side_effect = lambda fd: fd if fd == fileno else mock.DEFAULT
mock_socket_dup.side_effect = lambda fd: fd
conn = mock.MagicMock()
addr = mock.MagicMock()
sock = mock.MagicMock()
Expand Down
4 changes: 2 additions & 2 deletions tests/http/exceptions/test_http_proxy_auth_failed.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ class TestHttpProxyAuthFailed(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup')
self.mock_socket_dup = mocker.patch('socket.dup')
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_server_conn = mocker.patch(
'proxy.http.proxy.server.TcpServerConnection',
)

self.fileno = 10
self.mock_os_dup.side_effect = lambda fd: fd if fd == self.fileno else mocker.DEFAULT
self.mock_socket_dup.side_effect = lambda fd: fd

self._addr = ('127.0.0.1', 54382)
self.flags = FlagParser.initialize(
Expand Down
4 changes: 2 additions & 2 deletions tests/http/proxy/test_http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def _setUp(self, mocker: MockerFixture) -> None:
)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup')
self.mock_socket_dup = mocker.patch('socket.dup')

self.fileno = 10
self.mock_os_dup.side_effect = lambda fd: fd if fd == self.fileno else mocker.DEFAULT
self.mock_socket_dup.side_effect = lambda fd: fd

self._addr = ('127.0.0.1', 54382)
self.flags = FlagParser.initialize(threaded=True)
Expand Down
4 changes: 2 additions & 2 deletions tests/http/proxy/test_http_proxy_tls_interception.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async def test_e2e(self, mocker: MockerFixture) -> None:
host, port = uuid.uuid4().hex, 443
netloc = '{0}:{1}'.format(host, port)

self.mock_os_dup = mocker.patch('os.dup')
self.mock_socket_dup = mocker.patch('socket.dup')
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_sign_csr = mocker.patch('proxy.http.proxy.server.sign_csr')
self.mock_gen_csr = mocker.patch('proxy.http.proxy.server.gen_csr')
Expand All @@ -54,7 +54,7 @@ async def test_e2e(self, mocker: MockerFixture) -> None:
self.mock_gen_public_key.return_value = True

self.fileno = 10
self.mock_os_dup.side_effect = lambda fd: fd if fd == self.fileno else mocker.DEFAULT
self.mock_socket_dup.side_effect = lambda fd: fd

# Used for server side wrapping
self.mock_ssl_context = mocker.patch('ssl.create_default_context')
Expand Down
4 changes: 2 additions & 2 deletions tests/http/test_protocol_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class TestHttpProtocolHandlerWithoutServerMock(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')

self.fileno = 10
Expand Down Expand Up @@ -149,7 +149,7 @@ class TestHttpProtocolHandler(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_server_connection = mocker.patch(
'proxy.http.proxy.server.TcpServerConnection',
Expand Down
10 changes: 5 additions & 5 deletions tests/http/web/test_web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

def test_on_client_connection_called_on_teardown(mocker: MockerFixture) -> None:
plugin = mocker.MagicMock()
mock_os_dup = mocker.patch('os.dup')
mock_os_dup.side_effect = lambda fd: fd
mock_socket_dup = mocker.patch('socket.dup')
mock_socket_dup.side_effect = lambda fd: fd
flags = FlagParser.initialize(threaded=True)
flags.plugins = {b'HttpProtocolHandlerPlugin': [plugin]}
_conn = mocker.MagicMock()
Expand Down Expand Up @@ -143,7 +143,7 @@ class TestWebServerPluginWithPacFilePlugin(Assertions):
) # type: ignore[misc]
def _setUp(self, request: Any, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.fileno = 10
self._addr = ('127.0.0.1', 54382)
Expand Down Expand Up @@ -198,7 +198,7 @@ class TestStaticWebServerPlugin(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.fileno = 10
self._addr = ('127.0.0.1', 54382)
Expand Down Expand Up @@ -320,7 +320,7 @@ class TestWebServerPlugin(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.fileno = 10
self._addr = ('127.0.0.1', 54382)
Expand Down
2 changes: 1 addition & 1 deletion tests/plugin/test_http_proxy_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class TestHttpProxyPluginExamples(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, request: Any, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_server_conn = mocker.patch(
'proxy.http.proxy.server.TcpServerConnection',
Expand Down
4 changes: 2 additions & 2 deletions tests/plugin/test_http_proxy_plugins_with_tls_interception.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestHttpProxyPluginExamplesWithTlsInterception(Assertions):

@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, request: Any, mocker: MockerFixture) -> None:
self.mock_os_dup = mocker.patch('os.dup')
self.mock_socket_dup = mocker.patch('socket.dup')
self.mock_selector = mocker.patch('selectors.DefaultSelector')
self.mock_sign_csr = mocker.patch('proxy.http.proxy.server.sign_csr')
self.mock_gen_csr = mocker.patch('proxy.http.proxy.server.gen_csr')
Expand All @@ -53,7 +53,7 @@ def _setUp(self, request: Any, mocker: MockerFixture) -> None:
self.mock_gen_public_key.return_value = True

self.fileno = 10
self.mock_os_dup.side_effect = lambda fd: fd if fd == self.fileno else mocker.DEFAULT
self.mock_socket_dup.side_effect = lambda fd: fd

self._addr = ('127.0.0.1', 54382)
self.flags = FlagParser.initialize(
Expand Down
2 changes: 1 addition & 1 deletion tests/socks/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestHttpProtocolHandlerWithoutServerMock(Assertions):
@pytest.fixture(autouse=True) # type: ignore[misc]
def _setUp(self, mocker: MockerFixture) -> None:
self.mock_socket = mocker.patch('socket.socket')
self.mock_os_dup = mocker.patch('os.dup', side_effect=lambda fd: fd)
self.mock_socket_dup = mocker.patch('socket.dup', side_effect=lambda fd: fd)
self.mock_selector = mocker.patch('selectors.DefaultSelector')

self.fileno = 10
Expand Down

0 comments on commit 4d58af2

Please sign in to comment.