mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 14:06:36 +08:00
528240026d
- make format 现在与 code-check 使用同一格式化器(golangci-lint fmt),消除 goimports -local 与 gofumpt 的格式拉锯 - .golangci.yml 关闭默认 50/3 截断,完整上报所有问题(只增强不弱化) - 全库 gofumpt 规范化(203 files, 纯格式无行为变更)
90 lines
2.3 KiB
Go
90 lines
2.3 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Package cap provides the proof-of-work (PoW) CAPTCHA verification domain plugin for Cordis.
|
|
package cap
|
|
|
|
import (
|
|
"Wavelet/core"
|
|
"Wavelet/core/contracts"
|
|
"Wavelet/core/extpoints"
|
|
"reflect"
|
|
)
|
|
|
|
// Plugin implements core.Plugin to provide CAPTCHA generation, validation, and route protection.
|
|
type Plugin struct{}
|
|
|
|
// New creates a new cap domain plugin.
|
|
func New() *Plugin {
|
|
return &Plugin{}
|
|
}
|
|
|
|
// Name returns the unique identifier for the cap domain plugin.
|
|
func (p *Plugin) Name() string {
|
|
return "cap"
|
|
}
|
|
|
|
// Inject declares required dependencies for the cap domain plugin.
|
|
func (p *Plugin) Inject() []reflect.Type {
|
|
return []reflect.Type{
|
|
reflect.TypeFor[contracts.DBService](),
|
|
}
|
|
}
|
|
|
|
// Manifest returns the plugin metadata.
|
|
func (p *Plugin) Manifest() core.Manifest {
|
|
return core.Manifest{
|
|
Name: "cap",
|
|
Version: "1.0.0",
|
|
Description: "Proof-of-work CAPTCHA challenge and verification domain plugin",
|
|
Author: "Wavelet Team",
|
|
}
|
|
}
|
|
|
|
// Apply registers the cap routes and settings into the Context.
|
|
func (p *Plugin) Apply(ctx *core.Context) error {
|
|
// 0. Bind DBService from Context
|
|
if db, err := core.Inject[contracts.DBService](ctx); err == nil && db != nil {
|
|
setDBService(db)
|
|
} else {
|
|
core.When[contracts.DBService](ctx, func(db contracts.DBService) {
|
|
setDBService(db)
|
|
})
|
|
}
|
|
ctx.OnDispose(func() error {
|
|
setDBService(nil)
|
|
return nil
|
|
})
|
|
|
|
// Listen to system config changed events to invalidate cached settings
|
|
ctx.Events().On(contracts.EventTopicConfigChanged, func(_ any) {
|
|
InvalidateRuntimeSettings()
|
|
})
|
|
|
|
// Register HTTP Routes
|
|
capGroup := ctx.Router().Group("/api/v1/cap")
|
|
{
|
|
capGroup.GET("/challenge", Challenge)
|
|
capGroup.POST("/challenge", Challenge)
|
|
capGroup.POST("/redeem", Redeem)
|
|
}
|
|
|
|
// Register Settings Schemas
|
|
ctx.Settings().Register(extpoints.SettingSchema{
|
|
Key: "cap.login_enabled",
|
|
Default: false,
|
|
Description: "Whether to require CAPTCHA verification for user login",
|
|
Type: "boolean",
|
|
Category: "security",
|
|
})
|
|
ctx.Settings().Register(extpoints.SettingSchema{
|
|
Key: "cap.challenge_count",
|
|
Default: 1,
|
|
Description: "Number of PoW puzzle challenges to solve",
|
|
Type: "integer",
|
|
Category: "security",
|
|
})
|
|
|
|
return nil
|
|
}
|