Documentation
¶
Overview ¶
Package nativemux 实现 Unix 原生 OpenSSH PROTOCOL.mux v4(SCM_RIGHTS 单 FD 传递)双向互操作(ADR-019 D3 / v2.2 S3-Q3):
- 出站客户端(xops → 原生 OpenSSH Master):支持 MUX_C_NEW_SESSION(3 枚 FD、PTY 经 NativeTTYFDProvider 借用真实 TTY、 非 PTY/SFTP 走 os.Pipe)、MUX_C_NEW_STDIO_FWD(2 枚 FD)与 SIGWINCH 窗口缩放中继;
- 入站服务端(原生 OpenSSH 客户端 → xops Master):严格失败关闭门控(DisallowNative + AllowUnchecked + //xops:syncclosure 作用域授权回调 AuthorizeSessionScope / AuthorizeStdioScope 携带单实例 NodeID/Endpoint/PlanDigest)、 硬拒 X11、校验 Master ForwardAgent 策略、3/2 枚单 FD 零泄漏接收及 SIGWINCH+TIOCGWINSZ 中继;
- 全包严守 core/osfs.{SendUnixFD,RecvUnixFD,TerminalSizeOfFD} 边界,零 x/sys/unix 与零行内 lint 豁免。
Index ¶
- Constants
- func DispatchPacket(ctx context.Context, conn net.Conn, msgType uint32, body []byte, h ServerHooks) (bool, bool, error)
- func EncodeNewSession(w NewSessionWire) []byte
- func EncodeStdioFwd(reqID uint32, target netaddr.HostPort) []byte
- func OpenRemoteFS(ctx context.Context, deps ClientDeps) (ports.RemoteFSPort, error)
- func OpenStdioForward(ctx context.Context, deps ClientDeps, target netaddr.HostPort) (net.Conn, error)
- func RecvFDs(conn *net.UnixConn, count int) ([]*os.File, error)
- func SendFDs(conn *net.UnixConn, files ...*os.File) error
- func SignalMasterWINCH(pid uint32) error
- type AuthorizeSessionScope
- type AuthorizeStdioScope
- type ClientDeps
- type NewSessionWire
- 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) StartSubsystem(ctx context.Context, subsystem string, 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) UsesDirectNativeTTY() bool
- func (s *RemoteSession) Wait(ctx context.Context) (execution.ExecutionOutcome, error)
- type ServerHooks
- type SessionRequest
- type StdioFwdRequest
- type StdioFwdWire
Constants ¶
const ( CNewSession uint32 = 0x10000002 CNewStdioFwd uint32 = 0x10000008 SExitMessage uint32 = 0x80000004 SSessionOpened uint32 = 0x80000006 STTYAllocFail uint32 = 0x80000008 EscapeCharNone uint32 = 0xffffffff )
OpenSSH PROTOCOL.mux v4 原生会话与 stdio 转发报文常量(严格对齐 openssh-portable/PROTOCOL.mux)。
Variables ¶
This section is empty.
Functions ¶
func DispatchPacket ¶
func DispatchPacket(ctx context.Context, conn net.Conn, msgType uint32, body []byte, h ServerHooks) (bool, bool, error)
DispatchPacket 在服务端处理入站原生 OpenSSH PROTOCOL.mux v4(NEW_SESSION / NEW_STDIO_FWD)请求。
func EncodeNewSession ¶
func EncodeNewSession(w NewSessionWire) []byte
EncodeNewSession 将原生会话请求按 OpenSSH PROTOCOL.mux §1.2 精确字段序编码为报文体。
func EncodeStdioFwd ¶
EncodeStdioFwd 将 TCP 目标按 OpenSSH PROTOCOL.mux §1.7 编码为 MUX_C_NEW_STDIO_FWD 报文体。
func OpenRemoteFS ¶
func OpenRemoteFS(ctx context.Context, deps ClientDeps) (ports.RemoteFSPort, error)
OpenRemoteFS 经原生 OpenSSH MUX_C_NEW_SESSION(subsystem=true, cmd="sftp") + 3 枚管道 FD 打开 SFTP v3 客户端。
func OpenStdioForward ¶
func OpenStdioForward(ctx context.Context, deps ClientDeps, target netaddr.HostPort) (net.Conn, error)
OpenStdioForward 经原生 OpenSSH PROTOCOL.mux v4 MUX_C_NEW_STDIO_FWD + 2 枚独立 SCM_RIGHTS 管道 FD 打开 -W 双向流。
func SignalMasterWINCH ¶
SignalMasterWINCH 向原生 OpenSSH ControlMaster 主进程发送 SIGWINCH 通知终端窗口尺寸变更(无虚构 MUX_C_TERM_CHNG)。
Types ¶
type AuthorizeSessionScope ¶
type AuthorizeSessionScope func(ctx context.Context, req SessionRequest, run func(authCtx context.Context) (execution.ExecutionOutcome, error)) error
AuthorizeSessionScope 定义入站原生 OpenSSH MUX_C_NEW_SESSION 的同步作用域授权回调(ADR-019 v2.2 S3-Q3): 在门控授权通过后同步调用 run(authCtx) 执行完整 RecvFDs -> OpenSession -> 会话生命周期并返回真实 ExecutionOutcome; 终态控制报文(MUX_S_EXIT_MESSAGE + 优雅半关闭、建链前 MUX_S_FAILURE 或进入前拒绝 MUX_S_PERMISSION_DENIED) 严格在回调返回之后发送,确保终态审计落盘与 .lock 清理恒先于客户端观测且慢客户端无法卡滞门控作用域。
type AuthorizeStdioScope ¶
type AuthorizeStdioScope func(ctx context.Context, req StdioFwdRequest, run func(authCtx context.Context) error) error
AuthorizeStdioScope 定义入站原生 OpenSSH MUX_C_NEW_STDIO_FWD 的同步作用域授权回调(ADR-019 v2.2 S3-Q3): 在门控授权通过后同步调用 run(authCtx) 执行完整 RecvFDs -> DialTCP -> stdio 桥接生命周期; 进入前拒绝(MUX_S_PERMISSION_DENIED)与建链前失败(MUX_S_FAILURE)严格在回调返回后发送,确保审计先于客户端确认。
type ClientDeps ¶
type ClientDeps struct {
LifeNursery *conc.Nursery
IsAlive func() bool
DialUnix func(ctx context.Context) (*net.UnixConn, error)
MasterPID uint32
Endpoint string
Terminal ports.TerminalPort
EmitDiag func(ctx context.Context, level uint8, scope, hostLabel, msg string)
}
ClientDeps 封装出站原生 OpenSSH PROTOCOL.mux v4 客户端操作所需的显式依赖(零全局状态)。
type NewSessionWire ¶
type NewSessionWire struct {
ReqID uint32
WantTTY bool
WantX11 bool
WantAgent bool
Subsystem bool
EscapeChar uint32
Term string
Cmd string
Env map[string]string
}
NewSessionWire 承载 OpenSSH PROTOCOL.mux v4 MUX_C_NEW_SESSION 的线缆解码字段。
func DecodeNewSession ¶
func DecodeNewSession(body []byte) (NewSessionWire, error)
DecodeNewSession 解码并严格校验 OpenSSH PROTOCOL.mux v4 MUX_C_NEW_SESSION 报文体。
type RemoteSession ¶
type RemoteSession struct {
// contains filtered or unexported fields
}
RemoteSession 实现面向原生 OpenSSH ControlMaster 的 Unix SCM_RIGHTS 会话适配器 (同时实现 ports.RemoteSession、ports.SubsystemRemoteSession 与 ports.DirectTTYRemoteSession);零值恒失败关闭。
func NewRemoteSession ¶
func NewRemoteSession(deps ClientDeps) (*RemoteSession, error)
NewRemoteSession 构造面向原生 OpenSSH Master 的延迟启动会话句柄。
func (*RemoteSession) Close ¶
func (s *RemoteSession) Close() error
Close 幂等关闭所有原生管道句柄与控制套接字并回收全部后台协程(零 FD / goroutine 泄漏)。
func (*RemoteSession) ResizePTY ¶
func (s *RemoteSession) ResizePTY(ctx context.Context, size ports.TerminalSize) error
ResizePTY 向原生 OpenSSH ControlMaster 主进程发送 SIGWINCH 通知终端尺寸变化(无虚构 MUX_C_TERM_CHNG)。
func (*RemoteSession) Signal ¶
func (s *RemoteSession) Signal(ctx context.Context, signal string) error
Signal 在原生 PROTOCOL.mux v4 协议中无对应帧类型,校验活跃状态后诚实返回 ports.ErrUnsupported。
func (*RemoteSession) Start ¶
func (s *RemoteSession) Start(ctx context.Context, cmd execution.CommandPlan, streams ports.ExecStreams) error
Start 经原生 OpenSSH MUX_C_NEW_SESSION 启动远端 CommandPlan 执行。
func (*RemoteSession) StartShell ¶
func (s *RemoteSession) StartShell(ctx context.Context, streams ports.ExecStreams) error
StartShell 经原生 OpenSSH MUX_C_NEW_SESSION 启动交互式或非交互式远端 Shell。
func (*RemoteSession) StartSubsystem ¶
func (s *RemoteSession) StartSubsystem(ctx context.Context, subsystem string, streams ports.ExecStreams) error
StartSubsystem 实现 ports.SubsystemRemoteSession,经原生 MUX_C_NEW_SESSION(subsystem=true) 启动远端子系统。
func (*RemoteSession) StderrPipe ¶
func (s *RemoteSession) StderrPipe() (io.Reader, error)
StderrPipe 在启动前借出有界缓冲的标准错误读取管道(与 ExecStreams.Stderr 互斥)。
func (*RemoteSession) StdinPipe ¶
func (s *RemoteSession) StdinPipe() (io.WriteCloser, error)
StdinPipe 在启动前借出远端标准输入写入管道(与 ExecStreams.Stdin 及脚本负载互斥)。
func (*RemoteSession) StdoutPipe ¶
func (s *RemoteSession) StdoutPipe() (io.Reader, error)
StdoutPipe 在启动前借出有界缓冲的标准输出读取管道(与 ExecStreams.Stdout 互斥)。
func (*RemoteSession) UsesDirectNativeTTY ¶
func (s *RemoteSession) UsesDirectNativeTTY() bool
UsesDirectNativeTTY 实现 ports.DirectTTYRemoteSession(ADR-019 C1 / D3): 标识本会话在 PTY 模式下经 OS SCM_RIGHTS 直接移交控制终端 FD 给原生 OpenSSH Master;零值或已关闭时返回 false。
func (*RemoteSession) Wait ¶
func (s *RemoteSession) Wait(ctx context.Context) (execution.ExecutionOutcome, error)
Wait 等待原生会话退出帧并交付封闭 execution.ExecutionOutcome,取消时 O(1) 中止。
type ServerHooks ¶
type ServerHooks struct {
NodeID string
Endpoint netaddr.Endpoint
PlanDigest algebraic.Option[plan.PlanDigest]
Upstream ports.TransportConn
HandshakeTimeout time.Duration
AgentEnabled bool
BoundAgent algebraic.Option[inventory.AgentSpec]
DisallowNative bool
AllowUnchecked bool
AuthorizeSession AuthorizeSessionScope
AuthorizeStdio AuthorizeStdioScope
OnAuthorizeSession func(ctx context.Context, req SessionRequest) error
OnAuthorizeStdio func(ctx context.Context, req StdioFwdRequest) error
NextSessionID func() uint32
OnSessionDelta func(delta int64)
EmitDiag func(ctx context.Context, level uint8, scope, hostLabel, msg string)
}
ServerHooks 封装入站原生 OpenSSH PROTOCOL.mux v4 服务端分发所需的单实例身份、策略与作用域门控钩子。
type SessionRequest ¶
type SessionRequest struct {
NodeID string
Endpoint algebraic.Option[netaddr.Endpoint]
PlanDigest algebraic.Option[plan.PlanDigest]
Command string
Subsystem bool
WantTTY bool
WantAgent bool
Term string
Env map[string]string
}
SessionRequest 描述入站原生 OpenSSH MUX_C_NEW_SESSION 的授权校验与目标身份绑定上下文(ADR-019 v2.2 S3-Q3)。
type StdioFwdRequest ¶
type StdioFwdRequest struct {
NodeID string
Endpoint algebraic.Option[netaddr.Endpoint]
PlanDigest algebraic.Option[plan.PlanDigest]
Host string
Port uint16
}
StdioFwdRequest 描述入站原生 OpenSSH MUX_C_NEW_STDIO_FWD 的授权校验与目标身份绑定上下文(ADR-019 v2.2 S3-Q3)。
type StdioFwdWire ¶
StdioFwdWire 承载 OpenSSH PROTOCOL.mux v4 MUX_C_NEW_STDIO_FWD 的线缆解码字段。
func DecodeStdioFwd ¶
func DecodeStdioFwd(body []byte) (StdioFwdWire, error)
DecodeStdioFwd 解码并校验 MUX_C_NEW_STDIO_FWD 报文体中的主机与端口。