moderation

package
v0.33.3 Latest Latest
Warning

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

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

Documentation

Overview

Package moderation 内容审核钩子(治理 2026-10-03,方案 A:管控优先、审核留钩子)。

设计:上传/分享入口在写库前调用 Moderator 做内容判定,命中可疑内容的处置 由配置决定(直接拒绝 或 建分享后置 pending_review 进管理端队列)。 内置 v1 只做文本敏感词;文件侧仅留接口(内置实现恒放行),外接扫描器 (ClamAV/NSFW 等)以实现 Moderator 接口注入即可,无需改动业务代码。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClamAVConfig

type ClamAVConfig struct {
	Enabled        bool   `mapstructure:"enabled"`         // 总开关(env: PB_MODERATION_CLAMAV_ENABLED)
	Addr           string `mapstructure:"addr"`            // host:port(默认 localhost:3310)
	TimeoutSeconds int    `mapstructure:"timeout_seconds"` // 单文件扫描超时,默认 60
	MaxScanBytes   int64  `mapstructure:"max_scan_bytes"`  // 超过跳过扫描(0=不限,建议 512MB)
}

ClamAVConfig clamd 连接配置(conf.ModerationConfig.ClamAV)

func (ClamAVConfig) Normalized

func (c ClamAVConfig) Normalized() ClamAVConfig

Normalized 补齐默认值

type ClamAVModerator

type ClamAVModerator struct {
	// contains filtered or unexported fields
}

ClamAVModerator 文件侧 Moderator:对 meta.StoragePath 指向的内容做 clamd INSTREAM 扫描。

func NewClamAVModerator

func NewClamAVModerator(addr string, timeoutSeconds int, maxScanBytes int64, opener ContentOpener) *ClamAVModerator

NewClamAVModerator 构建 clamd 扫描审核器(仅文件侧;文本判定请叠加词表审核器)。

func (*ClamAVModerator) InspectFile

func (m *ClamAVModerator) InspectFile(ctx context.Context, meta UploadMeta) Verdict

InspectFile 文件侧判定:扫描 meta.StoragePath 内容。 无路径/无 opener/超过上限 → 放行(记日志);扫描器故障 → 放行(fail-open); 命中签名 → 按 BlockAction 语义由调用方决定的 Verdict(此处恒 VerdictReject; pending 策略由 CombinedModerator/配置层转换——与词表审核器同一约定)。

func (*ClamAVModerator) InspectText

func (m *ClamAVModerator) InspectText(context.Context, string) Verdict

InspectText 文件侧审核器不判文本(组合审核器把文本路由给词表)。

func (*ClamAVModerator) Ping

func (m *ClamAVModerator) Ping() error

Ping clamd 存活检查(部署验证/健康巡检用)

func (*ClamAVModerator) ScanReader

func (m *ClamAVModerator) ScanReader(ctx context.Context, r io.Reader) (string, error)

ScanReader 扫描任意 reader(INSTREAM)。返回 (签名, 错误);clean 时签名为空。

type CombinedModerator

type CombinedModerator struct {
	// contains filtered or unexported fields
}

CombinedModerator 文本与文件各走独立审核器(词表 + ClamAV 组合)。

func NewCombinedModerator

func NewCombinedModerator(text, file Moderator) *CombinedModerator

NewCombinedModerator 组合审核器:文本判定走 text,文件判定走 file。

func (*CombinedModerator) InspectFile

func (m *CombinedModerator) InspectFile(ctx context.Context, meta UploadMeta) Verdict

func (*CombinedModerator) InspectText

func (m *CombinedModerator) InspectText(ctx context.Context, text string) Verdict

type ContentOpener

type ContentOpener interface {
	GetFileReader(ctx context.Context, filePath string) (io.ReadCloser, int64, error)
}

ContentOpener 扫描器读取文件内容的抽象(bootstrap 注入 storage service)

type Moderator

type Moderator interface {
	InspectText(ctx context.Context, text string) Verdict
	InspectFile(ctx context.Context, meta UploadMeta) Verdict
}

Moderator 内容审核接口(业务入口唯一依赖)。

type NoopModerator

type NoopModerator struct{}

NoopModerator 空审核器(moderation.enabled=false 或未注入时的等价行为)。

func (NoopModerator) InspectFile

func (NoopModerator) InspectText

func (NoopModerator) InspectText(context.Context, string) Verdict

type UploadMeta

type UploadMeta struct {
	Code        string
	FileName    string
	ContentType string
	Size        int64
	OwnerIP     string
	UserID      *uint
	UploadID    string // 分片会话 ID(分片/秒传通道)
	Channel     string // 上传通道:direct/chunk/presign
	// StoragePath 已落存储的相对路径(文件内容扫描用,如 ClamAV;
	// 直传/绑定入口在审核调用时填充,文本分享为空)
	StoragePath string
}

UploadMeta 文件上传元信息(文件侧钩子入参;路径内容不做读取,由外接扫描器自行处理)

type Verdict

type Verdict int

Verdict 内容判定结论

const (
	VerdictAllow   Verdict = iota // 放行
	VerdictReject                 // 拒绝(建分享前拦截)
	VerdictPending                // 待审核(建分享后置 pending_review,取件拒绝)
)

func (Verdict) String

func (v Verdict) String() string

type WordListModerator

type WordListModerator struct {
	// contains filtered or unexported fields
}

WordListModerator 敏感词表实现(子串匹配,大小写不敏感)。 Action: "reject"(默认)或 "pending",决定命中后的处置策略。

func NewWordListModerator

func NewWordListModerator(words []string, action string) *WordListModerator

NewWordListModerator 构建敏感词审核器。words 为空时全部放行(功能未启用)。

func (*WordListModerator) InspectFile

func (m *WordListModerator) InspectFile(_ context.Context, _ UploadMeta) Verdict

InspectFile 文件审核钩子:内置实现恒放行(v1 不做文件内容扫描)。 外接扫描器以自定义 Moderator 注入。

func (*WordListModerator) InspectText

func (m *WordListModerator) InspectText(_ context.Context, text string) Verdict

InspectText 文本敏感词检查。词表为空恒放行。

func (*WordListModerator) UpdateWords

func (m *WordListModerator) UpdateWords(words []string)

UpdateWords 热更新词表(管理端后续扩展用;并发安全)。

Jump to

Keyboard shortcuts

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