clientproxy

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 14 Imported by: 0

README

caddy-clientproxy

This Caddy module provides a handler that proxies traffic from your Caddy server to your origin. Using the sibling clientproxy package, your origin initiates and maintains a connection to your Caddy server. Caddy uses this connection to proxy requests to your origin. This way your origin does not need to accept any connections, and need only support outbound connections.

Usage

  1. Make sure you're using https as appropriate.
  2. Use a sufficiently large shared secret.
  3. Order the handlers correctly. This is a terminal handler, in that it does not continue the chain if the reverse proxy is available.
  4. Use clientproxyd to make your server(s) available via your caddy.

Configuration

You'll need to order this handler, or use route:

{
	order client_proxy before respond
}

example.com {
	client_proxy 46f20973162c43d09bf7ca2311a9c3ca
}

clientproxyd

On the machine which hosts your origin server, you'll need to run clientproxyd. This process will maintain a connection to your Caddy instance, and accept and proxy requests to your origin server. You'll need a configuration file:

[[proxy]]
register = "https://example.com/46f20973162c43d09bf7ca2311a9c3ca"
forward = "http://localhost:8080"

Run the clientproxyd server:

clientproxyd config.toml

Now a request to https://example.com should get proxied to your origin server.

Implementation

In Caddy, when the module recieves a valid client request that intends to become the server, it Hijacks the connection, and converts it to a HTTP2 Client Connection, which can be used as a http.RoundTripper. This serves as the reverse proxy target.

The server makes a TLS secured HTTP/1.1 connection to Caddy, and then treats that connection as a HTTP2 Server Connection. It then starts serving requests on this connection.

Testing

In terminal 1, start the caddy server with the sample Caddyfile:

xcaddy run -c Caddyfile

In terminal 2, start the example server. This is actually the process that handles the HTTP requests, but it does not listen on any ports.

cd clientproxy
go run ./cmd/example-server

In terminal 3, make a request using curl to your caddy server:

curl -k https://localhost:4430/

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

type Middleware struct {
	// The secret to allow for registering a client.
	Secret string `json:"secret,omitempty"`
	// contains filtered or unexported fields
}

Middleware implements an HTTP handler that allows for a client to become the reverse proxy.

func (*Middleware) CaddyModule

func (*Middleware) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*Middleware) Provision

func (m *Middleware) Provision(ctx caddy.Context) error

Provision implements caddy.Provisioner.

func (*Middleware) ServeHTTP

func (m *Middleware) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*Middleware) UnmarshalCaddyfile

func (m *Middleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

func (*Middleware) Validate

func (m *Middleware) Validate() error

Validate implements caddy.Validator.

Jump to

Keyboard shortcuts

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