clientnet

package
v0.3.18 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package clientnet classifies authenticated frontend peer and proxy addresses.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientNet

func ClientNet(r *http.Request, trusted ...netip.Prefix) (netip.Addr, bool)

ClientNet classifies the peer and returns its client address. Forwarding headers are trusted only from loopback or explicitly configured proxies. Malformed chains fail closed. A public hop cannot be hidden by a preceding client-supplied private address.

func ForwardedHost

func ForwardedHost(r *http.Request) string

ForwardedHost returns the sanitized first X-Forwarded-Host entry.

func ForwardedPrefix

func ForwardedPrefix(r *http.Request) string

ForwardedPrefix returns the sanitized first X-Forwarded-Prefix entry.

func ForwardedProto

func ForwardedProto(r *http.Request) string

ForwardedProto returns the first X-Forwarded-Proto value if it is "http" or "https".

func IsPublicIP

func IsPublicIP(ip net.IP) bool

IsPublicIP reports whether ip is a globally routable unicast address.

func JoinPrefix

func JoinPrefix(forwardedPrefix, mountPrefix string) string

JoinPrefix combines a reverse-proxy prefix with an Ether mount prefix without duplicating mountPrefix when forwardedPrefix already ends with it on a path segment boundary.

func LAN

func LAN(ip netip.Addr) bool

LAN reports whether an address belongs to a LAN or overlay network.

func ParsePlexDirectHost

func ParsePlexDirectHost(host string) (ip net.IP, hash string, ok bool)

ParsePlexDirectHost parses "<ip-label>.<hash>.plex.direct" hosts.

func ParseProxyEntry

func ParseProxyEntry(raw string) (netip.Prefix, error)

ParseProxyEntry parses one trusted proxy IP address (treated as /32 or /128) or CIDR prefix. Wildcard ranges (0.0.0.0/0, ::/0) are rejected so any internet peer cannot spoof forwarding headers.

func ProxyPrefixes

func ProxyPrefixes(cidrs []string) []netip.Prefix

ProxyPrefixes parses configured proxy IPs and networks. Invalid entries grant no trust.

func RedirectPath

func RedirectPath(forwardedPrefix, mountPrefix, targetPath string) string

RedirectPath builds a safe relative redirect Location combining any trusted X-Forwarded-Prefix, mountPrefix, and targetPath (which should start with "/").

func SanitizeHost

func SanitizeHost(host string) string

SanitizeHost validates and trims an HTTP Host or X-Forwarded-Host value.

func SanitizePrefix

func SanitizePrefix(raw string) string

SanitizePrefix validates a reverse-proxy path prefix such as "/media" or "/ether/app", stripping trailing slashes and rejecting open-redirect or traversal forms ("//evil.com", "/../x", control characters, queries).

func TrustedPeer

func TrustedPeer(r *http.Request, trusted ...netip.Prefix) bool

TrustedPeer reports whether r's direct connecting peer is loopback or within one of the explicitly trusted proxy prefixes.

Types

type RequestOrigin

type RequestOrigin struct {
	Scheme  string // "http" or "https"
	Host    string // sanitized host or host:port
	Prefix  string // sanitized X-Forwarded-Prefix ("" or "/foo/bar", never trailing slash)
	Proxied bool   // true when trusted X-Forwarded-{Proto,Host,Prefix} headers were applied
}

RequestOrigin describes the scheme, host, and optional reverse-proxy prefix a client used to reach Ether on an HTTP request.

func Origin

func Origin(r *http.Request, trusted ...netip.Prefix) (RequestOrigin, bool)

Origin derives the client-visible origin for r. Forwarded headers (X-Forwarded-Proto, X-Forwarded-Host, X-Forwarded-Prefix) are honoured only when the direct connecting peer is loopback or within trusted.

func (RequestOrigin) BaseURL

func (o RequestOrigin) BaseURL(mountPrefix string) string

BaseURL returns "scheme://host[+prefix]" without a trailing slash, combining any trusted X-Forwarded-Prefix with mountPrefix without duplicating an already-present trailing mount segment.

func (RequestOrigin) CanonicalBase

func (o RequestOrigin) CanonicalBase(mountPrefix string) string

CanonicalBase returns BaseURL normalized for deduplication: lowercased host and omitted redundant default ports (:80 for http, :443 for https).

func (RequestOrigin) Hostname

func (o RequestOrigin) Hostname() string

Hostname returns the host without any port or IPv6 brackets.

func (RequestOrigin) Port

func (o RequestOrigin) Port() int

Port returns the explicit port carried on Host, or 0 if none was specified.

Jump to

Keyboard shortcuts

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