@seasongold4/webssh2-frontend-fixed
Version:
WebSSH2 前端终端库 - 修复版本,完全兼容原项目后端,解决终端显示乱码问题
291 lines (265 loc) • 10.8 kB
HTML
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>完整 WebSSH 客户端示例</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.config-panel {
background: white;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}
.form-group input, .form-group select {
width: 100%;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
}
.form-row {
display: flex;
gap: 15px;
}
.form-row .form-group {
flex: 1;
}
.btn {
background: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-right: 10px;
}
.btn:hover {
background: #0056b3;
}
.btn-danger {
background: #dc3545;
}
.btn-danger:hover {
background: #c82333;
}
.terminal-container {
background: white;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
height: 700px;
}
.info {
background: #e7f3ff;
border: 1px solid #bee5eb;
border-radius: 4px;
padding: 15px;
margin-bottom: 20px;
}
.info h3 {
margin-top: 0;
color: #0c5460;
}
.info ul {
margin-bottom: 0;
}
.info li {
margin-bottom: 5px;
}
</style>
</head>
<body>
<div class="container">
<h1>完整 WebSSH 客户端示例</h1>
<div class="info">
<h3>🎯 完整 WebSSH 界面功能</h3>
<ul>
<li><strong>完整的原生界面</strong> - 保留了原始WebSSH的所有界面元素</li>
<li><strong>底部菜单栏</strong> - 包含日志记录、下载等功能</li>
<li><strong>状态显示</strong> - 实时显示连接状态和服务器信息</li>
<li><strong>日志功能</strong> - 可以开始/停止日志记录并下载</li>
<li><strong>响应式布局</strong> - 自动适应窗口大小变化</li>
</ul>
</div>
<div class="config-panel">
<h2>SSH 连接配置</h2>
<form id="connectionForm">
<div class="form-row">
<div class="form-group">
<label for="socketUrl">后端Socket.IO地址:</label>
<input type="text" id="socketUrl" value="http://localhost:9092" required>
</div>
<div class="form-group">
<label for="socketPath">Socket路径:</label>
<input type="text" id="socketPath" value="/ssh/socket.io">
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="host">SSH主机:</label>
<input type="text" id="host" value="192.168.1.100" required>
</div>
<div class="form-group">
<label for="port">端口:</label>
<input type="number" id="port" value="22" required>
</div>
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" id="username" value="root" required>
</div>
</div>
<div class="form-row">
<div class="form-group">
<label for="authType">认证方式:</label>
<select id="authType" onchange="toggleAuthType()">
<option value="password">密码认证</option>
<option value="privatekey">私钥认证</option>
</select>
</div>
<div class="form-group" id="passwordGroup">
<label for="password">密码:</label>
<input type="password" id="password" value="password123">
</div>
<div class="form-group" id="privatekeyGroup" style="display: none;">
<label for="privatekey">私钥:</label>
<textarea id="privatekey" rows="3" placeholder="-----BEGIN RSA PRIVATE KEY-----"></textarea>
</div>
</div>
</form>
<div style="margin-top: 20px;">
<button class="btn" onclick="connectSSH()">连接SSH</button>
<button class="btn btn-danger" onclick="disconnectSSH()">断开连接</button>
</div>
</div>
<div class="terminal-container" id="terminalContainer">
<!-- 完整的WebSSH客户端将在这里显示 -->
</div>
</div>
<!-- 引入打包后的完整WebSSH客户端 -->
<script src="../dist/webssh2-frontend.js"></script>
<script>
let sshClient = null;
function toggleAuthType() {
const authType = document.getElementById('authType').value;
const passwordGroup = document.getElementById('passwordGroup');
const privatekeyGroup = document.getElementById('privatekeyGroup');
if (authType === 'password') {
passwordGroup.style.display = 'block';
privatekeyGroup.style.display = 'none';
} else {
passwordGroup.style.display = 'none';
privatekeyGroup.style.display = 'block';
}
}
function connectSSH() {
// 如果已经有连接,先断开
if (sshClient) {
sshClient.destroy();
}
// 获取表单数据
const socketUrl = document.getElementById('socketUrl').value;
const socketPath = document.getElementById('socketPath').value;
const host = document.getElementById('host').value;
const port = parseInt(document.getElementById('port').value);
const username = document.getElementById('username').value;
const authType = document.getElementById('authType').value;
const password = authType === 'password' ? document.getElementById('password').value : undefined;
const privateKey = authType === 'privatekey' ? document.getElementById('privatekey').value : undefined;
// 验证输入
if (!socketUrl || !host || !username) {
alert('请填写必要的连接信息');
return;
}
if (authType === 'password' && !password) {
alert('请输入密码');
return;
}
if (authType === 'privatekey' && !privateKey) {
alert('请输入私钥');
return;
}
// 创建完整的WebSSH客户端配置
const config = {
socketUrl: socketUrl,
socketPath: socketPath,
host: host,
port: port,
username: username,
password: password,
privateKey: privateKey,
// 事件回调
onConnected: () => {
console.log('SSH连接已建立');
updateStatus('已连接', 'success');
},
onDisconnected: () => {
console.log('SSH连接已断开');
updateStatus('已断开', 'error');
},
onError: (error) => {
console.error('SSH连接错误:', error);
updateStatus('连接错误: ' + error, 'error');
alert('连接错误: ' + error);
}
};
try {
// 创建完整的WebSSH客户端实例
sshClient = new WebSSHClient('terminalContainer', config);
// 连接SSH
sshClient.connect();
updateStatus('连接中...', 'warning');
} catch (error) {
console.error('创建WebSSH客户端失败:', error);
alert('创建WebSSH客户端失败: ' + error.message);
}
}
function disconnectSSH() {
if (sshClient) {
sshClient.disconnect();
updateStatus('已断开', 'error');
}
}
function updateStatus(message, type) {
console.log(`状态更新 [${type}]: ${message}`);
// 状态会在WebSSH客户端的底部状态栏中显示
}
// 页面加载完成后的初始化
document.addEventListener('DOMContentLoaded', function() {
console.log('完整WebSSH客户端示例页面已加载');
// 检查是否加载了WebSSH客户端库
if (typeof WebSSHClient === 'undefined') {
alert('WebSSH客户端库未正确加载,请检查script标签引用');
} else {
console.log('WebSSH客户端库已成功加载');
}
});
// 页面卸载时清理资源
window.addEventListener('beforeunload', function() {
if (sshClient) {
sshClient.destroy();
}
});
</script>
</body>
</html>