修改SSH链接属性字段
This commit is contained in:
parent
24f62c5719
commit
9325166132
@ -44,7 +44,7 @@ type ConnectionStatus = 'connecting' | 'connected' | 'reconnecting' | 'disconnec
|
|||||||
|
|
||||||
interface WebSocketMessage {
|
interface WebSocketMessage {
|
||||||
type: 'output' | 'input' | 'status' | 'error';
|
type: 'output' | 'input' | 'status' | 'error';
|
||||||
data: string;
|
data: string | { response: { type: string; data: string } }; // 支持字符串或 response 包装格式
|
||||||
timestamp?: number;
|
timestamp?: number;
|
||||||
metadata?: Record<string, any> | null;
|
metadata?: Record<string, any> | null;
|
||||||
}
|
}
|
||||||
@ -213,11 +213,34 @@ export const SSHTerminalContent: React.FC<SSHTerminalContentProps> = ({
|
|||||||
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
||||||
wsRef.current.send(JSON.stringify({
|
wsRef.current.send(JSON.stringify({
|
||||||
type: 'input',
|
type: 'input',
|
||||||
data: data,
|
data: {
|
||||||
|
request: {
|
||||||
|
type: 'input',
|
||||||
|
command: data,
|
||||||
|
}
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 监听终端尺寸变化
|
||||||
|
terminal.onResize((size) => {
|
||||||
|
console.log('📐 终端尺寸变化:', size);
|
||||||
|
if (wsRef.current?.readyState === WebSocket.OPEN) {
|
||||||
|
wsRef.current.send(JSON.stringify({
|
||||||
|
type: 'resize',
|
||||||
|
data: {
|
||||||
|
request: {
|
||||||
|
type: 'resize',
|
||||||
|
rows: size.rows,
|
||||||
|
cols: size.cols,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
console.log('📤 已发送尺寸调整消息:', size);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 延迟连接
|
// 延迟连接
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
connectWebSocket();
|
connectWebSocket();
|
||||||
@ -254,6 +277,22 @@ export const SSHTerminalContent: React.FC<SSHTerminalContentProps> = ({
|
|||||||
ws.onopen = () => {
|
ws.onopen = () => {
|
||||||
console.log('🔗 WebSocket已连接:', wsUrl);
|
console.log('🔗 WebSocket已连接:', wsUrl);
|
||||||
console.log('📺 Terminal实例存在:', !!terminalInstanceRef.current);
|
console.log('📺 Terminal实例存在:', !!terminalInstanceRef.current);
|
||||||
|
|
||||||
|
// 发送初始终端尺寸
|
||||||
|
if (terminalInstanceRef.current) {
|
||||||
|
const terminal = terminalInstanceRef.current;
|
||||||
|
ws.send(JSON.stringify({
|
||||||
|
type: 'resize',
|
||||||
|
data: {
|
||||||
|
request: {
|
||||||
|
type: 'resize',
|
||||||
|
rows: terminal.rows,
|
||||||
|
cols: terminal.cols,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
console.log('📤 已发送初始终端尺寸:', { rows: terminal.rows, cols: terminal.cols });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onmessage = (event) => {
|
ws.onmessage = (event) => {
|
||||||
@ -262,28 +301,33 @@ export const SSHTerminalContent: React.FC<SSHTerminalContentProps> = ({
|
|||||||
const msg: WebSocketMessage = JSON.parse(event.data);
|
const msg: WebSocketMessage = JSON.parse(event.data);
|
||||||
console.log('📦 解析后的消息:', msg);
|
console.log('📦 解析后的消息:', msg);
|
||||||
|
|
||||||
|
// 提取实际数据:处理后端 response 包装格式
|
||||||
|
const actualData = typeof msg.data === 'string'
|
||||||
|
? msg.data
|
||||||
|
: msg.data?.response?.data || '';
|
||||||
|
|
||||||
switch (msg.type) {
|
switch (msg.type) {
|
||||||
case 'output':
|
case 'output':
|
||||||
console.log('📝 输出数据:', msg.data?.substring(0, 100));
|
console.log('📝 输出数据:', actualData?.substring(0, 100));
|
||||||
console.log('📺 Terminal实例:', !!terminalInstanceRef.current);
|
console.log('📺 Terminal实例:', !!terminalInstanceRef.current);
|
||||||
if (msg.data && terminalInstanceRef.current) {
|
if (actualData && terminalInstanceRef.current) {
|
||||||
terminalInstanceRef.current.write(msg.data);
|
terminalInstanceRef.current.write(actualData);
|
||||||
console.log('✅ 数据已写入终端');
|
console.log('✅ 数据已写入终端');
|
||||||
} else {
|
} else {
|
||||||
console.warn('⚠️ 无法写入终端:', { hasData: !!msg.data, hasTerminal: !!terminalInstanceRef.current });
|
console.warn('⚠️ 无法写入终端:', { hasData: !!actualData, hasTerminal: !!terminalInstanceRef.current });
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'error':
|
case 'error':
|
||||||
console.error('❌ SSH错误:', msg.data);
|
console.error('❌ SSH错误:', actualData);
|
||||||
terminalInstanceRef.current?.writeln(`\r\n\x1b[31m错误: ${msg.data}\x1b[0m\r\n`);
|
terminalInstanceRef.current?.writeln(`\r\n\x1b[31m错误: ${actualData}\x1b[0m\r\n`);
|
||||||
message.error(msg.data || '连接错误');
|
message.error(actualData || '连接错误');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'status':
|
case 'status':
|
||||||
console.log('📊 状态变化:', msg.data);
|
console.log('📊 状态变化:', actualData);
|
||||||
setConnectionStatus(msg.data as ConnectionStatus);
|
setConnectionStatus(actualData as ConnectionStatus);
|
||||||
if (msg.data === 'connected') {
|
if (actualData === 'connected') {
|
||||||
// 显示审计警告
|
// 显示审计警告
|
||||||
terminalInstanceRef.current?.writeln('\r\n\x1b[33m┌─────────────────────────────────────────────────────────────\x1b[0m');
|
terminalInstanceRef.current?.writeln('\r\n\x1b[33m┌─────────────────────────────────────────────────────────────\x1b[0m');
|
||||||
terminalInstanceRef.current?.writeln('\x1b[33m│ ⚠️ 链宇技术有限公司 - 安全提示\x1b[0m');
|
terminalInstanceRef.current?.writeln('\x1b[33m│ ⚠️ 链宇技术有限公司 - 安全提示\x1b[0m');
|
||||||
|
|||||||
@ -168,18 +168,16 @@ const ServerList: React.FC = () => {
|
|||||||
setTestingServerId(server.id);
|
setTestingServerId(server.id);
|
||||||
try {
|
try {
|
||||||
const result = await testServerConnection(server.id);
|
const result = await testServerConnection(server.id);
|
||||||
// 后端返回 { success: true, data: true/false }
|
if (result.connected) {
|
||||||
// data 为 true 表示连接成功, false 表示连接失败
|
|
||||||
if (result === true) {
|
|
||||||
toast({
|
toast({
|
||||||
title: '连接成功',
|
title: '连接成功',
|
||||||
description: '服务器连接正常',
|
description: `${result.hostname} - ${result.osVersion} (响应时间: ${result.responseTime}ms)`,
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
toast({
|
toast({
|
||||||
variant: 'destructive',
|
variant: 'destructive',
|
||||||
title: '连接失败',
|
title: '连接失败',
|
||||||
description: '无法连接到服务器',
|
description: result.errorMessage || '无法连接到服务器',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import type {
|
|||||||
ServerCategoryRequest,
|
ServerCategoryRequest,
|
||||||
ServerResponse,
|
ServerResponse,
|
||||||
ServerRequest,
|
ServerRequest,
|
||||||
|
ServerConnectionTestResult,
|
||||||
} from './types';
|
} from './types';
|
||||||
import type { ServerFormValues } from './schema';
|
import type { ServerFormValues } from './schema';
|
||||||
import type { Page } from '@/types/base';
|
import type { Page } from '@/types/base';
|
||||||
@ -111,8 +112,8 @@ export const batchDeleteServers = (ids: number[]) =>
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试服务器连接
|
* 测试服务器连接
|
||||||
* 返回 true 表示连接成功, false 表示连接失败
|
* 返回连接测试结果,包含连接状态和服务器信息
|
||||||
*/
|
*/
|
||||||
export const testServerConnection = (id: number) =>
|
export const testServerConnection = (id: number) =>
|
||||||
request.post<boolean>(`${SERVER_URL}/${id}/test-connection`);
|
request.post<ServerConnectionTestResult>(`${SERVER_URL}/${id}/test-connection`);
|
||||||
|
|
||||||
|
|||||||
@ -224,3 +224,27 @@ export const AuthTypeLabels: Record<AuthType, { label: string; description: stri
|
|||||||
[AuthType.KEY]: { label: '密钥认证', description: '使用SSH私钥进行认证' },
|
[AuthType.KEY]: { label: '密钥认证', description: '使用SSH私钥进行认证' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 服务器连接测试结果
|
||||||
|
*/
|
||||||
|
export interface ServerConnectionTestResult {
|
||||||
|
/** 是否连接成功 */
|
||||||
|
connected: boolean;
|
||||||
|
/** 错误信息(连接失败时返回) */
|
||||||
|
errorMessage: string | null;
|
||||||
|
/** 连接时间 */
|
||||||
|
connectTime: string;
|
||||||
|
/** 响应时间(ms) */
|
||||||
|
responseTime: number;
|
||||||
|
/** 主机名 */
|
||||||
|
hostname: string;
|
||||||
|
/** 操作系统版本 */
|
||||||
|
osVersion: string;
|
||||||
|
/** CPU核心数 */
|
||||||
|
cpuCores: number;
|
||||||
|
/** 内存大小(GB) */
|
||||||
|
memorySize: number;
|
||||||
|
/** 磁盘大小(GB) */
|
||||||
|
diskSize: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user