chanconn

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: MIT 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 chanconn 为 XOps v2 的 ssh.Channel 虚拟连接提供受 core/conc.Nursery 监督的 取消/截止时间看门狗与多路复用操作原语(ADR-009 / ADR-015)。

本包隶属 core/internal/sshnet/dial 体系,供 sshnet/dial 及后续 sshnet/{sftp,forward,mux} 复用:

  • 全包 0 裸 go 语句,所有后台看门狗、带外排空与脱离读写均受 core/conc.Nursery 托管;
  • WatchedConn 将无原生 SetDeadline 的 ssh.Channel 封装为标准 net.Conn,在半开黑洞或 截止到期时立即解阻并零协程泄漏收敛;
  • RunWithAbortWatchdog 基于 core/conc.RunScoped 保护单次阻塞操作(如 SSH 握手)。

Index

Constants

View Source
const (
	// MaxCopyChunk 定义通道分块读写与拷贝的默认单次缓冲上限(32 KiB)。
	MaxCopyChunk = 32 * 1024
	// CloseGraceTimeout 定义关闭通道或会话时等待后台排空协程收敛的有界宽限时长。
	CloseGraceTimeout = 5 * time.Millisecond
)
View Source
const (
	// AgentForwardChannelType 是 OpenSSH 服务端回连客户端 SSH Agent 所使用的入站通道类型。
	AgentForwardChannelType = "auth-agent@openssh.com"
)

Variables

This section is empty.

Functions

func RunMultiplexedOp

func RunMultiplexedOp[T any](
	ctx context.Context,
	lifeNursery *conc.Nursery,
	op func() (T, error),
	cleanup func(T),
) (T, error)

RunMultiplexedOp 在连接生命周期 Nursery 监督下执行单次多路复用控制操作: 单一调用方取消 ctx 时绝不关闭共享父连接(避免误杀同连接上其他活跃会话或子跳), 并在互斥锁线性化下保证迟到完成的孤立资源恰好被 cleanup 释放一次且随父连接关闭零泄漏收敛。

func RunWithAbortWatchdog

func RunWithAbortWatchdog(ctx context.Context, onAbort func(), op func() error) error

RunWithAbortWatchdog 在 core/conc.RunScoped 监督下执行阻塞操作 op: 一旦 ctx 取消或超时且 op 尚未完成,看门狗协程立即调用 onAbort(通常关闭底层连接或 ssh.Channel) 解除阻塞;op 正常完成时先原子置位 completed 再关闭 doneCh,杜绝 select 随机竞态误伤健康连接。

func WaitBounded

func WaitBounded(done <-chan struct{}, grace time.Duration)

WaitBounded 在最多 grace 时长内等待 done 关闭,避免半开黑洞对端在关闭路径造成永久阻塞。

Types

type Addr

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

Addr 为基于 ssh.Channel 的虚拟 net.Conn 提供轻量级 net.Addr 实现。

func NewAddr

func NewAddr(network, address string) Addr

NewAddr 构造基于网络类型与地址文本的不可变通道地址。

func (Addr) Network

func (a Addr) Network() string

Network 返回通道地址所属网络类型(如 "tcp"、"unix"、"tun"、"subsystem")。

func (Addr) String

func (a Addr) String() string

String 返回通道地址文本表示。

type AgentForwardRouter added in v1.3.0

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

AgentForwardRouter 管理单条 SSH 连接的 auth-agent@openssh.com 入站代理通道路由(ADR-019 A2)。 常驻排空 HandleChannelOpen 通道(防止未读入站通道堵塞 -R 等其他通道), 按 DisallowForwardAgent、活跃会话单一有效目标不变量与单连接并发上限(32)严格门控,且所有诊断发射均在锁外执行。

func NewAgentForwardRouter added in v1.3.0

func NewAgentForwardRouter(cfg AgentForwardRouterConfig) *AgentForwardRouter

NewAgentForwardRouter 构造连接级 Agent 转发路由器;未启用或 Disallow 时零风险拒绝全部入站代理通道。

func (*AgentForwardRouter) ActivateSession added in v1.3.0

func (r *AgentForwardRouter) ActivateSession(ctx context.Context, requested inventory.AgentSpec) (func(), bool, error)

ActivateSession 为单条会话激活代理转发 epoch(ADR-019 A2 单一活跃有效目标不变量):

  • ctx 已取消/为 nil 时提前失败关闭,绝不插入孤立 epoch;
  • "none"(大小写不敏感)或无效规格立即返回 ports.ErrInvalidArgument,绝不按字面相对路径拨号;
  • Disallow==true、缺少 SSHAgentPort 或默认继承命中 IdentityAgent=none 时非致命跳过(返回 nil, false, nil);
  • "default" / "SSH_AUTH_SOCK" / "$SSH_AUTH_SOCK" 统一继承当前跳板生效的 IdentityAgent,自定义路径覆写之;
  • 同连接并发活跃 epoch 必须解析到完全一致的有效目标,冲突时失败关闭返回 ports.ErrPermissionDenied;
  • 全部活跃 epoch 撤销后清空绑定目标,允许串行复用同一传输连接的不同套接字会话。

func (*AgentForwardRouter) StartDrain added in v1.3.0

func (r *AgentForwardRouter) StartDrain(incoming <-chan ssh.NewChannel) error

StartDrain 在 lifeNursery 下常驻排空服务端发起的 auth-agent@openssh.com 通道请求,防止阻塞其他入站通道。

type AgentForwardRouterConfig added in v1.3.0

type AgentForwardRouterConfig struct {
	LifeNursery  *conc.Nursery
	Agent        ports.SSHAgentPort
	Disallow     bool
	DefaultSpec  algebraic.Option[inventory.AgentSpec]
	IdentityNone bool
	OnDiag       func(ctx context.Context, level uint8, msg string)
}

AgentForwardRouterConfig 定义单连接 Agent 转发路由器的显式依赖与策略配置。

type PausableDeadline added in v1.3.0

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

PausableDeadline 管理支持交互提示期间暂停与恢复的 SSH 握手 ConnectTimeout 截止预算(ADR-019 B2)。 暂停期间仅冻结 ConnectTimeout 计时,父上下文取消仍立即生效并中断阻塞操作。

func NewPausableDeadline added in v1.3.0

func NewPausableDeadline(parent context.Context, timeout time.Duration, clk ports.Clock) (*PausableDeadline, context.Context, func())

NewPausableDeadline 基于父上下文与可选超时预算构造 PausableDeadline 及受其控制的派生上下文。 timeout <= 0 时不启用超时倒计时(Pause 为安全空操作),但派生上下文仍响应父取消与主动释放。

func (*PausableDeadline) HasTimeout added in v1.3.0

func (d *PausableDeadline) HasTimeout() bool

HasTimeout 报告是否配置了正向 ConnectTimeout 超时预算。

func (*PausableDeadline) Pause added in v1.3.0

func (d *PausableDeadline) Pause() func()

Pause 暂停 ConnectTimeout 倒计时并返回幂等恢复函数;允许可重入嵌套调用。

func (*PausableDeadline) Remaining added in v1.3.0

func (d *PausableDeadline) Remaining() (time.Duration, bool)

Remaining 返回当前剩余超时预算(未启用超时时返回 0, false)。

func (*PausableDeadline) RunWithWatchdog added in v1.3.0

func (d *PausableDeadline) RunWithWatchdog(ctx context.Context, onAbort func(), op func() error) error

RunWithWatchdog 在 core/conc.RunScoped 监督下执行阻塞握手 op: 兼顾父上下文立即取消与可暂停 ConnectTimeout 倒计时,任一触发且 op 未完成时即刻调用 onAbort 解阻。

type WatchedConn

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

WatchedConn 将不原生支持 SetDeadline 的 ssh.Channel 封装为带强制取消/截止时间看门狗的 net.Conn(ADR-015)。

生命周期与半开黑洞防挂死不变量:

  • 所有带外排空、O(1) 截止看门狗与脱离的 Read/Write 均受上游 lifeNursery 统一托管,零裸 go;
  • 仅当具体方向存在活跃 I/O 且该方向当前截止时间确已到期时才中止通道,清除/延长截止或空闲态永不误杀;
  • 脱离的 Read 读入内部独立缓冲、Write 拷贝内部切片,通道中止返回后绝不触碰调用方切片(-race 安全);
  • 对端进入半开黑洞(不回传 SSH_MSG_CHANNEL_CLOSE)时,abortCh 立即唤醒 Read/Write 与上层握手, Close() 经有界宽限返回且不伤健康父连接,父连接 Close() 关底层 TCP 时 100% 回收全部协程(零泄漏)。

func WrapChannel

func WrapChannel(
	lifeNursery *conc.Nursery,
	ch ssh.Channel,
	reqs <-chan *ssh.Request,
	localAddr, remoteAddr net.Addr,
	clk ports.Clock,
) (*WatchedConn, error)

WrapChannel 创建受 lifeNursery 监督的 ssh.Channel 包装器并启动带外排空与 O(1) 截止看门狗。

func (*WatchedConn) Close

func (c *WatchedConn) Close() error

Close 幂等关闭底层 ssh.Channel、立即唤醒阻塞读写与 O(1) 看门狗,并在有界宽限内收敛带外排空。

func (*WatchedConn) CloseWrite

func (c *WatchedConn) CloseWrite() error

CloseWrite 半关闭底层 ssh.Channel 的写方向(发送 SSH_MSG_CHANNEL_EOF),供 SFTP/透传子系统使用。

func (*WatchedConn) LocalAddr

func (c *WatchedConn) LocalAddr() net.Addr

LocalAddr 返回虚拟通道本端地址。

func (*WatchedConn) Read

func (c *WatchedConn) Read(p []byte) (int, error)

Read 从底层 ssh.Channel 读取字节;受 O(1) 读截止看门狗与半开黑洞即时解阻保护。

func (*WatchedConn) RemoteAddr

func (c *WatchedConn) RemoteAddr() net.Addr

RemoteAddr 返回虚拟通道远端地址。

func (*WatchedConn) SetDeadline

func (c *WatchedConn) SetDeadline(t time.Time) error

SetDeadline 原子更新读写截止时间并通知 O(1) 看门狗;无活跃 I/O 时绝不提前销毁通道。

func (*WatchedConn) SetOnClose added in v1.3.0

func (c *WatchedConn) SetOnClose(fn func())

SetOnClose 注册通道关闭时恰好触发一次的清理回调(若通道已关闭则立即在锁外同步触发)。

func (*WatchedConn) SetReadDeadline

func (c *WatchedConn) SetReadDeadline(t time.Time) error

SetReadDeadline 原子更新读截止时间并通知 O(1) 看门狗;清除或延长截止时立即取消旧到期判断。

func (*WatchedConn) SetWriteDeadline

func (c *WatchedConn) SetWriteDeadline(t time.Time) error

SetWriteDeadline 原子更新写截止时间并通知 O(1) 看门狗;清除或延长截止时立即取消旧到期判断。

func (*WatchedConn) Write

func (c *WatchedConn) Write(p []byte) (int, error)

Write 向底层 ssh.Channel 分块写入字节;受 O(1) 写截止看门狗与半开黑洞即时解阻保护。

Jump to

Keyboard shortcuts

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