feat: 后端: 在建立 SSH 连接时应用代理配置

This commit is contained in:
Baobhan Sith
2025-04-15 07:31:25 +08:00
parent 0e863456a2
commit 4f2f8b9f07
19 changed files with 1444 additions and 197 deletions
@@ -23,7 +23,8 @@ interface ConnectionInfoBase {
* 创建新连接 (POST /api/v1/connections)
*/
export const createConnection = async (req: Request, res: Response): Promise<void> => {
const { name, host, port = 22, username, auth_method, password, private_key, passphrase } = req.body;
// 新增 proxy_id
const { name, host, port = 22, username, auth_method, password, private_key, passphrase, proxy_id } = req.body;
const userId = req.session.userId; // 从会话获取用户 ID
// 输入验证 (基础)
@@ -67,13 +68,14 @@ export const createConnection = async (req: Request, res: Response): Promise<voi
// 插入数据库
const result = await new Promise<{ lastID: number }>((resolve, reject) => {
const stmt = db.prepare(
`INSERT INTO connections (name, host, port, username, auth_method, encrypted_password, encrypted_private_key, encrypted_passphrase, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
`INSERT INTO connections (name, host, port, username, auth_method, encrypted_password, encrypted_private_key, encrypted_passphrase, proxy_id, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` // 添加 proxy_id
);
// 注意:这里没有存储 userId,因为 MVP 只有一个用户。如果未来支持多用户,需要添加 user_id 字段。
stmt.run(
name, host, port, username, auth_method,
encryptedPassword, encryptedPrivateKey, encryptedPassphrase,
proxy_id ?? null, // 如果未提供则设为 null
now, now,
function (this: Statement, err: Error | null) {
if (err) {
@@ -87,11 +89,13 @@ export const createConnection = async (req: Request, res: Response): Promise<voi
});
// 返回成功响应 (不包含敏感信息)
// 返回成功响应 (包含 proxy_id)
res.status(201).json({
message: '连接创建成功。',
connection: {
id: result.lastID,
name, host, port, username, auth_method,
proxy_id: proxy_id ?? null, // 返回 proxy_id
created_at: now, updated_at: now, last_connected_at: null
}
});
@@ -111,12 +115,13 @@ export const getConnections = async (req: Request, res: Response): Promise<void>
try {
// 查询数据库,排除敏感字段 encrypted_password, encrypted_private_key, encrypted_passphrase
// 注意:如果未来支持多用户,需要添加 WHERE user_id = ? 条件
const connections = await new Promise<ConnectionInfoBase[]>((resolve, reject) => {
// 新增:包含 proxy_id
const connections = await new Promise<(ConnectionInfoBase & { proxy_id: number | null })[]>((resolve, reject) => {
db.all(
`SELECT id, name, host, port, username, auth_method, created_at, updated_at, last_connected_at
`SELECT id, name, host, port, username, auth_method, proxy_id, created_at, updated_at, last_connected_at
FROM connections
ORDER BY name ASC`, // 按名称排序
(err, rows: ConnectionInfoBase[]) => { // 使用更新后的接口
ORDER BY name ASC`,
(err, rows: (ConnectionInfoBase & { proxy_id: number | null })[]) => {
if (err) {
console.error('查询连接列表时出错:', err.message);
return reject(new Error('获取连接列表失败'));
@@ -149,13 +154,14 @@ export const getConnectionById = async (req: Request, res: Response): Promise<vo
try {
// 查询数据库,排除敏感字段
// 注意:如果未来支持多用户,需要添加 AND user_id = ? 条件
const connection = await new Promise<ConnectionInfoBase | null>((resolve, reject) => {
// 新增:包含 proxy_id
const connection = await new Promise<(ConnectionInfoBase & { proxy_id: number | null }) | null>((resolve, reject) => {
db.get(
`SELECT id, name, host, port, username, auth_method, created_at, updated_at, last_connected_at
`SELECT id, name, host, port, username, auth_method, proxy_id, created_at, updated_at, last_connected_at
FROM connections
WHERE id = ?`,
[connectionId],
(err, row: ConnectionInfoBase) => { // 使用更新后的接口
(err, row: (ConnectionInfoBase & { proxy_id: number | null })) => {
if (err) {
console.error(`查询连接 ${connectionId} 时出错:`, err.message);
return reject(new Error('获取连接信息失败'));
@@ -182,7 +188,8 @@ export const getConnectionById = async (req: Request, res: Response): Promise<vo
*/
export const updateConnection = async (req: Request, res: Response): Promise<void> => {
const connectionId = parseInt(req.params.id, 10);
const { name, host, port, username, auth_method, password, private_key, passphrase } = req.body;
// 新增 proxy_id
const { name, host, port, username, auth_method, password, private_key, passphrase, proxy_id } = req.body;
const userId = req.session.userId;
if (isNaN(connectionId)) {
@@ -191,7 +198,8 @@ export const updateConnection = async (req: Request, res: Response): Promise<voi
}
// 输入验证 (与创建类似,但允许部分更新)
if (!name && !host && port === undefined && !username && !auth_method && !password && !private_key && passphrase === undefined) {
// 更新验证逻辑以包含 proxy_id
if (!name && !host && port === undefined && !username && !auth_method && !password && !private_key && passphrase === undefined && proxy_id === undefined) {
res.status(400).json({ message: '没有提供要更新的字段。' });
return;
}
@@ -200,13 +208,37 @@ export const updateConnection = async (req: Request, res: Response): Promise<voi
return;
}
// 如果提供了 auth_method,需要确保对应的凭证也提供了或已存在
// (更复杂的验证逻辑可能需要先查询现有记录)
// (更复杂的验证逻辑可能需要先查询现有记录) - 现在实现它
try {
// 1. 先查询当前的连接信息
const currentConnection = await new Promise<(ConnectionInfoBase & { encrypted_password?: string | null, encrypted_private_key?: string | null, encrypted_passphrase?: string | null, proxy_id?: number | null }) | null>((resolve, reject) => {
// 注意:需要查询加密字段以进行比较和保留
db.get(
`SELECT id, name, host, port, username, auth_method, encrypted_password, encrypted_private_key, encrypted_passphrase, proxy_id
FROM connections
WHERE id = ?`,
[connectionId],
(err, row: any) => { // 使用 any 避免类型冲突,或定义更完整的接口
if (err) {
console.error(`查询连接 ${connectionId} 时出错:`, err.message);
return reject(new Error('获取连接信息失败'));
}
resolve(row || null);
}
);
});
if (!currentConnection) {
res.status(404).json({ message: '连接未找到。' });
return;
}
const fieldsToUpdate: { [key: string]: any } = {};
const params: any[] = [];
let newAuthMethod = auth_method || currentConnection.auth_method; // 确定最终的认证方式
// 构建要更新的字段和参数
// 构建要更新的非敏感字段和参数
if (name !== undefined) { fieldsToUpdate.name = name; params.push(name); }
if (host !== undefined) { fieldsToUpdate.host = host; params.push(host); }
if (port !== undefined) {
@@ -217,55 +249,69 @@ export const updateConnection = async (req: Request, res: Response): Promise<voi
fieldsToUpdate.port = port; params.push(port);
}
if (username !== undefined) { fieldsToUpdate.username = username; params.push(username); }
// 新增:处理 proxy_id 更新 (允许设为 null)
if (proxy_id !== undefined) { fieldsToUpdate.proxy_id = proxy_id; params.push(proxy_id ?? null); }
// 处理认证方式和凭证更新
if (auth_method) {
// --- 处理认证方式和凭证更新 (重构逻辑) ---
if (auth_method && auth_method !== currentConnection.auth_method) {
// --- Case 1: 认证方式已改变 ---
fieldsToUpdate.auth_method = auth_method;
params.push(auth_method);
if (auth_method === 'password') {
// 切换到密码认证
if (!password) {
res.status(400).json({ message: '更新为密码认证时需要提供 password。' });
// 必须提供密码才能切换
res.status(400).json({ message: '切换到密码认证时需要提供 password。' });
return;
}
fieldsToUpdate.encrypted_password = encrypt(password);
params.push(fieldsToUpdate.encrypted_password);
fieldsToUpdate.encrypted_private_key = null; // 清除旧密钥
// 清除旧密钥信息
fieldsToUpdate.encrypted_private_key = null;
params.push(null);
fieldsToUpdate.encrypted_passphrase = null; // 清除旧密码
fieldsToUpdate.encrypted_passphrase = null;
params.push(null);
} else if (auth_method === 'key') {
} else { // auth_method === 'key'
// 切换到密钥认证
if (!private_key) {
res.status(400).json({ message: '更新为密钥认证时需要提供 private_key。' });
// 必须提供私钥才能切换
res.status(400).json({ message: '切换到密钥认证时需要提供 private_key。' });
return;
}
fieldsToUpdate.encrypted_private_key = encrypt(private_key);
params.push(fieldsToUpdate.encrypted_private_key);
// 密码短语是可选的
fieldsToUpdate.encrypted_passphrase = passphrase ? encrypt(passphrase) : null;
params.push(fieldsToUpdate.encrypted_passphrase);
fieldsToUpdate.encrypted_password = null; // 清除旧密码
// 清除旧密码信息
fieldsToUpdate.encrypted_password = null;
params.push(null);
}
} else {
// 如果只更新凭证而不改变 auth_method (需要先查询当前 auth_method)
// 为了简化,这里假设如果提供了 password/private_key,则 auth_method 也被提供了
// 或者,可以先查询记录再决定如何更新
if (password) {
// 假设当前是 password 方式或要切换到 password
fieldsToUpdate.encrypted_password = encrypt(password);
params.push(fieldsToUpdate.encrypted_password);
}
if (private_key) {
// 假设当前是 key 方式或要切换到 key
fieldsToUpdate.encrypted_private_key = encrypt(private_key);
params.push(fieldsToUpdate.encrypted_private_key);
fieldsToUpdate.encrypted_passphrase = passphrase ? encrypt(passphrase) : null;
params.push(fieldsToUpdate.encrypted_passphrase);
} else if (passphrase !== undefined && auth_method === 'key') { // 仅更新 passphrase
fieldsToUpdate.encrypted_passphrase = passphrase ? encrypt(passphrase) : null;
params.push(fieldsToUpdate.encrypted_passphrase);
// --- Case 2: 认证方式未改变 (或请求中未指定 auth_method) ---
// 仅当提供了新的凭证时才更新
if (currentConnection.auth_method === 'password') {
if (password) { // 如果提供了新密码
fieldsToUpdate.encrypted_password = encrypt(password);
params.push(fieldsToUpdate.encrypted_password);
}
// 如果没提供新密码,则不更新密码字段,保留旧密码
} else if (currentConnection.auth_method === 'key') {
if (private_key) { // 如果提供了新私钥
fieldsToUpdate.encrypted_private_key = encrypt(private_key);
params.push(fieldsToUpdate.encrypted_private_key);
// 如果提供了新私钥,则密码短语也必须一起更新(即使是清空)
fieldsToUpdate.encrypted_passphrase = passphrase ? encrypt(passphrase) : null;
params.push(fieldsToUpdate.encrypted_passphrase);
} else if (passphrase !== undefined) { // 如果只提供了密码短语 (允许清空)
fieldsToUpdate.encrypted_passphrase = passphrase ? encrypt(passphrase) : null;
params.push(fieldsToUpdate.encrypted_passphrase);
}
// 如果私钥和密码短语都未提供,则不更新这两个字段,保留旧值
}
}
// --- 凭证处理结束 ---
const now = Math.floor(Date.now() / 1000);
fieldsToUpdate.updated_at = now;
@@ -304,10 +350,11 @@ export const updateConnection = async (req: Request, res: Response): Promise<voi
// 获取更新后的信息(不含敏感数据)并返回
const updatedConnection = await new Promise<ConnectionInfoBase | null>((resolve, reject) => {
db.get(
`SELECT id, name, host, port, username, auth_method, created_at, updated_at, last_connected_at
// 新增:包含 proxy_id
`SELECT id, name, host, port, username, auth_method, proxy_id, created_at, updated_at, last_connected_at
FROM connections WHERE id = ?`,
[connectionId],
(err, row: ConnectionInfoBase) => err ? reject(err) : resolve(row || null)
(err, row: ConnectionInfoBase & { proxy_id: number | null }) => err ? reject(err) : resolve(row || null)
);
});
res.status(200).json({ message: '连接更新成功。', connection: updatedConnection });
+8 -6
View File
@@ -9,9 +9,10 @@ import bcrypt from 'bcrypt'; // 引入 bcrypt 用于哈希密码
import { getDb } from './database';
import { runMigrations } from './migrations';
import authRouter from './auth/auth.routes'; // 导入认证路由
import connectionsRouter from './connections/connections.routes'; // 导入连接路由
import sftpRouter from './sftp/sftp.routes'; // 导入 SFTP 路由
import { initializeWebSocket } from './websocket'; // 导入 WebSocket 初始化函数
import connectionsRouter from './connections/connections.routes';
import sftpRouter from './sftp/sftp.routes';
import proxyRoutes from './proxies/proxies.routes'; // 导入代理路由
import { initializeWebSocket } from './websocket';
// 基础 Express 应用设置 (后续会扩展)
const app = express();
@@ -79,9 +80,10 @@ declare module 'express-session' {
const port = process.env.PORT || 3001; // 示例端口,可配置
// --- API 路由 ---
app.use('/api/v1/auth', authRouter); // 挂载认证相关的路由
app.use('/api/v1/connections', connectionsRouter); // 挂载连接相关的路由
app.use('/api/v1/sftp', sftpRouter); // 挂载 SFTP 相关的路由
app.use('/api/v1/auth', authRouter);
app.use('/api/v1/connections', connectionsRouter);
app.use('/api/v1/sftp', sftpRouter);
app.use('/api/v1/proxies', proxyRoutes); // 挂载代理相关的路由
// 状态检查接口
app.get('/api/v1/status', (req: Request, res: Response) => {
+34 -6
View File
@@ -22,17 +22,32 @@ CREATE TABLE IF NOT EXISTS connections (
username TEXT NOT NULL,
auth_method TEXT NOT NULL CHECK(auth_method IN ('password', 'key')), -- 更新 CHECK 约束
encrypted_password TEXT NULL,
encrypted_private_key TEXT NULL, -- 取消注释
encrypted_passphrase TEXT NULL, -- 取消注释
-- proxy_id INTEGER NULL, -- 代理相关字段 (暂未实现)
encrypted_private_key TEXT NULL,
encrypted_passphrase TEXT NULL,
proxy_id INTEGER NULL, -- 新增:关联的代理 ID
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL,
last_connected_at INTEGER NULL
last_connected_at INTEGER NULL,
FOREIGN KEY (proxy_id) REFERENCES proxies(id) ON DELETE SET NULL -- 设置外键约束,删除代理时将关联设为 NULL
);
`;
// 新增:创建 proxies 表的 SQL
const createProxiesTableSQL = `
CREATE TABLE IF NOT EXISTS proxies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
type TEXT NOT NULL CHECK(type IN ('SOCKS5', 'HTTP')), -- 代理类型,目前支持 SOCKS5 和 HTTP
host TEXT NOT NULL,
port INTEGER NOT NULL,
username TEXT NULL, -- 代理认证用户名 (可选)
encrypted_password TEXT NULL, -- 加密存储的代理密码 (可选)
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
);
`;
// 未来可能需要的其他表 (根据项目文档)
// const createProxiesTableSQL = \`...\`; // 代理表
// const createTagsTableSQL = \`...\`; // 标签表
// const createConnectionTagsTableSQL = \`...\`; // 连接与标签的关联表
// const createSettingsTableSQL = \`...\`; // 设置表
@@ -122,9 +137,22 @@ export const runMigrations = async (db: Database): Promise<void> => {
await addColumnIfNotExists(db, 'connections', 'auth_method', "TEXT NOT NULL DEFAULT 'password'"); // Add default for existing rows
await addColumnIfNotExists(db, 'connections', 'encrypted_private_key', 'TEXT NULL');
await addColumnIfNotExists(db, 'connections', 'encrypted_passphrase', 'TEXT NULL');
// 新增:添加 proxy_id 列到 connections 表 (如果不存在)
// 注意:直接添加带 FOREIGN KEY 的列在旧版 SQLite 中可能有限制,但现代版本通常支持。
// 如果遇到问题,可能需要更复杂的迁移步骤(创建新表,复制数据,重命名)。
// 这里我们先尝试直接添加。ON DELETE SET NULL 意味着如果代理被删除,关联的连接不会被删除,只是 proxy_id 变为空。
await addColumnIfNotExists(db, 'connections', 'proxy_id', 'INTEGER NULL REFERENCES proxies(id) ON DELETE SET NULL');
// 创建 proxies 表 (如果不存在)
await new Promise<void>((resolve, reject) => {
db.run(createProxiesTableSQL, (err) => {
if (err) return reject(new Error(`创建 proxies 表时出错: ${err.message}`));
console.log('Proxies 表已检查/创建。');
resolve();
});
});
// Add other tables or columns here in the future
// await addColumnIfNotExists(db, 'connections', 'proxy_id', 'INTEGER NULL');
console.log('数据库迁移检查完成。');
@@ -0,0 +1,232 @@
import { Request, Response } from 'express';
import { getDb } from '../database';
import { encrypt, decrypt } from '../utils/crypto'; // 引入加解密工具
// 定义代理信息接口 (用于类型提示)
interface ProxyData {
name: string;
type: 'SOCKS5' | 'HTTP';
host: string;
port: number;
username?: string | null;
password?: string | null; // 接收原始密码
}
// 获取所有代理配置 (不含密码)
export const getAllProxies = async (req: Request, res: Response) => {
const db = getDb();
try {
// 查询所有代理,排除 encrypted_password 字段
const sql = `SELECT id, name, type, host, port, username, created_at, updated_at FROM proxies`;
const proxies = await new Promise<any[]>((resolve, reject) => {
db.all(sql, [], (err, rows) => {
if (err) {
return reject(err);
}
resolve(rows);
});
});
res.status(200).json(proxies);
} catch (error: any) {
res.status(500).json({ message: '获取代理列表失败', error: error.message });
}
};
// 获取单个代理配置 (不含密码)
export const getProxyById = async (req: Request, res: Response) => {
const db = getDb();
const { id } = req.params;
try {
// 查询单个代理,排除 encrypted_password 字段
const sql = `SELECT id, name, type, host, port, username, created_at, updated_at FROM proxies WHERE id = ?`;
const proxy = await new Promise<any>((resolve, reject) => {
db.get(sql, [id], (err, row) => {
if (err) {
return reject(err);
}
resolve(row); // 如果找不到,row 会是 undefined
});
});
if (proxy) {
res.status(200).json(proxy);
} else {
res.status(404).json({ message: `未找到 ID 为 ${id} 的代理` });
}
} catch (error: any) {
res.status(500).json({ message: `获取代理 ${id} 失败`, error: error.message });
}
};
// 创建新的代理配置
export const createProxy = async (req: Request, res: Response) => {
const db = getDb();
const { name, type, host, port, username, password }: ProxyData = req.body;
const now = Math.floor(Date.now() / 1000); // 当前时间戳 (秒)
// 基本验证
if (!name || !type || !host || !port) {
return res.status(400).json({ message: '缺少必要的代理信息 (name, type, host, port)' });
}
if (type !== 'SOCKS5' && type !== 'HTTP') {
return res.status(400).json({ message: '无效的代理类型,仅支持 SOCKS5 或 HTTP' });
}
try {
let encryptedPassword: string | null = null;
if (password) {
encryptedPassword = encrypt(password); // 加密密码
}
const sql = `INSERT INTO proxies (name, type, host, port, username, encrypted_password, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
const params = [name, type, host, port, username ?? null, encryptedPassword, now, now];
// 使用 Promise 包装 db.run 以便使用 async/await
const result = await new Promise<{ id: number } | null>((resolve, reject) => {
db.run(sql, params, function (err) { // 使用 function 获取 this.lastID
if (err) {
return reject(err);
}
// this.lastID 包含新插入行的 ID
resolve({ id: this.lastID });
});
});
if (result) {
// 返回成功消息和新创建的代理信息 (不含密码)
res.status(201).json({
message: '代理创建成功',
proxy: {
id: result.id,
name,
type,
host,
port,
username: username ?? null,
created_at: now,
updated_at: now
}
});
} else {
// 这理论上不应该发生,除非 db.run 内部逻辑问题
throw new Error('未能获取新创建代理的 ID');
}
} catch (error: any) {
if (error.message.includes('UNIQUE constraint failed')) {
// 可以添加更具体的唯一约束错误处理,例如判断是哪个字段冲突
return res.status(409).json({ message: '创建代理失败:可能存在同名字段冲突', error: error.message });
}
res.status(500).json({ message: '创建代理失败', error: error.message });
}
};
// 更新代理配置
export const updateProxy = async (req: Request, res: Response) => {
const db = getDb();
const { id } = req.params;
const { name, type, host, port, username, password }: Partial<ProxyData> = req.body;
const now = Math.floor(Date.now() / 1000);
// 验证至少有一个字段被更新
if (!name && !type && !host && port === undefined && username === undefined && password === undefined) {
return res.status(400).json({ message: '没有提供任何要更新的字段' });
}
if (type && type !== 'SOCKS5' && type !== 'HTTP') {
return res.status(400).json({ message: '无效的代理类型,仅支持 SOCKS5 或 HTTP' });
}
try {
let encryptedPasswordToUpdate: string | null | undefined = undefined; // undefined 表示不更新密码
if (password !== undefined) { // 检查 password 字段是否存在于请求体中
encryptedPasswordToUpdate = password ? encrypt(password) : null; // 如果提供了新密码则加密,如果提供空字符串或 null 则设为 null
}
// 构建动态 SQL 更新语句
const fieldsToUpdate: string[] = [];
const params: any[] = [];
if (name !== undefined) { fieldsToUpdate.push('name = ?'); params.push(name); }
if (type !== undefined) { fieldsToUpdate.push('type = ?'); params.push(type); }
if (host !== undefined) { fieldsToUpdate.push('host = ?'); params.push(host); }
if (port !== undefined) { fieldsToUpdate.push('port = ?'); params.push(port); }
// username 可以设为 null
if (username !== undefined) { fieldsToUpdate.push('username = ?'); params.push(username ?? null); }
// 只有当 password 在请求体中明确提供了 (包括空字符串或 null),才更新密码字段
if (encryptedPasswordToUpdate !== undefined) {
fieldsToUpdate.push('encrypted_password = ?');
params.push(encryptedPasswordToUpdate);
}
// 总是更新 updated_at 时间戳
fieldsToUpdate.push('updated_at = ?');
params.push(now);
// 添加 WHERE 条件的参数
params.push(id);
const sql = `UPDATE proxies SET ${fieldsToUpdate.join(', ')} WHERE id = ?`;
const result = await new Promise<{ changes: number }>((resolve, reject) => {
db.run(sql, params, function (err) { // 使用 function 获取 this.changes
if (err) {
return reject(err);
}
// this.changes 包含受影响的行数
resolve({ changes: this.changes });
});
});
if (result.changes > 0) {
// 更新成功后,获取更新后的代理信息 (不含密码) 并返回
const updatedProxy = await new Promise<any>((resolve, reject) => {
db.get(`SELECT id, name, type, host, port, username, created_at, updated_at FROM proxies WHERE id = ?`, [id], (err, row) => {
if (err) return reject(err);
resolve(row);
});
});
if (updatedProxy) {
res.status(200).json({ message: '代理更新成功', proxy: updatedProxy });
} else {
// 理论上更新成功后应该能找到,除非并发删除了
res.status(404).json({ message: `更新成功,但未能找到 ID 为 ${id} 的代理` });
}
} else {
// 如果 changes 为 0,说明没有找到对应 ID 的代理
res.status(404).json({ message: `未找到 ID 为 ${id} 的代理进行更新` });
}
} catch (error: any) {
if (error.message.includes('UNIQUE constraint failed')) {
return res.status(409).json({ message: '更新代理失败:可能存在同名字段冲突', error: error.message });
}
res.status(500).json({ message: `更新代理 ${id} 失败`, error: error.message });
}
};
// 删除代理配置
export const deleteProxy = async (req: Request, res: Response) => {
const db = getDb();
const { id } = req.params;
try {
const sql = `DELETE FROM proxies WHERE id = ?`;
const result = await new Promise<{ changes: number }>((resolve, reject) => {
db.run(sql, [id], function (err) { // 使用 function 获取 this.changes
if (err) {
return reject(err);
}
resolve({ changes: this.changes });
});
});
if (result.changes > 0) {
res.status(200).json({ message: `代理 ${id} 删除成功` });
} else {
// 如果 changes 为 0,说明没有找到对应 ID 的代理
res.status(404).json({ message: `未找到 ID 为 ${id} 的代理进行删除` });
}
} catch (error: any) {
res.status(500).json({ message: `删除代理 ${id} 失败`, error: error.message });
}
};
@@ -0,0 +1,24 @@
import express, { RequestHandler } from 'express'; // 引入 RequestHandler
import { isAuthenticated } from '../auth/auth.middleware';
import {
getAllProxies,
getProxyById,
createProxy,
updateProxy,
deleteProxy
} from './proxies.controller'; // 引入控制器函数
const router = express.Router();
// 应用认证中间件到所有代理路由
router.use(isAuthenticated);
// 定义代理 CRUD 路由
// 显式类型断言以解决潜在的类型不匹配问题
router.get('/', getAllProxies as RequestHandler);
router.get('/:id', getProxyById as RequestHandler);
router.post('/', createProxy as RequestHandler);
router.put('/:id', updateProxy as RequestHandler); // 类型断言
router.delete('/:id', deleteProxy as RequestHandler); // 类型断言
export default router;
+218 -92
View File
@@ -6,6 +6,8 @@ import { WriteStream } from 'fs'; // 需要 WriteStream 类型 (虽然 ssh2 的
import { getDb } from './database'; // 引入数据库实例
import { decrypt } from './utils/crypto'; // 引入解密函数
import path from 'path'; // 需要 path
import { HttpsProxyAgent } from 'https-proxy-agent'; // 引入 HTTP 代理支持
import { SocksClient } from 'socks'; // 引入 SOCKS 代理支持
// 扩展 WebSocket 类型以包含会话和 SSH/SFTP 连接信息
interface AuthenticatedWebSocket extends WebSocket {
@@ -25,21 +27,32 @@ export const activeSshConnections = new Map<AuthenticatedWebSocket, { client: Cl
// 注意:WriteStream 类型来自 'fs',但 ssh2 的流行为类似
const activeUploads = new Map<string, WriteStream>();
// 数据库连接信息接口 (包含所有可能的凭证字段)
// 数据库连接信息接口 (包含所有可能的凭证字段和 proxy_id)
interface DbConnectionInfo {
id: number;
name: string;
host: string;
port: number;
username: string;
auth_method: 'password' | 'key'; // 支持密码或密钥
auth_method: 'password' | 'key';
encrypted_password?: string | null;
encrypted_private_key?: string | null;
encrypted_passphrase?: string | null;
// proxy_id: number | null; // 待添加代理支持
proxy_id?: number | null; // 关联的代理 ID
// 其他字段...
}
// 新增:数据库代理信息接口
interface DbProxyInfo {
id: number;
name: string;
type: 'SOCKS5' | 'HTTP';
host: string;
port: number;
username?: string | null;
encrypted_password?: string | null;
}
/**
* 清理指定 WebSocket 连接关联的 SSH 资源
@@ -63,7 +76,7 @@ const cleanupSshConnection = (ws: AuthenticatedWebSocket) => {
};
// --- 状态获取相关 ---
const STATUS_POLL_INTERVAL = 5000; // 每 5 秒获取一次状态
const STATUS_POLL_INTERVAL = 1000; // 每 5 秒获取一次状态
// Helper function to execute a command and return its stdout
const executeSshCommand = (client: Client, command: string): Promise<string> => {
@@ -494,15 +507,14 @@ export const initializeWebSocket = (server: http.Server, sessionParser: RequestH
console.log(`WebSocket: 用户 ${ws.username} 请求连接到 ID: ${connectionId}`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: '正在获取连接信息...' }));
// 1. 从数据库获取连接信息 (包括所有凭证字段)
// 1. 从数据库获取连接信息 (包括 proxy_id)
const connInfo = await new Promise<DbConnectionInfo | null>((resolve, reject) => {
// 注意:如果多用户,需要验证 connectionId 是否属于当前 userId
db.get(
`SELECT id, name, host, port, username, auth_method,
`SELECT id, name, host, port, username, auth_method, proxy_id,
encrypted_password, encrypted_private_key, encrypted_passphrase
FROM connections WHERE id = ?`,
FROM connections WHERE id = ?`, // 添加 proxy_id
[connectionId],
(err, row: DbConnectionInfo) => {
(err, row: DbConnectionInfo) => { // 类型已更新
if (err) {
console.error(`查询连接 ${connectionId} 详细信息时出错:`, err);
return reject(new Error('查询连接信息失败'));
@@ -523,9 +535,35 @@ export const initializeWebSocket = (server: http.Server, sessionParser: RequestH
// return;
}
// 2. 获取代理信息 (如果 connInfo.proxy_id 存在)
let proxyInfo: DbProxyInfo | null = null;
if (connInfo.proxy_id) {
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在获取代理 ${connInfo.proxy_id} 信息...` }));
try {
proxyInfo = await new Promise<DbProxyInfo | null>((resolve, reject) => {
db.get(
`SELECT id, name, type, host, port, username, encrypted_password FROM proxies WHERE id = ?`,
[connInfo.proxy_id],
(err, row: DbProxyInfo) => {
if (err) return reject(new Error(`查询代理 ${connInfo.proxy_id} 失败: ${err.message}`));
resolve(row ?? null);
}
);
});
if (!proxyInfo) {
throw new Error(`未找到 ID 为 ${connInfo.proxy_id} 的代理配置。`);
}
console.log(`使用代理: ${proxyInfo.name} (${proxyInfo.type})`);
} catch (proxyError: any) {
console.error(`获取代理信息失败:`, proxyError);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `获取代理信息失败: ${proxyError.message}` }));
return; // 获取代理失败则停止连接
}
}
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在连接到 ${connInfo.host}...` }));
// 2. 解密凭证并构建连接配置
// 3. 解密凭证并构建连接配置
let connectConfig: any = {
host: connInfo.host,
port: connInfo.port,
@@ -558,97 +596,87 @@ export const initializeWebSocket = (server: http.Server, sessionParser: RequestH
return;
}
// 3. 建立 SSH 连接
const sshClient = new Client();
ws.sshClient = sshClient; // 关联 client
// 4. 处理代理配置(如果存在)并建立连接
const sshClient = new Client(); // 创建 SSH Client 实例
sshClient.on('ready', () => {
console.log(`SSH: 用户 ${ws.username} ${connInfo.host} 连接成功!`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: 'SSH 连接成功,正在打开 Shell...' }));
// 4. 请求 Shell 通道
sshClient.shell((err, stream) => {
if (err) {
console.error(`SSH: 用户 ${ws.username} 打开 Shell 失败:`, err);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `打开 Shell 失败: ${err.message}` }));
cleanupSshConnection(ws);
return;
if (proxyInfo) {
console.log(`WebSocket: 检测到连接 ${connInfo.id} 使用代理 ${proxyInfo.id} (${proxyInfo.type})`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在应用代理 ${proxyInfo.name}...` }));
try {
let proxyPassword = '';
if (proxyInfo.encrypted_password) {
proxyPassword = decrypt(proxyInfo.encrypted_password);
}
ws.sshShellStream = stream; // 关联 stream
// 存储活动连接 (此时 sftp 可能还未就绪)
activeSshConnections.set(ws, { client: sshClient, shell: stream });
console.log(`SSH: 用户 ${ws.username} Shell 通道已打开。`);
// 尝试初始化 SFTP 会话
sshClient.sftp((sftpErr, sftp) => {
if (sftpErr) {
console.error(`SFTP: 用户 ${ws.username} 初始化失败:`, sftpErr);
// 即使 SFTP 失败,也保持 Shell 连接,但发送错误通知
ws.send(JSON.stringify({ type: 'sftp:error', payload: `SFTP 初始化失败: ${sftpErr.message}` }));
// 不再发送 ssh:connected,因为 SFTP 也是核心功能的一部分
// ws.send(JSON.stringify({ type: 'ssh:connected' }));
// 可以在这里发送一个包含错误的状态
ws.send(JSON.stringify({ type: 'ssh:status', payload: 'Shell 已连接,但 SFTP 初始化失败。' }));
return;
if (proxyInfo.type === 'SOCKS5') {
const socksOptions = {
proxy: {
host: proxyInfo.host,
port: proxyInfo.port,
type: 5 as 5, // SOCKS 版本 5
userId: proxyInfo.username || undefined,
password: proxyPassword || undefined,
},
command: 'connect' as 'connect',
destination: {
host: connInfo.host,
port: connInfo.port,
},
timeout: connectConfig.readyTimeout ?? 20000, // 使用连接超时时间
};
console.log(`WebSocket: 正在通过 SOCKS5 代理 ${proxyInfo.host}:${proxyInfo.port} 连接到目标 ${connInfo.host}:${connInfo.port}...`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在通过 SOCKS5 代理 ${proxyInfo.name} 连接...` }));
SocksClient.createConnection(socksOptions)
.then(({ socket }) => {
console.log(`WebSocket: SOCKS5 代理连接成功。正在建立 SSH 连接...`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: 'SOCKS5 代理连接成功,正在建立 SSH...' }));
connectConfig.sock = socket; // 使用建立的 SOCKS socket
connectSshClient(ws, sshClient, connectConfig, connInfo); // 通过代理连接 SSH
})
.catch(socksError => {
console.error(`WebSocket: SOCKS5 代理连接失败:`, socksError);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `SOCKS5 代理连接失败: ${socksError.message}` }));
cleanupSshConnection(ws);
});
// 注意:对于 SOCKS5,连接逻辑在 .then 回调中处理
} else if (proxyInfo.type === 'HTTP') {
let proxyUrl = `http://`;
if (proxyInfo.username) {
proxyUrl += `${proxyInfo.username}`;
if (proxyPassword) {
proxyUrl += `:${proxyPassword}`;
}
proxyUrl += '@';
}
console.log(`SFTP: 用户 ${ws.username} 会话已初始化。`);
// 将 SFTP 实例存入 Map
const existingConn = activeSshConnections.get(ws);
if (existingConn) {
existingConn.sftp = sftp;
// SFTP 就绪后,才真正通知前端连接完成
ws.send(JSON.stringify({ type: 'ssh:connected' }));
// 启动状态轮询
startStatusPolling(ws, sshClient);
} else {
// This case should ideally not happen if the connection was set earlier
console.error(`SFTP: 无法找到用户 ${ws.username} 的活动连接记录以存储 SFTP 或启动轮询。`);
ws.send(JSON.stringify({ type: 'ssh:error', payload: '内部服务器错误:无法关联 SFTP 会话。' }));
cleanupSshConnection(ws);
}
});
// 5. 数据转发:Shell -> WebSocket (发送 Base64 编码的数据)
stream.on('data', (data: Buffer) => {
// console.log('SSH Output Buffer Length:', data.length); // Debug log
ws.send(JSON.stringify({
type: 'ssh:output',
payload: data.toString('base64'), // 将 Buffer 转为 Base64 字符串
encoding: 'base64' // 明确告知前端编码方式
}));
});
// 6. 处理 Shell 关闭
stream.on('close', () => {
console.log(`SSH: 用户 ${ws.username} Shell 通道已关闭。`);
ws.send(JSON.stringify({ type: 'ssh:disconnected', payload: 'Shell 通道已关闭。' }));
cleanupSshConnection(ws); // 清理资源
});
// Stderr 也使用 Base64 发送
stream.stderr.on('data', (data: Buffer) => {
console.error(`SSH Stderr (${ws.username}): ${data.toString('utf8').substring(0,100)}...`); // 日志中尝试 utf8 解码预览
ws.send(JSON.stringify({
type: 'ssh:output', // 同样使用 ssh:output 类型
payload: data.toString('base64'),
encoding: 'base64'
}));
});
});
}).on('error', (err) => {
console.error(`SSH: 用户 ${ws.username} 连接错误:`, err);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `SSH 连接错误: ${err.message}` }));
cleanupSshConnection(ws);
}).on('close', () => {
console.log(`SSH: 用户 ${ws.username} 连接已关闭。`);
// 确保即使 shell 没关闭,也要通知前端并清理
if (activeSshConnections.has(ws)) {
ws.send(JSON.stringify({ type: 'ssh:disconnected', payload: 'SSH 连接已关闭。' }));
proxyUrl += `${proxyInfo.host}:${proxyInfo.port}`;
console.log(`WebSocket: 为连接 ${connInfo.id} 配置 HTTP 代理: ${proxyUrl.replace(/:[^:]*@/, ':***@')}`);
connectConfig.agent = new HttpsProxyAgent(proxyUrl);
console.log(`WebSocket: 已配置 HTTP 代理。正在建立 SSH 连接...`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在通过 HTTP 代理 ${proxyInfo.name} 连接...` }));
connectSshClient(ws, sshClient, connectConfig, connInfo); // 通过代理连接 SSH
} else {
console.error(`WebSocket: 未知的代理类型: ${proxyInfo.type}`);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `未知的代理类型: ${proxyInfo.type}` }));
cleanupSshConnection(ws);
}
} catch (proxyProcessError: any) {
console.error(`处理代理 ${proxyInfo.id} 配置或凭证失败:`, proxyProcessError);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `无法处理代理配置: ${proxyProcessError.message}` }));
cleanupSshConnection(ws);
}
}).connect(connectConfig); // 使用前面构建的 connectConfig 对象
} else {
// 5. 无代理,直接连接
console.log(`WebSocket: 未配置代理。正在直接建立 SSH 连接...`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: `正在直接连接到 ${connInfo.host}...` }));
connectSshClient(ws, sshClient, connectConfig, connInfo); // 直接连接 SSH
}
break;
} // end case 'ssh:connect'
// --- 处理 SSH 输入 ---
// --- 处理 SSH 输入 ---
case 'ssh:input': {
const connection = activeSshConnections.get(ws);
@@ -1146,3 +1174,101 @@ export const initializeWebSocket = (server: http.Server, sessionParser: RequestH
console.log('WebSocket 服务器初始化完成。');
return wss;
};
// --- 辅助函数:建立 SSH 连接并处理事件 ---
function connectSshClient(ws: AuthenticatedWebSocket, sshClient: Client, connectConfig: any, connInfo: DbConnectionInfo) {
ws.sshClient = sshClient; // 关联 client
sshClient.on('ready', () => {
console.log(`SSH: 用户 ${ws.username}${connInfo.host} 连接成功!`);
ws.send(JSON.stringify({ type: 'ssh:status', payload: 'SSH 连接成功,正在打开 Shell...' }));
// 请求 Shell 通道
sshClient.shell((err, stream) => {
if (err) {
console.error(`SSH: 用户 ${ws.username} 打开 Shell 失败:`, err);
ws.send(JSON.stringify({ type: 'ssh:error', payload: `打开 Shell 失败: ${err.message}` }));
cleanupSshConnection(ws);
return;
}
ws.sshShellStream = stream; // 关联 stream
// 存储活动连接 (此时 sftp 可能还未就绪)
// 确保 client 和 shell 都存在才存储
if (activeSshConnections.has(ws)) {
// 如果已存在(例如 SOCKS 连接后),更新 shell
const existing = activeSshConnections.get(ws)!;
existing.shell = stream;
} else {
activeSshConnections.set(ws, { client: sshClient, shell: stream });
}
console.log(`SSH: 用户 ${ws.username} Shell 通道已打开。`);
// 尝试初始化 SFTP 会话
sshClient.sftp((sftpErr, sftp) => {
if (sftpErr) {
console.error(`SFTP: 用户 ${ws.username} 初始化失败:`, sftpErr);
ws.send(JSON.stringify({ type: 'sftp:error', payload: `SFTP 初始化失败: ${sftpErr.message}` }));
ws.send(JSON.stringify({ type: 'ssh:status', payload: 'Shell 已连接,但 SFTP 初始化失败。' }));
// SFTP 失败不应断开整个连接,但需要标记
const existingConn = activeSshConnections.get(ws);
if (existingConn) {
// SFTP 失败,但 Shell 仍可用,启动状态轮询
startStatusPolling(ws, sshClient);
}
return;
}
console.log(`SFTP: 用户 ${ws.username} 会话已初始化。`);
const existingConn = activeSshConnections.get(ws);
if (existingConn) {
existingConn.sftp = sftp;
ws.send(JSON.stringify({ type: 'ssh:connected' })); // SFTP 就绪后通知前端
startStatusPolling(ws, sshClient); // 启动状态轮询
} else {
console.error(`SFTP: 无法找到用户 ${ws.username} 的活动连接记录以存储 SFTP 或启动轮询。`);
ws.send(JSON.stringify({ type: 'ssh:error', payload: '内部服务器错误:无法关联 SFTP 会话。' }));
cleanupSshConnection(ws);
}
});
// 数据转发:Shell -> WebSocket
stream.on('data', (data: Buffer) => {
ws.send(JSON.stringify({
type: 'ssh:output',
payload: data.toString('base64'),
encoding: 'base64'
}));
});
// 处理 Shell 关闭
stream.on('close', () => {
console.log(`SSH: 用户 ${ws.username} Shell 通道已关闭。`);
ws.send(JSON.stringify({ type: 'ssh:disconnected', payload: 'Shell 通道已关闭。' }));
cleanupSshConnection(ws);
});
// Stderr 转发
stream.stderr.on('data', (data: Buffer) => {
console.error(`SSH Stderr (${ws.username}): ${data.toString('utf8').substring(0,100)}...`);
ws.send(JSON.stringify({
type: 'ssh:output',
payload: data.toString('base64'),
encoding: 'base64'
}));
});
});
}).on('error', (err) => {
console.error(`SSH: 用户 ${ws.username} 连接错误:`, err);
// 避免在 SOCKS 错误后重复发送错误
if (!ws.CLOSED && !ws.CLOSING) { // 检查 WebSocket 状态
ws.send(JSON.stringify({ type: 'ssh:error', payload: `SSH 连接错误: ${err.message}` }));
}
cleanupSshConnection(ws);
}).on('close', () => {
console.log(`SSH: 用户 ${ws.username} 连接已关闭。`);
if (activeSshConnections.has(ws)) {
if (!ws.CLOSED && !ws.CLOSING) {
ws.send(JSON.stringify({ type: 'ssh:disconnected', payload: 'SSH 连接已关闭。' }));
}
cleanupSshConnection(ws);
}
}).connect(connectConfig);
}