Home >Backend Development >Golang >How to Establish UTLS Connections via HTTP 1.1 Proxy?

How to Establish UTLS Connections via HTTP 1.1 Proxy?

Susan Sarandon
Susan SarandonOriginal
2024-11-15 13:29:02851browse

How to Establish UTLS Connections via HTTP 1.1 Proxy?

Proxied UTLS Connections via HTTP 1.1

When establishing UTLS connections, you may encounter the need to utilize a proxy, such as an HTTP or SOCKS5 proxy. To achieve this, you can leverage the following strategies:

First, you'll need to create a custom proxy dialer function. This function takes the form of dialTLS(network, addr string, _ *tls.Config) (net.Conn, error). Using this function, you can dial the proxy to establish a net.Conn. Afterward, use the net.Conn to create a UTLS client before performing the handshake.

Below is an example of how your custom dialTLS function could look:

import (
    "crypto/tls"
    "net"
    "net/url"

    "github.com/magisterquis/connectproxy"
    "golang.org/x/net/proxy"
    utls "github.com/refraction-networking/utls"
)

var proxyString = "http://127.0.0.1:8080"

dialTLS := func(network, addr string, _ *tls.Config) (net.Conn, error) {
    proxyURI, _ := url.Parse(proxyString)

    var proxyDialer proxy.Dialer
    switch proxyURI.Scheme {
    case "socks5":
        proxyDialer, err = proxy.SOCKS5("tcp", proxyString, nil, proxy.Direct)
    case "http":
        proxyDialer, err = connectproxy.New(proxyURI, proxy.Direct)
    }

    conn, err := proxyDialer.Dial("tcp", addr)
    uconn := utls.UClient(conn, cfg, &utls.HelloRandomizedALPN)
    // ... continue handshake and request
}

Here are a few additional tips:

  • Consider using the "connectproxy" module if you plan to tunnel through an HTTP CONNECT proxy.
  • Explore the Meek pluggable transport source for Tor. Its 'utls.go' module simplifies the process, including setting up HTTP or HTTP2 transport based on the negotiated ALPN protocol. It currently supports SOCKS but can be adapted for HTTP proxies.

The above is the detailed content of How to Establish UTLS Connections via HTTP 1.1 Proxy?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn