connect_proxy_scheme

package module
v0.0.0-...-2b62bcb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2022 License: Apache-2.0 Imports: 9 Imported by: 3

README

Test Quality Coverage Status

HTTP Connect Scheme Support for golang.org/x/net/proxy

A golang.org/x/net/proxy compatible scheme registration that establishes the TCP connection over an HTTP CONNECT Tunnel.

This is a refactor of https://github.com/mwitkow/go-http-dialer to make it compatible with golang.org/x/net/proxy

Features

  • unencrypted connection to proxy (e.g. http://proxy.example.com:3128
  • TLS connection to proxy (customizeable) (e.g. https://proxy.example.com)
  • customizeable for Proxy-Authenticate, with challenge-response semantics
  • out of the box support for Basic auth
  • appropriate RemoteAddr remapping

Usage

Simple

Just call proxy.RegisterDialerType("http", connect_proxy_scheme.ConnectProxy) somewhere in your application to register the handler for HTTP.

Be aware that HTTP_PROXY and HTTPS_PROXY and related environment variables are not picked up automatically by golang.org/x/net/proxy because it does not have support for them (which this library adds). If you want to use environment variable configuration you'll need to write it yourself.

Worked Example
package main
import (
	"fmt"
	"net/http"
	"net/url"
	"golang.org/x/net/proxy"
	"github.com/wrouesnel/go.connect-proxy-scheme"
)

func init() {
	proxy.RegisterDialerType("http", connect_proxy_scheme.ConnectProxy)
}

func main() {
	u, err := url.Parse("http://some-squid-proxy:3128")
	if err != nil {
		panic(err)
    }
	
	dialer, err := proxy.FromURL(u, proxy.Direct)
	if err != nil {
		panic(err)
    }

	tr := &http.Transport{
		DialContext: proxy.DialContext,
	}
	client := http.Client{Transport: tr}
	
	resp, err := client.Get("http://google.com")
	if err != nil {
		panic(err)
    }
	
	fmt.Println(resp)
}

Documentation

Overview

Package http_dialer provides HTTP(S) CONNECT tunneling net.Dialer. It allows you to establish arbitrary TCP connections (as long as your proxy allows them) through a HTTP(S) CONNECT point.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectProxy

func ConnectProxy(proxyUrl *url.URL, dialer proxy.Dialer) (proxy.Dialer, error)

Types

type HttpConnectTunnel

type HttpConnectTunnel struct {
	// contains filtered or unexported fields
}

HttpConnectTunnel represents a configured HTTP Connect Tunnel dialer.

func New

func New(proxyUrl *url.URL, dialer proxy.Dialer) (*HttpConnectTunnel, error)

New constructs an HttpConnectTunnel to be used a net.Dial command. The first parameter is a proxy URL, for example https://foo.example.com:9090 will use foo.example.com as proxy on port 9090 using TLS for connectivity.

func (HttpConnectTunnel) Dial

func (t HttpConnectTunnel) Dial(network string, address string) (net.Conn, error)

Dial is an implementation of net.Dialer, and returns a TCP connection handle to the host that HTTP CONNECT reached.

func (HttpConnectTunnel) DialContext

func (t HttpConnectTunnel) DialContext(ctx context.Context, network string, address string) (net.Conn, error)

type ProxyAuthorization

type ProxyAuthorization interface {
	// Type represents what kind of Authorization, e.g. "Bearer", "Token", "Digest".
	Type() string

	// Initial allows you to specify an a-priori "Proxy-Authenticate" response header, attached to first request,
	// so you don't need to wait for an additional challenge. If empty string is returned, "Proxy-Authenticate"
	// header is added.
	InitialResponse() string

	// ChallengeResponse returns the content of the "Proxy-Authenticate" response header, that has been chose as
	// response to "Proxy-Authorization" request header challenge.
	ChallengeResponse(challenge string) string
}

ProxyAuthorization allows for plugging in arbitrary implementations of the "Proxy-Authorization" handler.

func AuthBasic

func AuthBasic(username string, password string) ProxyAuthorization

AuthBasic returns a ProxyAuthorization that implements "Basic" protocol while ignoring realm challenges.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL