bridge

package
v0.2.0-rc.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: GPL-3.0 Imports: 23 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

Index

Constants

View Source
const (
	CodeInvalidRequest   = "INVALID_REQUEST"
	CodeInternal         = "INTERNAL_ERROR"
	CodeRateLimited      = "RATE_LIMITED"
	CodeOperationExpired = "OPERATION_EXPIRED"
	CodeConflict         = "CONFLICT"
)

Variables

View Source
var (
	// ErrNotConnected 表示当前没有已配对的扩展连接。
	ErrNotConnected = errors.New("extension not connected")
	// ErrDisconnected 表示在途请求因扩展连接断开而作废。
	ErrDisconnected = errors.New("extension connection lost")
)
View Source
var ErrInstanceNotFound = store.ErrInstanceNotFound

ErrInstanceNotFound 表示引用不匹配任何已配对的浏览器实例。

Functions

This section is empty.

Types

type BrowserListener added in v0.2.0

type BrowserListener interface {
	// OnNotification 送达已认证浏览器实例发来的 debugger.event / debugger.detached 与 debugger.tabCreated / tabUpdated / tabRemoved,params 是通知对象本身。
	OnNotification(instanceID, method string, params json.RawMessage)
	// OnInstanceGone 在实例的一条已登记连接丢失(断开、被同一实例重连替换、被忘记)时对该连接恰好触发一次。
	OnInstanceGone(instanceID string)
}

BrowserListener 接收浏览器实例的入站通知与连接丢失事件,由 daemon 内的自动化组件实现。 回调都在不持有任何服务锁时调用,可以安全地回调 Server;但 OnNotification 运行在该连接的读循环里, 调用方必须快速返回(排队后异步处理),否则会挡住应答与心跳,连接随之被判死。

type Error

type Error struct {
	Code        string `json:"code"`
	Message     string `json:"message"`
	OperationID string `json:"operationId,omitempty"`
}

func (*Error) Error

func (e *Error) Error() string

type ErrorData

type ErrorData struct {
	Code        string `json:"code"`
	OperationID string `json:"operationId,omitempty"`
}

type InstanceForgottenListener added in v0.2.0

type InstanceForgottenListener interface {
	OnInstanceForgotten(instanceID string)
}

InstanceForgottenListener 是 BrowserListener 的可选扩展:实现它的监听者还会在实例被 sctl browsers forget 时 收到通知,无论实例当时在线与否。在线实例会先触发 OnInstanceGone 再触发本回调;离线实例只触发本回调, 因为没有连接丢失。回调同样在不持有任何服务锁时调用。

type InstanceInfo added in v0.2.0

type InstanceInfo struct {
	ID               string
	Name             string
	Online           bool
	Product          string
	ProductVersion   string
	ExtensionVersion string
	ConnectedAt      time.Time
}

InstanceInfo 是一个已配对浏览器实例的当前视图。产品与版本取自最近一次连接;离线时 ConnectedAt 为零值。

type Message

type Message struct {
	JSONRPC string          `json:"jsonrpc"`
	ID      string          `json:"id,omitempty"`
	Method  string          `json:"method,omitempty"`
	Params  json.RawMessage `json:"params,omitempty"`
	Result  json.RawMessage `json:"result,omitempty"`
	Error   *RPCError       `json:"error,omitempty"`
}

Message is one JSON-RPC 2.0 request, notification, success response, or error response.

type RPCError

type RPCError struct {
	Code    int        `json:"code"`
	Message string     `json:"message"`
	Data    *ErrorData `json:"data,omitempty"`
}

type Request

type Request struct {
	ClientID string `json:"clientId"`
	Action   string `json:"action"`
	// Browser 是浏览器方法的目标实例:名称或实例 ID 前缀,空表示按在线实例自动选择。
	Browser string          `json:"browser,omitempty"`
	Input   json.RawMessage `json:"input"`
	// OnPending 在对端报告这个请求进入人工审批时被调用,至多一次,在 Call 的 goroutine 里执行;
	// 请求在进入审批前就得到答复时不会被调用。nil 表示调用方不关心。
	OnPending func() `json:"-"`
}

type Response

type Response struct {
	OK     bool            `json:"ok"`
	Result json.RawMessage `json:"result,omitempty"`
	Error  *Error          `json:"error,omitempty"`
}

type Server

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

Server 是桥接 daemon 的 WS 服务核心:accept、双向认证握手、envelope 路由与阻塞写模型。 ScriptCat 只保留一条连接,新完成握手的 ScriptCat 连接替换旧连接;浏览器实例按实例 ID 各保留一条连接,同一实例重连只替换它自己。

信任模型是扁平的:接入(enrollment)为每个对端建立长期密钥即确立信任,CLI 与所有 MCP agent 都 经这些可信通道继承信任,不再逐客户端配对/铸令牌/撤销(docs/threat-model.md)。

func NewServer

func NewServer(version string, p *protocol.Protocol, keys *store.KeyStore, browsers *store.BrowserRegistry, log *zap.Logger) *Server

NewServer 用协议定义的常量与持久化后端构造服务:keys 存 ScriptCat 的长期密钥,browsers 存浏览器实例的登记与密钥。

func (*Server) Action

func (s *Server) Action(name string) (protocol.Action, bool)

Action 按名字查协议动作定义(scope 与读写属性),未知 action 返回 ok=false。

func (*Server) AddBrowserListener added in v0.2.0

func (s *Server) AddBrowserListener(l BrowserListener)

AddBrowserListener 追加一个浏览器监听者,与其他监听者一样收到全部通知与实例丢失事件; 实现了 InstanceForgottenListener 的还会收到忘记事件。追加后不可移除,生命周期同 Server。

func (*Server) AuditSnapshot

func (s *Server) AuditSnapshot() []audit.Event

AuditSnapshot 返回守卫侧安全事件的快照。

func (*Server) BeginEnrollment

func (s *Server) BeginEnrollment() (display string, err error)

BeginEnrollment 打开一次接入窗口,返回展示形配对码(XXXX-XXXX)。供 sctl connect 使用; 码只在终端显示、绝不经 WS 下发给任何连接(docs/threat-model.md)。

func (*Server) Call

func (s *Server) Call(ctx context.Context, req Request) (Response, error)

Call 按方法归属把一次 bridge action 转发给对端并阻塞等待应答:scripts.* 发给 ScriptCat, 浏览器方法发给由 req.Browser 解析出的浏览器实例(见 resolveBrowsers)。write=true 的写调用可能挂起数分钟 (等待用户在浏览器审批)。调用方 ctx 取消 / 超过 writeDecisionTTL / 对端断开时:

  • ctx 取消 / 超时:向对端发 $/cancelRequest 作废该操作,返回相应错误;
  • 对端断开:隐式作废全部在途请求,返回 ErrDisconnected。

func (*Server) CallInstance added in v0.2.0

func (s *Server) CallInstance(ctx context.Context, instanceID string, req Request) (Response, error)

CallInstance 调用精确实例 ID 对应的在线浏览器实例;不做名称或前缀解析,那是 ResolveBrowser 的职责。 实例不在线(离线、未配对或已被忘记)返回 BROWSER_OFFLINE。

func (*Server) ExtConnected

func (s *Server) ExtConnected() bool

ExtConnected 报告当前是否有完成握手的 ScriptCat 连接。

func (*Server) ForgetInstance added in v0.2.0

func (s *Server) ForgetInstance(ref string) error

ForgetInstance 按名称或完整实例 ID 删除一个已配对浏览器实例的密钥与登记;实例在线时断开它, 之后它的握手会因实例未配对而失败。

func (*Server) Instances added in v0.2.0

func (s *Server) Instances() []InstanceInfo

Instances 列出所有已配对的浏览器实例(在线与离线),按名称排序。

func (*Server) ResolveBrowser added in v0.2.0

func (s *Server) ResolveBrowser(target string) (InstanceInfo, error)

ResolveBrowser 按调用路径同样的规则解析目标浏览器:名称精确匹配优先于 ID 前缀,target 为空时要求恰好一个在线实例。 错误码与 Call 一致(BROWSER_NOT_FOUND / BROWSER_AMBIGUOUS / BROWSER_OFFLINE / NO_BROWSER_CONNECTED)。

func (*Server) ResolvePairedBrowser added in v0.2.0

func (s *Server) ResolvePairedBrowser(target string) (InstanceInfo, error)

ResolvePairedBrowser 与 ResolveBrowser 规则相同,只是点名(target 非空)的离线实例也算:原始 CDP 端点在浏览器离线时 依然有效,浏览器重新连上后地址又能连接,查看与关闭(撤销地址)它不能要求浏览器在线。target 为空时仍要求恰好一个在线实例。

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, ln net.Listener, mux *http.ServeMux) error

Serve 在给定 listener 上运行 WS 服务,阻塞至 ctx 取消后优雅停机。 mux 由调用方提供并可预先挂载其他路径(组装层在此挂上 /control/*,见 internal/daemon); WS 面注册在根路径 "/"。

func (*Server) SetBrowserListener added in v0.2.0

func (s *Server) SetBrowserListener(l BrowserListener)

SetBrowserListener 设置主浏览器监听者,替换先前通过它设置的那个(不影响 AddBrowserListener 追加的);传 nil 取消。

func (*Server) Version

func (s *Server) Version() string

Version 返回注入的 daemon 版本(hello.daemonVersion 与控制 API 都读它)。

Jump to

Keyboard shortcuts

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