control

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 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"
)

控制 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 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 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"`
	Input  json.RawMessage `json:"input"`
}

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"`
}

CallResult 是 /control/call 的响应体,映射桥接的 JSON-RPC result/error。

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) Call

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

Call 转发一次 bridge action 调用并阻塞至应答/作废。ctx 取消(如 CLI Ctrl-C)会切断连接, daemon 侧据此发 $/cancelRequest 作废该操作;此时 Do 返回 context.Canceled。

func (*Client) Enroll

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

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

func (*Client) Health

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

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

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 HealthResult

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

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

type StatusResult

type StatusResult struct {
	DaemonVersion string        `json:"daemonVersion"`
	ExtConnected  bool          `json:"extConnected"`
	SecurityCount int           `json:"securityCount"`
	Security      []audit.Event `json:"security,omitempty"`
}

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

Jump to

Keyboard shortcuts

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