httpproxy

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 12 Imported by: 0

README

build coverage goreport Docs

httpproxy

HTTP(S) and WebSocket forward proxy.

Supports user authentication, ContextDialer selection per proxy request and custom RoundTripper construction.

Only depends on the standard library. (Though the WebSocket tests use github.com/coder/websocket).

Documentation

Overview

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/linkdata/httpproxy"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/{$}", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "This is a web proxy server.")
	})
	// the httpproxy.Server will handle CONNECT and absolute URL requests,
	// and all others will be forwarded to the http.Handler we set.
	go http.ListenAndServe(":8080", &httpproxy.Server{Handler: mux})
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBodyNotReadWriter = errors.New("response body not an io.ReadWriter")
View Source
var ErrHijackingNotSupported = errors.New("http.ResponseWriter does not support http.Hijacker")
View Source
var ErrUnauthorized = errors.New("unauthorized")
View Source
var MaxCachedRoundTrippers = 100

Functions

func DefaultMakeRoundTripper

func DefaultMakeRoundTripper(cd ContextDialer) http.RoundTripper

DefaultMakeRoundTripper clones http.DefaultTransport, sets it's DialContext member and returns it.

func GetBasicAuth

func GetBasicAuth(hdr http.Header) (username, password string, err error)

func RemoveRequestHeaders

func RemoveRequestHeaders(r *http.Request)

RemoveRequestHeaders removes request headers which should not propagate to the next hop.

func SetBasicAuth

func SetBasicAuth(hdr http.Header, username, password string)

Types

type ContextDialer

type ContextDialer interface {
	DialContext(ctx context.Context, network, address string) (conn net.Conn, err error)
}
var DefaultContextDialer ContextDialer = &net.Dialer{}

type CredentialsValidator

type CredentialsValidator interface {
	ValidateCredentials(username, password, address string) bool
}

CredentialsValidator is used to support user/pass authentication optional network address filtering.

type DialerSelector

type DialerSelector interface {
	// SelectDialer returns the ContextDialer to use.
	//
	// If username is the empty string no authorization has taken place (anonymous usage).
	// If you return the error httpproxy.ErrUnauthorized then httpproxy will generate
	// the HTTP status code 401 Unauthorized.
	SelectDialer(username, network, address string) (cd ContextDialer, err error)
}

A DialerSelector returns the ContextDialer to use.

type Logger

type Logger interface {
	Debug(msg string, keyValuePairs ...any)
	Info(msg string, keyValuePairs ...any)
	Warn(msg string, keyValuePairs ...any)
	Error(msg string, keyValuePairs ...any)
}

Logger matches the log/slog.Logger interface.

type RoundTripperMaker

type RoundTripperMaker interface {
	MakeRoundTripper(cd ContextDialer) (rt http.RoundTripper)
}

type Server

type Server struct {
	Logger               Logger               // optional logger to use
	Handler              http.Handler         // optional handler for requests that aren't proxy requests
	DialerSelector       DialerSelector       // optional handler to select ContextDialer per proxy request, otherwise uses DefaultContextDialer
	CredentialsValidator CredentialsValidator // optional credentials validator
	RoundTripperMaker    RoundTripperMaker    // optional RoundTripperMaker, defaults to DefaultMakeRoundTripper
	// contains filtered or unexported fields
}

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

type StaticCredentials

type StaticCredentials map[string]string

StaticCredentials enables using a map directly as a credential store

func (StaticCredentials) ValidateCredentials

func (s StaticCredentials) ValidateCredentials(username, password, _ string) bool

type WriterFlusher

type WriterFlusher interface {
	io.Writer
	http.Flusher
}

Jump to

Keyboard shortcuts

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