Documentation
¶
Overview ¶
Package script 实现管理员动态 goja 脚本的编译与执行。
Index ¶
Constants ¶
View Source
const ( HookRequest = "onRequest" HookUpstream = "onUpstreamRequest" HookResponse = "onResponse" )
Variables ¶
This section is empty.
Functions ¶
func MatchScope ¶
func MatchScope(scope models.ScriptScope, input MatchInput) bool
MatchScope 报告 scope 是否对目标请求生效。 五类条件均空时全局命中;非空条件按 OR 匹配。
Types ¶
type Compiled ¶
type Compiled struct {
ID uint
Name string
Priority int
Scope models.ScriptScope
Program *goja.Program
// contains filtered or unexported fields
}
Compiled 是单个脚本的编译产物 + 元数据 + 复用池。不可变、可被多个请求并发只读共享。
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine 在 relay 挂点执行脚本。并发安全:每次执行新建独立 *goja.Runtime。
func (*Engine) Run ¶
func (e *Engine) Run(in HookInput) HookResult
Run 按 Priority 顺序对命中脚本执行 in.Hook 钩子。 任一脚本 reject 立即短路;脚本报错/超时 fail-open(跳过其改动,串原 body)。
type HookInput ¶
type HookInput struct {
Hook string // HookRequest / HookUpstream / HookResponse
Match MatchInput
User map[string]any
Headers map[string]string
Channel map[string]any // 出站/响应阶段为 {id,name}
Protocol string // 上游协议(出站/响应)
Body []byte
}
HookInput 是一次钩子执行的输入。
type HookResult ¶
type HookResult struct {
Body []byte
Changed bool
Rejected bool
Status int
Message string
// HeaderOps 是脚本通过 ctx.setHeader/removeHeader 累积的请求头改写操作,
// 按调用顺序排列。仅出站 onUpstreamRequest 挂点会应用它们(见 native backend)。
HeaderOps []HeaderOp
}
HookResult 是一次(整条脚本链)执行结果。
type MatchInput ¶ added in v0.0.14
type MatchInput struct {
Source attemptproxy.ChannelSource
ChannelID uint
Model string
UserID uint
GroupID uint
}
MatchInput 是脚本 scope 匹配所需的请求身份与路由信息。
type ScriptProvider ¶
type ScriptProvider interface {
MatchScripts(MatchInput) []*Compiled
}
ScriptProvider 返回命中(已按 scope 过滤 + enabled)的已编译脚本,按 Priority 升序。
Click to show internal directories.
Click to hide internal directories.