🔨 优化会话连接逻辑.
This commit is contained in:
@@ -36,6 +36,7 @@ import java.util.Map;
|
|||||||
* @version 1.0.0
|
* @version 1.0.0
|
||||||
* @since 2023-9-11 14:16
|
* @since 2023-9-11 14:16
|
||||||
*/
|
*/
|
||||||
|
// TODO 待优化
|
||||||
public interface HostConfigService {
|
public interface HostConfigService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -141,6 +141,7 @@ public class ExecCommandServiceImpl implements ExecCommandService {
|
|||||||
// 查询主机信息
|
// 查询主机信息
|
||||||
List<HostDO> hosts = hostDAO.selectBatchIds(hostIdList);
|
List<HostDO> hosts = hostDAO.selectBatchIds(hostIdList);
|
||||||
// 查询主机配置
|
// 查询主机配置
|
||||||
|
// TODO 待优化
|
||||||
Map<Long, HostSshConfigModel> hostConfigMap = hostConfigService.buildHostConfigMap(hosts, HostTypeEnum.SSH);
|
Map<Long, HostSshConfigModel> hostConfigMap = hostConfigService.buildHostConfigMap(hosts, HostTypeEnum.SSH);
|
||||||
// 插入日志
|
// 插入日志
|
||||||
ExecLogDO execLog = ExecLogDO.builder()
|
ExecLogDO execLog = ExecLogDO.builder()
|
||||||
|
|||||||
@@ -388,6 +388,7 @@ public class ExecLogServiceImpl implements ExecLogService {
|
|||||||
}
|
}
|
||||||
Valid.notEmpty(hostLogs, ErrorMessage.LOG_ABSENT);
|
Valid.notEmpty(hostLogs, ErrorMessage.LOG_ABSENT);
|
||||||
// 获取编码集
|
// 获取编码集
|
||||||
|
// TODO 待优化
|
||||||
List<Long> hostIdList = hostLogs.stream()
|
List<Long> hostIdList = hostLogs.stream()
|
||||||
.map(ExecHostLogDO::getHostId)
|
.map(ExecHostLogDO::getHostId)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|||||||
@@ -28,11 +28,15 @@ export default abstract class BaseSession implements ITerminalSession {
|
|||||||
this.canWrite = canWrite;
|
this.canWrite = canWrite;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 连接
|
// 设置已连接
|
||||||
connect(): void {
|
setConnected(): void {
|
||||||
this.connected = true;
|
this.connected = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 连接会话
|
||||||
|
connect(): void {
|
||||||
|
}
|
||||||
|
|
||||||
// 断开连接
|
// 断开连接
|
||||||
disconnect(): void {
|
disconnect(): void {
|
||||||
this.connected = false;
|
this.connected = false;
|
||||||
|
|||||||
@@ -28,12 +28,23 @@ export default class SftpSession extends BaseSession implements ISftpSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 设置已连接
|
// 设置已连接
|
||||||
connect(): void {
|
setConnected(): void {
|
||||||
super.connect();
|
super.setConnected();
|
||||||
// 连接回调
|
// 连接回调
|
||||||
this.resolver.connectCallback();
|
this.resolver.connectCallback();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 连接会话
|
||||||
|
connect(): void {
|
||||||
|
super.connect();
|
||||||
|
// 发送会话初始化请求
|
||||||
|
this.channel.send(InputProtocol.CHECK, {
|
||||||
|
sessionId: this.sessionId,
|
||||||
|
hostId: this.hostId,
|
||||||
|
connectType: this.type,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 设置显示隐藏文件
|
// 设置显示隐藏文件
|
||||||
setShowHiddenFile(show: boolean): void {
|
setShowHiddenFile(show: boolean): void {
|
||||||
this.showHiddenFile = show;
|
this.showHiddenFile = show;
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import type { XtermAddons } from '@/types/xterm';
|
|||||||
import { defaultFontFamily } from '@/types/xterm';
|
import { defaultFontFamily } from '@/types/xterm';
|
||||||
import { useTerminalStore } from '@/store';
|
import { useTerminalStore } from '@/store';
|
||||||
import { InputProtocol } from '@/types/protocol/terminal.protocol';
|
import { InputProtocol } from '@/types/protocol/terminal.protocol';
|
||||||
import { PanelSessionType, TerminalShortcutType, TerminalSessionStatus } from '../types/const';
|
import { PanelSessionType, TerminalSessionStatus, TerminalShortcutType } from '../types/const';
|
||||||
import { Terminal } from '@xterm/xterm';
|
import { Terminal } from '@xterm/xterm';
|
||||||
import { FitAddon } from '@xterm/addon-fit';
|
import { FitAddon } from '@xterm/addon-fit';
|
||||||
import { WebLinksAddon } from '@xterm/addon-web-links';
|
import { WebLinksAddon } from '@xterm/addon-web-links';
|
||||||
@@ -205,8 +205,9 @@ export default class SshSession extends BaseSession implements ISshSession {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 设置已连接
|
// 设置已连接
|
||||||
connect(): void {
|
setConnected(): void {
|
||||||
super.connect();
|
super.setConnected();
|
||||||
|
// 设置状态
|
||||||
this.status = TerminalSessionStatus.CONNECTED;
|
this.status = TerminalSessionStatus.CONNECTED;
|
||||||
this.inst.focus();
|
this.inst.focus();
|
||||||
}
|
}
|
||||||
@@ -221,6 +222,19 @@ export default class SshSession extends BaseSession implements ISshSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 连接会话
|
||||||
|
connect(): void {
|
||||||
|
super.connect();
|
||||||
|
// 设置状态
|
||||||
|
this.status = TerminalSessionStatus.CONNECTING;
|
||||||
|
// 发送会话初始化请求
|
||||||
|
this.channel.send(InputProtocol.CHECK, {
|
||||||
|
sessionId: this.sessionId,
|
||||||
|
hostId: this.hostId,
|
||||||
|
connectType: this.type,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// 写入数据
|
// 写入数据
|
||||||
write(value: string): void {
|
write(value: string): void {
|
||||||
this.inst.write(value);
|
this.inst.write(value);
|
||||||
|
|||||||
@@ -65,8 +65,8 @@ export default class TerminalOutputProcessor implements ITerminalOutputProcessor
|
|||||||
if (success) {
|
if (success) {
|
||||||
// 设置可写
|
// 设置可写
|
||||||
ssh.setCanWrite(true);
|
ssh.setCanWrite(true);
|
||||||
// 执行连接逻辑
|
// 设置已连接
|
||||||
ssh.connect();
|
ssh.setConnected();
|
||||||
} else {
|
} else {
|
||||||
// 未成功展示错误信息
|
// 未成功展示错误信息
|
||||||
ssh.write(`[91m${msg || ''}[0m\r\n\r\n[91m输入回车重新连接...[0m\r\n\r\n`);
|
ssh.write(`[91m${msg || ''}[0m\r\n\r\n[91m输入回车重新连接...[0m\r\n\r\n`);
|
||||||
@@ -77,8 +77,8 @@ export default class TerminalOutputProcessor implements ITerminalOutputProcessor
|
|||||||
if (success) {
|
if (success) {
|
||||||
// 设置可写
|
// 设置可写
|
||||||
sftp.setCanWrite(true);
|
sftp.setCanWrite(true);
|
||||||
// 执行连接逻辑
|
// 设置已连接
|
||||||
sftp.connect();
|
sftp.setConnected();
|
||||||
} else {
|
} else {
|
||||||
// 未成功提示错误信息
|
// 未成功提示错误信息
|
||||||
sftp.resolver?.onClose(false, msg);
|
sftp.resolver?.onClose(false, msg);
|
||||||
|
|||||||
@@ -52,12 +52,8 @@ export default class TerminalSessionManager implements ITerminalSessionManager {
|
|||||||
// 添加会话
|
// 添加会话
|
||||||
const sessionId = tab.sessionId;
|
const sessionId = tab.sessionId;
|
||||||
this.sessions[sessionId] = session;
|
this.sessions[sessionId] = session;
|
||||||
// 发送会话初始化请求
|
// 连接会话
|
||||||
this.channel.send(InputProtocol.CHECK, {
|
session.connect();
|
||||||
sessionId,
|
|
||||||
hostId: tab.hostId,
|
|
||||||
connectType: PanelSessionType.SSH.type
|
|
||||||
});
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,17 +68,13 @@ export default class TerminalSessionManager implements ITerminalSessionManager {
|
|||||||
// 添加会话
|
// 添加会话
|
||||||
const sessionId = tab.sessionId;
|
const sessionId = tab.sessionId;
|
||||||
this.sessions[sessionId] = session;
|
this.sessions[sessionId] = session;
|
||||||
// 发送会话初始化请求
|
// 连接会话
|
||||||
this.channel.send(InputProtocol.CHECK, {
|
session.connect();
|
||||||
sessionId,
|
|
||||||
hostId: tab.hostId,
|
|
||||||
connectType: PanelSessionType.SFTP.type
|
|
||||||
});
|
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 重新打开会话
|
// 重新打开会话
|
||||||
async reOpenSession(type: string, sessionId: string, newSessionId: string): Promise<void> {
|
async reOpenSession(sessionId: string, newSessionId: string): Promise<void> {
|
||||||
// 初始化客户端
|
// 初始化客户端
|
||||||
await this.initChannel();
|
await this.initChannel();
|
||||||
// 获取会话并且重新设置 sessionId
|
// 获取会话并且重新设置 sessionId
|
||||||
@@ -90,12 +82,8 @@ export default class TerminalSessionManager implements ITerminalSessionManager {
|
|||||||
session.sessionId = newSessionId;
|
session.sessionId = newSessionId;
|
||||||
this.sessions[sessionId] = undefined as unknown as ITerminalSession;
|
this.sessions[sessionId] = undefined as unknown as ITerminalSession;
|
||||||
this.sessions[newSessionId] = session;
|
this.sessions[newSessionId] = session;
|
||||||
// 发送会话初始化请求
|
// 连接会话
|
||||||
this.channel.send(InputProtocol.CHECK, {
|
session.connect();
|
||||||
sessionId: newSessionId,
|
|
||||||
hostId: session.hostId,
|
|
||||||
connectType: type,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取终端会话
|
// 获取终端会话
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ export interface ITerminalSessionManager {
|
|||||||
// 打开 sftp 会话
|
// 打开 sftp 会话
|
||||||
openSftp: (tab: TerminalPanelTabItem, resolver: ISftpSessionResolver) => Promise<ISftpSession>;
|
openSftp: (tab: TerminalPanelTabItem, resolver: ISftpSessionResolver) => Promise<ISftpSession>;
|
||||||
// 重新打开会话
|
// 重新打开会话
|
||||||
reOpenSession: (type: string, sessionId: string, newSessionId: string) => Promise<void>;
|
reOpenSession: (sessionId: string, newSessionId: string) => Promise<void>;
|
||||||
// 获取终端会话
|
// 获取终端会话
|
||||||
getSession: <T extends ITerminalSession>(sessionId: string) => T;
|
getSession: <T extends ITerminalSession>(sessionId: string) => T;
|
||||||
// 关闭终端会话
|
// 关闭终端会话
|
||||||
@@ -211,7 +211,9 @@ export interface ITerminalSession {
|
|||||||
|
|
||||||
// 设置是否可写
|
// 设置是否可写
|
||||||
setCanWrite: (canWrite: boolean) => void;
|
setCanWrite: (canWrite: boolean) => void;
|
||||||
// 连接
|
// 设置已连接
|
||||||
|
setConnected: () => void;
|
||||||
|
// 连接会话
|
||||||
connect: () => void;
|
connect: () => void;
|
||||||
// 断开连接
|
// 断开连接
|
||||||
disconnect: () => void;
|
disconnect: () => void;
|
||||||
|
|||||||
Reference in New Issue
Block a user