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

Added support for Custom DNS resolver when using proxy (Asyncio). #42

Closed
wants to merge 1 commit into from

Conversation

MKiwi
Copy link

@MKiwi MKiwi commented Jan 8, 2024

import socket
import asyncio
import aiohttp

from aiohttp_socks import ProxyType, ProxyConnector
from python_socks.async_.asyncio._resolver import Resolver
from aiohttp import AsyncResolver


class CustomResolver(Resolver):

    def __init__(self, loop: asyncio.AbstractEventLoop):
        super().__init__(loop)
        self.resolver = AsyncResolver(loop, nameservers=['208.67.222.123'], udp_port=5353)

    async def resolve(self, host, port=0, family=socket.AF_UNSPEC):
        hosts = await self.resolver.resolve(host, port, family)
        return '', hosts[0]['host']


async def main():
    loop = asyncio.get_event_loop()
    resolver = CustomResolver(loop)
    proxy_host = '199.102.104.70'
    proxy_port = 4145

    connector = ProxyConnector(proxy_type=ProxyType.SOCKS5,
                               host=proxy_host,
                               port=proxy_port,
                               rdns=False,
                               proxy_resolver=resolver,
                               ssl=False
                               )
    async with aiohttp.ClientSession(connector=connector) as session:
        async with session.get("http://ipapi.co/json") as resp:
            print(await resp.json())


if __name__ == '__main__':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
    asyncio.run(main())

@gaby
Copy link

gaby commented Aug 8, 2024

@romis2012 Any chance of getting this merged?

@romis2012
Copy link
Owner

What host do you want to resolve with a custom resolver? If it's a proxy host, this PR doesn't solve that problem.

@romis2012
Copy link
Owner

In addition, adding a custom resolver involves exposing its public API, which I really don't want to do for a number of reasons.

@MKiwi
Copy link
Author

MKiwi commented Aug 8, 2024

The purpose of this is to have option to specify different nameservers when making resolutions and connections through proxy.

@romis2012
Copy link
Owner

romis2012 commented Aug 8, 2024

The purpose of this is to have option to specify different nameservers when making resolutions and connections through proxy.

This (and this) PR does nothing for that. I suspect you want to use aiodns, if so, then open an issue

@romis2012 romis2012 closed this Aug 8, 2024
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

Successfully merging this pull request may close these issues.

3 participants