control

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: GPL-3.0 Imports: 17 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package control 定义 sctl 前端(sctl mcp / CLI 动词)与常驻 daemon(sctl serve)之间的 「控制连接」:一套跑在 daemon listener 上、与扩展 WS 面同端口但独立路径的 HTTP/JSON 控制 API。它不是桥接协议(见 docs/protocol.md)的一部分——桥接协议只约定 扩展 ↔ daemon 那条 WS 连接;控制 API 是同一二进制内部前端 → daemon 的私有通道。

信任模型见 docs/threat-model.md——扁平信任:

  • 控制 API 的唯一传输闸门是「控制令牌」——daemon 绑定端口后写入受限文件。网页可以 new WebSocket / fetch 到该端口,但读不到令牌即被拒;非默认监听地址由操作者承担传输风险。
  • 带上控制令牌即拥有全部能力:CLI 与所有 MCP agent 经已接入的可信通道继承信任,不再逐客户端 配对 / 铸令牌 / 撤销。可选的客户端标签仅用于审计归因,不构成授权。
  • 无论哪种调用方,写操作仍需过扩展侧人工审批、源码读取仍需过披露闸门(第二道闸门)。

Index

Constants

View Source
const (
	PathHealth          = "/control/health"
	PathCall            = "/control/call"
	PathEnroll          = "/control/enroll"
	PathStatus          = "/control/status"
	PathBrowsers        = "/control/browsers"
	PathBrowserForget   = "/control/browsers/forget"
	PathScriptCats      = "/control/scriptcats"
	PathScriptCatForget = "/control/scriptcats/forget"
	PathPage            = "/control/page"
	// 原始 CDP 端点:创建或查看、只查看、关闭。请求体都是 CDPRequest。
	PathCDPEndpoint = "/control/cdp/endpoint"
	PathCDPStatus   = "/control/cdp/status"
	PathCDPClose    = "/control/cdp/close"
)

控制 API 路径。健康检查故意不鉴权(仅暴露「端口开着」这一威胁模型已接受的信息)。

View Source
const (
	// HeaderControlToken 携带控制令牌。所有非健康检查请求必带。
	HeaderControlToken = "X-Sctl-Control-Token"
	// HeaderClientLabel 可选,携带调用方自报的客户端标签(如 MCP 客户端名)。仅用于审计归因,
	// 不构成授权;缺省即内建 CLI 身份标签。自报、未认证、可伪造——只入审计,不上审批界面。
	HeaderClientLabel = "X-Sctl-Client"
)

控制 API 请求头。

View Source
const CLIClientID = "sctl-cli"

CLIClientID 是缺省客户端标签(JSON-RPC params.clientId 回填,仅供扩展侧审计展示)。

Variables

View Source
var ErrDaemonUnreachable = errors.New("cannot connect to the sctl daemon; start it with `sctl serve`")

ErrDaemonUnreachable 表示当前没有可连接的 daemon。serve 的生命周期由用户或外部服务管理器负责。

Functions

func Address added in v0.3.0

func Address() (string, error)

Address 返回前端连接的 daemon 地址(host:port):SetAddress 配置的地址,否则协议默认端口。

func Healthy added in v0.3.0

func Healthy(ctx context.Context) bool

Healthy 报告配置的地址上是否有 daemon 回答健康检查,不读控制令牌:令牌在别的数据目录时也算。

func NewControlToken

func NewControlToken() (string, error)

NewControlToken 生成 256-bit 随机控制令牌(小写 hex)。daemon 每次绑定端口后新生成一份, 旧令牌随之失效——控制通道只在进程存活期内有效,无需持久语义。

func ReadControlToken

func ReadControlToken() (string, error)

ReadControlToken 读取控制令牌。文件不存在返回 os.ErrNotExist,调用方据此判断 daemon 未就绪。

func SetAddress

func SetAddress(address string)

SetAddress configures the daemon address for this process. An empty value uses the protocol default.

func WriteControlToken

func WriteControlToken(token string) error

WriteControlToken 原子写入控制令牌到 paths.ControlTokenFile()(目录 0700 / 文件 0600)。 必须在绑定端口成功之后调用:端口竞态的失败方不会走到这里,因此不会覆盖胜出者写下的令牌。

Types

type BrowserInfo added in v0.2.0

type BrowserInfo struct {
	ID               string    `json:"id"`
	Name             string    `json:"name"`
	Online           bool      `json:"online"`
	Product          string    `json:"product,omitempty"`
	ProductVersion   string    `json:"productVersion,omitempty"`
	ExtensionVersion string    `json:"extensionVersion,omitempty"`
	ConnectedAt      time.Time `json:"connectedAt,omitzero"`
}

BrowserInfo 是一个已配对浏览器实例的当前视图,镜像 bridge.InstanceInfo(控制 API 不直接暴露 bridge 类型,见 internal/daemon/controlapi 的窄接口约定)。离线实例的 ConnectedAt 为零值,JSON 中省略。

type BrowsersResult added in v0.2.0

type BrowsersResult struct {
	Browsers []BrowserInfo `json:"browsers"`
}

BrowsersResult 是 /control/browsers 的响应体:全部已配对浏览器实例(在线与离线)。

type CDPBrowserRef added in v0.2.0

type CDPBrowserRef struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

CDPBrowserRef 标识端点所属的浏览器实例。

type CDPCloseResult added in v0.2.0

type CDPCloseResult struct {
	Browser CDPBrowserRef `json:"browser"`
	Closed  bool          `json:"closed"`
}

CDPCloseResult 是 /control/cdp/close 的结果:Closed 为 false 表示本来就没有端点。

type CDPEndpoint added in v0.2.0

type CDPEndpoint struct {
	// HTTPURL 给 Playwright chromium.connectOverCDP。
	HTTPURL string `json:"httpUrl"`
	// WSURL 给 Puppeteer connect({browserWSEndpoint})。
	WSURL           string    `json:"wsUrl"`
	ClientConnected bool      `json:"clientConnected"`
	ConnectedAt     time.Time `json:"connectedAt,omitzero"`
	ExpiresAt       time.Time `json:"expiresAt,omitzero"`
}

CDPEndpoint 是一个原始 CDP 端点的当前视图。地址里带着密钥,本身就是凭据。ExpiresAt 在客户端连着时省略: 60 分钟的失效计时从客户端断开时才开始。

type CDPEndpointResult added in v0.2.0

type CDPEndpointResult struct {
	Browser  CDPBrowserRef `json:"browser"`
	Endpoint *CDPEndpoint  `json:"endpoint"`
}

CDPEndpointResult 是 /control/cdp/endpoint 与 /control/cdp/status 的结果(CallResult.Result): Endpoint 为 null 表示这个浏览器没有端点(只有 status 会这样回答)。

type CDPRequest added in v0.2.0

type CDPRequest struct {
	Browser string `json:"browser,omitempty"`
}

CDPRequest 是 /control/cdp/* 的请求体:Browser 是目标浏览器(名称或实例 ID 前缀),空串由 daemon 按在线实例选择。

type CallError

type CallError struct {
	Code    string `json:"code"`
	Message string `json:"message"`
}

CallError 是失败调用的结构化错误(code 取桥接 protocol.json errorCodes)。

func (*CallError) Error

func (e *CallError) Error() string

type CallRequest

type CallRequest struct {
	Action string `json:"action"`
	// Browser 是浏览器方法的可选目标:实例名称或实例 ID 前缀,由 daemon 解析(docs/protocol.md §3.1)。
	Browser string `json:"browser,omitempty"`
	// ScriptCat 是 scripts.* 方法的可选目标:ScriptCat 实例名称或实例 ID 前缀,由 daemon 解析(docs/protocol.md §3.1)。
	ScriptCat string          `json:"scriptcat,omitempty"`
	Input     json.RawMessage `json:"input"`
	// ReportPending 请 daemon 在对端报告请求进入人工审批时先写出一行 CallResult{Pending: true},
	// 结论随后另起一行。不带时响应体只有结论一行。
	ReportPending bool `json:"reportPending,omitempty"`
}

CallRequest 是 /control/call 的请求体:转发一次 bridge action 调用。

type CallResult

type CallResult struct {
	OK     bool            `json:"ok"`
	Result json.RawMessage `json:"result,omitempty"`
	Error  *CallError      `json:"error,omitempty"`
	// Pending 只出现在 ReportPending 请求的中间行上:请求已进入浏览器里的人工审批,这一行不是结论。
	Pending bool `json:"pending,omitempty"`
}

CallResult 是 /control/call 的响应体,映射桥接的 JSON-RPC result/error。 列表类方法未指定目标且多个同类实例在线时,Result 是各实例结果按 mergeField 拼接后的汇总, 该数组的每一项多一个来源字段:浏览器方法是 "browser": {"id","name"},scripts.list 是 "scriptcat": {"id","name"}(老版 ScriptCat 没有 id);其余情况 Result 是单个对端的原始结果。

type Client

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

Client 是前端连接 daemon 控制 API 的 HTTP 客户端。零值不可用,须经 Dial 构造。

func Connect

func Connect(ctx context.Context) (*Client, error)

Connect 与 Dial 相同,保留此名称供 status 等只读探测命令表达调用意图。

func Dial

func Dial(ctx context.Context) (*Client, error)

Dial 解析 daemon 地址并连接已运行的 daemon。它不负责启动或托管 serve 进程。

func (*Client) Browsers added in v0.2.0

func (c *Client) Browsers(ctx context.Context) ([]BrowserInfo, error)

Browsers 列出所有已配对的浏览器实例(在线与离线)。

func (*Client) CDP added in v0.2.0

func (c *Client) CDP(ctx context.Context, path string, req CDPRequest) (CallResult, error)

CDP 发送一个原始 CDP 端点请求;path 是 PathCDPEndpoint、PathCDPStatus 或 PathCDPClose。

func (*Client) Call

func (c *Client) Call(ctx context.Context, action string, target Target, input json.RawMessage, onPending func()) (CallResult, error)

Call 转发一次 bridge action 调用并阻塞至应答/作废。target 指定要路由到的实例,见 Target。 onPending 非 nil 时请 daemon 报告请求进入人工审批,并在那时(结论到达之前)调用它,至多一次;审批前就得出结论的请求 不会回调。ctx 取消(如 CLI Ctrl-C)会切断连接,daemon 侧据此发 $/cancelRequest 作废该操作。

func (*Client) Enroll

func (c *Client) Enroll(ctx context.Context) (string, error)

Enroll 打开一次接入窗口,返回展示形配对码(供 sctl connect 在终端展示,用户输入扩展页面)。

func (*Client) ForgetBrowser added in v0.2.0

func (c *Client) ForgetBrowser(ctx context.Context, ref string) error

ForgetBrowser 删除一个已配对浏览器实例的密钥与登记(按精确名称或精确实例 ID), 若它在线则断开其连接;之后该实例的握手会失败。ref 不匹配任何已配对实例时返回错误。

func (*Client) ForgetScriptCat added in v0.3.0

func (c *Client) ForgetScriptCat(ctx context.Context, ref string) error

ForgetScriptCat 删除一个已配对 ScriptCat 实例的密钥与登记(按精确名称或精确实例 ID;scriptcat 删除老版的 pairing.key),若它在线则断开其连接;之后该实例的握手会失败。ref 不匹配任何实例时返回错误。

func (*Client) Health

func (c *Client) Health(ctx context.Context) error

Health 显式做一次健康检查(供 status 等命令使用)。

func (*Client) Page added in v0.2.0

func (c *Client) Page(ctx context.Context, req PageRequest) (CallResult, error)

Page 在浏览器标签页上执行一次页面动作并阻塞至完成。ctx 取消会切断连接,daemon 侧随之结束该动作。

func (*Client) ScriptCats added in v0.3.0

func (c *Client) ScriptCats(ctx context.Context) ([]ScriptCatInfo, error)

ScriptCats 列出所有已配对的 ScriptCat 实例(在线与离线);存在 pairing.key 时含保留名 scriptcat 的老版一行。

func (*Client) Status

func (c *Client) Status(ctx context.Context) (StatusResult, error)

Status 查询 daemon 与扩展连接概览。

func (*Client) WithClientLabel

func (c *Client) WithClientLabel(label string) *Client

WithClientLabel 返回携带指定审计标签的副本;后续调用把该标签随请求上报,仅供扩展侧审计归因。

type EnrollResult

type EnrollResult struct {
	Code string `json:"code"`
}

EnrollResult 是 /control/enroll 的响应体:打开接入窗口后返回展示形配对码。

type ForgetBrowserRequest added in v0.2.0

type ForgetBrowserRequest struct {
	Ref string `json:"ref"`
}

ForgetBrowserRequest 是 /control/browsers/forget 的请求体:Ref 是实例名称或完整实例 ID。

type ForgetScriptCatRequest added in v0.3.0

type ForgetScriptCatRequest struct {
	Ref string `json:"ref"`
}

ForgetScriptCatRequest 是 /control/scriptcats/forget 的请求体:Ref 是实例名称、完整实例 ID,或老版的保留名 scriptcat。

type HealthResult

type HealthResult struct {
	OK      bool   `json:"ok"`
	Version string `json:"version"`
}

HealthResult 是 /control/health 的响应体(无鉴权)。

type PageRequest added in v0.2.0

type PageRequest struct {
	Action    string          `json:"action"`
	Browser   string          `json:"browser,omitempty"`
	TabID     *int            `json:"tabId,omitempty"`
	Activate  bool            `json:"activate,omitempty"`
	TimeoutMs int             `json:"timeoutMs,omitempty"`
	Input     json.RawMessage `json:"input,omitempty"`
}

PageRequest 是 /control/page 的请求体:在一个浏览器标签页上执行一次页面动作,响应体是 CallResult。 TabID 为 nil 时由 daemon 在命令开始时取该浏览器最后获得焦点窗口的激活标签页;Activate 先让目标成为 窗口内的激活标签页(不聚焦窗口);TimeoutMs 为 0 时使用动作的默认超时。Input 是动作自己的参数。

type ScriptCatInfo added in v0.3.0

type ScriptCatInfo BrowserInfo

ScriptCatInfo 是一个已配对 ScriptCat 实例的当前视图,字段与 BrowserInfo 相同。不带实例 ID 的老版 ScriptCat 以保留名 scriptcat 出现,ID 为空,产品与版本为空。

type ScriptCatsResult added in v0.3.0

type ScriptCatsResult struct {
	ScriptCats []ScriptCatInfo `json:"scriptcats"`
}

ScriptCatsResult 是 /control/scriptcats 的响应体:全部已配对 ScriptCat 实例(在线与离线)。

type StatusResult

type StatusResult struct {
	DaemonVersion string        `json:"daemonVersion"`
	ExtConnected  bool          `json:"extConnected"`
	SecurityCount int           `json:"securityCount"`
	Security      []audit.Event `json:"security,omitempty"`
	Browsers      []BrowserInfo `json:"browsers,omitempty"`
	// ExtConnected 为 true 当且仅当 ScriptCats 中有实例在线;ScriptCats 逐个列出实例,含老版的保留名一行。
	ScriptCats []ScriptCatInfo `json:"scriptcats,omitempty"`
}

StatusResult 是 /control/status 的响应体:daemon 与扩展连接概览,附守卫侧安全事件与已配对浏览器实例。

type Target added in v0.3.0

type Target struct {
	Browser   string
	ScriptCat string
}

Target 是一次 /control/call 的可选目标实例(名称或实例 ID 前缀),空串表示由 daemon 按在线实例选择。 Browser 只用于浏览器方法,ScriptCat 只用于 scripts.*;给错一类由 daemon 以 INVALID_REQUEST 拒绝。

Jump to

Keyboard shortcuts

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