Documentation
¶
Overview ¶
Package ssh SSH远程连接模块 - HTTP/WebSocket 处理器
Package ssh SSH远程连接模块 - 数据模型定义 ¶
Package ssh SSH远程连接模块 - 提供SSH终端和SFTP功能 ¶
Package ssh SSH远程连接模块 - 服务层实现
Index ¶
- Constants
- type ConnectRequest
- type Connection
- type ControlMessage
- type CreateConnectionRequest
- type CreateTransferRequest
- type DeleteRequest
- type DownloadRequest
- type FileInfo
- type Handler
- func (h *Handler) CancelTransfer(c *gin.Context)
- func (h *Handler) ClearTransfers(c *gin.Context)
- func (h *Handler) CloseSession(c *gin.Context)
- func (h *Handler) CreateConnection(c *gin.Context)
- func (h *Handler) CreateSession(c *gin.Context)
- func (h *Handler) CreateTransfer(c *gin.Context)
- func (h *Handler) Delete(c *gin.Context)
- func (h *Handler) DeleteConnection(c *gin.Context)
- func (h *Handler) Download(c *gin.Context)
- func (h *Handler) GetConnection(c *gin.Context)
- func (h *Handler) ListConnections(c *gin.Context)
- func (h *Handler) ListDir(c *gin.Context)
- func (h *Handler) ListSessions(c *gin.Context)
- func (h *Handler) ListTransfers(c *gin.Context)
- func (h *Handler) Mkdir(c *gin.Context)
- func (h *Handler) RegisterRoutes(router *gin.RouterGroup, tokenManager *auth.TokenManager)
- func (h *Handler) Rename(c *gin.Context)
- func (h *Handler) Resize(c *gin.Context)
- func (h *Handler) Stat(c *gin.Context)
- func (h *Handler) TestConnection(c *gin.Context)
- func (h *Handler) TestConnectionByID(c *gin.Context)
- func (h *Handler) TransferProgressWS(c *gin.Context)
- func (h *Handler) UpdateConnection(c *gin.Context)
- func (h *Handler) Upload(c *gin.Context)
- func (h *Handler) WebSocket(c *gin.Context)
- type ListDirRequest
- type MkdirRequest
- type Module
- func (m *Module) Dependencies() []string
- func (m *Module) GetService() *Service
- func (m *Module) ID() string
- func (m *Module) Init(ctx *module.Context) error
- func (m *Module) Name() string
- func (m *Module) RegisterRoutes(router *gin.RouterGroup)
- func (m *Module) Start() error
- func (m *Module) Stop() error
- func (m *Module) Version() string
- type RenameRequest
- type ResizeRequest
- type Service
- func (s *Service) CancelTransferTask(taskID string) error
- func (s *Service) CheckPort(host string, port int) error
- func (s *Service) ClearCompletedTasks()
- func (s *Service) CloseSession(sessionID string) error
- func (s *Service) Connect(connID string, cols, rows uint16) (*Session, error)
- func (s *Service) CreateConnection(req *CreateConnectionRequest) (*Connection, error)
- func (s *Service) CreateTransferTask(sessionID, taskType, localPath, remotePath, fileName string, size int64) (*TransferTask, error)
- func (s *Service) Delete(sessionID string, paths []string) error
- func (s *Service) DeleteConnection(id string) error
- func (s *Service) DownloadFile(sessionID, remotePath, localPath string, progressChan chan<- int64) error
- func (s *Service) GetConnection(id string) (*Connection, error)
- func (s *Service) GetProgressChannel() <-chan *TransferProgress
- func (s *Service) GetSession(sessionID string) (*Session, error)
- func (s *Service) GetTransferTask(taskID string) (*TransferTask, error)
- func (s *Service) ListConnections() ([]Connection, error)
- func (s *Service) ListDir(sessionID, path string) ([]FileInfo, error)
- func (s *Service) ListSessions() []*SessionInfo
- func (s *Service) ListTransferTasks(sessionID string) []*TransferTask
- func (s *Service) Mkdir(sessionID, path string) error
- func (s *Service) Rename(sessionID, oldPath, newPath string) error
- func (s *Service) ResizeTerminal(sessionID string, cols, rows uint16) error
- func (s *Service) Stat(sessionID, path string) (*FileInfo, error)
- func (s *Service) Stop()
- func (s *Service) TestConnection(req *TestConnectionRequest) error
- func (s *Service) UpdateConnection(id string, req *UpdateConnectionRequest) (*Connection, error)
- func (s *Service) UploadFile(sessionID, localPath, remotePath string, progressChan chan<- int64) error
- func (s *Service) UploadFromReader(sessionID, remotePath string, reader io.Reader, size int64) error
- type Session
- type SessionInfo
- type TestConnectionRequest
- type TransferProgress
- type TransferQueue
- type TransferTask
- type UpdateConnectionRequest
- type UploadRequest
Constants ¶
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连接配置(持久化存储)
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 (*Handler) CancelTransfer ¶
CancelTransfer 取消传输任务
func (*Handler) ClearTransfers ¶
ClearTransfers 清除已完成的任务
func (*Handler) CreateConnection ¶
CreateConnection 创建SSH连接配置
func (*Handler) CreateSession ¶
CreateSession 创建SSH会话
func (*Handler) CreateTransfer ¶
CreateTransfer 创建传输任务
func (*Handler) DeleteConnection ¶
DeleteConnection 删除连接配置
func (*Handler) GetConnection ¶
GetConnection 获取单个连接配置
func (*Handler) ListConnections ¶
ListConnections 获取所有连接配置
func (*Handler) ListTransfers ¶
ListTransfers 列出传输任务
func (*Handler) RegisterRoutes ¶
func (h *Handler) RegisterRoutes(router *gin.RouterGroup, tokenManager *auth.TokenManager)
RegisterRoutes 注册路由
func (*Handler) TestConnection ¶
TestConnection 测试连接(不保存)
func (*Handler) TestConnectionByID ¶
TestConnectionByID 测试已保存的连接
func (*Handler) TransferProgressWS ¶
TransferProgressWS 传输进度 WebSocket
func (*Handler) UpdateConnection ¶
UpdateConnection 更新连接配置
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 (*Module) RegisterRoutes ¶
func (m *Module) RegisterRoutes(router *gin.RouterGroup)
RegisterRoutes 注册路由
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 ¶
NewService 创建服务实例
func (*Service) CancelTransferTask ¶
CancelTransferTask 取消传输任务
func (*Service) ClearCompletedTasks ¶
func (s *Service) ClearCompletedTasks()
ClearCompletedTasks 清除已完成的任务
func (*Service) CloseSession ¶
CloseSession 关闭会话
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) DeleteConnection ¶
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 ¶
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) ListTransferTasks ¶
func (s *Service) ListTransferTasks(sessionID string) []*TransferTask
ListTransferTasks 列出所有传输任务
func (*Service) ResizeTerminal ¶
ResizeTerminal 调整终端大小
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连接配置
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会话(内存中)
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场景)