UNPKG

@seasongold4/webssh2-frontend-fixed

Version:

WebSSH2 前端终端库 - 修复版本,完全兼容原项目后端,解决终端显示乱码问题

299 lines (271 loc) 10.9 kB
<!DOCTYPE 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; } .header { 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; } .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: 600px; } .controls { display: flex; gap: 10px; margin-bottom: 20px; } </style> </head> <body> <div class="container"> <div class="header"> <h1>WebSSH 前端库使用示例</h1> <p>演示如何使用独立的前端库连接到 Java Spring Boot 后端</p> <form id="connectionForm"> <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> <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="headerText">终端标题:</label> <input type="text" id="headerText" value="我的SSH终端"> </div> </div> </form> <div class="controls"> <button class="btn" onclick="connectSSH()">连接SSH</button> <button class="btn btn-danger" onclick="disconnectSSH()">断开连接</button> <button class="btn" onclick="clearTerminal()">清空终端</button> </div> </div> <div class="terminal-container" id="terminalContainer"> <!-- 终端将在这里显示 --> </div> </div> <!-- 引入前端库 --> <script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script> <script src="../dist/webssh2-frontend.js"></script> <script> let sshTerminal = 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 (sshTerminal) { sshTerminal.destroy(); } // 获取表单数据 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; const socketUrl = document.getElementById('socketUrl').value; const headerText = document.getElementById('headerText').value; // 验证输入 if (!host || !username) { alert('请填写必要的连接信息'); return; } if (authType === 'password' && !password) { alert('请输入密码'); return; } if (authType === 'privatekey' && !privateKey) { alert('请输入私钥'); return; } // 创建终端配置 const config = { // 连接配置 socketUrl: socketUrl, socketPath: '/socket.io', // SSH连接参数 host: host, port: port, username: username, password: password, privateKey: privateKey, // 终端配置 terminal: { fontSize: 14, cursorBlink: true, scrollback: 1000, theme: { background: '#1e1e1e', foreground: '#d4d4d4', cursor: '#ffffff', selection: '#264f78' } }, // UI配置 ui: { headerText: headerText, headerBackground: '#2c3e50', showReconnectButton: true }, // 事件回调 onConnected: () => { console.log('SSH连接已建立'); updateStatus('已连接', 'success'); }, onDisconnected: () => { console.log('SSH连接已断开'); updateStatus('已断开', 'error'); }, onError: (error) => { console.error('SSH连接错误:', error); updateStatus('连接错误: ' + error, 'error'); alert('连接错误: ' + error); }, onData: (data) => { // 可以在这里处理接收到的数据 console.log('接收数据:', data); } }; try { // 创建终端实例 sshTerminal = new WebSSHTerminal('terminalContainer', config); // 连接SSH sshTerminal.connect(); updateStatus('连接中...', 'warning'); } catch (error) { console.error('创建终端失败:', error); alert('创建终端失败: ' + error.message); } } function disconnectSSH() { if (sshTerminal) { sshTerminal.disconnect(); updateStatus('已断开', 'error'); } } function clearTerminal() { if (sshTerminal) { sshTerminal.clear(); } } function updateStatus(message, type) { // 可以添加状态显示逻辑 console.log(`状态更新 [${type}]: ${message}`); } // 页面加载完成后的初始化 document.addEventListener('DOMContentLoaded', function() { console.log('WebSSH 前端库示例页面已加载'); // 检查是否加载了前端库 if (typeof WebSSHTerminal === 'undefined') { alert('WebSSH前端库未正确加载,请检查script标签引用'); } }); // 页面卸载时清理资源 window.addEventListener('beforeunload', function() { if (sshTerminal) { sshTerminal.destroy(); } }); </script> </body> </html>