Help us shape the future of Go! Take the 2026 Go Developer Survey

gate

package
v0.14.5 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package gate 上传/下载准入闸门。

服务端强制执行 upload.open_upload / upload.require_login / download.require_login (此前三者只透传给前端做 UI 裁剪,后端不校验,形同虚设)。 判定规则:

  • open_upload=false:拒绝全部匿名上传(登录用户不受影响)
  • upload.require_login=true:拒绝匿名上传(含 chunk/presign 等纯匿名通道)
  • download.require_login=true:拒绝匿名取件/下载

quota.go 匿名上传 per-IP 日配额(治理 2026-10-03)。

此前配额只对登录用户生效(share.CreateShare → CheckQuota),匿名分片/直传 完全绕过——刷爆存储的主通道。本文件以"自然日"为窗口对匿名来源 IP 计数 (次数 + 字节双维度,任一超限即拒):

  • Redis 可用:INCR + 当日剩余秒数 EXPIRE(多实例共享计数)
  • 无 Redis:进程内 map + 日期轮转(单实例语义,重启清零)

Index

Constants

This section is empty.

Variables

View Source
var ErrLoginRequired = newGateErr(errcode.CodeUnauthorized, "该操作需要登录后使用")

ErrLoginRequired 需要登录才能执行该操作。

View Source
var ErrPresignDisabled = newGateErr(errcode.CodePresignDisabled, "匿名直传未开放,文件将改用普通通道上传")

ErrPresignDisabled 匿名直传已被管理员关闭(download.presign_anonymous_enabled=false)。

View Source
var ErrUploadDisabled = newGateErr(errcode.CodeUploadDisabled, "管理员已关闭匿名上传功能")

ErrUploadDisabled 上传已被管理员关闭(匿名通道)。

Functions

func CheckAnonymousQuota

func CheckAnonymousQuota(ctx context.Context, ip string, addBytes int64) error

CheckAnonymousQuota 匿名上传配额检查并计数(仅对匿名请求调用)。 超限返回 *GateError(业务码 10014);不限或登录请求恒通过。

func CheckDownloadLogin

func CheckDownloadLogin(userID *uint) error

CheckDownloadLogin 取件/下载登录要求(download.require_login)。

func CheckPresignPolicy

func CheckPresignPolicy(userID *uint) error

CheckPresignPolicy 直传策略闸门(download.presign_policy 三档):

  • everyone:所有人可直传
  • authenticated:仅登录用户(匿名 Init 返回 10015,前端回退分片中转)
  • disabled:完全关闭直传(登录用户也拒绝——密钥轮换/通道故障时的总闸)

func CheckUploadAllowed

func CheckUploadAllowed(userID *uint) error

CheckUploadAllowed 匿名上传总开关(upload.open_upload)。 登录用户(userID != nil)不受该开关限制。

func CheckUploadLogin

func CheckUploadLogin(userID *uint) error

CheckUploadLogin 上传登录要求(upload.require_login)。

func QuotaLimits

func QuotaLimits() (count int64, bytes int64)

QuotaLimits 当前生效的匿名日配额(0 = 不限)。

func SetQuotaRedis

func SetQuotaRedis(rdb *redis.Client)

SetQuotaRedis 注入 Redis(bootstrap 调用;nil = 内存模式)。 nil 归一化:typed-nil 接口会骗过内存模式守卫(!= nil 判真)。

Types

type GateError

type GateError struct {
	Code int
	Msg  string
}

GateError 携带业务码的闸门拒绝错误,handler 侧按 ErrCode 透传给前端。

func (*GateError) ErrCode

func (e *GateError) ErrCode() int

ErrCode 实现 app 侧 typed error 约定(同 user.QuotaExceededError)。

func (*GateError) Error

func (e *GateError) Error() string

Jump to

Keyboard shortcuts

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