control

package
v0.0.0-...-632f32b Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2026 License: MIT Imports: 41 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

This section is empty.

Variables

View Source
var (
	ClaudeLoginStartFn  func(host, email, handoff string) error
	ClaudeLoginStatusFn func() (any, error)
)

ClaudeLoginStartFn and ClaudeLoginStatusFn are injected by cmd/ccfly. The control package cannot import mesh (mesh already imports control), while the command owns both the enrolled-cloud identity and the existing login flow.

These endpoints do not introduce a second credential protocol: POST merely starts the same `ccfly claude login` path a person can run locally. Through cc.hn it is protected by the existing owner-scoped /x/{device} gateway.

View Source
var ClaudeEgressAccountFn func() string

ClaudeEgressAccountFn 由 cmd/ccfly 注入:当前 SNI 段绑定的账号(云端认定的出口归属)。 它与库里的 current 不一致 = 凭据与出口错配(那台机器一发请求就被拒),前端要显眼地标出来。 注入而非直接调用,是因为 control 不能 import mesh(mesh 已经 import control)。

View Source
var SNIStatusFn func(fresh bool) any

SNIStatusFn 由 cmd/ccfly 注入为 mesh.SNIStatusJSON(fresh bool) any。nil = 本进程未跑 mesh。

Functions

func AdoptLiveCredential

func AdoptLiveCredential(email, host string) error

AdoptLiveCredential 认领活凭据:库里还不知道当前账号是谁时(升级前就有一份凭据的老设备), 用调用方掌握的账号名把它收进库,这样列表一开始就不是空的。已知 current 则 no-op。

func ApplyCredential

func ApplyCredential(data []byte) (string, error)

ApplyCredential 把一份凭据落成「活凭据」:原子写 ~/.claude/.credentials.json(0600,旧的留 .bak), macOS 再打「待 seed」标记并**当场**灌进登录钥匙串。返回落地路径。

这段原先长在 cmd/ccfly 的 writeCredentials 里;交付与切换两条路都要用,搬到这里只留一份实现。

func AttnKinds

func AttnKinds() map[string]string

AttnKinds 返回 sid → attn_kind(只含非空项;无待办/离线会话不出现)。memoized。 自包含(自己 list-panes / 扫会话),供 syncer 直接调用;落地页 handleSessions 亦复用此 memo。

func CLIAttachArgs

func CLIAttachArgs(sid string, claudeArgs []string) []string

CLIAttachArgs 给 ccfly attach:返回 tmux 参数(new-session -A,必要时注入 claude --resume)。 复用 /term 的 resume 注入逻辑(claudeResumeCmd),行为与 web 端完全一致。 claudeArgs 是追加到 `claude --resume <sid>` 后的额外参数(如 --permission-mode plan / --dangerously-skip-permissions);仅在「离线 resume 重建」时生效——会话已 live 时 -A 只 attach、 忽略命令,claude 已在跑改不了权限模式。

func CaptureLiveCredential

func CaptureLiveCredential() ([]byte, error)

CaptureLiveCredential 读回「claude 此刻真正在用的凭据」。

macOS 必须先读登录钥匙串:claude 在 mac 上以钥匙串为主源,refresh 之后只更新钥匙串, ~/.claude/.credentials.json 会停在 ccfly 写它的那一刻 —— 拿文件当快照存进库,等于把一份 早已被轮换掉的 refreshToken 存起来。钥匙串读不到(没人登录图形会话等)才回退文件。

func CurrentPaneSession

func CurrentPaneSession() string

CurrentPaneSession 返回**调用者自己所在**的那个 claude 会话 sid(不在任何会话里 → "")。

用途:换号要关掉在跑的会话,但绝不能关掉「跑着这条命令的那个」—— 那正是有人在用的对话 (典型情形:用户让会话里的 agent 去执行换号,关掉它 = 把提问的人一起挂了)。

两条判据,缺一不可:

  • `$TMUX_PANE` + panemap 真值表(SessionStart hook 现场登记):人自己在 tmux 里敲命令时最准, 要求 pane 名双匹配,防 tmux 重启后 %N 复用。
  • **祖先进程回溯**:agent 经工具执行时环境里往往没有 TMUX_PANE(实测就是空的),但它的进程 一定挂在那个 claude 进程下面 —— 顺着 ppid 往上找,撞上注册表里的 claude pid 即得会话。

func ErrNoCredEntry

func ErrNoCredEntry() error

ErrNoCredEntry 供调用方区分「库里没有」(该去网页推一次)与其他失败。

func ForgetCredential

func ForgetCredential(email string) (bool, error)

ForgetCredential 从库里删掉一个账号的凭据。

当前账号删不得:库里的 current 是「活凭据属于谁」的**唯一**判据(凭据 JSON 里没有账号字段), 删了它,下一次换号就不知道该把被换下去的凭据存回谁名下 —— 要么丢快照,要么记到别人头上。 想删就先换到别的号。

func Handler

func Handler() http.Handler

Handler 构造并返回控制服务的 HTTP 处理器(所有端点)。消费方可自行包一层反代/鉴权。

func InstallSessionHook

func InstallSessionHook()

InstallSessionHook 把 SessionStart hook 幂等写入 ~/.claude/settings.json,并具备**自愈**: 由巡检(scanner.go)每拍重申。不带 matcher(startup/resume/clear/compact 全触发——sid 在 这些时刻都可能变)。CCFLY_NO_HOOK=1 跳过。

自愈语义(实案教训:一个跑在 /private/tmp 下的临时 ccfly 实例曾把 hook 命令改写成自己的 路径,临时目录删除后**每次 SessionStart 都报错**——错误块异步注入 TUI,直接打断表世界的 读屏/菜单流;panemap 也全面停更):

  • 现有条目的二进制仍在 → 不动(不管是不是本进程的路径:多个并存实例绝不互相抢写, 任何一个活着的合法二进制都能写真值表);
  • 现有条目的二进制已不存在(临时实例被删、npm 旧版本被清)→ 用本进程路径修复;
  • 无条目 → 安装。

func RunPaneMapHook

func RunPaneMapHook(stdin io.Reader) error

RunPaneMapHook 是 `ccfly panemap-hook` 子命令的实现:作为 Claude Code 的 SessionStart hook 运行,把「本 pane → 本会话」写进真值表。

输入:stdin 的 hook JSON(取 session_id / cwd);环境变量 TMUX_PANE 定位 pane。 铁律:**绝不向 stdout 输出任何字节**(SessionStart hook 的 stdout 会被注入 Claude 上下文), 任何失败都静默返回 nil(hook 绝不打扰/拖慢会话启动)。

func RunScanner

func RunScanner(ctx context.Context)

RunScanner 周期性巡检 + 暖缓存,随 ctx 结束而停。每进程起一个: `ccfly serve` 经 Serve 起;`ccfly connect`(默认/生产路径,进程内跑 Handler())经 main 起。

func SandboxEnvArgs

func SandboxEnvArgs(skip bool) []string

SandboxEnvArgs 导出给 cmd/ccfly(ccfly new / picker 的 newSession)复用同一判定。

func SeedKeychain

func SeedKeychain() error

SeedKeychain 非 darwin 无钥匙串,恒成功。

func SeedPendingPath

func SeedPendingPath(home string) string

SeedPendingPath 与 darwin 版保持同一落点,便于跨平台代码无分支引用。

func Serve

func Serve(ctx context.Context, bind string) error

Serve 在 bind(如 "127.0.0.1:7699")上起控制服务,直到 ctx 取消后优雅关停。 bind 由调用方(cmd/ccfly)从 --bind/--port / env 解析好;不在这里探测任何网卡/mesh。

func SetClaudeDir

func SetClaudeDir(dir string)

SetClaudeDir 显式注入 Claude projects 目录(main 解析 --claude-dir/env 后调用)。 传空串表示「清除注入,回落 env/默认」。

func StartKeychainSeeder

func StartKeychainSeeder(stop <-chan struct{})

StartKeychainSeeder 非 darwin no-op。

func StartUISync

func StartUISync(ctx context.Context)

StartUISync 一次性启动后台同步:先采纳已缓存的更新版本(离线友好),再起周期巡检。 由 staticHandler 调用(serve / mesh 两条路都经它),用 sync.Once 保证只起一次。

func StoreDeliveredCredential

func StoreDeliveredCredential(email, host string, data []byte, egressV6, outNodeIP string) (string, bool, error)

StoreDeliveredCredential 在凭据交付(网页推送 / ccfly claude login)落地时调用。

**默认只入库,不接管当前账号**:本机已经在用某个号时,交付一份新凭据只是给凭据库添一份备用, 活凭据一动不动。理由是「接管」远不止换个文件 —— 正在跑的 claude 进程内存里还是旧号的 token, 换完它们既发不出请求、又会在续期时把新凭据顶掉,所以接管必须伴随「列会话 → 确认 → 关会话」, 而那只可能发生在设备跟前(`ccfly claude use`)。远程推送做不到那一步,就别偷偷接管。

一个例外:本机**还没有**可用的活凭据(全新设备的首次交付,或活凭据缺失/损坏)——此时没有 「当前账号」可被打断,交付即生效,引导页「推送即可用」的路径不变。

返回 (活凭据路径, 是否接管了当前账号, err);只入库时路径为空。

func TmuxProxyEnvArgs

func TmuxProxyEnvArgs() []string

TmuxProxyEnvArgs 导出给 cmd/ccfly(ccfly new / attach)复用同一注入逻辑。

func TmuxProxyEnvKV

func TmuxProxyEnvKV() []string

TmuxProxyEnvKV 导出给 cmd/ccfly(execTmux):CLI 的 tmux 调用同样可能是「spawn psmux server」 的那一次,server 环境定格,必须带上同一组代理/CA KV(Windows 竞态盲区#3)。

func TmuxTitleArgs

func TmuxTitleArgs(name string) []string

TmuxTitleArgs 导出 tmuxTitleArgs 供 cmd 层的「全新会话」路径(ccfly new)复用, 与 attach 路径走同一套标题口径。

func TrustFolder

func TrustFolder(dir string)

TrustFolder 导出给 cmd/ccfly(newSession)复用。

func TrySeedKeychain

func TrySeedKeychain()

TrySeedKeychain 非 darwin no-op。

func WillAdoptDelivery

func WillAdoptDelivery(email string) bool

WillAdoptDelivery 预判「现在给这台机器交付 email 的凭据,会不会接管当前账号」。 交付发起方(cmd/ccfly)用它在**发起前**告诉云端「这次只入库」,好让云端别把出口也指过去 —— 出口指了新号而活凭据还是旧号,那台机器一发请求就被拒。

Types

type CLISessionRow

type CLISessionRow struct {
	Sid   string
	Live  bool
	State string
	Age   int64
	Cwd   string
	Title string
	// Tmux:该会话实际跑在的 tmux 会话名(panemap 真值表/解析结果),仅 live 时非空。
	// ccfly ls 据此给出可直接执行的 `tmux a -t <名>` 接管命令。
	Tmux string
}

CLISessionRow 是 ccfly list 的一行。

func CLISessions

func CLISessions() ([]CLISessionRow, error)

CLISessions 返回全部会话(按 live 优先、再按新近排序由调用方决定;此处保持扫描序)。

type CloseSkip

type CloseSkip struct {
	Sid    string `json:"sid"`
	Reason string `json:"reason"` // not_live | attached | generating | awaiting_input | kill_failed
}

CloseSkip 是回执里「被跳过」的一条(未关闭)及其原因。

func CloseSessions

func CloseSessions(sids []string, force bool) ([]string, []CloseSkip, error)

CloseSessions 是 /close 的本体,也给 CLI 用(`ccfly claude use` 换号前要把在跑的会话都关掉 —— 老进程内存里还攥着旧账号的 token,留着它们只会继续用旧号、并在续期时把刚换上的凭据顶掉)。 语义见文件头:只 `tmux kill-session`,jsonl 原样保留,之后仍可 resume。

type CredAccount

type CredAccount struct {
	Email            string `json:"email"`
	Host             string `json:"host,omitempty"`
	SubscriptionType string `json:"subscription_type,omitempty"`
	ReceivedAt       int64  `json:"received_at,omitempty"`
	UpdatedAt        int64  `json:"updated_at,omitempty"`
	Source           string `json:"source,omitempty"`
	Current          bool   `json:"current"`
	Expired          bool   `json:"expired"` // refresh token 已过期 → 换过去也是死号,得重推
}

CredAccount 是给 API / CLI 看的条目视图 —— **不含密文**。

func CredAccounts

func CredAccounts() (current string, list []CredAccount, err error)

CredAccounts 列出库里的账号(不含密文),按最近入库倒序;当前账号恒排第一。

func SwitchCredential

func SwitchCredential(email string) (CredAccount, error)

SwitchCredential 把库里已有的某个账号换成活凭据。

**调用方必须先让云端把出口重指到该账号**(凭据绑源 IP);这里只管本地那一半。 返回换上的条目(不含密文由调用方自己注意别打印)。

type CredEntry

type CredEntry struct {
	Email                 string `json:"email"`
	Host                  string `json:"host,omitempty"` // 交付所经的云端
	CredentialB64         string `json:"credential_b64"` // 原始 .credentials.json 字节(保真)
	SubscriptionType      string `json:"subscription_type,omitempty"`
	RateLimitTier         string `json:"rate_limit_tier,omitempty"`
	ExpiresAt             int64  `json:"expires_at,omitempty"`               // access token 到期(ms,claude 自带口径)
	RefreshTokenExpiresAt int64  `json:"refresh_token_expires_at,omitempty"` // refresh token 到期(ms)
	EgressV6              string `json:"egress_v6,omitempty"`
	OutNodeIP             string `json:"out_node_ip,omitempty"`
	ReceivedAt            int64  `json:"received_at,omitempty"` // 首次入库(unix 秒)
	UpdatedAt             int64  `json:"updated_at,omitempty"`
	Source                string `json:"source,omitempty"` // push | switch | capture | adopt
}

CredEntry 是库里的一份凭据 + 它的元信息。密文只在 CredentialB64 里,永不进日志/列表响应。

type CredVault

type CredVault struct {
	Version int         `json:"version"`
	Current string      `json:"current,omitempty"` // 当前活凭据属于哪个账号(本机认为的)
	Entries []CredEntry `json:"entries"`
}

CredVault 是整个库(单文件,原子替换)。

type RunningSession

type RunningSession struct {
	Sid    string `json:"sid"`             // 完整会话 uuid —— 换号后靠它 resume
	Title  string `json:"title,omitempty"` // 会话标题(可能为空)
	Cwd    string `json:"cwd,omitempty"`
	State  string `json:"state,omitempty"`
	Tmux   string `json:"tmux,omitempty"` // 非空 = ccfly 起的、可关;空 = 用户自己终端里的
	PIDs   []int  `json:"pids,omitempty"`
	AgeSec int64  `json:"age_sec,omitempty"`
}

RunningSession 是一个正在跑的 claude 会话(换号前要展示/关闭的对象)。

func RunningClaudeSessions

func RunningClaudeSessions() ([]RunningSession, error)

RunningClaudeSessions 返回此刻在跑的 claude 会话,tmux 里的排在前面,再按最近活动排序。

func (RunningSession) InTmux

func (r RunningSession) InTmux() bool

InTmux 报告这个会话是否跑在 ccfly 管的 tmux 里(即换号时能被关掉)。

type SessionDigest

type SessionDigest struct {
	Path string `json:"-"`
	Size int64  `json:"-"`
	// contains filtered or unexported fields
}

SessionDigest 是单个会话的摘要快照,外加其本地 jsonl 文件位置与当前字节数——同步的数据源。 内嵌 claudeSnapshot 的导出字段(SessionID/Cwd/GitBranch/Title/Model/State/Turns/Tokens/ LastTs/Preview…)对外可见;Path/Size 仅本机用(json:"-",不外泄进摘要文档)。

func SessionDigests

func SessionDigests() ([]SessionDigest, error)

SessionDigests 扫描本地 Claude 会话,返回每个会话的摘要快照 + 其 jsonl 路径与当前大小。 复用 claudescan 的 (mtime+size) 缓存(cachedScanOne):idle 会话命中缓存,只有变动文件付全扫。

type TakeoverResult

type TakeoverResult struct {
	Sid    string `json:"sid"`
	Killed []int  `json:"killed"`
}

TakeoverResult 是 Takeover 的结果(端点与 CLI 共用)。

func Takeover

func Takeover(sidOrName string) (TakeoverResult, error)

Takeover 定位并终止 sid 的既有 claude 进程(CLI attach 与 /takeover 共用)。 入参可为完整 session id 或 cc-<sid8> 形式的 tmux 名。

Jump to

Keyboard shortcuts

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