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

Https proxy (via CONNECT) support #155

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open

Conversation

uralm1
Copy link
Contributor

@uralm1 uralm1 commented Aug 29, 2023

If someone interested, I have added preliminary HTTPS proxy support, known as CONNECT proxy tunnel. It can be used to pass https requests through http proxy (this is how most proxies are working today).

New proxy type Request::Proxy::Type::HTTPS is added for CONNECT type proxy.

No proxy authentication support done yet. Sorry.

Example: (see https://github.com/jgaa/restc-cpp/blob/master/doc/Tutorial.md#send-a-request-going-trough-a-http-proxy)

// Add the proxy information to the properties used by the client
    Request::Properties properties;
    //// use HTTPS proxy type
    properties.proxy.type = Request::Proxy::Type::HTTPS;
    properties.proxy.address = "http://127.0.0.1:3003";

    //// or detect proxy automatically
    properties.proxy.detect();

    // Create the client with our configuration
    auto rest_client = RestClient::Create(properties);
    rest_client->ProcessWithPromise([&](Context& ctx) {
        // Here we are again in a co-routine, running in a worker-thread.
        // Asynchronously connect to a server trough a HTTP proxy and fetch some data.
        auto reply = RequestBuilder(ctx)
            ////
            //// https request is passed via http proxy CONNECT tunnel
            ////
            .Get("https://api.example.com/normal/posts/1")

            // Send the request.
            .Execute();
        // Dump the data
        cout << "Got: " << reply->GetBodyAsString();
    }).get();

Also I changed TlsSocket class, so we can pass unencrypted data over TLS socket before tls handshake is done. Otherwise we get tls unintialized protocol error (I know, code is imperfect, more ideas are welcome).

Though I haven't done much testing, sequental proxied requests correctly use the same cached connection to proxy.
If we send proxied requests asyncronously in parallel, they will open new proxy tunnels one for each request.

Testing nginx container is modified so it will serve https requests. Benefit from this, old https tests can be rewritten to request the local nginx instead of the public internet.

@uralm1 uralm1 force-pushed the https_proxy branch 4 times, most recently from ec6256a to 96008a5 Compare August 30, 2023 07:51
@@ -0,0 +1,5 @@
#!/bin/bash

openssl req -x509 -newkey rsa:4096 -keyout test-key.pem -out test.pem -sha256 -days 3650 -nodes -subj "/C=XX/ST=Test/L=Test/OU=Test/O=Test/CN=localhost"

Choose a reason for hiding this comment

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

3000:80"
nginx:
build: nginx
ports:
- "3001:80"
- "3002:443"
links:
- "json:api"
squid:
build: squid
ports:
- "3003:3128"
links:
- "nginx:api.example.com"

socks:
    image: jgaafromnorth/shinysocks
    environment:
      - LOG_LEVEL=trace
    ports:
      - "3004:1080"
    links: 
        - "nginx:api.example.com"

@@ -0,0 +1,5 @@
#!/bin/bash

openssl req -x509 -newkey rsa:4096 -keyout test-key.pem -out test.pem -sha256 -days 3650 -nodes -subj "/C=XX/ST=Test/L=Test/OU=Test/O=Test/CN=localhost"

Choose a reason for hiding this comment

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

3000:80"
nginx:
build: nginx
ports:
- "3001:80"
- "3002:443"
links:
- "json:api"
squid:
build: squid
ports:
- "3003:3128"
links:
- "nginx:api.example.com"

socks:
    image: jgaafromnorth/shinysocks
    environment:
      - LOG_LEVEL=trace
    ports:
      - "3004:1080"
    links: 
        - "nginx:api.example.com"

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.

2 participants