ssh

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2026 License: GPL-3.0 Imports: 20 Imported by: 0

Documentation

Overview

Package ssh SSH远程连接模块 - HTTP/WebSocket 处理器

Package ssh SSH远程连接模块 - 数据模型定义

Package ssh SSH远程连接模块 - 提供SSH终端和SFTP功能

Package ssh SSH远程连接模块 - 服务层实现

Index

Constants

View Source
const (
	DefaultPort         = 22
	DefaultCols         = 80
	DefaultRows         = 24
	MaxSessionsPerUser  = 10
	WebSocketBufferSize = 64 * 1024 // 64KB
	HeartbeatInterval   = 30 * time.Second
	WriteWait           = 10 * time.Second
	PongWait            = 60 * time.Second
	PingPeriod          = (PongWait * 9) / 10
	TransferWorkers     = 3 // 并发传输数
)

配置常量

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectRequest

type ConnectRequest struct {
	ConnectionID string `json:"connection_id" binding:"required"`
	Cols         uint16 `json:"cols" binding:"omitempty,min=20,max=500"`
	Rows         uint16 `json:"rows" binding:"omitempty,min=5,max=200"`
}

ConnectRequest 建立连接请求

type Connection

type Connection struct {
	ID                  string `json:"id" gorm:"primaryKey;size:36"`
	Name                string `json:"name" gorm:"size:100;not null"`
	Host                string `json:"host" gorm:"size:255;not null"`
	Port                int    `json:"port" gorm:"default:22"`
	Username            string `json:"username" gorm:"size:100;not null"`
	AuthMethod          string `json:"auth_method" gorm:"size:20;not null"` // password | key
	EncryptedCredential string `json:"-" gorm:"type:text"`                  // AES加密的密码或私钥
	Passphrase          string `json:"-" gorm:"type:text"`                  // 私钥密码(加密)
	CreatedAt           int64  `json:"created_at" gorm:"autoCreateTime"`
	UpdatedAt           int64  `json:"updated_at" gorm:"autoUpdateTime"`
	LastUsedAt          int64  `json:"last_used_at"`
}

Connection SSH连接配置(持久化存储)

func (Connection) TableName

func (Connection) TableName() string

TableName 指定表名

type ControlMessage

type ControlMessage struct {
	Type string `json:"type"`
	Cols uint16 `json:"cols,omitempty"`
	Rows uint16 `json:"rows,omitempty"`
}

ControlMessage WebSocket 控制消息

type CreateConnectionRequest

type CreateConnectionRequest struct {
	Name       string `json:"name" binding:"required,max=100"`
	Host       string `json:"host" binding:"required,max=255"`
	Port       int    `json:"port" binding:"omitempty,min=1,max=65535"`
	Username   string `json:"username" binding:"required,max=100"`
	AuthMethod string `json:"auth_method" binding:"required,oneof=password key"`
	Password   string `json:"password" binding:"omitempty"`    // 明文密码
	PrivateKey string `json:"private_key" binding:"omitempty"` // 私钥内容
	Passphrase string `json:"passphrase" binding:"omitempty"`  // 私钥密码
}

CreateConnectionRequest 创建连接请求

type CreateTransferRequest

type CreateTransferRequest struct {
	SessionID   string   `json:"session_id" binding:"required"`
	Type        string   `json:"type" binding:"required,oneof=upload download"`
	LocalPaths  []string `json:"local_paths" binding:"required_if=Type download,omitempty"`
	RemotePaths []string `json:"remote_paths" binding:"required_if=Type upload,omitempty"`
	LocalDir    string   `json:"local_dir" binding:"required_if=Type download,omitempty"`
	RemoteDir   string   `json:"remote_dir" binding:"required_if=Type upload,omitempty"`
}

CreateTransferRequest 创建传输任务请求

type DeleteRequest

type DeleteRequest struct {
	Paths []string `json:"paths" binding:"required,min=1"`
}

DeleteRequest 删除请求

type DownloadRequest

type DownloadRequest struct {
	RemotePaths []string `json:"remote_paths" binding:"required,min=1"`
	LocalDir    string   `json:"local_dir" binding:"required"`
}

DownloadRequest 下载请求

type FileInfo

type FileInfo struct {
	Name    string `json:"name"`
	Path    string `json:"path"`
	Size    int64  `json:"size"`
	Mode    string `json:"mode"`
	ModTime int64  `json:"mod_time"`
	IsDir   bool   `json:"is_dir"`
	IsLink  bool   `json:"is_link"`
}

FileInfo SFTP文件信息

type Handler

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

Handler HTTP 处理器

func NewHandler

func NewHandler(service *Service, logger *zap.Logger) *Handler

NewHandler 创建处理器

func (*Handler) CancelTransfer

func (h *Handler) CancelTransfer(c *gin.Context)

CancelTransfer 取消传输任务

func (*Handler) ClearTransfers

func (h *Handler) ClearTransfers(c *gin.Context)

ClearTransfers 清除已完成的任务

func (*Handler) CloseSession

func (h *Handler) CloseSession(c *gin.Context)

CloseSession 关闭会话

func (*Handler) CreateConnection

func (h *Handler) CreateConnection(c *gin.Context)

CreateConnection 创建SSH连接配置

func (*Handler) CreateSession

func (h *Handler) CreateSession(c *gin.Context)

CreateSession 创建SSH会话

func (*Handler) CreateTransfer

func (h *Handler) CreateTransfer(c *gin.Context)

CreateTransfer 创建传输任务

func (*Handler) Delete

func (h *Handler) Delete(c *gin.Context)

Delete 删除文件/目录

func (*Handler) DeleteConnection

func (h *Handler) DeleteConnection(c *gin.Context)

DeleteConnection 删除连接配置

func (*Handler) Download

func (h *Handler) Download(c *gin.Context)

Download 下载文件到本地

func (*Handler) GetConnection

func (h *Handler) GetConnection(c *gin.Context)

GetConnection 获取单个连接配置

func (*Handler) ListConnections

func (h *Handler) ListConnections(c *gin.Context)

ListConnections 获取所有连接配置

func (*Handler) ListDir

func (h *Handler) ListDir(c *gin.Context)

ListDir 列出目录

func (*Handler) ListSessions

func (h *Handler) ListSessions(c *gin.Context)

ListSessions 获取所有会话

func (*Handler) ListTransfers

func (h *Handler) ListTransfers(c *gin.Context)

ListTransfers 列出传输任务

func (*Handler) Mkdir

func (h *Handler) Mkdir(c *gin.Context)

Mkdir 创建目录

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(router *gin.RouterGroup, tokenManager *auth.TokenManager)

RegisterRoutes 注册路由

func (*Handler) Rename

func (h *Handler) Rename(c *gin.Context)

Rename 重命名/移动

func (*Handler) Resize

func (h *Handler) Resize(c *gin.Context)

Resize 调整终端大小

func (*Handler) Stat

func (h *Handler) Stat(c *gin.Context)

Stat 获取文件信息

func (*Handler) TestConnection

func (h *Handler) TestConnection(c *gin.Context)

TestConnection 测试连接(不保存)

func (*Handler) TestConnectionByID

func (h *Handler) TestConnectionByID(c *gin.Context)

TestConnectionByID 测试已保存的连接

func (*Handler) TransferProgressWS

func (h *Handler) TransferProgressWS(c *gin.Context)

TransferProgressWS 传输进度 WebSocket

func (*Handler) UpdateConnection

func (h *Handler) UpdateConnection(c *gin.Context)

UpdateConnection 更新连接配置

func (*Handler) Upload

func (h *Handler) Upload(c *gin.Context)

Upload 上传文件(multipart)

func (*Handler) WebSocket

func (h *Handler) WebSocket(c *gin.Context)

WebSocket 处理WebSocket连接

type ListDirRequest

type ListDirRequest struct {
	Path string `json:"path" binding:"required"`
}

ListDirRequest 列目录请求

type MkdirRequest

type MkdirRequest struct {
	Path string `json:"path" binding:"required"`
}

MkdirRequest 创建目录请求

type Module

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

Module SSH模块实现

func New

func New() *Module

New 创建模块实例

func (*Module) Dependencies

func (m *Module) Dependencies() []string

Dependencies 返回依赖的模块

func (*Module) GetService

func (m *Module) GetService() *Service

GetService 获取服务实例

func (*Module) ID

func (m *Module) ID() string

ID 返回模块唯一标识

func (*Module) Init

func (m *Module) Init(ctx *module.Context) error

Init 初始化模块

func (*Module) Name

func (m *Module) Name() string

Name 返回模块名称

func (*Module) RegisterRoutes

func (m *Module) RegisterRoutes(router *gin.RouterGroup)

RegisterRoutes 注册路由

func (*Module) Start

func (m *Module) Start() error

Start 启动模块

func (*Module) Stop

func (m *Module) Stop() error

Stop 停止模块

func (*Module) Version

func (m *Module) Version() string

Version 返回模块版本

type RenameRequest

type RenameRequest struct {
	OldPath string `json:"old_path" binding:"required"`
	NewPath string `json:"new_path" binding:"required"`
}

RenameRequest 重命名请求

type ResizeRequest

type ResizeRequest struct {
	Cols uint16 `json:"cols" binding:"required,min=20,max=500"`
	Rows uint16 `json:"rows" binding:"required,min=5,max=200"`
}

ResizeRequest 调整终端大小

type Service

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

Service SSH服务

func NewService

func NewService(db *gorm.DB, encryptionKey string, logger *zap.Logger) *Service

NewService 创建服务实例

func (*Service) CancelTransferTask

func (s *Service) CancelTransferTask(taskID string) error

CancelTransferTask 取消传输任务

func (*Service) CheckPort

func (s *Service) CheckPort(host string, port int) error

CheckPort 检查端口是否可达

func (*Service) ClearCompletedTasks

func (s *Service) ClearCompletedTasks()

ClearCompletedTasks 清除已完成的任务

func (*Service) CloseSession

func (s *Service) CloseSession(sessionID string) error

CloseSession 关闭会话

func (*Service) Connect

func (s *Service) Connect(connID string, cols, rows uint16) (*Session, error)

Connect 建立SSH连接

func (*Service) CreateConnection

func (s *Service) CreateConnection(req *CreateConnectionRequest) (*Connection, error)

CreateConnection 创建SSH连接配置

func (*Service) CreateTransferTask

func (s *Service) CreateTransferTask(sessionID, taskType, localPath, remotePath, fileName string, size int64) (*TransferTask, error)

CreateTransferTask 创建传输任务

func (*Service) Delete

func (s *Service) Delete(sessionID string, paths []string) error

Delete 删除文件或目录

func (*Service) DeleteConnection

func (s *Service) DeleteConnection(id string) error

DeleteConnection 删除SSH连接配置

func (*Service) DownloadFile

func (s *Service) DownloadFile(sessionID, remotePath, localPath string, progressChan chan<- int64) error

DownloadFile 下载单个文件

func (*Service) GetConnection

func (s *Service) GetConnection(id string) (*Connection, error)

GetConnection 获取单个连接配置

func (*Service) GetProgressChannel

func (s *Service) GetProgressChannel() <-chan *TransferProgress

GetProgressChannel 获取进度通知通道

func (*Service) GetSession

func (s *Service) GetSession(sessionID string) (*Session, error)

GetSession 获取会话

func (*Service) GetTransferTask

func (s *Service) GetTransferTask(taskID string) (*TransferTask, error)

GetTransferTask 获取传输任务

func (*Service) ListConnections

func (s *Service) ListConnections() ([]Connection, error)

ListConnections 获取所有连接配置

func (*Service) ListDir

func (s *Service) ListDir(sessionID, path string) ([]FileInfo, error)

ListDir 列出目录内容

func (*Service) ListSessions

func (s *Service) ListSessions() []*SessionInfo

ListSessions 获取所有会话

func (*Service) ListTransferTasks

func (s *Service) ListTransferTasks(sessionID string) []*TransferTask

ListTransferTasks 列出所有传输任务

func (*Service) Mkdir

func (s *Service) Mkdir(sessionID, path string) error

Mkdir 创建目录

func (*Service) Rename

func (s *Service) Rename(sessionID, oldPath, newPath string) error

Rename 重命名/移动文件

func (*Service) ResizeTerminal

func (s *Service) ResizeTerminal(sessionID string, cols, rows uint16) error

ResizeTerminal 调整终端大小

func (*Service) Stat

func (s *Service) Stat(sessionID, path string) (*FileInfo, error)

Stat 获取文件信息

func (*Service) Stop

func (s *Service) Stop()

Stop 停止服务,关闭所有会话

func (*Service) TestConnection

func (s *Service) TestConnection(req *TestConnectionRequest) error

TestConnection 测试SSH连接

func (*Service) UpdateConnection

func (s *Service) UpdateConnection(id string, req *UpdateConnectionRequest) (*Connection, error)

UpdateConnection 更新SSH连接配置

func (*Service) UploadFile

func (s *Service) UploadFile(sessionID, localPath, remotePath string, progressChan chan<- int64) error

UploadFile 上传单个文件

func (*Service) UploadFromReader

func (s *Service) UploadFromReader(sessionID, remotePath string, reader io.Reader, size int64) error

UploadFromReader 从Reader上传文件

type Session

type Session struct {
	ID           string    `json:"id"`
	ConnectionID string    `json:"connection_id"`
	ConnName     string    `json:"conn_name"` // 连接名称,用于显示
	Host         string    `json:"host"`
	Username     string    `json:"username"`
	CreatedAt    time.Time `json:"created_at"`
	LastActivity time.Time `json:"last_activity"`
	// contains filtered or unexported fields
}

Session SSH会话(内存中)

func (*Session) ToInfo

func (s *Session) ToInfo() *SessionInfo

ToInfo 转换为会话信息

type SessionInfo

type SessionInfo struct {
	ID           string    `json:"id"`
	ConnectionID string    `json:"connection_id"`
	ConnName     string    `json:"conn_name"`
	Host         string    `json:"host"`
	Username     string    `json:"username"`
	CreatedAt    time.Time `json:"created_at"`
	LastActivity time.Time `json:"last_activity"`
}

SessionInfo 会话信息(用于列表返回)

type TestConnectionRequest

type TestConnectionRequest struct {
	Host       string `json:"host" binding:"required"`
	Port       int    `json:"port" binding:"omitempty,min=1,max=65535"`
	Username   string `json:"username" binding:"required"`
	AuthMethod string `json:"auth_method" binding:"required,oneof=password key"`
	Password   string `json:"password" binding:"omitempty"`
	PrivateKey string `json:"private_key" binding:"omitempty"`
	Passphrase string `json:"passphrase" binding:"omitempty"`
}

TestConnectionRequest 测试连接请求

type TransferProgress

type TransferProgress struct {
	TaskID      string  `json:"task_id"`
	FileName    string  `json:"file_name"`
	Type        string  `json:"type"`
	Size        int64   `json:"size"`
	Transferred int64   `json:"transferred"`
	Speed       float64 `json:"speed"` // bytes/s
	Status      string  `json:"status"`
	Error       string  `json:"error,omitempty"`
}

TransferProgress 传输进度(WebSocket推送)

type TransferQueue

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

TransferQueue 传输队列

type TransferTask

type TransferTask struct {
	ID          string `json:"id"`
	SessionID   string `json:"session_id"`
	Type        string `json:"type"` // upload | download
	LocalPath   string `json:"local_path"`
	RemotePath  string `json:"remote_path"`
	FileName    string `json:"file_name"`
	Size        int64  `json:"size"`
	Transferred int64  `json:"transferred"`
	Status      string `json:"status"` // pending | running | done | failed | cancelled
	Error       string `json:"error,omitempty"`
	CreatedAt   int64  `json:"created_at"`
	StartedAt   int64  `json:"started_at,omitempty"`
	FinishedAt  int64  `json:"finished_at,omitempty"`
}

TransferTask 文件传输任务

type UpdateConnectionRequest

type UpdateConnectionRequest struct {
	Name       string `json:"name" binding:"omitempty,max=100"`
	Host       string `json:"host" binding:"omitempty,max=255"`
	Port       int    `json:"port" binding:"omitempty,min=1,max=65535"`
	Username   string `json:"username" binding:"omitempty,max=100"`
	AuthMethod string `json:"auth_method" binding:"omitempty,oneof=password key"`
	Password   string `json:"password" binding:"omitempty"`
	PrivateKey string `json:"private_key" binding:"omitempty"`
	Passphrase string `json:"passphrase" binding:"omitempty"`
}

UpdateConnectionRequest 更新连接请求

type UploadRequest

type UploadRequest struct {
	LocalPaths []string `json:"local_paths" binding:"required,min=1"`
	RemoteDir  string   `json:"remote_dir" binding:"required"`
}

UploadRequest 上传请求(用于非multipart场景)

Jump to

Keyboard shortcuts

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