websvc

package
v0.0.0-...-b4faca2 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: AGPL-3.0 Imports: 30 Imported by: 0

Documentation

Overview

Package websvc contains the AdGuard DNS web service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BindData

type BindData struct {
	// TLS is the optional TLS configuration.
	TLS *tls.Config

	// Address is the binding address.  It must not be empty.
	Address netip.AddrPort
}

BindData is data for binding one HTTP server to an address.

type BlockPageServerConfig

type BlockPageServerConfig struct {
	// ContentFilePath is the path to HTML block page content file.  It must not
	// be empty.
	ContentFilePath string

	// Bind are the addresses on which to serve the block page.  At least one
	// must be provided.  All items must not be nil.
	Bind []*BindData
}

BlockPageServerConfig is the blocking page server configuration.

type CertificateValidator

type CertificateValidator interface {
	// IsValidWellKnownRequest returns true if r is a valid HTTP request for
	// certificate validation.  r must not be nil.
	IsValidWellKnownRequest(ctx context.Context, r *http.Request) (ok bool)
}

CertificateValidator checks if an HTTP request is a TLS-certificate validation request.

type Config

type Config struct {
	// Logger is used for logging the operation of the web service.  It must not
	// be nil.
	Logger *slog.Logger

	// AdultBlocking is the optional adult-blocking block-page web server.
	AdultBlocking *BlockPageServerConfig

	// GeneralBlocking is the optional general block-page web server.
	GeneralBlocking *BlockPageServerConfig

	// SafeBrowsing is the optional safe-browsing block-page web server.
	SafeBrowsing *BlockPageServerConfig

	// LinkedIP is the optional linked IP web server.
	LinkedIP *LinkedIPServer

	// RootRedirectURL is the optional URL to which root HTTP requests are
	// redirected.  If not set, these requests are responded with a 404 page.
	RootRedirectURL *url.URL

	// CertificateValidator checks if an HTTP request is a TLS-certificate
	// validation request.  It must not be nil.
	CertificateValidator CertificateValidator

	// StaticContent is the content that is served statically at the given
	// paths.  It must not be nil; use [http.NotFoundHandler] if not needed.
	StaticContent http.Handler

	// DNSCheck is the HTTP handler for DNS checks.  It must not be nil.
	DNSCheck http.Handler

	// ErrColl is used to collect linked IP proxy errors and other errors.  It
	// must not be nil.
	ErrColl errcoll.Interface

	// Metrics is used for the collection of the web service requests
	// statistics.  It must not be nil.
	Metrics Metrics

	// Error404 is the optional content of the HTML page for the 404 status.  If
	// not set, a simple plain-text 404 response is served.
	Error404 []byte

	// Error500 is the optional content of the HTML page for the 500 status.  If
	// not set, a simple plain-text 500 response is served.
	Error500 []byte

	// NonDoHBind are the bind addresses and optional TLS configuration for the
	// web service in addition to the ones in the DNS-over-HTTPS handlers.  All
	// items must not be nil.
	NonDoHBind []*BindData

	// Timeout is the timeout for all server operations.  It must be positive.
	Timeout time.Duration
}

Config is the AdGuard DNS web service configuration structure.

type EmptyMetrics

type EmptyMetrics struct{}

EmptyMetrics is the implementation of the Metrics interface that does nothing.

func (EmptyMetrics) IncrementReqCount

func (EmptyMetrics) IncrementReqCount(_ context.Context, _ RequestType)

IncrementReqCount implements the Metrics interface for EmptyMetrics.

type LinkedIPServer

type LinkedIPServer struct {
	// TargetURL is the URL to which linked IP API requests are proxied.  It
	// must not be nil.
	TargetURL *url.URL

	// Bind are the addresses on which to serve the linked IP API.  At least one
	// must be provided.  All items must not be nil.
	Bind []*BindData
}

LinkedIPServer is the linked IP server configuration.

type Metrics

type Metrics interface {
	// IncrementReqCount increments the web service request count for a given
	// RequestType.  reqType must be one of the RequestType values.
	IncrementReqCount(ctx context.Context, reqType RequestType)
}

Metrics is an interface for collecting web service request statistics.

type RejectCertificateValidator

type RejectCertificateValidator struct{}

RejectCertificateValidator is a CertificateValidator which rejects all HTTP requests.

func (RejectCertificateValidator) IsValidWellKnownRequest

func (RejectCertificateValidator) IsValidWellKnownRequest(
	_ context.Context,
	_ *http.Request,
) (ok bool)

IsValidWellKnownRequest implements the CertificateValidator interface for RejectCertificateValidator. It always returns false.

type RequestType

type RequestType = string

RequestType is a type alias for string that represents the request type for web service metrics.

const (
	RequestTypeError404            RequestType = "error404"
	RequestTypeError500            RequestType = "error500"
	RequestTypeStaticContent       RequestType = "static_content"
	RequestTypeDNSCheckTest        RequestType = "dnscheck_test"
	RequestTypeRobotsTxt           RequestType = "robots_txt"
	RequestTypeRootRedirect        RequestType = "root_redirect"
	RequestTypeLinkedIPProxy       RequestType = "linkip"
	RequestTypeAdultBlockingPage   RequestType = "adult_blocking_page"
	RequestTypeGeneralBlockingPage RequestType = "general_blocking_page"
	RequestTypeSafeBrowsingPage    RequestType = "safe_browsing_page"
)

List of web service requests of type RequestType.

NOTE: Keep in sync with [metrics.RequestType].

type ServerGroup

type ServerGroup = string

ServerGroup is a semantic alias for names of server groups.

const (
	ServerGroupAdultBlockingPage   ServerGroup = "adult_blocking_page"
	ServerGroupGeneralBlockingPage ServerGroup = "general_blocking_page"
	ServerGroupLinkedIP            ServerGroup = "linked_ip"
	ServerGroupNonDoH              ServerGroup = "non_doh"
	ServerGroupSafeBrowsingPage    ServerGroup = "safe_browsing_page"
)

Valid server groups.

type Service

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

Service is the AdGuard DNS web service. A nil *Service serves a simple plain-text 404 page.

func New

func New(c *Config) (svc *Service)

New returns a new properly initialized *Service. If c is nil, svc is a nil *Service that only serves a simple plain-text 404 page. The service must be refreshed with Service.Refresh before use.

func (*Service) Handler

func (svc *Service) Handler() (h http.Handler)

Handler returns a handler that wraps svc with [httputil.LogMiddleware].

TODO(a.garipov): Ensure logging in module dnssvc and remove this crutch.

func (*Service) Refresh

func (svc *Service) Refresh(ctx context.Context) (err error)

Refresh implements the service.Refresher interface for *Service. svc may be nil.

func (*Service) ServeHTTP

func (svc *Service) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface for *Service. This handler is used for the non-DoH queries on the DoH server as well as on the additional servers, which usually serve this handler over plain HTTP.

func (*Service) Shutdown

func (svc *Service) Shutdown(ctx context.Context) (err error)

Shutdown implements the service.Interface interface for *Service. svc may be nil.

func (*Service) Start

func (svc *Service) Start(ctx context.Context) (err error)

Start implements the service.Interface interface for *Service. It starts serving all endpoints but does not wait for them to actually go online. svc may be nil. err is always nil; if any endpoint fails to start, it panics.

TODO(a.garipov): Wait for the services to go online.

type StaticContent

type StaticContent map[string]*StaticFile

StaticContent serves static content with the given content type. Elements must not be nil.

func (StaticContent) ServeHTTP

func (sc StaticContent) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements the http.Handler interface for StaticContent.

type StaticFile

type StaticFile struct {
	// Headers contains headers of the HTTP response.
	Headers http.Header

	// Content is the file content.
	Content []byte
}

StaticFile is a single file in a [StaticFS].

Jump to

Keyboard shortcuts

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