mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-03-31 08:19:54 +02:00
113 lines
3.3 KiB
Go
113 lines
3.3 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// WebShellConnection WebShell 连接配置
|
|
type WebShellConnection struct {
|
|
ID string `json:"id"`
|
|
URL string `json:"url"`
|
|
Password string `json:"password"`
|
|
Type string `json:"type"`
|
|
Method string `json:"method"`
|
|
CmdParam string `json:"cmdParam"`
|
|
Remark string `json:"remark"`
|
|
CreatedAt time.Time `json:"createdAt"`
|
|
}
|
|
|
|
// ListWebshellConnections 列出所有 WebShell 连接,按创建时间倒序
|
|
func (db *DB) ListWebshellConnections() ([]WebShellConnection, error) {
|
|
query := `
|
|
SELECT id, url, password, type, method, cmd_param, remark, created_at
|
|
FROM webshell_connections
|
|
ORDER BY created_at DESC
|
|
`
|
|
rows, err := db.Query(query)
|
|
if err != nil {
|
|
db.logger.Error("查询 WebShell 连接列表失败", zap.Error(err))
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var list []WebShellConnection
|
|
for rows.Next() {
|
|
var c WebShellConnection
|
|
err := rows.Scan(&c.ID, &c.URL, &c.Password, &c.Type, &c.Method, &c.CmdParam, &c.Remark, &c.CreatedAt)
|
|
if err != nil {
|
|
db.logger.Warn("扫描 WebShell 连接行失败", zap.Error(err))
|
|
continue
|
|
}
|
|
list = append(list, c)
|
|
}
|
|
return list, rows.Err()
|
|
}
|
|
|
|
// GetWebshellConnection 根据 ID 获取一条连接
|
|
func (db *DB) GetWebshellConnection(id string) (*WebShellConnection, error) {
|
|
query := `
|
|
SELECT id, url, password, type, method, cmd_param, remark, created_at
|
|
FROM webshell_connections WHERE id = ?
|
|
`
|
|
var c WebShellConnection
|
|
err := db.QueryRow(query, id).Scan(&c.ID, &c.URL, &c.Password, &c.Type, &c.Method, &c.CmdParam, &c.Remark, &c.CreatedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
db.logger.Error("查询 WebShell 连接失败", zap.Error(err), zap.String("id", id))
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
// CreateWebshellConnection 创建 WebShell 连接
|
|
func (db *DB) CreateWebshellConnection(c *WebShellConnection) error {
|
|
query := `
|
|
INSERT INTO webshell_connections (id, url, password, type, method, cmd_param, remark, created_at)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
`
|
|
_, err := db.Exec(query, c.ID, c.URL, c.Password, c.Type, c.Method, c.CmdParam, c.Remark, c.CreatedAt)
|
|
if err != nil {
|
|
db.logger.Error("创建 WebShell 连接失败", zap.Error(err), zap.String("id", c.ID))
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UpdateWebshellConnection 更新 WebShell 连接
|
|
func (db *DB) UpdateWebshellConnection(c *WebShellConnection) error {
|
|
query := `
|
|
UPDATE webshell_connections
|
|
SET url = ?, password = ?, type = ?, method = ?, cmd_param = ?, remark = ?
|
|
WHERE id = ?
|
|
`
|
|
result, err := db.Exec(query, c.URL, c.Password, c.Type, c.Method, c.CmdParam, c.Remark, c.ID)
|
|
if err != nil {
|
|
db.logger.Error("更新 WebShell 连接失败", zap.Error(err), zap.String("id", c.ID))
|
|
return err
|
|
}
|
|
affected, _ := result.RowsAffected()
|
|
if affected == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteWebshellConnection 删除 WebShell 连接
|
|
func (db *DB) DeleteWebshellConnection(id string) error {
|
|
result, err := db.Exec(`DELETE FROM webshell_connections WHERE id = ?`, id)
|
|
if err != nil {
|
|
db.logger.Error("删除 WebShell 连接失败", zap.Error(err), zap.String("id", id))
|
|
return err
|
|
}
|
|
affected, _ := result.RowsAffected()
|
|
if affected == 0 {
|
|
return sql.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|