Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrapmodule affects not just the wrapped module #158

Open
CyberShadow opened this issue Jan 8, 2022 · 2 comments
Open

wrapmodule affects not just the wrapped module #158

CyberShadow opened this issue Jan 8, 2022 · 2 comments

Comments

@CyberShadow
Copy link

wrapmodule is defined simply as:

module.socket.socket = socksocket

However, that will affect not just the socket function as it is visible from the module, but from everywhele else as well. Therefore, e.g. socks.wrapmodule(smtplib) will affect the entire application.

@PhilipGarnero
Copy link

Same issue here. Is there any other method to wrap a single module ?

@PhilipGarnero
Copy link

PhilipGarnero commented Mar 4, 2025

I've figured out something :

class PatchedSocketModule:
    def __init__(self, proxy_type, proxy_host, proxy_port, proxy_user, proxy_password):
        """
        Initialize an SMTP proxy configuration container.

        Args:
            proxy_type (str): Type of proxy (e.g., 'http', 'socks4', 'socks5')
            proxy_host (str): Hostname or IP address of the proxy server
            proxy_port (int): Port number for the proxy server
            proxy_user (str): Username for proxy authentication (optional)
            proxy_password (str): Password for proxy authentication (optional)
        """
        self.proxy_type = proxy_type
        self.proxy_host = proxy_host
        self.proxy_port = proxy_port
        self.proxy_user = proxy_user
        self.proxy_password = proxy_password

    def create_connection_patched(self, *args, **kwargs):
        """Patched version of socket.create_connection that routes through proxy."""
        return socks.create_connection(
            *args, **kwargs,
            proxy_type=self.proxy_type,
            proxy_addr=self.proxy_host,
            proxy_port=self.proxy_port,
            proxy_username=self.proxy_user,
            proxy_password=self.proxy_password,
        )

    def __getattr__(self, name):
        if name == 'socket':
            return socks.socksocket
        if name == 'create_connection':
            return self.create_connection_patched
        return getattr(socket, name)


def configure_smtp_proxy(proxy_type, proxy_host, proxy_port, proxy_user, proxy_password):
    """Configures the global SMTP proxy settings."""
    if not hasattr(configure_smtp_proxy, 'configured'):
        socks.setdefaultproxy(proxy_type, proxy_host, proxy_port, username=proxy_user, password=proxy_password)
        p = patch('smtplib.socket', PatchedSocketModule(proxy_type, proxy_host, proxy_port, proxy_user, proxy_password))
        p.start()
        configure_smtp_proxy.configured = True

It's not pretty but it works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants