horizon

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package horizon implements entrypoints based on the network they belong to

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DNSRemoteAddr

func DNSRemoteAddr(rw dns.ResponseWriter) (netip.Addr, error)

DNSRemoteAddr extracts the remote address associated with an dns.ResponseWriter

func ForbiddenExchange

func ForbiddenExchange(_ context.Context, req *dns.Msg) (*dns.Msg, error)

ForbiddenExchange is a resolver.ExchangerFunc that refuses all requests

func ForbiddenHTTP

func ForbiddenHTTP(rw http.ResponseWriter, _ *http.Request)

ForbiddenHTTP is an http.HandlerFunc that always return a 403 error

func HTTPRemoteAddr

func HTTPRemoteAddr(req *http.Request) (netip.Addr, error)

HTTPRemoteAddr extracts the remote address associated with an http.Request

func HandleForbiddenExchange

func HandleForbiddenExchange(rw dns.ResponseWriter, req *dns.Msg)

HandleForbiddenExchange is a dns.HandlerFunc that refuses all requests

func NewContextKey added in v0.3.1

func NewContextKey(label string) *core.ContextKey[Match]

NewContextKey creates a core.ContextKey compatible with Horizons.

Types

type Config

type Config struct {
	Name   string
	Ranges []netip.Prefix

	Middleware         func(http.Handler) http.Handler
	ExchangeMiddleware func(resolver.Exchanger) resolver.Exchanger
}

Config describe a Horizon

func (*Config) New

func (hc *Config) New(h http.Handler, e resolver.Exchanger) *Horizon

New assembles a new Horizon using the Config and the given entrypoints

type Configs

type Configs []Config

Configs represents a sorted list of Horizon configurations

func (Configs) Must

func (hcc Configs) Must(h http.Handler, e resolver.Exchanger) *Horizons

Must assembles a new Horizons using the Configs list but panics if there is an error.

func (Configs) New

func (hcc Configs) New(h http.Handler, e resolver.Exchanger) (*Horizons, error)

New assembles a new Horizons using the Configs list and the given entrypoints

type Horizon

type Horizon struct {
	// contains filtered or unexported fields
}

Horizon is one horizon

func (*Horizon) Exchange

func (z *Horizon) Exchange(ctx context.Context, req *dns.Msg) (*dns.Msg, error)

Exchange implements the resolver.Exchanger interface

func (*Horizon) InRange

func (z *Horizon) InRange(addr netip.Addr) bool

InRange checks if the remote address belongs on this Horizon

func (*Horizon) Lookup

func (z *Horizon) Lookup(ctx context.Context, qName string, qType uint16) (*dns.Msg, error)

Lookup implements the resolver.Lookuper interface

func (*Horizon) Match

func (z *Horizon) Match(addr netip.Addr) (netip.Prefix, bool)

Match finds the first matching CIDR for the given address

func (*Horizon) Name

func (z *Horizon) Name() string

Name returns the name of the Horizon

func (*Horizon) ServeHTTP

func (z *Horizon) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface

func (*Horizon) SetDefaults

func (z *Horizon) SetDefaults() error

SetDefaults fills gaps in the Horizon

func (*Horizon) String

func (z *Horizon) String() string

type Horizons

type Horizons struct {
	ExchangeContextFunc func(netip.Addr, *dns.Msg) context.Context
	ExchangeContext     context.Context
	ExchangeTimeoutFunc func(netip.Addr, *dns.Msg) time.Duration
	ExchangeTimeout     time.Duration

	ContextKey *core.ContextKey[Match]
	// contains filtered or unexported fields
}

Horizons is a list of all known horizons sorted by priority.

func (*Horizons) Append

func (s *Horizons) Append(z *Horizon) error

Append attaches an existing Horizon. Name must be unique.

func (*Horizons) AppendNew

func (s *Horizons) AppendNew(hc Config, h http.Handler, e resolver.Exchanger) error

AppendNew creates a Horizon based on a Config and endpoints. [Config.Name] must be unique.

func (Horizons) Exchange

func (s Horizons) Exchange(ctx context.Context, req *dns.Msg) (*dns.Msg, error)

Exchange implements the resolver.Exchanger interface but requires a Match in the context

func (Horizons) Get

func (s Horizons) Get(name string) *Horizon

Get finds a Horizon by name.

func (*Horizons) Len

func (s *Horizons) Len() int

Len returns the number of defined horizons

func (Horizons) Match

func (s Horizons) Match(addr netip.Addr) (*Horizon, netip.Prefix, bool)

Match finds the CIDR and Horizon corresponding to the given address

func (Horizons) MatchDNSRequest

func (s Horizons) MatchDNSRequest(rw dns.ResponseWriter) (*Horizon, Match, bool)

MatchDNSRequest find the Horizon corresponding to an http.Request and prepares a Match to include in the context.

func (Horizons) MatchHTTPRequest

func (s Horizons) MatchHTTPRequest(req *http.Request) (*Horizon, Match, bool)

MatchHTTPRequest find the Horizon corresponding to an http.Request and prepares a Match to include in the context.

func (Horizons) ServeDNS

func (s Horizons) ServeDNS(rw dns.ResponseWriter, req *dns.Msg)

ServeDNS implements the dns.Handler interface

func (Horizons) ServeHTTP

func (s Horizons) ServeHTTP(rw http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface

type Match

type Match struct {
	Horizon    string
	RemoteAddr netip.Addr
	CIDR       netip.Prefix
}

Match specifies how the Remote made it through, and it's stored in the request's context.

func (Match) IsValid

func (m Match) IsValid() bool

IsValid checks if the Match contains consistent information

Jump to

Keyboard shortcuts

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