Documentation
¶
Index ¶
- type Authenticator
- func (a *Authenticator) GetAdminURL(host string) string
- func (a *Authenticator) GetLoginURL(host string) string
- func (a *Authenticator) GetPath() string
- func (a *Authenticator) GetToken() string
- func (a *Authenticator) GetTokenFromRequest(r *http.Request) string
- func (a *Authenticator) IsAuthenticated(r *http.Request) bool
- func (a *Authenticator) SetCookie(w http.ResponseWriter)
- func (a *Authenticator) ValidateToken(token string) bool
- type Handler
- func (h *Handler) GetAdminURL(host string) string
- func (h *Handler) GetLoginURL(host string) string
- func (h *Handler) GetPath() string
- func (h *Handler) HandleChartData(w http.ResponseWriter, r *http.Request)
- func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request)
- func (h *Handler) HandleUI(w http.ResponseWriter, r *http.Request)
- type Renderer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator handles admin authentication with secure token generation.
func NewAuthenticator ¶
func NewAuthenticator(useHTTPS bool) (*Authenticator, error)
NewAuthenticator creates a new authenticator with secure random token and path.
Parameters:
- useHTTPS: whether HTTPS is being used (affects cookie Secure flag)
Returns a new Authenticator instance or an error if random generation fails.
func (*Authenticator) GetAdminURL ¶
func (a *Authenticator) GetAdminURL(host string) string
GetAdminURL returns the admin UI URL (without token).
Parameters:
- host: the host to use in the URL (e.g., "localhost:8000")
Returns the admin UI URL.
func (*Authenticator) GetLoginURL ¶
func (a *Authenticator) GetLoginURL(host string) string
GetLoginURL returns the login URL with token parameter.
Parameters:
- host: the host to use in the URL (e.g., "localhost:8000")
Returns the complete login URL.
func (*Authenticator) GetPath ¶
func (a *Authenticator) GetPath() string
GetPath returns the admin endpoint path.
func (*Authenticator) GetToken ¶
func (a *Authenticator) GetToken() string
GetToken returns the authentication token.
func (*Authenticator) GetTokenFromRequest ¶
func (a *Authenticator) GetTokenFromRequest(r *http.Request) string
GetTokenFromRequest extracts the admin token from either cookie or query parameter.
For backward compatibility, it checks cookies first (preferred), then query parameters.
Parameters:
- r: the HTTP request
Returns the token if found, empty string otherwise.
func (*Authenticator) IsAuthenticated ¶
func (a *Authenticator) IsAuthenticated(r *http.Request) bool
IsAuthenticated checks if the request is authenticated with a valid admin token.
Parameters:
- r: the HTTP request
Returns true if authenticated, false otherwise.
func (*Authenticator) SetCookie ¶
func (a *Authenticator) SetCookie(w http.ResponseWriter)
SetCookie sets the admin authentication cookie.
The Secure flag is set based on the useHTTPS configuration to ensure the cookie is only transmitted over secure connections when HTTPS is enabled.
Parameters:
- w: the HTTP response writer
func (*Authenticator) ValidateToken ¶
func (a *Authenticator) ValidateToken(token string) bool
ValidateToken checks if the provided token matches the admin token using constant-time comparison to prevent timing attacks.
Parameters:
- token: the token to validate
Returns true if the token is valid, false otherwise.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler handles admin UI HTTP requests.
func NewHandler ¶
NewHandler creates a new admin handler.
Parameters:
- auth: authenticator instance
- statsManager: stats manager for retrieving data
- logger: structured logger instance
Returns a new Handler instance.
func (*Handler) GetAdminURL ¶
GetAdminURL returns the admin UI URL (without token).
Parameters:
- host: the host to use in the URL (e.g., "localhost:8000")
Returns the admin UI URL.
func (*Handler) GetLoginURL ¶
GetLoginURL returns the login URL.
Parameters:
- host: the host to use in the URL (e.g., "localhost:8000")
Returns the complete login URL with token.
func (*Handler) HandleChartData ¶
func (h *Handler) HandleChartData(w http.ResponseWriter, r *http.Request)
HandleChartData handles requests for chart data in JSON format.
It validates the authentication token and returns chart data as JSON.
Parameters:
- w: the HTTP response writer
- r: the HTTP request
func (*Handler) HandleLogin ¶
func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request)
HandleLogin handles login requests for the admin UI.
It accepts a token via query parameter, validates it, and sets an HTTP cookie if valid. For backward compatibility, it also accepts the token in the URL.
Parameters:
- w: the HTTP response writer
- r: the HTTP request
func (*Handler) HandleUI ¶
func (h *Handler) HandleUI(w http.ResponseWriter, r *http.Request)
HandleUI handles requests to the admin UI endpoint.
It validates authentication via cookie or query parameter (for backward compatibility). If authentication fails, it returns a 403 Forbidden response. Otherwise, it displays connection statistics including total requests, IP counts, user agent counts, and recent request history in a formatted HTML page.
Parameters:
- w: the HTTP response writer
- r: the HTTP request
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
Renderer handles HTML generation for the admin UI.
func NewRenderer ¶
NewRenderer creates a new renderer.
Parameters:
- adminPath: the admin endpoint path
Returns a new Renderer instance.
func (*Renderer) RenderAdminUI ¶
func (r *Renderer) RenderAdminUI( chartData stats.ChartData, uptime time.Duration, totalRequests, uniqueIPs, uniqueUAs int, recentRequests []stats.RequestInfo, maxDisplay int, nonce string, ) string
RenderAdminUI generates the complete admin UI HTML.
Parameters:
- chartData: data for charts (top IPs and user agents)
- uptime: server uptime duration
- totalRequests: total number of requests
- uniqueIPs: number of unique IP addresses
- uniqueUAs: number of unique user agents
- recentRequests: slice of recent request entries
- maxDisplay: maximum number of recent requests to display
- nonce: CSP nonce for inline scripts (empty string if not using nonces)
Returns the complete HTML as a string.