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
- func GetBaseTemplate(cacheSize string, filesNumber string, availableSize string, memoryUsage string, ...) string
- func GetInternalResType(url string) int
- func GetRewriteRulesByMode(mode int) []distro.Rule
- func HandleHomePage(rw http.ResponseWriter, r *http.Request, cacheDir string)
- func InitUpstreamTransport(enableKeepAlive bool)
- func IsInternalUrls(url string) bool
- func MatchingRule(path string, rules []distro.Rule) (*distro.Rule, bool)
- func RefreshMirrors()
- func RefreshRewriters(rewriters *URLRewriters, mode int)
- func RenderInternalUrls(url string, cacheDir string) (string, int)
- func RewriteRequestByMode(r *http.Request, rewriters *URLRewriters, mode int)
- func ServeStaticLogo(w http.ResponseWriter, r *http.Request)
- type HomePageData
- type PackageStruct
- type RetryableTransport
- type URLRewriter
- type URLRewriters
Constants ¶
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.
const ( InternalPageHome string = "/" InternalPagePing string = "/_/ping/" )
const ( TypeNotFound int = 0 TypeHome int = 1 TypePing int = 2 )
const LabelNoValidValue = "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 GetRewriteRulesByMode ¶
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 MatchingRule ¶
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, and from the /api/mirrors/refresh endpoint. The mutex serializes concurrent refreshes (the rewriter pointer swap inside RefreshRewriters has its own finer-grained lock; this outer lock prevents two refresh runs from racing to clear the benchmark cache and re-elect mirrors at the same time).
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 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.
func ServeStaticLogo ¶ added in v0.10.0
func ServeStaticLogo(w http.ResponseWriter, r *http.Request)
ServeStaticLogo writes the embedded apt-proxy logo PNG with long-lived caching headers. It is intentionally minimal so it can be wired up via any router that accepts an http.HandlerFunc.
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) RefreshMirrors ¶ added in v0.10.0
func (ap *PackageStruct) RefreshMirrors()
RefreshMirrors refreshes this PackageStruct's mirror configuration. Use this method on a PackageStruct instance instead of the package-level proxy.RefreshMirrors() to avoid touching the global state shared with other instances or tests.
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 ¶
RoundTrip implements http.RoundTripper interface with retry and tracing support.
Retry safety: when the request has a body, we only retry if the caller supplied req.GetBody (e.g. via http.NewRequest with bytes.Reader/strings.Reader). Otherwise the body would already be consumed by the first attempt; we make a single attempt in that case rather than send a corrupt second request.
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.