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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SOCKS proxy support #51

Closed

Conversation

yeraydiazdiaz
Copy link
Contributor

Adds SOCKS4 proxy support via socksio 馃帀

You can test this either locally using Dante via Docker as we do in socksio or simply running an ssh -vND <PORT> <OTHER_MACHINE>, then running the following script, assuming the proxy is running in localhost:1080:

import asyncio
import httpcore

async def read_body(stream):
    try:
        body = []
        async for chunk in stream:
            body.append(chunk)
        return b"".join(body)
    finally:
        await stream.aclose()

async def socks_proxy(proxy_server, proxy_mode):
    async with httpcore.AsyncHTTPProxy(proxy_server, proxy_mode=proxy_mode) as http:
        method = b"GET"
        url = (b"http", b"93.184.216.34", 80, b"/")
        headers = [(b"host", b"example.org")]
        http_version, status_code, reason, headers, stream = await http.request(
            method, url, headers
        )
        body = await read_body(stream)
        print(body)

        assert http_version == b"HTTP/1.1"
        assert status_code == 200
        assert reason == b"OK"

def main():
    proxy_server = (b'http', b'localhost', 1080)
    proxy_mode = 'SOCKS4'
    asyncio.run(socks_proxy(proxy_server, proxy_mode))

if __name__ == '__main__':
    main()

Before I carry on with SOCKS4A and SOCKS5 (or possibly I should open a different PR?) I'd like some feedback.

@yeraydiazdiaz yeraydiazdiaz requested a review from a team April 1, 2020 16:47
origin: Origin,
proxy_origin: Origin,
socks_version: str,
user_id: bytes = b"httpcore",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required by SOCKS4.

SOCKS5 will require username/password and likely defining acceptable authentication methods. Which makes me think we might want separate AsyncSOCKS4Connection and AsyncSOCKS5Connection.

@yeraydiazdiaz
Copy link
Contributor Author

I realize there's a fair bit of cleanup noise as well, sorry about that. Hopefully the important bits are clear.

@tomchristie
Copy link
Member

tomchristie commented Apr 30, 2020

Looks like there's now a merge conflict between this and master, that'd need resolving.

Screenshot 2020-04-30 at 10 45 25

@yeraydiazdiaz
Copy link
Contributor Author

Yep, this branch predates the recent changes regarding proxies.

Will update it soon.

@yeraydiazdiaz
Copy link
Contributor Author

I think I'll hold off on this until we've decided if proxy modes are something we want at all.

encode/httpx#927 (comment) and encode/httpx#927 (comment)

@tomchristie
Copy link
Member

SOCKS5 is now supported in 0.14.5 - https://www.encode.io/httpcore/proxies/#socks-proxy-support

(Not sure what we'd want to do about socks4, socks4a)

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

Successfully merging this pull request may close these issues.

None yet

3 participants