Documentation
¶
Overview ¶
Package muxstream 提供 OpenSSH PROTOCOL.mux v4 线缆编解码与跨平台 XOps 流式复用桥接原语 (零 core/osfs 与 SCM_RIGHTS 依赖,Linux/macOS/FreeBSD/Windows 字节级一致)。
本包仅面向同域上层包 core/internal/sshnet/mux 使用,统一承载:
- PROTOCOL.mux v4 控制报文与 xops-stream-mux@xops.dev 扩展帧编解码(wire_codec.go);
- 远端会话(ports.RemoteSession)双向帧桥接客户端与服务端实现;
- 远端监听器(ListenTCP / ListenStreamLocal)多流复用客户端与服务端实现;
- 基于 core/conc.Nursery 的零泄漏双向字节流拼接(SpliceStreams / SpliceStreamsStrict) 与 Windows afunix.sys 防 RST 优雅半关闭排空(GracefulHalfClose)。
Index ¶
- Constants
- func AppendBytes(dst, b []byte) []byte
- func AppendStr(dst []byte, s string) []byte
- func AppendU32(dst []byte, v uint32) []byte
- func DecodeCommandPlan(raw []byte) (execution.CommandPlan, error)
- func DecodeForwardWire(body []byte) (uint32, ports.ForwardSpec, error)
- func DecodeOutcomePayload(body []byte) (execution.ExecutionOutcome, error, error)
- func DecodePTYAndEnv(rd *Reader) (algebraic.Option[ports.PTYRequest], map[string]string, error)
- func DecodeServerError(msgType uint32, body []byte) error
- func DecodeTUNForwardWire(body []byte) (uint32, ports.ForwardSpec, error)
- func EncodeCommandPlan(cmd execution.CommandPlan) ([]byte, error)
- func EncodeForwardWire(reqID uint32, spec ports.ForwardSpec) (uint32, []byte, error)
- func EncodeOutcomePayload(outcome execution.ExecutionOutcome, waitErr error) ([]byte, error)
- func EncodePTYAndEnv(dst []byte, ptyOpt algebraic.Option[ports.PTYRequest], env map[string]string) []byte
- func ExchangeClientHello(rw io.ReadWriter) (map[string]string, error)
- func FirstErr(errs ...error) error
- func GracefulHalfClose(conn net.Conn, maxWait time.Duration)
- func IsBenignIOErr(err error) bool
- func ReadHello(r io.Reader) (map[string]string, error)
- func ReadPacket(r io.Reader) (uint32, []byte, error)
- func SendAliveCheck(rw io.ReadWriter, reqID uint32) (uint32, error)
- func ServeListenerBridge(ctx context.Context, conn net.Conn, ln net.Listener)
- func ServeSessionBridge(ctx context.Context, conn net.Conn, sess ports.RemoteSession)
- func SpliceStreams(ctx context.Context, left, right io.ReadWriteCloser) error
- func SpliceStreamsStrict(ctx context.Context, left, right io.ReadWriteCloser) error
- func WireToTUNMode(v uint32) (ports.TUNMode, error)
- func WriteHello(w io.Writer, advertiseXOps bool) error
- func WritePacket(w io.Writer, msgType uint32, body []byte) error
- func WriteStatusReply(w io.Writer, msgType, reqID uint32, reason string) error
- func ZeroVal[T any]() (z T)
- type HalfWriteCloser
- type Reader
- type RemoteListener
- type RemoteSession
- func (s *RemoteSession) Close() error
- func (s *RemoteSession) ResizePTY(ctx context.Context, size ports.TerminalSize) error
- func (s *RemoteSession) Signal(ctx context.Context, signal string) error
- func (s *RemoteSession) Start(ctx context.Context, cmd execution.CommandPlan, streams ports.ExecStreams) error
- func (s *RemoteSession) StartShell(ctx context.Context, streams ports.ExecStreams) error
- func (s *RemoteSession) StderrPipe() (io.Reader, error)
- func (s *RemoteSession) StdinPipe() (io.WriteCloser, error)
- func (s *RemoteSession) StdoutPipe() (io.Reader, error)
- func (s *RemoteSession) Wait(ctx context.Context) (execution.ExecutionOutcome, error)
- type SyncFrameWriter
Constants ¶
const ( // MaxPacketBytes 限制单个 PROTOCOL.mux / 流式复用帧体上限(1 MiB),防止恶意对端 OOM。 MaxPacketBytes = 1 << 20 // CopyChunkBytes 限制单次流拷贝与帧分片大小(32 KiB),保持低内存占用与良好响应性。 CopyChunkBytes = 32 * 1024 // DefaultHalfCloseDrainTimeout 限制服务端终态回复后等待对端读毕关闭的最大排空时间, // 避免 Windows afunix.sys 立即 closesocket 引发 WSAECONNRESET。 DefaultHalfCloseDrainTimeout = 250 * time.Millisecond )
const ( ProtocolVersion uint32 = 4 MsgHello uint32 = 0x00000001 CAliveCheck uint32 = 0x10000004 CTerminate uint32 = 0x10000005 COpenFwd uint32 = 0x10000006 CCloseFwd uint32 = 0x10000007 CStopListening uint32 = 0x10000009 CProxy uint32 = 0x1000000f SOK uint32 = 0x80000001 SPermissionDenied uint32 = 0x80000002 SFailure uint32 = 0x80000003 SAlive uint32 = 0x80000005 SRemotePort uint32 = 0x80000007 SProxy uint32 = 0x8000000f FwdLocal uint32 = 1 FwdRemote uint32 = 2 FwdDynamic uint32 = 3 FwdPortUnix uint32 = 0xfffffffe // OpenSSH StreamLocal 端口哨兵值 (-2) )
OpenSSH PROTOCOL.mux v4 标准报文类型与转发类别常量(严格对齐 openssh-portable/PROTOCOL.mux)。
const ( ExtXOpsStreamMux = "xops-stream-mux@xops.dev" XOpsCOpenSession uint32 = 0x7f000001 XOpsCOpenSFTP uint32 = 0x7f000002 XOpsCDialTCP uint32 = 0x7f000003 XOpsCDialStreamLocal uint32 = 0x7f000004 XOpsCOpenTUN uint32 = 0x7f000005 XOpsCListenTCP uint32 = 0x7f000006 XOpsCListenStreamLocal uint32 = 0x7f000007 XOpsCOpenTUNFwd uint32 = 0x7f000008 XOpsCCloseTUNFwd uint32 = 0x7f000009 XOpsSBoundAddr uint32 = 0x7f800001 SessMsgStartExec uint32 = 0x7f100001 SessMsgStartShell uint32 = 0x7f100002 SessMsgStdinData uint32 = 0x7f100003 SessMsgStdinEOF uint32 = 0x7f100004 SessMsgResizePTY uint32 = 0x7f100005 SessMsgSignal uint32 = 0x7f100006 SessMsgClose uint32 = 0x7f100007 SessMsgStarted uint32 = 0x7f200001 SessMsgStdoutData uint32 = 0x7f200002 SessMsgStdoutEOF uint32 = 0x7f200003 SessMsgStderrData uint32 = 0x7f200004 SessMsgStderrEOF uint32 = 0x7f200005 SessMsgOutcome uint32 = 0x7f200006 SessMsgCtrlReply uint32 = 0x7f200007 LstMsgAccept uint32 = 0x7f300001 LstMsgData uint32 = 0x7f300002 LstMsgCloseWrite uint32 = 0x7f300003 LstMsgClose uint32 = 0x7f300004 LstMsgListenerClosed uint32 = 0x7f300005 )
XOps v4 跨平台流式扩展常量(经 MUX_MSG_HELLO 扩展对协商,零 SCM_RIGHTS 依赖,四平台一致)。
Variables ¶
This section is empty.
Functions ¶
func DecodeCommandPlan ¶
func DecodeCommandPlan(raw []byte) (execution.CommandPlan, error)
DecodeCommandPlan 反序列化并严格校验 execution.CommandPlan。
func DecodeForwardWire ¶
func DecodeForwardWire(body []byte) (uint32, ports.ForwardSpec, error)
DecodeForwardWire 解码标准 PROTOCOL.mux v4 MUX_C_OPEN_FWD / MUX_C_CLOSE_FWD 负载。
func DecodeOutcomePayload ¶
func DecodeOutcomePayload(body []byte) (execution.ExecutionOutcome, error, error)
DecodeOutcomePayload 解码远端会话退出帧携带的 execution.ExecutionOutcome 与等待错误。
func DecodePTYAndEnv ¶
DecodePTYAndEnv 解码可选 PTY 请求与有界环境变量映射。
func DecodeServerError ¶
DecodeServerError 将 ControlMaster 返回的 MUX_S_PERMISSION_DENIED / MUX_S_FAILURE 映射为标准端口哨兵错误。
func DecodeTUNForwardWire ¶
func DecodeTUNForwardWire(body []byte) (uint32, ports.ForwardSpec, error)
DecodeTUNForwardWire 解码 XOps TUN 动态转发控制帧负载。
func EncodeCommandPlan ¶
func EncodeCommandPlan(cmd execution.CommandPlan) ([]byte, error)
EncodeCommandPlan 使用确定性 json/v2 编码不可变 execution.CommandPlan。
func EncodeForwardWire ¶
EncodeForwardWire 将 ports.ForwardSpec 编码为标准 MUX_C_OPEN_FWD 或 XOps TUN 扩展控制帧。
func EncodeOutcomePayload ¶
func EncodeOutcomePayload(outcome execution.ExecutionOutcome, waitErr error) ([]byte, error)
EncodeOutcomePayload 使用 execution.MarshalOutcomeTo 编码封闭执行结果与错误文本。
func EncodePTYAndEnv ¶
func EncodePTYAndEnv(dst []byte, ptyOpt algebraic.Option[ports.PTYRequest], env map[string]string) []byte
EncodePTYAndEnv 编码可选 PTY 请求与会话环境变量映射。
func ExchangeClientHello ¶
func ExchangeClientHello(rw io.ReadWriter) (map[string]string, error)
ExchangeClientHello 发送客户端 HELLO 并读取服务端 HELLO 扩展表。
func GracefulHalfClose ¶
GracefulHalfClose 在服务端写出最终应答帧后执行半关闭(CloseWrite)并在有界截止时间内等待对端关闭, 防止 Windows afunix.sys 在客户端尚未读走内核发送缓冲时因立即 closesocket 触发 WSAECONNRESET。
func ReadPacket ¶
ReadPacket 完整读取并校验单条 PROTOCOL.mux 帧,返回报文类型与负载。
func SendAliveCheck ¶
func SendAliveCheck(rw io.ReadWriter, reqID uint32) (uint32, error)
SendAliveCheck 发送 MUX_C_ALIVE_CHECK 并解析对端通告的 ControlMaster PID。
func ServeListenerBridge ¶
ServeListenerBridge 在服务端将上游远端 net.Listener 的全部到站连接多路复用进单条 AF_UNIX 帧流, 完整保留双向 TCP/Unix 半关闭语义直至双侧均结束或客户端显式发送 LstMsgClose。
func ServeSessionBridge ¶
ServeSessionBridge 在服务端将单条 AF_UNIX 帧流桥接至上游 ports.RemoteSession, 全程受 core/conc.Nursery 与 chanconn.RunWithAbortWatchdog 监督(0 裸 go,零泄漏)。
func SpliceStreams ¶
func SpliceStreams(ctx context.Context, left, right io.ReadWriteCloser) error
SpliceStreams 在 core/conc.Nursery 监督下执行双向流透传: 当源流正常到达 io.EOF 且目标支持 CloseWrite() 且半关闭成功时保留反方向排空; 若目标不支持 CloseWrite()、半关闭失败或任一侧以非 EOF 错误中止,立即同步关闭双端杜绝僵尸隧道。
func SpliceStreamsStrict ¶
func SpliceStreamsStrict(ctx context.Context, left, right io.ReadWriteCloser) error
SpliceStreamsStrict 在 core/conc.Nursery 监督下执行无半关闭双向透传(适用于 TUN 等帧设备): 任一侧到达 EOF 或发生错误均立即同步关闭双端,防止无 EOF 侧永久挂起。
func WireToTUNMode ¶
WireToTUNMode 以 fail-closed 枚举映射将线缆整数还原为合法 ports.TUNMode。
func WriteHello ¶
WriteHello 发送 PROTOCOL.mux v4 MUX_MSG_HELLO(可选通告 XOps 跨平台流式复用扩展)。
func WritePacket ¶
WritePacket 编码并完整写出单条 PROTOCOL.mux 帧([4B len][4B type][body])。
func WriteStatusReply ¶
WriteStatusReply 发送 MUX_S_OK、MUX_S_FAILURE 或 MUX_S_PERMISSION_DENIED 应答。
Types ¶
type HalfWriteCloser ¶
type HalfWriteCloser interface {
CloseWrite() error
}
HalfWriteCloser 表达支持 TCP/Unix/SSH 半关闭(写方向 EOF)的底层流接口。
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader 为 PROTOCOL.mux 大端变长字段提供边界安全的顺序解码游标。
func (*Reader) ReadString ¶
ReadString 读取一个带 32 位长度前缀的 UTF-8/字节字符串。
func (*Reader) ReadUint32 ¶
ReadUint32 读取一个大端 32 位无符号整数。
type RemoteListener ¶
type RemoteListener struct {
// contains filtered or unexported fields
}
RemoteListener 通过单条帧复用连接实现远端 ListenTCP / ListenStreamLocal 的 net.Listener 视图;零值恒失败关闭。
func NewRemoteListener ¶
func NewRemoteListener(lifeNursery *conc.Nursery, conn io.ReadWriteCloser, network, boundAddr string) (*RemoteListener, error)
NewRemoteListener 构造受 lifeNursery 监督的多路复用远端监听器客户端。
func (*RemoteListener) Accept ¶
func (l *RemoteListener) Accept() (net.Conn, error)
Accept 接收下一条由远端监听器经控制通道复用投递的入站连接。
func (*RemoteListener) Close ¶
func (l *RemoteListener) Close() error
Close 幂等关闭复用监听器与所有在途子连接,并等待后台读泵同步退出。
type RemoteSession ¶
type RemoteSession struct {
// contains filtered or unexported fields
}
RemoteSession 通过独立帧流实现 ports.RemoteSession,严格保持管道互斥、缓冲解耦与零泄漏语义;零值恒失败关闭。
func NewRemoteSession ¶
func NewRemoteSession(lifeNursery *conc.Nursery, isAlive func() bool, conn io.ReadWriteCloser) (*RemoteSession, error)
NewRemoteSession 在已通过 xopsCOpenSession 握手的连接上构造客户端 RemoteSession。
func (*RemoteSession) Close ¶
func (s *RemoteSession) Close() error
Close 幂等关闭复用会话管道与底层连接并在有界宽限内收敛后台读泵(零泄漏、对阻塞态外部 Stdin 有界解阻)。
func (*RemoteSession) ResizePTY ¶
func (s *RemoteSession) ResizePTY(ctx context.Context, size ports.TerminalSize) error
ResizePTY 向已启动的复用会话发送带关联 reqID 的窗口尺寸更新请求。
func (*RemoteSession) Signal ¶
func (s *RemoteSession) Signal(ctx context.Context, signal string) error
Signal 向已启动的复用会话发送带关联 reqID 的规范化 POSIX 信号。
func (*RemoteSession) Start ¶
func (s *RemoteSession) Start(ctx context.Context, cmd execution.CommandPlan, streams ports.ExecStreams) error
Start 通过 ControlMaster 启动远端不可变 CommandPlan 执行。
func (*RemoteSession) StartShell ¶
func (s *RemoteSession) StartShell(ctx context.Context, streams ports.ExecStreams) error
StartShell 通过 ControlMaster 启动远端交互式 Shell。
func (*RemoteSession) StderrPipe ¶
func (s *RemoteSession) StderrPipe() (io.Reader, error)
StderrPipe 在 Start/StartShell 前借出带独立缓冲的远端标准错误读取管道(与 ExecStreams.Stderr 互斥)。
func (*RemoteSession) StdinPipe ¶
func (s *RemoteSession) StdinPipe() (io.WriteCloser, error)
StdinPipe 在 Start/StartShell 前借出远端标准输入写入管道(与 ExecStreams.Stdin 互斥)。
func (*RemoteSession) StdoutPipe ¶
func (s *RemoteSession) StdoutPipe() (io.Reader, error)
StdoutPipe 在 Start/StartShell 前借出带独立缓冲的远端标准输出读取管道(与 ExecStreams.Stdout 互斥)。
func (*RemoteSession) Wait ¶
func (s *RemoteSession) Wait(ctx context.Context) (execution.ExecutionOutcome, error)
Wait 等待远端复用会话退出并返回完整封闭 execution.ExecutionOutcome。 当 ctx 取消或超时时立即停用本地输出写入、关闭管道与底层连接,O(1) 返回 (OutcomeUnknown, ctx.Err()), 绝不因调用方阻塞态 ExecStreams.Stdin 挂死。
type SyncFrameWriter ¶
type SyncFrameWriter struct {
// contains filtered or unexported fields
}
SyncFrameWriter 对底层 io.Writer 串行化写入完整 PROTOCOL.mux 帧,防止并发写交错破坏帧边界。
func NewSyncFrameWriter ¶
func NewSyncFrameWriter(w io.Writer) *SyncFrameWriter
NewSyncFrameWriter 构造绑定目标写入器的并发安全帧写入器。