amp

package module
v0.0.0-...-600f8e8 Latest Latest
Warning

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

Go to latest
Published: Jan 13, 2026 License: GPL-3.0 Imports: 31 Imported by: 1

README

amp

AMP cache

Documentation

Overview

Package amp implements an AMP client for communicating with an AMP broker.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewAMPClientConn

func NewAMPClientConn(conn net.Conn, brokerURL, cacheURL *url.URL, front string) (net.Conn, error)

NewAMPClientConn creates a new AMP client connection that implements net.Conn. This connection is not encrypted!

func NewCryptClientConn

func NewCryptClientConn(conn net.Conn, serverPublicKey *rsa.PublicKey) (*cryptClientConn, error)

NewCryptClientConn creates a new cryptConn that implements net.Conn. This connection is encrypted using a hybrid encryption scheme: a symmetric key is generated for encrypting the data with ChaCha20-Poly1305, and this key is then encrypted with the server's RSA public key. This connector should be used to wrap the ampClientConn to provide encryption.

Types

type AMPRequest

type AMPRequest struct {
	ClientRequestEncoded string        `json:"p"`
	Key                  string        `json:"k"`
	Nonce                string        `json:"n"`
	ClientRequest        ClientRequest `json:"-"`
	// contains filtered or unexported fields
}

type Broker

type Broker interface {
	Handle(http.ResponseWriter, *http.Request)
}

func NewBroker

func NewBroker(clientTimeout time.Duration, privateKey *rsa.PrivateKey, client *http.Client) Broker

NewBroker creates a new Broker instance with the specified client timeout, private key for decrypting client requests, and an optional HTTP client.

type BrokerResponse

type BrokerResponse struct {
	Response string `json:"response"`
	Nonce    string `json:"nonce"`
}

BrokerResponse is a struct that represents an HTTP response.

type Client

type Client interface {
	Exchange([]byte) (io.ReadCloser, error)
	RoundTripper() (http.RoundTripper, error)
}

func NewClient

func NewClient(brokerURL, cacheURL *url.URL, fronts []string, transport http.RoundTripper, serverPublicKey *rsa.PublicKey, dialer dialFunc) (Client, error)

NewClient creates a new AMP client that can communicate with an AMP broker. If cacheURL is non-nil, the client will use the AMP cache at that URL. If fronts is non-empty, the client will use domain fronting by randomly selecting one of the provided front domains. transport is a optional parameter since it's only used by the Exchange method (for AMP cache support but be aware! Exchange func doesn't encrypt your data!). The server public key must be provided for the RoundTripper method to work. The dialer parameter is optional and can be nil, in which case the default net.Dialer will be used.

func NewClientWithOptions

func NewClientWithOptions(ctx context.Context, opts ...Option) (Client, error)

NewClientWithOptions builds a new amp client with the provided options. It supports options for retrieving the latest configuration given a poll interval, http client and config url address until context is canceled. If the config storage path option is provided and the file exists, the client will load the config and use it; the config updater will also store the latest changes in the provided path.

type ClientRequest

type ClientRequest struct {
	Method  string      `json:"method"`
	Host    string      `json:"host"`
	URL     string      `json:"url,omitempty"`
	Body    []byte      `json:"body"`
	Headers http.Header `json:"headers"`
}

ClientRequest is a struct that represents an HTTP request so it can be encoded into JSON, encrypted with the RSA public key by the client and decryptod/decoded by the broker

type Config

type Config struct {
	BrokerURL string   `yaml:"brokerURL"`
	CacheURL  string   `yaml:"cacheURL"`
	Fronts    []string `yaml:"fronts"`
	PublicKey string   `yaml:"publicKey"`
}

Config contains the parameters required for sending requests with the amp client

type HTTPResponse

type HTTPResponse struct {
	StatusCode    int         `json:"status_code"`
	StatusText    string      `json:"status_text"`
	ContentLength int64       `json:"content_length"`
	Headers       http.Header `json:"headers"`
	Body          []byte      `json:"body"`
}

type Option

type Option func(*client) error

Option is a function type used to configure the amp client instance.

func WithConfig

func WithConfig(cfg Config) Option

WithConfig set the initial configuration for the amp client

func WithConfigStoragePath

func WithConfigStoragePath(storagePath string) Option

WithConfigStoragePath sets the filepath to store the retrieved config locally.

func WithConfigURL

func WithConfigURL(configURL string) Option

WithConfigURL sets the URL to retrieve the latest configuration

func WithDialer

func WithDialer(dial dialFunc) Option

WithDialer sets the network dialer function used for creating connections.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient sets the HTTP client used during the configuration synchronization.

func WithPollInterval

func WithPollInterval(t time.Duration) Option

WithPollInterval sets the poll interval for fetching new configurations. By default it's set as 12h

Jump to

Keyboard shortcuts

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