middleware

package
v0.0.0-...-f17429d Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: GPL-3.0 Imports: 27 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RequestIdKey = "X-Oneapi-Request-Id"
)

Variables

This section is empty.

Functions

func ApplySecurityMiddleware

func ApplySecurityMiddleware(r *gin.RouterGroup)

ApplySecurityMiddleware applies security middleware to router group

func CSRFMiddleware

func CSRFMiddleware(config *SecurityConfig) gin.HandlerFunc

CSRFMiddleware CSRF保护中间件

func Cache

func Cache() func(c *gin.Context)

func CorsMiddleware

func CorsMiddleware() gin.HandlerFunc

CorsMiddleware handles cross-origin resource sharing

func DisableCache

func DisableCache() gin.HandlerFunc

func GetCarrotSessionField

func GetCarrotSessionField() string

func InjectDB

func InjectDB(db *gorm.DB) gin.HandlerFunc

InjectDB 注入数据库实例

func InputValidationMiddleware

func InputValidationMiddleware() gin.HandlerFunc

InputValidationMiddleware 输入验证中间件

func LoggerMiddleware

func LoggerMiddleware(logger *zap.Logger) gin.HandlerFunc

LoggerMiddleware 请求日志中间件

func OpenAPICredentialFromContext

func OpenAPICredentialFromContext(c *gin.Context) (*models.Credential, bool)

OpenAPICredentialFromContext returns the credential bound by OpenAPIEmailCredential middleware.

func OpenAPIEmailCredential

func OpenAPIEmailCredential(db *gorm.DB) gin.HandlerFunc

OpenAPIEmailCredential 校验 LAuthorization: Bearer <APIKEY>,并要求 L-Timestamp(Unix 秒)与 L-Nonce(防重放)。 仅 kind=email 且启用、未过期的凭证可通过。

func OpenAPILLMCredentialFromContext

func OpenAPILLMCredentialFromContext(c *gin.Context) (*models.Credential, bool)

OpenAPILLMCredentialFromContext 由 OpenAPILLMProxyAuth 注入,kind=llm 的凭证。

func OpenAPILLMProxyAuth

func OpenAPILLMProxyAuth(db *gorm.DB, errStyle OpenAPILLMProxyAuthStyle) gin.HandlerFunc

OpenAPILLMProxyAuth 校验 LLM 代理调用:Authorization: Bearer <key>,或 x-api-key(Anthropic 客户端常用)。 使用 credentials 表中 kind=llm、status=1 的密钥;可选 IP 白名单与配额(与邮件 OpenAPI 一致)。

func OpenAPISpeechCredentialFromContext

func OpenAPISpeechCredentialFromContext(c *gin.Context) (*models.Credential, bool)

OpenAPISpeechCredentialFromContext 由 OpenAPISpeechProxyAuth 注入,kind 为 asr 或 tts。

func OpenAPISpeechProxyAuth

func OpenAPISpeechProxyAuth(db *gorm.DB, kind string) gin.HandlerFunc

OpenAPISpeechProxyAuth 与 LLM OpenAPI 相同 Bearer / x-api-key,凭证 kind 为 asr 或 tts。

func PoweredBy

func PoweredBy() gin.HandlerFunc

func RelayPanicRecover

func RelayPanicRecover() gin.HandlerFunc

func RequestId

func RequestId() func(c *gin.Context)

func SanitizeString

func SanitizeString(input string) string

SanitizeString 清理字符串,移除危险字符

func SecureCompare

func SecureCompare(a, b string) bool

SecureCompare 安全比较字符串,防止时序攻击

func SecurityMiddleware

func SecurityMiddleware(config *SecurityConfig) gin.HandlerFunc

SecurityMiddleware 安全中间件

func SecurityMiddlewareChain

func SecurityMiddlewareChain() []gin.HandlerFunc

SecurityMiddlewareChain returns security middleware chain

func ValidateEmail

func ValidateEmail(email string) bool

ValidateEmail 验证邮箱格式

func ValidatePassword

func ValidatePassword(password string) error

ValidatePassword 验证密码强度

func WithCookieSession

func WithCookieSession(secret string, maxAge int) gin.HandlerFunc

func WithMemSession

func WithMemSession(secret string) gin.HandlerFunc

func XSSProtectionMiddleware

func XSSProtectionMiddleware() gin.HandlerFunc

XSSProtectionMiddleware XSS防护中间件

Types

type OpenAPILLMProxyAuthStyle

type OpenAPILLMProxyAuthStyle string

OpenAPILLMProxyAuthStyle 鉴权失败时的错误 JSON 形态(贴近各协议常见字段)。

const (
	OpenAPILLMStyleOpenAI    OpenAPILLMProxyAuthStyle = "openai"
	OpenAPILLMStyleAnthropic OpenAPILLMProxyAuthStyle = "anthropic"
)

type SecurityConfig

type SecurityConfig struct {
	// CSRF配置
	CSRFSecret    string            `json:"csrf_secret"`
	CSRFTokenName string            `json:"csrf_token_name"`
	CSRFMaxAge    time.Duration     `json:"csrf_max_age"`
	CSRFSecure    bool              `json:"csrf_secure"`
	CSRFHttpOnly  bool              `json:"csrf_http_only"`
	CSRFSameSite  csrf.SameSiteMode `json:"csrf_same_site"`

	// XSS配置
	XSSProtection      bool   `json:"xss_protection"`
	ContentTypeNosniff bool   `json:"content_type_nosniff"`
	XFrameOptions      string `json:"x_frame_options"`

	// 输入验证配置
	MaxRequestSize int64    `json:"max_request_size"`
	AllowedOrigins []string `json:"allowed_origins"`

	// 安全头配置
	HSTSMaxAge     int    `json:"hsts_max_age"`
	ReferrerPolicy string `json:"referrer_policy"`
}

SecurityConfig 安全配置

func DefaultSecurityConfig

func DefaultSecurityConfig() *SecurityConfig

DefaultSecurityConfig 默认安全配置

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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