define

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package define defines constants and data structures in the application. Includes default configuration values, timeout durations, limit parameters and other constant definitions.

Index

Constants

View Source
const (
	PATH_ROOT           = "/"
	PATH_HEALTH         = "/health"
	PATH_HEALTHCHECK    = "/healthcheck"
	PATH_METRICS        = "/metrics"
	PATH_DATA_JSON      = "/data.json" // 与 GET / 行为一致,返回合并后的用户列表 JSON,便于作为“data.json API”消费
	PATH_USER           = "/user"
	PATH_LOG_LEVEL      = "/log/level"
	PATH_V1_USERS       = "/v1/users"
	PATH_V1_USER        = "/v1/user"
	PATH_V1_LOOKUP      = "/v1/lookup"
	PATH_V1_HEALTH      = "/v1/health"
	PATH_V1_HEALTHCHECK = "/v1/healthcheck"
)

HTTP path constants. Used for route registration, rate-limit skip paths, and access-log skip paths.

View Source
const (
	// DEFAULT_TASK_INTERVAL default task interval (seconds)
	DEFAULT_TASK_INTERVAL = 5 // 5s
	// DEFAULT_TIMEOUT default timeout (seconds)
	DEFAULT_TIMEOUT = 5
	// DEFAULT_LOCK_TIME default lock expiration time (seconds)
	DEFAULT_LOCK_TIME = 15
	// DEFAULT_MODE default mode
	DEFAULT_MODE = "DEFAULT" // 1: 2: 3: 4:

	// RATE_LIMIT_CLEANUP_INTERVAL rate limiter cleanup interval
	RATE_LIMIT_CLEANUP_INTERVAL = 1 * time.Minute

	// DEFAULT_PAGE_SIZE default page size
	DEFAULT_PAGE_SIZE = 100
	// MAX_PAGE_SIZE maximum page size
	MAX_PAGE_SIZE = 1000
	// MAX_IDENTIFIER_LENGTH maximum length for user query params (phone/mail/user_id), prevents DoS and log/cache bloat
	MAX_IDENTIFIER_LENGTH = 512

	// MAX_HEADER_BYTES maximum request header size (1MB)
	MAX_HEADER_BYTES = 1 << 20
	// MAX_REQUEST_BODY_SIZE maximum request body size (10KB)
	MAX_REQUEST_BODY_SIZE = 10 * 1024
	// MAX_JSON_SIZE maximum JSON response body size (10MB), prevents memory exhaustion attacks
	MAX_JSON_SIZE = 10 * 1024 * 1024
	// MAX_ENVELOPE_JSON_SIZE maximum encrypted-envelope JSON size before parsing.
	// The envelope is a JSON object with base64url-encoded ciphertext (~1.37x plaintext)
	// plus RSA/nonce/metadata overhead, so allow generous headroom over MAX_JSON_SIZE.
	MAX_ENVELOPE_JSON_SIZE = 2 * MAX_JSON_SIZE
	// SHUTDOWN_TIMEOUT graceful shutdown timeout
	SHUTDOWN_TIMEOUT = 5 * time.Second
	// HTTP_RETRY_MAX_RETRIES HTTP request maximum retry count
	HTTP_RETRY_MAX_RETRIES = 3
	// HTTP_RETRY_DELAY HTTP request retry delay
	HTTP_RETRY_DELAY = 1 * time.Second
	// IDLE_TIMEOUT HTTP connection idle timeout
	IDLE_TIMEOUT = 120 * time.Second

	// REDIS_CONNECTION_TIMEOUT Redis connection timeout
	REDIS_CONNECTION_TIMEOUT = 5 * time.Second

	// DEFAULT_RATE_LIMIT default rate limit: requests per minute
	DEFAULT_RATE_LIMIT = 60
	// DEFAULT_RATE_LIMIT_WINDOW default rate limit time window
	DEFAULT_RATE_LIMIT_WINDOW = 1 * time.Minute
	// MAX_VISITORS_MAP_SIZE maximum visitors map size, prevents memory leaks
	MAX_VISITORS_MAP_SIZE = 10000
	// MAX_WHITELIST_SIZE maximum whitelist size
	MAX_WHITELIST_SIZE = 1000

	// DEFAULT_MAX_IDLE_CONNS default maximum idle connections
	DEFAULT_MAX_IDLE_CONNS = 100
	// DEFAULT_MAX_IDLE_CONNS_PER_HOST default maximum idle connections per host
	DEFAULT_MAX_IDLE_CONNS_PER_HOST = 10
	// DEFAULT_IDLE_CONN_TIMEOUT default idle connection timeout
	DEFAULT_IDLE_CONN_TIMEOUT = 90 * time.Second
	// DEFAULT_LOAD_DATA_TIMEOUT default data loading timeout
	DEFAULT_LOAD_DATA_TIMEOUT = 30 * time.Second

	// DEFAULT_SLICE_POOL_CAPACITY default slice pool capacity
	DEFAULT_SLICE_POOL_CAPACITY = 100
	// SMALL_DATA_THRESHOLD small data threshold, data smaller than this is processed directly without using buffer pool
	SMALL_DATA_THRESHOLD = 100
	// LARGE_DATA_THRESHOLD large data threshold, data larger than this uses streaming JSON encoding
	LARGE_DATA_THRESHOLD = 10000

	// REDIS_RETRY_MAX_RETRIES Redis operation maximum retry count
	REDIS_RETRY_MAX_RETRIES = 3
	// REDIS_RETRY_DELAY Redis operation retry delay
	REDIS_RETRY_DELAY = 1 * time.Second
)
View Source
const (
	// WARN_RULE_NOT_FOUND rules file not found
	// Note: This constant is deprecated, please use i18n.T(r, "log.data_file_not_found") instead
	WARN_RULE_NOT_FOUND = "没有找到规则文件"
	// WARN_READ_RULE_ERR error reading rules file
	// Note: This constant is deprecated, please use i18n.T(r, "error.data_load_failed") instead
	WARN_READ_RULE_ERR = "读取规则文件遇到错误"
	// WARN_PARSE_RULE_ERR error parsing rules file
	// Note: This constant is deprecated, please use i18n.T(r, "error.data_parse_failed") instead
	WARN_PARSE_RULE_ERR = "解析规则文件遇到错误"
	// ERROR_CAN_NOT_OPEN_RULE error opening rules file
	// Note: This constant is deprecated, please use i18n.T(r, "error.data_load_failed") instead
	ERROR_CAN_NOT_OPEN_RULE = "读取规则文件出错"

	// ERR_REQ_INIT_FAILED network request component initialization failed
	// Note: This constant is deprecated, please use i18n.T(r, "error.http_request_failed") instead
	ERR_REQ_INIT_FAILED = "网络请求组件初始化失败"
	// ERR_GET_CONFIG_FAILED failed to get remote configuration
	// Note: This constant is deprecated, please use i18n.T(r, "error.config_load_failed") instead
	ERR_GET_CONFIG_FAILED = "获取远程配置失败"
	// ERR_READ_CONFIG_FAILED failed to read remote configuration
	// Note: This constant is deprecated, please use i18n.T(r, "error.config_load_failed") instead
	ERR_READ_CONFIG_FAILED = "读取远程配置失败"
	// ERR_PARSE_CONFIG_FAILED failed to parse remote configuration
	// Note: This constant is deprecated, please use i18n.T(r, "error.config_parse_failed") instead
	ERR_PARSE_CONFIG_FAILED = "解析远程配置失败"

	// WARN_GET_REMOTE_FAILED_FALLBACK_LOCAL error getting remote rules, only using local rules
	// Note: This constant is deprecated, please use i18n.T(r, "log.all_sources_failed") instead
	WARN_GET_REMOTE_FAILED_FALLBACK_LOCAL = "获取远程规则出错,仅使用本地规则"

	// INFO_REQ_REMOTE_API request data API
	// Note: This constant is deprecated, please use i18n.T(r, "log.request_data_api") instead
	INFO_REQ_REMOTE_API = "请求数据接口 🎩"
)
View Source
const DEFAULT_DATA_FILE = "./data.json"

DEFAULT_DATA_FILE default local user data file path

View Source
const DEFAULT_PORT = 8081

DEFAULT_PORT default port number

View Source
const DEFAULT_REDIS = "localhost:6379"

DEFAULT_REDIS default Redis address

View Source
const DEFAULT_REMOTE_CONFIG = "http://localhost:8080/data.json"

DEFAULT_REMOTE_CONFIG default remote configuration address

View Source
const DEFAULT_REMOTE_KEY = ""

DEFAULT_REMOTE_KEY default remote configuration key

View Source
const LABEL_OTHER = "other"

LABEL_OTHER is the bucket every unrecognized metric label value collapses into.

No Prometheus label may carry attacker-controlled input: the metrics middleware runs before authentication, so labelling series with a raw request value lets an unauthenticated caller mint an unbounded number of time series that persist for the lifetime of the process. Both the request path and the request method are attacker chosen — net/http accepts any valid token as a method and passes it through verbatim — so both collapse into this bucket when unrecognized.

Variables

KnownHTTPMethods are the request methods that keep their own metric label. Anything else, including a differently-cased spelling, collapses into LABEL_OTHER: HTTP methods are case-sensitive, so "get" is not GET and must not create a second series.

KnownRoutePaths is the complete, authoritative set of paths Warden registers. It is the single source of truth shared by route registration and the Prometheus "endpoint" label: a route may not be served unless it appears here, and no other value may ever reach the label. Keep it in sync with registerRoutes (a test asserts the two match exactly).

View Source
var SkipPathsHealthAndMetrics = []string{PATH_HEALTH, PATH_HEALTHCHECK, PATH_METRICS}

SkipPathsHealthAndMetrics is the path list to skip for rate limiting and access logging. Reduces log noise and keeps health/metrics probes from consuming rate limit.

Functions

func NormalizeEndpointLabel added in v1.3.0

func NormalizeEndpointLabel(path string) string

NormalizeEndpointLabel maps a request path onto a bounded metric label value. Known routes keep their own label; everything else — including unmatched paths that the catch-all 404 handler serves — collapses into LABEL_OTHER. An empty path is reported as PATH_ROOT.

func NormalizeMethodLabel added in v1.3.0

func NormalizeMethodLabel(method string) string

NormalizeMethodLabel maps a request method onto a bounded metric label value, capping the label at len(KnownHTTPMethods)+1 distinct values.

func ParseTrustedProxyIPs added in v0.8.0

func ParseTrustedProxyIPs(s string) []string

ParseTrustedProxyIPs parses comma-separated string (e.g. TRUSTED_PROXY_IPS env), trims each element, drops empty. Used by main and ip_whitelist so env parsing is explicit and consistent.

func RequireExplicitUserID added in v0.14.0

func RequireExplicitUserID() bool

RequireExplicitUserID reports whether an explicit user_id is required.

func SetRequireExplicitUserID added in v0.14.0

func SetRequireExplicitUserID(v bool)

SetRequireExplicitUserID toggles whether Normalize refuses to derive a missing id.

func SetUserIDStrategy added in v0.14.0

func SetUserIDStrategy(s UserIDStrategy)

SetUserIDStrategy sets the process-wide user_id derivation strategy.

Types

type AllowListUser

type AllowListUser struct {
	Phone          string   `json:"phone"`                     // User phone number (optional if Mail is set)
	Mail           string   `json:"mail"`                      // User email address (optional if Phone is set)
	UserID         string   `json:"user_id"`                   // User unique identifier (optional, auto-generated if not provided)
	Status         string   `json:"status"`                    // User status (e.g., "active", "inactive", "suspended")
	Scope          []string `json:"scope"`                     // User permission scope (optional)
	Role           string   `json:"role"`                      // User role (optional)
	Name           string   `json:"name,omitempty"`            // User display name (optional)
	DingtalkUserID string   `json:"dingtalk_userid,omitempty"` // DingTalk user ID for work notification (optional)
}

AllowListUser represents user information in the allow list.

This struct is used to store user's basic information, including phone number, email address, user ID, status, etc. This information is used to verify and authorize user access.

At least one of Phone or Mail must be non-empty. Email-only users are supported (Phone may be empty).

func (*AllowListUser) IsActive added in v0.3.0

func (u *AllowListUser) IsActive() bool

IsActive checks if the user status is active.

Returns true if the user status is "active", false otherwise. This method is used to verify if a user is allowed to access the system.

func (*AllowListUser) IsValid added in v0.3.0

func (u *AllowListUser) IsValid() bool

IsValid checks if the user has a valid status for authentication.

Returns true if the user status is one of the valid statuses (currently only "active"). This method can be extended in the future to support other valid statuses if needed.

func (*AllowListUser) Normalize

func (u *AllowListUser) Normalize()

Normalize normalizes user data, sets default values and generates user_id (if not provided)

This function will: - If user_id is empty, generate based on phone or mail - If status is empty, set to "inactive" (fail closed) - If scope is nil, set to empty array - If role is empty, set to empty string

type UserIDStrategy added in v0.14.0

type UserIDStrategy string

UserIDStrategy selects how an empty user_id is derived during Normalize.

const (
	// UserIDStrategyLegacy preserves the historical behavior: the first 16 hex
	// characters (~64 bits) of SHA-256(identifier). Kept as the default so existing
	// deployments do not silently change user IDs.
	UserIDStrategyLegacy UserIDStrategy = "legacy"
	// UserIDStrategySHA256_128 derives a 128-bit (32 hex char) id from SHA-256 over a
	// domain-separated, normalized input. Opt-in via USER_ID_STRATEGY=sha256-128.
	UserIDStrategySHA256_128 UserIDStrategy = "sha256-128"
)

func GetUserIDStrategy added in v0.14.0

func GetUserIDStrategy() UserIDStrategy

GetUserIDStrategy returns the current user_id derivation strategy.

func ParseUserIDStrategy added in v0.14.0

func ParseUserIDStrategy(s string) (UserIDStrategy, bool)

ParseUserIDStrategy parses a strategy string, defaulting to legacy for empty input.

Jump to

Keyboard shortcuts

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