87ccaecddf
实现 server/internal/devices 模块:
- handler.go: GET /v1/me/devices 列表、DELETE /v1/me/devices/{id} 移除
(chi 路由,挂在 /v1/me 下;JWT 中间件之后)。
- service.go: ListDevices / RegisterIfAbsent(隐式登记,按 plan.max_devices
校验,超限返回双语 DEVICE_LIMIT_EXCEEDED 含上限数字)/ DeleteDevice(事务硬删
+ audit_log → 调 CredentialRevoker.RevokeForUser 按用户回收凭证,dp_uuid 模型)
/ SubscriptionSummary / ResolvePlan / 纯函数 resolveEffectivePlan。
- middleware.go: 订阅校验中间件,解析最高档未过期订阅注入 context;
helpers PlanFromCtx / CheckDeviceQuota / RequirePaidTier;预留 60s Redis 缓存开关。
- store.go: devices/users/subscriptions/plans/audit_log 数据访问,按用户行锁串行化登记。
- context.go: user_id / plan 的 context key 与 helper。
- CredentialRevoker 接口(消费侧定义,避免与 nodes 循环依赖)+ NoopRevoker,
形状对齐 #5 的 Hub.Push(RevokeCredential),#5 落地前注入 no-op。
测试:service_test.go 15 个单测(trial→pro、过期回落 free、banned 拒绝、
最高档/同档最晚到期、UTC 严格边界、设备配额、双语限额、平台/名称归一化)全过;
devices_integration_test.go(testcontainers,build tag integration)覆盖
注册→connect 隐式登记→list→delete 全链路 + 回收断言 + 403/404。
apierr 增加 Unauthorized/Forbidden/NotFound/AccountBanned;
OpenAPI SubscriptionInfo.source 枚举补 free。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
108 lines
3.2 KiB
Go
108 lines
3.2 KiB
Go
// Package apierr defines the bilingual error response type used across all API modules.
|
|
// Error messages follow the desensitisation rules: no "VPN" / "翻墙" / "科学上网" wording.
|
|
package apierr
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
)
|
|
|
|
// Error is the canonical API error body: {code, message_zh, message_en}.
|
|
type Error struct {
|
|
Code string `json:"code"`
|
|
MessageZH string `json:"message_zh"`
|
|
MessageEn string `json:"message_en"`
|
|
}
|
|
|
|
func (e *Error) Error() string { return e.Code + ": " + e.MessageEn }
|
|
|
|
// Standard code-module errors.
|
|
var (
|
|
ErrInvalidCode = &Error{
|
|
Code: "INVALID_CODE",
|
|
MessageZH: "激活码格式无效,请检查后重试",
|
|
MessageEn: "Invalid code format, please verify and try again",
|
|
}
|
|
ErrCodeNotFound = &Error{
|
|
Code: "CODE_NOT_FOUND",
|
|
MessageZH: "激活码无效或已使用",
|
|
MessageEn: "Code not found or already used",
|
|
}
|
|
ErrCodeRedeemed = &Error{
|
|
Code: "CODE_REDEEMED",
|
|
MessageZH: "该激活码已被其他账户使用",
|
|
MessageEn: "This code has already been redeemed by another account",
|
|
}
|
|
ErrCodeVoid = &Error{
|
|
Code: "CODE_VOID",
|
|
MessageZH: "该激活码已失效",
|
|
MessageEn: "This code is no longer valid",
|
|
}
|
|
ErrRateLimited = &Error{
|
|
Code: "RATE_LIMITED",
|
|
MessageZH: "操作过于频繁,请稍后再试",
|
|
MessageEn: "Too many attempts, please try again later",
|
|
}
|
|
ErrLocked = &Error{
|
|
Code: "ACCOUNT_LOCKED",
|
|
MessageZH: "账户已临时锁定,请1小时后重试",
|
|
MessageEn: "Account temporarily locked, please retry in 1 hour",
|
|
}
|
|
ErrInternal = &Error{
|
|
Code: "INTERNAL_ERROR",
|
|
MessageZH: "服务器内部错误,请稍后重试",
|
|
MessageEn: "Internal server error, please try again later",
|
|
}
|
|
ErrBadRequest = &Error{
|
|
Code: "BAD_REQUEST",
|
|
MessageZH: "请求参数有误",
|
|
MessageEn: "Invalid request parameters",
|
|
}
|
|
|
|
// Auth / authorization errors shared across modules.
|
|
ErrUnauthorized = &Error{
|
|
Code: "UNAUTHORIZED",
|
|
MessageZH: "请先登录",
|
|
MessageEn: "Authentication required",
|
|
}
|
|
ErrForbidden = &Error{
|
|
Code: "FORBIDDEN",
|
|
MessageZH: "无权访问该资源",
|
|
MessageEn: "You do not have permission to access this resource",
|
|
}
|
|
ErrNotFound = &Error{
|
|
Code: "NOT_FOUND",
|
|
MessageZH: "资源不存在",
|
|
MessageEn: "Resource not found",
|
|
}
|
|
ErrAccountBanned = &Error{
|
|
Code: "ACCOUNT_BANNED",
|
|
MessageZH: "账户已被封禁,无法继续操作",
|
|
MessageEn: "This account has been banned",
|
|
}
|
|
|
|
// Webhook-specific errors.
|
|
ErrWebhookSignature = &Error{
|
|
Code: "WEBHOOK_INVALID_SIGNATURE",
|
|
MessageZH: "签名校验失败",
|
|
MessageEn: "Invalid webhook signature",
|
|
}
|
|
ErrWebhookTimestamp = &Error{
|
|
Code: "WEBHOOK_TIMESTAMP_EXPIRED",
|
|
MessageZH: "请求时间戳超出允许窗口",
|
|
MessageEn: "Webhook timestamp outside allowed window",
|
|
}
|
|
ErrWebhookReplay = &Error{
|
|
Code: "WEBHOOK_REPLAY",
|
|
MessageZH: "重复请求已忽略",
|
|
MessageEn: "Duplicate webhook request ignored",
|
|
}
|
|
)
|
|
|
|
// WriteJSON writes the given status code and error body as JSON.
|
|
func WriteJSON(w http.ResponseWriter, status int, e *Error) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(e)
|
|
}
|