react?express實現(xiàn)webssh?demo解析
正文
下面是一個簡單的 WebSSH Demo,實現(xiàn)了通過瀏覽器連接 SSH 服務器并進行交互的功能。
實現(xiàn) WebSSH 的基本思路
WebSSH 可以分成以下幾個模塊:
- 前端界面:使用 xterm.js 實現(xiàn)一個基于瀏覽器的終端界面。
- WebSocket 連接:使用 WebSocket 連接連接 WebSSH 服務器后端。
- SSH 連接:使用 ssh2.js 庫連接 SSH 服務器,然后在 WebSocket 和 SSH 之間建立一個雙向通訊。
實現(xiàn) Demo 的代碼
服務器端代碼
服務器端代碼使用 Node.js 和 WebSocket 模塊實現(xiàn),主要用于連接到遠程 SSH 服務器并與前端建立 WebSocket 連接。
const SSHClient = require('ssh2').Client; const utf8 = require('utf8'); export const createNewServer = (machineConfig: any, socket: any) => { const ssh = new SSHClient(); const { host, username, password } = machineConfig; // 連接成功 ssh.on('ready', function () { socket.send('\r\n*** SSH CONNECTION SUCCESS ***\r\n'); ssh.shell(function (err: any, stream: any) { // 出錯 if (err) { return socket.send('\r\n*** SSH SHELL ERROR: ' + err.message + ' ***\r\n'); } // 前端發(fā)送消息 socket.on('message', function (data: any) { stream.write(data); }); // 通過sh發(fā)送消息給前端 stream.on('data', function (d: any) { socket.send(utf8.decode(d.toString('binary'))); // 關閉連接 }).on('close', function () { ssh.end(); }); }) // 關閉連接 }).on('close', function () { socket.send('\r\n*** SSH CONNECTION CLOSED ***\r\n'); // 連接錯誤 }).on('error', function (err: any) { socket.send('\r\n*** SSH CONNECTION ERROR: ' + err.message + ' ***\r\n'); // 連接 }).connect({ port: 22, host, username, password }); }
前端代碼
前端代碼主要包括一個包裝 xterm.js 的 React 組件和一些 WebSockets 相關的代碼。
import React, { useEffect, useRef } from 'react'; import { Terminal } from 'xterm'; import { WebLinksAddon } from 'xterm-addon-web-links'; import { FitAddon } from 'xterm-addon-fit'; import 'xterm/css/xterm.css'; const FontSize = 14; const Col = 80; const WebTerminal = () => { const webTerminal = useRef(null); const ws = useRef(null); useEffect(() => { // 初始化終端 const ele = document.getElementById('terminal'); if (ele && !webTerminal.current) { const height = ele.clientHeight; // 初始化 const terminal = new Terminal({ cursorBlink: true, cols: Col, rows: Math.ceil(height / FontSize), }); // 輔助 const fitAddon = new FitAddon(); terminal.loadAddon(new WebLinksAddon()); terminal.loadAddon(fitAddon); terminal.open(ele); terminal.write('Hello from \x1B[1;3;31mxterm.js\x1B[0m $ '); fitAddon.fit(); webTerminal.current = terminal; } // 初始化ws連接 if (ws.current) ws.current.close(); const socket = new WebSocket('ws://localhost:3001'); socket.onopen = () => { socket.send('connect success'); }; ws.current = socket; }, []); useEffect(() => { // 新增監(jiān)聽事件 const terminal = webTerminal.current; const socket = ws.current; if (terminal && socket) { // 監(jiān)聽 terminal.onKey(e => { const { key } = e; socket.send(key); }); // ws監(jiān)聽 socket.onmessage = e => { console.log(e); if (typeof e.data === 'string') { terminal.write(e.data); } else { console.error('格式錯誤'); } }; } }, []); return <div id="terminal" style={{ backgroundColor: '#000', width: '100vw', height: '100vh' }}/>; }; export default WebTerminal;
WebSSH 組件借助 Hooks 特性進行 WebSocket 和 xterm.js 的初始化。具體來說,這個組件使用了 useEffect Hook 在組件掛載時完成以下工作:
- 初始化 Terminal 組件。
- 初始化 WebSocket 連接。
- 為 Terminal 組件綁定輸入事件和 WebSocket 發(fā)送數(shù)據(jù)的邏輯。
在 React 應用中使用 WebSSH 組件
你需要在你的 React的index.js 文件中引入 WebSSH 組件,并在你的應用中渲染它:
import WebSSH from './components/WebSSH'; import React from 'react'; import ReactDOM from 'react-dom'; ReactDOM.render( <WebSSH />, document.getElementById('root') );
效果
編輯
總結(jié)
在本篇博客中,我們學習了如何使用 xterm.js、WebSocket 和 ssh2.js 庫構(gòu)建一個 WebSSH 應用程序。我們創(chuàng)建了一個簡單的 Demo 來演示該過程。
完整代碼參考
GitHub - judithhuang/webssh-demo
以上就是react express實現(xiàn)webssh demo解析的詳細內(nèi)容,更多關于react express實現(xiàn)webssh的資料請關注腳本之家其它相關文章!
相關文章
如何解決React官方腳手架不支持Less的問題(小結(jié))
這篇文章主要介紹了如何解決React官方腳手架不支持Less的問題(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09React中使用Echarts無法顯示title、tooltip等組件的解決方案
這篇文章主要介紹了React中使用Echarts無法顯示title、tooltip等組件的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03