connect

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package connect provides an HTTP CONNECT tunneling handler.

The package owns CONNECT method validation, connection hijacking, upstream dialing, CONNECT acknowledgement, raw bidirectional tunneling, and neutral observation hooks. It does not own MITM, persistence, or application delivery contracts.

Example
upstreamLn, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
	panic(err)
}
defer upstreamLn.Close()

done := make(chan struct{})
go func() {
	defer close(done)
	conn, err := upstreamLn.Accept()
	if err == nil {
		_, _ = io.Copy(io.Discard, conn)
		_ = conn.Close()
	}
}()

server := httptest.NewServer(New(Options{}))
defer server.Close()

conn, err := net.Dial("tcp", strings.TrimPrefix(server.URL, "http://"))
if err != nil {
	panic(err)
}
defer conn.Close()

_, _ = fmt.Fprintf(conn, "CONNECT %s HTTP/1.1\r\nHost: %s\r\n\r\n", upstreamLn.Addr().String(), upstreamLn.Addr().String())
line, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
	panic(err)
}

fmt.Println(strings.TrimSpace(line))
_ = conn.Close()
<-done
Output:
HTTP/1.1 200 Connection Established

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrConnectRequired    = errors.New("connect: CONNECT method is required")
	ErrEmptyTarget        = errors.New("connect: target is required")
	ErrHijackNotSupported = errors.New("connect: hijacking not supported")
)

Functions

This section is empty.

Types

type DialContextFunc

type DialContextFunc func(context.Context, string, string) (net.Conn, error)

type ErrorWriter

type ErrorWriter func(http.ResponseWriter, *http.Request, int, error)

type Handler

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

func New

func New(opts Options) *Handler

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Options

type Options struct {
	DialContext       DialContextFunc
	GenerateSessionID func() string
	ObserveRequest    func(*http.Request) bool
	WriteError        ErrorWriter
	Hooks             observe.Hooks
	Network           string
}

Jump to

Keyboard shortcuts

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