文章目录

  • 前言
  • 扩展:什么是堡垒机
  • 实现
  • 依赖
  • 相关截图
  • 登录
  • 命令执行
  • 文件列表、删除、下载
  • 文件上传


前言

之前讲过,我现在在一家国企上班,由于集团网络环境是封闭的内网,平时的部署上线,日志排查是通过一个跳板堡垒机去连接内网机器,大致情况如下图:

springboot整合opentelemetry springBoot整合ssh_Java

以及联想到Alibaba开源的Java诊断工具Arthas,其Web Console在浏览器上就可以执行命令,操作简单,界面也非常的nice。

springboot整合opentelemetry springBoot整合ssh_ssh_02

然后就想着去实现一个自己的Web SSH,去探究其内部的原理。

扩展:什么是堡垒机

堡垒机,即在一个特定的网络环境下,为了保障网络和数据不受来自外部和内部用户的入侵和破坏,而运用各种技术手段监控和记录运维人员对网络内的服务器、网络设备、安全设备、数据库等设备的操作行为,以便集中报警及时处理及审计定责

来自百度百科

实现

webssh泛指一种技术可以在网页上实现一个SSH终端。从而无需Xshell之类的模拟终端工具进行SSH连接,将SSH这一比较低层的操作也从C/S架构扭成了B/S架构。

工作原理

+---------+     http     +--------+    ssh    +-----------+
| browser | <==========> | webssh | <=======> | ssh server|
+---------+   websocket  +--------+    ssh    +-----------+
  • WebSocket长连接,用于指令发送和响应的实时交互
  • xterm.js是一个基于WebSocket的容器,它可以帮助我们在前端实现命令行的样式。
  • JSch是SSH2的一个纯Java实现。它允许你连接到一个sshd 服务器,使用端口转发,X11转发,文件传输等等。

实现功能

  • 命令执行
  • 文件上传/下载

相关代码

依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.7.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <!-- Web相关 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- jsch支持 -->
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>0.1.54</version>
    </dependency>
    <!-- WebSocket 支持 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>
</dependencies>

相关截图

登录

springboot整合opentelemetry springBoot整合ssh_ssh_03

命令执行

springboot整合opentelemetry springBoot整合ssh_java_04

文件列表、删除、下载

springboot整合opentelemetry springBoot整合ssh_ssh_05

文件上传

springboot整合opentelemetry springBoot整合ssh_堡垒机_06

由于相关实现较为简单,只要看下JSchxterm.js即可实现,还能自由扩展相关功能。