app

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Unlicense, MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Period = 3 * time.Minute

Period can be changed before opening a Listener to alter its KeepAlivePeriod.

Functions

func GoVanity

func GoVanity(hn, ba string, mux *http.ServeMux)

GoVanity configures an HTTP handler for redirecting requests to vanity URLs based on the provided hostname and backend address.

Parameters

- hn (string): The hostname associated with the vanity URL.

- ba (string): The backend address, expected to be in the format "git+<repository-path>".

- mux (*http.ServeMux): The HTTP serve multiplexer where the handler will be registered.

Expected behaviour

- Splits the backend address to extract the repository path from the "git+" prefix.

- If the split fails, logs an error and returns without registering a handler.

- Generates an HTML redirect page containing metadata for Go import and redirects to the extracted repository path.

- Registers a handler on the provided ServeMux that serves this redirect page when requests are made to the specified hostname.

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target *url.URL) (rp *httputil.ReverseProxy)

NewSingleHostReverseProxy is a copy of httputil.NewSingleHostReverseProxy with the addition of forwarding headers:

- Legacy X-Forwarded-* headers (X-Forwarded-Proto, X-Forwarded-For, X-Forwarded-Host)

- Standardized Forwarded header according to RFC 7239 (https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/Forwarded)

func NostrDNS

func NostrDNS(hn, ba string, mux *http.ServeMux) (err error)

NostrDNS handles the configuration and registration of a Nostr DNS endpoint for a given hostname and backend address.

Parameters

- hn (string): The hostname for which the Nostr DNS entry is being configured.

- ba (string): The path to the JSON file containing the Nostr DNS data.

- mux (*http.ServeMux): The HTTP serve multiplexer to which the Nostr DNS handler will be registered.

Return Values

- err (error): An error if any step fails during the configuration or registration process.

Expected behaviour

- Reads the JSON file specified by `ba` and parses its contents into a NostrJSON struct.

- Registers a new HTTP handler on the provided `mux` for the `.well-known/nostr.json` endpoint under the specified hostname.

- The handler serves the parsed Nostr DNS data with appropriate HTTP headers set for CORS and content type.

func ReadMapping

func ReadMapping(file string) (m map[string]string, err error)

ReadMapping reads a mapping file and returns a map of hostnames to backend addresses.

Parameters

- file (string): The path to the mapping file to read.

Return Values

- m (map[string]string): A map containing the hostname to backend address mappings parsed from the file.

- err (error): An error if any step during reading or parsing fails.

Expected behaviour

- Opens the specified file and reads its contents line by line.

- Skips lines that are empty or start with a '#'.

- Splits each valid line into two parts using the first colon as the separator.

- Trims whitespace from both parts and adds them to the map.

- Returns any error encountered during file operations or parsing.

func Run

func Run(c context.T, args RunArgs) (err error)

func SetProxy

func SetProxy(mapping map[string]string) (h http.Handler, err error)

SetProxy creates an HTTP handler that routes incoming requests to specified backend addresses based on hostname mappings.

Parameters

- mapping (map[string]string): A map where keys are hostnames and values are the corresponding backend addresses.

Return Values

- h (http.Handler): The HTTP handler configured with the proxy settings. - err (error): An error if the mapping is empty or invalid.

Expected behaviour

- Validates that the provided hostname to backend address mapping is not empty.

- Creates a new ServeMux and configures it to route requests based on the specified hostnames and backend addresses.

- Handles special cases such as vanity URLs, Nostr DNS entries, and Unix socket connections.

func SetupServer

func SetupServer(a RunArgs) (s *http.Server, h http.Handler, err error)

SetupServer configures and returns an HTTP server instance with proxy handling and automatic certificate management based on the provided RunArgs configuration.

Parameters

- a (RunArgs): The configuration arguments containing settings for the server address, cache directory, mapping file, HSTS header, email, and certificates.

Return Values

- s (*http.Server): The configured HTTP server instance.

- h (http.Handler): The HTTP handler used for proxying requests and managing automatic certificate challenges.

- err (error): An error if any step during setup fails.

Expected behaviour

- Reads the hostname to backend address mapping from the specified configuration file.

- Sets up a proxy handler that routes incoming requests based on the defined mappings.

- Enables HSTS header support if enabled in the RunArgs.

- Creates the cache directory for storing certificates and keys if it does not already exist.

- Configures an autocert.Manager to handle automatic certificate management, including hostname whitelisting, email contact, and cache storage.

- Initializes the HTTP server with proxy handler, address, and TLS configuration.

func TLSConfig

func TLSConfig(m *autocert.Manager, certs ...string) (tc *tls.Config)

TLSConfig creates a custom TLS configuration that combines automatic certificate management with explicitly provided certificates.

Parameters

- m (*autocert.Manager): The autocert manager used for managing automatic certificate generation and retrieval.

- certs (...string): A variadic list of certificate definitions in the format "domain:/path/to/cert", where each domain maps to a certificate file. The corresponding key file is expected to be at "/path/to/cert.key".

Return Values

- tc (*tls.Config): A new TLS configuration that prioritises explicitly provided certificates over automatically generated ones.

Expected behaviour

- Loads all explicitly provided certificates and maps them to their respective domains.

- Creates a custom GetCertificate function that checks if the requested domain matches any of the explicitly provided certificates, returning those first.

- Falls back to the autocert manager's GetCertificate method if no explicit certificate is found for the requested domain.

Types

type Conn

type Conn struct {
	time.Duration
	*net.TCPConn
}

Conn extends deadline after successful read or write operations

func (Conn) Read

func (c Conn) Read(b []byte) (n int, e error)

func (Conn) Write

func (c Conn) Write(b []byte) (n int, e error)

type Listener

type Listener struct {
	time.Duration
	*net.TCPListener
}

Listener sets TCP keep-alive timeouts on accepted connections. It is used by ListenAndServe and ListenAndServeTLS so dead TCP connections (e.g. closing laptop mid-download) eventually go away.

func (Listener) Accept

func (ln Listener) Accept() (conn net.Conn, e error)

type NostrJSON

type NostrJSON struct {
	Names  map[string]string   `json:"names"`
	Relays map[string][]string `json:"relays"`
}

type Pool

type Pool struct{}

func (Pool) Get

func (bp Pool) Get() []byte

func (Pool) Put

func (bp Pool) Put(b []byte)

type Proxy

type Proxy struct {
	http.Handler
}

func (*Proxy) ServeHTTP

func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

type RunArgs

type RunArgs struct {
	Addr  string        `arg:"-l,--listen" default:":https" help:"address to listen at"`
	Conf  string        `arg:"-m,--map" default:"mapping.txt" help:"file with host/backend mapping"`
	Cache string        `arg:"-c,--cachedir" default:"/var/cache/letsencrypt" help:"path to directory to cache key and certificates"`
	HSTS  bool          `arg:"-h,--hsts" help:"add Strict-Transport-Security header"`
	Email string        `arg:"-e,--email" help:"contact email address presented to letsencrypt CA"`
	HTTP  string        `arg:"--http" default:":http" help:"optional address to serve http-to-https redirects and ACME http-01 challenge responses"`
	RTO   time.Duration `arg:"-r,--rto" default:"1m" help:"maximum duration before timing out read of the request"`
	WTO   time.Duration `arg:"-w,--wto" default:"5m" help:"maximum duration before timing out write of the response"`
	Idle  time.Duration `arg:"-i,--idle" help:"how long idle connection is kept before closing (set rto, wto to 0 to use this)"`
	Certs []string      `` /* 246-byte string literal not displayed */

}

Jump to

Keyboard shortcuts

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