transport

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultOutputLimit = 1024 * 1024 // 1 MiB
View Source
const (
	MaxSmallFileBytes = 1024 * 1024 // 1 MiB(兼容小文件限长接口)

)

Variables

This section is empty.

Functions

func BuildHostKeyCallback

func BuildHostKeyCallback(pinnedFingerprint string, knownHostsPaths []string) (ssh.HostKeyCallback, error)

func CheckedScopedPath

func CheckedScopedPath(sc *sftp.Client, roots []string, remotePath string, forWrite bool) (string, error)

CheckedScopedPath 校验受控目录范围(用于兼容 file_roots 沙箱检查,防 `..` 与软链接逃逸)。

func FingerprintSHA256

func FingerprintSHA256(key ssh.PublicKey) string

func FormatBytes

func FormatBytes(b int64) string

func IsKoKoTargetAuthFailure

func IsKoKoTargetAuthFailure(output string) bool

func ObserveHostFingerprint

func ObserveHostFingerprint(ctx context.Context, host string, port int) (string, error)

ObserveHostFingerprint 仅完成 SSH 密钥交换并提取服务端公钥指纹,不发起任何用户认证。

func ResolveRemotePath

func ResolveRemotePath(
	sc *sftp.Client,
	rootCtx *SFTPRootContext,
	fileRoots []string,
	rawRemotePath string,
	forWrite bool,
) (string, error)

ResolveRemotePath 将用户输入的远端路径(支持 `~/...`、相对路径、绝对路径)解析为 KoKo SFTP 路径, 并在平台将 SFTP 根目录限制为家目录时拦截越界绝对路径(如 `/tmp`),防止被静默写到 `~/tmp`。

func VerifyHostPrecheck

func VerifyHostPrecheck(ctx context.Context, host string, port int, verifyCb ssh.HostKeyCallback) error

VerifyHostPrecheck 在读取任何秘密或创建令牌之前,对 KoKo 进行真实 SSH 握手与主机密钥验证。

Types

type Connection

type Connection struct {
	Client    *ssh.Client
	Target    string
	AssetID   string
	FileRoots []string
	// contains filtered or unexported fields
}

func DialKoKo

func DialKoKo(
	ctx context.Context,
	host string,
	port int,
	hostKeyCb ssh.HostKeyCallback,
	tokenID, tokenValue string,
) (*Connection, error)

func (*Connection) Close

func (c *Connection) Close()

func (*Connection) ExecStream

func (c *Connection) ExecStream(ctx context.Context, command string, stdin io.Reader, stdout, stderr io.Writer) (int, error)

ExecStream 在不分配 PTY 的 exec 通道上运行命令,并把 stdin/stdout/stderr 原样透传, 用于 rsync -e 这类需要二进制安全管道的场景。返回远端退出码。

func (*Connection) Execute

func (c *Connection) Execute(
	ctx context.Context,
	command string,
	timeout time.Duration,
	cwd string,
	maxBytes int,
) (*ExecResult, error)

func (*Connection) IsClosed

func (c *Connection) IsClosed() bool

func (*Connection) OpenSFTP

func (c *Connection) OpenSFTP() (*sftp.Client, error)

func (*Connection) ProbeSFTP

func (c *Connection) ProbeSFTP(homeCandidate string) (*SFTPProbeResult, error)

ProbeSFTP 只读探测 KoKo 令牌登录下的 SFTP 虚拟目录与根目录映射(P0 契约验证使用,不写入任何远端文件)。

func (*Connection) RunInteractiveShell

func (c *Connection) RunInteractiveShell(ctx context.Context, onTargetConnected func()) error

RunInteractiveShell 启动远端 PTY + Shell。 onTargetConnected 在确认 KoKo 已成功连上目标服务器(而非目标认证失败立即断开)后调用一次,用于实现“登录成功后才保存密码”。

type ExecResult

type ExecResult struct {
	Status                   string  `json:"status"`
	Stdout                   string  `json:"stdout"`
	Stderr                   string  `json:"stderr"`
	ExitCode                 *int    `json:"exit_code"`
	StdoutStderrMerged       bool    `json:"stdout_stderr_merged"`
	ExitStatusSource         string  `json:"exit_status_source"`
	TargetExecutionVerified  bool    `json:"target_execution_verified"`
	DurationSeconds          float64 `json:"duration_seconds"`
	DurationMs               int64   `json:"duration_ms"`
	Truncated                bool    `json:"truncated"`
	RemoteProcessMayStillRun bool    `json:"remote_process_may_still_run,omitempty"`
}

type SFTPProbeResult

type SFTPProbeResult struct {
	WorkingDir       string   `json:"working_dir"`
	RootRealPath     string   `json:"root_realpath"`
	RootEntriesCount int      `json:"root_entries_count"`
	SampleEntries    []string `json:"sample_entries,omitempty"`
	TmpAccessible    bool     `json:"tmp_accessible"`
	HomeAccessible   bool     `json:"home_accessible"`
}

type SFTPRootContext

type SFTPRootContext struct {
	Username     string `json:"username"`
	HomeDir      string `json:"home_dir"`
	IsHomeRooted bool   `json:"is_home_rooted"`
}

SFTPRootContext 描述 KoKo 在令牌登录下对目标资产的 SFTP 根目录映射上下文。

func DetectSFTPRoot

func DetectSFTPRoot(sc *sftp.Client, username string) (*SFTPRootContext, error)

DetectSFTPRoot 验证目标 SFTP 会话已成功连上目标服务器,并探测平台 SFTP 根目录是否锁定在用户家目录。

type TransferItemResult

type TransferItemResult struct {
	Source      string `json:"source"`
	Destination string `json:"destination"`
	Bytes       int64  `json:"bytes"`
	Overwritten bool   `json:"overwritten"`
}

type TransferOptions

type TransferOptions struct {
	Recursive        bool
	Overwrite        bool
	Interactive      bool
	Quiet            bool
	MaxBytes         int64
	ProgressOut      io.Writer
	ConfirmOverwrite func(prompt string) (bool, error)
}

type TransferResult

type TransferResult struct {
	Direction       string               `json:"direction"` // "upload" or "download"
	Target          string               `json:"target,omitempty"`
	FilesCount      int                  `json:"files_count"`
	DirsCount       int                  `json:"dirs_count"`
	TotalBytes      int64                `json:"total_bytes"`
	DurationSeconds float64              `json:"duration_seconds"`
	DurationMs      int64                `json:"duration_ms"`
	Items           []TransferItemResult `json:"items,omitempty"`
	SkippedSymlinks []string             `json:"skipped_symlinks,omitempty"`
}

func DownloadPath

func DownloadPath(
	ctx context.Context,
	sc *sftp.Client,
	rootCtx *SFTPRootContext,
	fileRoots []string,
	rawRemoteSrc string,
	localDst string,
	opts TransferOptions,
) (*TransferResult, error)

DownloadPath 执行远端 SFTP 文件或目录(-r)到本地的流式下载。 遵循约定: - 默认不跟随远端软链接; - 先写本地 `<目标>.js-partial`(0600 权限),完整落盘后再原子重命名为最终文件名; - 本地目标已存在时:-f 直接覆盖,交互模式询问确认,非交互且无 -f 报错; - 失败不自动重试,提示本地 `.js-partial` 残留位置。

func UploadPath

func UploadPath(
	ctx context.Context,
	sc *sftp.Client,
	rootCtx *SFTPRootContext,
	fileRoots []string,
	localSrc string,
	rawRemoteDst string,
	opts TransferOptions,
) (*TransferResult, error)

UploadPath 执行本地文件或目录(-r)到远端 SFTP 的流式上传。 遵循约定: - 默认不跟随软链接; - 先写 `<目标>.js-partial`,完整关闭后再重命名为最终文件名; - 目标已存在时:-f 直接先删旧文件再改名,交互模式询问确认,非交互且无 -f 报错; - 失败不自动重试,明确提示残留 `.js-partial` 位置。

Jump to

Keyboard shortcuts

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