webterminal

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2026 License: Apache-2.0 Imports: 47 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 webterminal 实现 Web 终端:通过 WebSocket 在浏览器中使用 SSH、Telnet 或本机 Shell, 会话由服务端持久保存,浏览器断开后可按设置保留并重新附着。

SSH 主机密钥采用"首次信任":第一次连接时记录指纹,之后指纹变化会拒绝连接,防止中间人攻击。 本机 Shell 等同于服务器的完全控制权,默认关闭,需要在模块设置中显式开启。

Index

Constants

View Source
const (
	ProxySOCKS5 = "socks5"
	ProxyHTTP   = "http"
)

代理类型

View Source
const (
	TypeSSH    = "ssh"
	TypeTelnet = "telnet"
	TypeLocal  = "local"
)

连接类型

View Source
const AuthCredential = "credential"

AuthCredential 表示 SSH 连接引用认证预设,凭据在连接时从预设中解析。

View Source
const ID = "webterminal"

ID 是模块标识。

Variables

This section is empty.

Functions

func Fingerprint

func Fingerprint(k ssh.PublicKey) string

Fingerprint 返回主机密钥的 SHA256 指纹(与 ssh-keygen -lf 输出格式一致)。

Types

type Connection

type Connection struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	// Group 为列表中的分组名称,只影响管理界面的展示,留空为未分组
	Group  string       `json:"group"`
	Type   string       `json:"type"`
	SSH    SSHConfig    `json:"ssh"`
	Telnet TelnetConfig `json:"telnet"`
	Local  LocalConfig  `json:"local"`
	Remark string       `json:"remark"`
	// Enable 为复用通用接口保留,终端连接始终可用
	Enable bool `json:"enable"`
	// QuickAccessDirs 为 SFTP 快速访问目录
	QuickAccessDirs []string `json:"quickAccessDirs"`
}

Connection 是一个终端连接配置。

func (*Connection) Validate

func (c *Connection) Validate() error

Validate 校验连接配置并补全默认值。

type Credential added in v0.4.6

type Credential struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	// AuthType 为 password 或 key
	AuthType string `json:"authType"`
	// Username 为可选的默认用户名,连接未填写用户名时使用
	Username   string `json:"username"`
	Password   string `json:"password"`
	PrivateKey string `json:"privateKey"`
	// KeyFile 为服务器本机上的私钥文件路径,PrivateKey 为空时使用
	KeyFile    string `json:"keyFile"`
	Passphrase string `json:"passphrase"`
	Remark     string `json:"remark"`
	// Enable 为复用通用接口保留,预设始终可用
	Enable bool `json:"enable"`
}

Credential 是可被多个 SSH 连接复用的认证预设(密码或私钥)。

func (*Credential) Validate added in v0.4.6

func (c *Credential) Validate() error

Validate 校验认证预设并补全默认值。

type LocalConfig

type LocalConfig struct {
	Shell   string `json:"shell"`
	WorkDir string `json:"workDir"`
	// Env 为附加环境变量
	Env map[string]string `json:"env"`
}

LocalConfig 是本机 Shell 参数。

type Module

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

Module 是 Web 终端模块。

func New

func New() *Module

New 创建模块。

func (*Module) Exec added in v0.6.6

func (m *Module) Exec(ctx context.Context, connKey, command string) (string, error)

Exec 经指定 SSH 连接执行一条命令并返回输出(最多保留 16 KB),供网络唤醒关机等功能使用。 每次新建连接、执行完关闭,不经连接池:目标设备可能刚重启过,池中的旧连接已失效但还没被发现, 用它打开会话会一直等到超时。无人值守时不能确认主机身份,因此要求该连接已记录主机密钥。 命令执行失败时返回的错误包装了 ssh 库的原始错误(如 *ssh.ExitError、*ssh.ExitMissingError),可用 errors.As 判断。

func (*Module) ID

func (m *Module) ID() string

ID 实现 module.Module。

func (*Module) ImportLucky

func (m *Module) ImportLucky(doc luckyimport.Document, opts luckyimport.Options) (*luckyimport.Report, error)

ImportLucky 实现 luckyimport.Importer。

func (*Module) Init

func (m *Module) Init(env module.Env) error

Init 实现 module.Module。旧配置缺失的字段保持默认值。

func (*Module) Push added in v0.5.5

func (m *Module) Push(ctx context.Context, connKey, dir string, files []PushFile, command string) (string, error)

Push 经指定 SSH 连接把文件写到远程目录(不存在时创建),全部写入后执行 command(可为空),返回命令输出。 无人值守时不能确认主机身份,因此要求该连接已记录主机密钥(在 Web 终端中连接过一次)。

func (*Module) Routes

func (m *Module) Routes(r module.Router)

Routes 实现 module.Module。

func (*Module) SSHTargets added in v0.5.5

func (m *Module) SSHTargets() []SSHTarget

SSHTargets 返回全部 SSH 连接,供证书部署等功能选择目标主机。

func (*Module) Start

func (m *Module) Start(context.Context) error

Start 实现 module.Module。

func (*Module) Stop

func (m *Module) Stop() error

Stop 实现 module.Module:结束全部会话并关闭连接池。

func (*Module) Title

func (m *Module) Title() string

Title 实现 module.Module。

type Proxy added in v0.3.0

type Proxy struct {
	Enabled  bool   `json:"enabled"`
	Type     string `json:"type"`
	Host     string `json:"host"`
	Port     int    `json:"port"`
	Username string `json:"username"`
	Password string `json:"password"`
}

Proxy 是 SSH / Telnet 连接使用的出口代理。

type PushFile added in v0.5.5

type PushFile struct {
	Name string
	Data []byte
	Mode os.FileMode
}

PushFile 是要经 SSH 上传到远程主机的文件。

type SSHConfig

type SSHConfig struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	Username string `json:"username"`
	// AuthType 为 password、key 或 credential(引用认证预设)
	AuthType string `json:"authType"`
	// CredentialKey 为 AuthType=credential 时引用的认证预设 key
	CredentialKey string `json:"credentialKey"`
	Password      string `json:"password"`
	PrivateKey    string `json:"privateKey"`
	// KeyFile 为服务器本机上的私钥文件路径,PrivateKey 为空时使用
	KeyFile    string `json:"keyFile"`
	Passphrase string `json:"passphrase"`
	// HostKey 为首次连接时记录的主机密钥指纹(SHA256),为空表示下次连接时重新信任
	HostKey string `json:"hostKey"`
	// HostKeyType 为该指纹对应的密钥类型(如 ssh-ed25519、ecdsa-sha2-nistp256),连接时只协商这种主机密钥。
	// 服务器通常同时有多把不同类型的主机密钥,只记指纹时无法知道该让服务器出示哪一把;为空(旧配置或手工填写的指纹)时连接会自动探测并补记
	HostKeyType string `json:"hostKeyType"`
	// KeepAlive 为保活间隔(秒)
	KeepAlive int `json:"keepAlive"`
	// WorkDir 为登录后切换到的目录
	WorkDir string `json:"workDir"`
	// Timeout 为连接超时(秒)
	Timeout int   `json:"timeout"`
	Proxy   Proxy `json:"proxy"`
}

SSHConfig 是 SSH 连接参数。

type SSHTarget added in v0.5.5

type SSHTarget struct {
	Key  string `json:"key"`
	Name string `json:"name"`
	Host string `json:"host"`
	// Trusted 表示已记录主机密钥,可用于无人值守的推送
	Trusted bool `json:"trusted"`
}

SSHTarget 是可供其他模块选择的 SSH 连接。

type Security added in v0.3.0

type Security struct {
	AgreementAccepted bool `json:"agreementAccepted"`
	TwoFAEnable       bool `json:"twoFAEnable"`
	// TwoFAKey 为 TOTP 密钥,仅在服务端保存,接口不输出
	TwoFAKey string `json:"twoFAKey"`
}

Security 是用户协议与二次验证配置。

type Settings

type Settings struct {
	// AllowLocalShell 允许打开本机 Shell
	AllowLocalShell bool `json:"allowLocalShell"`
	MaxSessions     int  `json:"maxSessions"`
	// IdleTimeout 为会话无输入输出自动结束的时间(分钟),0 不限
	IdleTimeout int `json:"idleTimeout"`
	// BufferSize 为每个会话的输出回放缓冲(KB)
	BufferSize int `json:"bufferSize"`
	// HeartbeatInterval 为 WebSocket 心跳间隔(秒)
	HeartbeatInterval int `json:"heartbeatInterval"`
	// SessionKeepAlive 为浏览器断开后会话保留时间(分钟),0 表示断开即结束
	SessionKeepAlive int `json:"sessionKeepAlive"`
}

Settings 是模块级配置。

type Shortcut added in v0.3.0

type Shortcut struct {
	Name      string `json:"name"`
	Command   string `json:"command"`
	SendEnter bool   `json:"sendEnter"`
}

Shortcut 是全局快捷命令。

type TelnetConfig added in v0.3.0

type TelnetConfig struct {
	Host     string `json:"host"`
	Port     int    `json:"port"`
	Username string `json:"username"`
	Password string `json:"password"`
	// Timeout 为连接超时(秒)
	Timeout int   `json:"timeout"`
	Proxy   Proxy `json:"proxy"`
}

TelnetConfig 是 Telnet 连接参数。

Jump to

Keyboard shortcuts

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