Skip to main content

TCP TLS tunnel for HTTP requests with HTTP2 support.

Project description

TCP TLS Tunnel Adapters for Python

Provides functionality for creating a TCP-TLS tunnel for HTTP / HTTPS requests. Currently supported libraries are request, hyper and httpx. Our TLS Layer pass ciphers and has SSL session ticket support by default. If you are really interested in testing it for free, you can find out more details in our Discord Channel.

Usage examples

These sections show usage examples for HTTP/1.1 and HTTP/2.0.

Requests adapter (HTTP/1.1)

Installation

pip install tls-tunnel

Example

Let's show, how it works for requests in case of HTTP/1.1:

from requests import Session
from tcp_tls_tunnel.requests_adapter import TunneledHTTPAdapter
from tcp_tls_tunnel.constants import Client
from tcp_tls_tunnel.dto import AdapterOptions, ProxyOptions


# if necessary
proxy_opts = ProxyOptions(
    host="your.proxy.host",
    port=1234,
    auth_login="YOUR_LOGIN",
    auth_password="YOUR_PASSWORD",
)

adapter = TunneledHTTPAdapter(
    adapter_opts=AdapterOptions(
        host="127.0.0.1",  # tunnel address
        port=1337,  # tunnel port
        auth_login="YOUR_LOGIN",
        auth_password="YOUR_PASSWORD",
        client=Client.CHROME,  # imitated Client that will be used
    ),
    proxy_opts=proxy_opts  # or None if not required
)

session = Session()

# connect adapter for requests.Session instance
session.mount("http://", adapter)
session.mount("https://", adapter)

Request to howsmyssl.com:

response = session.get('https://www.howsmyssl.com/a/check')

Output:

# response.status_code
200

# response.headers
{'Content-Length': '874', 
 'Access-Control-Allow-Origin': '*', 
 'Connection': 'close', 
 'Content-Type': 'application/json', 
 'Date': 'Mon, 12 Oct 2020 21:55:42 GMT', 
 'Strict-Transport-Security': 'max-age=631138519; includeSubdomains; preload'}

# response.json()
{'able_to_detect_n_minus_one_splitting': False,
 'beast_vuln': False,
 'ephemeral_keys_supported': True,
 'given_cipher_suites': ['TLS_GREASE_IS_THE_WORD_2A',
                         'TLS_AES_128_GCM_SHA256',
                         'TLS_AES_256_GCM_SHA384',
                         'TLS_CHACHA20_POLY1305_SHA256',
                         'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256',
                         'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256',
                         'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384',
                         'TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384',
                         'TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256',
                         'TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256',
                         'TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA',
                         'TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA',
                         'TLS_RSA_WITH_AES_128_GCM_SHA256',
                         'TLS_RSA_WITH_AES_256_GCM_SHA384',
                         'TLS_RSA_WITH_AES_128_CBC_SHA',
                         'TLS_RSA_WITH_AES_256_CBC_SHA'],
 'insecure_cipher_suites': {},
 'rating': 'Probably Okay',
 'session_ticket_supported': True,
 'tls_compression_supported': False,
 'tls_version': 'TLS 1.3',
 'unknown_cipher_suite_supported': False}

Requests adapter (HTTP/2.0 with hyper)

Installation

pip install 'tls-tunnel[hyper]'

Example

Let's show, how it works for requests in case of HTTP/2.0:

import requests
from tcp_tls_tunnel.dto import ProxyOptions, AdapterOptions
from tcp_tls_tunnel.hyper_http2_adapter import TunnelHTTP20Adapter


adapter = TunnelHTTP20Adapter(
    proxy_opts=ProxyOptions(
        host="your.proxy.host",
        port=1234,
        auth_login="YOUR_LOGIN",
        auth_password="YOUR_PASSWORD",
    ),
    adapter_opts=AdapterOptions(
        host="127.0.0.1",  # tunnel address
        port=1337,  # tunnel port
        auth_login="YOUR_LOGIN",
        auth_password="YOUR_PASSWORD",
    ),
)

session = requests.Session()
session.mount("http://", adapter)
session.mount("https://", adapter)

Request to http2.pro:

response = session.get("https://http2.pro/api/v1")
print(response.json())

Output:

{
    'http2': 1, 
    'protocol': 'HTTP/2.0', 
    'push': 0, 
    'user_agent': 'python-requests/2.24.0'
}

HTTPX Transport (HTTP/2.0)

Installation

pip install 'tls-tunnel[httpx]'

Example

Let's show, how it works for httpx in case of HTTP/2.0:

from tcp_tls_tunnel.dto import AdapterOptions, ProxyOptions
from tcp_tls_tunnel.httpx_adapter import TunnelHTTPTransport

transport = TunnelHTTPTransport(
    adapter_opts=AdapterOptions(
        host="127.0.0.1",  # tunnel address
        port=1337,  # tunnel port
        auth_login="YOUR_LOGIN",
        auth_password="YOUR_PASSWORD",
    ),
    proxy_opts=ProxyOptions(
        host="your.proxy.host",
        port=1234,
        auth_login="YOUR_LOGIN",
        auth_password="YOUR_PASSWORD",
    ),
)

Request to http2.pro:

from httpx import Client


headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
}

with Client(transport=transport, headers=headers) as client:
    response = client.get("https://http2.pro/api/v1")
    print(response.json())

Output:

{
    'http2': 1, 
    'protocol': 'HTTP/2.0', 
    'push': 0, 
    'user_agent': 'python-httpx/0.18.2'
}

Tests

In addition, you can try to run the tests that are available in the tests directory, there are more examples of using the right adapters.

In order to use environment variables it is recommended to create directories with tests .env file, which will be used automatically at startup.

You can do this with the command:

python -m unittest -v tests/*_tests.py

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tcp-tls-tunnel-1.0.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tcp_tls_tunnel-1.0.1-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file tcp-tls-tunnel-1.0.1.tar.gz.

File metadata

  • Download URL: tcp-tls-tunnel-1.0.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for tcp-tls-tunnel-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bde632d274d3aa95ea75cc9119a615be543114a5ce2b238453e4f4d70b5a79b5
MD5 bbf66f9f0cb9343d52fe549b676b58c4
BLAKE2b-256 3dc0e7025a38483f89fb71a0ca4faa8f27febd97139d954b80d4171f8368dcf7

See more details on using hashes here.

File details

Details for the file tcp_tls_tunnel-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: tcp_tls_tunnel-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

File hashes

Hashes for tcp_tls_tunnel-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e65dcb0f3462fca566a5d48ddddb1dbb3693b8fd7033c567f003b2d9212ba2ed
MD5 6451b4cecc546a4e618a6e6005914a21
BLAKE2b-256 0c4812e13a35551447d6005926563b8da756f9d460c2a543c9511821a5516422

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page