proxy

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package proxy provides URL rewriting and reverse proxy functionality for apt-proxy. It handles distribution-specific URL patterns and routes requests to configured mirrors.

Index

Constants

View Source
const (
	DefaultResponseHeaderTimeout = 45 * time.Second
	DefaultIdleConnTimeout       = 90 * time.Second
	DefaultMaxIdleConns          = 100
)

Default transport timeouts and limits for upstream requests. Extract to constants for tuning and documentation.

View Source
const (
	INTERNAL_PAGE_HOME string = "/"
	INTERNAL_PAGE_PING string = "/_/ping/"
)
View Source
const (
	TYPE_NOT_FOUND int = 0
	TYPE_HOME      int = 1
	TYPE_PING      int = 2
)
View Source
const LABEL_NO_VALID_VALUE = "N/A"

Variables

This section is empty.

Functions

func GetBaseTemplate

func GetBaseTemplate(cacheSize string, filesNumber string, availableSize string,
	memoryUsage string, goroutines string) string

GetBaseTemplate renders the home page template with the provided statistics. It uses html/template for safe rendering and returns the rendered HTML string.

func GetInternalResType

func GetInternalResType(url string) int

func GetRewriteRulesByMode

func GetRewriteRulesByMode(mode int) []distro.Rule

GetRewriteRulesByMode returns caching rules for a specific mode. Prefers registry (config-loaded) rules when present.

func HandleHomePage

func HandleHomePage(rw http.ResponseWriter, r *http.Request, cacheDir string)

HandleHomePage serves the home page with statistics

func InitUpstreamTransport added in v0.9.0

func InitUpstreamTransport(enableKeepAlive bool)

InitUpstreamTransport configures the upstream HTTP transport (e.g. from config). Call before CreatePackageStructRouter or CreatePackageStructRouterAsync. enableKeepAlive: true = reuse connections to mirrors (recommended); false = disable keep-alives.

func IsInternalUrls

func IsInternalUrls(url string) bool

func MatchingRule

func MatchingRule(path string, rules []distro.Rule) (*distro.Rule, bool)

MatchingRule finds a matching rule for the given path

func RefreshMirrors

func RefreshMirrors()

RefreshMirrors refreshes the mirror configurations for all distributions. This is typically called in response to a SIGHUP signal for hot reload.

func RefreshRewriters

func RefreshRewriters(rewriters *URLRewriters, mode int)

RefreshRewriters refreshes the rewriters with updated mirror configurations. This function is safe to call concurrently and will update the mirrors based on the current state configuration. It clears the benchmark cache to force fresh benchmark tests.

IMPORTANT: This function creates new rewriters outside the lock to avoid blocking request processing during potentially slow network operations (benchmark tests). The lock is only held briefly during the pointer swap.

func RenderInternalUrls

func RenderInternalUrls(url string, cacheDir string) (string, int)

func RewriteRequestByMode

func RewriteRequestByMode(r *http.Request, rewriters *URLRewriters, mode int)

RewriteRequestByMode rewrites the request URL to point to the configured mirror for the specified distribution mode. It matches the request path against distribution-specific patterns and replaces the URL scheme, host, and path with the mirror's configuration. If rewriters is nil, the function returns early.

Types

type HomePageData

type HomePageData struct {
	CacheSize     string
	FilesNumber   string
	AvailableSize string
	MemoryUsage   string
	Goroutines    string
}

HomePageData holds the data for rendering the home page template

type PackageStruct

type PackageStruct struct {
	Handler  http.Handler  // The underlying HTTP handler (typically a reverse proxy)
	Rules    []distro.Rule // Caching rules for different package types
	CacheDir string        // Cache directory path for statistics
	// contains filtered or unexported fields
}

PackageStruct is the main HTTP handler that routes requests to appropriate distribution-specific handlers and applies caching rules.

func CreatePackageStructRouter

func CreatePackageStructRouter(cacheDir string, log *logger.Logger) *PackageStruct

CreatePackageStructRouter initializes and returns a new PackageStruct instance configured for the current proxy mode. Uses synchronous benchmark (may block startup). For faster startup, use CreatePackageStructRouterAsync instead.

func CreatePackageStructRouterAsync

func CreatePackageStructRouterAsync(cacheDir string, log *logger.Logger) *PackageStruct

CreatePackageStructRouterAsync initializes and returns a new PackageStruct instance using async benchmark for faster startup (recommended).

func (*PackageStruct) ServeHTTP

func (ap *PackageStruct) ServeHTTP(rw http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. It processes incoming requests, matches them against caching rules, and routes them to the appropriate handler. If a matching rule is found, the request is processed with cache control headers.

type RetryableTransport

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

RetryableTransport wraps an http.RoundTripper with retry logic and tracing support

func NewRetryableTransport

func NewRetryableTransport(baseTransport http.RoundTripper) *RetryableTransport

NewRetryableTransport creates a new RetryableTransport with http-kit integration

func (*RetryableTransport) RoundTrip

func (rt *RetryableTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper interface with retry and tracing support

func (*RetryableTransport) SetRetryOptions

func (rt *RetryableTransport) SetRetryOptions(opts *httpkit.RetryOptions)

SetRetryOptions allows customizing retry behavior

type URLRewriter

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

URLRewriter holds the mirror and pattern for URL rewriting

type URLRewriters

type URLRewriters struct {
	Ubuntu      *URLRewriter
	UbuntuPorts *URLRewriter
	Debian      *URLRewriter
	Centos      *URLRewriter
	Alpine      *URLRewriter
	Mu          sync.RWMutex
}

URLRewriters manages rewriters for different distributions

func CreateNewRewriters

func CreateNewRewriters(mode int) *URLRewriters

CreateNewRewriters initializes rewriters based on mode. This uses synchronous benchmark which may block startup for up to 30 seconds. For faster startup, use CreateNewRewritersAsync instead.

func CreateNewRewritersAsync

func CreateNewRewritersAsync(mode int) *URLRewriters

CreateNewRewritersAsync initializes rewriters based on mode using async benchmark. This allows the server to start immediately with default mirrors while benchmark runs in the background. Once benchmark completes, mirrors are automatically updated. This is the recommended method for production use to minimize startup time.

Jump to

Keyboard shortcuts

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