简介

项目中需要用到堡垒机功能,调研了一大圈,发现了Apache Guacamole这个开源项目。

Apache Guacamole 是一个无客户端的远程桌面网关,它支持众多标准管理协议,例如 VNC(RFB),RDP,SSH 等等。该项目是Apache基金会旗下的一个开源项目,也是一个较高标准,并具有广泛应用前景的项目。

当Guacamole被部署在服务器上后,用户通过浏览器即可访问已经开启 VNC(RFB),RDP,SSH 等远程管理服务的主机,屏蔽用户使用环境差异,跨平台,另外由于Guacamole本身被设计为一种代理工作模型,方便对用户集中授权监控等管理,,也被众多堡垒机项目所集成,例如‘jumpserver’,‘next-terminal’。

Guacamole项目的主页如下:

Apache Guacamole™

Guacamole项目的架构如下图:

包括了guacd、guacamole、前端页面等几个模块。

其中,guacd是由C语言编写,接受并处理guacamole发送来的请求,然后翻译并转换这个请求,动态的调用遵循那些标准管理协议开发的开源客户端,例如FreeRDP,libssh2,LibVNC,代为连接Remote Desktops,最后回传数据给guacamole,guacamole回传数据给web browser。

guacamole是web工程,包含了java后端服务和angular前端页面, 通过servlet或websocket与前端界面交互,通过tcp与guacd交互。同时集成了用户管理、权限验证、数据管理等各种功能。这块的模块组成如下:

我们项目中有很多自己的业务需求和界面需求,所以,Web这块决定不用开源自带的后端和界面,自己开发。基于guacamole-common和js库进行二次开发。

SpringBoot集成

POM:包含了guacamole-common、guacamole-common-js,以及servlet、websocket等。

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-websocket</artifactId>
</dependency><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope>
</dependency>
<dependency><groupId>javax.websocket</groupId><artifactId>javax.websocket-api</artifactId><version>1.0</version><scope>provided</scope>
</dependency><dependency><groupId>org.apache.guacamole</groupId><artifactId>guacamole-common</artifactId><version>1.5.1</version>
</dependency>
<dependency><groupId>org.apache.guacamole</groupId><artifactId>guacamole-ext</artifactId><version>1.5.1</version>
</dependency><dependency><groupId>org.apache.guacamole</groupId><artifactId>guacamole-common-js</artifactId><version>1.5.1</version><type>zip</type><scope>runtime</scope>
</dependency>

可以通过servlet或websocket两种方式进行集成,推荐采用websocket方式,性能更好。

配置文件application.yml

server:port: 8080servlet:context-path: /spring:servlet:multipart:enabled: falsemax-file-size: 1024MBdatasource:url: jdbc:mysql://127.0.0.1:3306/guac?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8username: rootpassword: 123456driver-class-name: com.mysql.cj.jdbc.Drivermybatis-plus:mapper-locations: classpath:mapper/*.xmlguacamole:ip: 192.168.110.2port: 4822

WebSocket方式

从GuacamoleWebSocketTunnelEndpoint中继承类,重载createTunnel方法。

@ServerEndpoint(value = "/webSocket", subprotocols = "guacamole")
@Component
public class WebSocketTunnel extends GuacamoleWebSocketTunnelEndpoint {private String uuid;private static IDeviceLoginInfoService deviceLoginInfoService;private static String guacIp;private static Integer guacPort;private GuacamoleTunnel guacamoleTunnel;// websocket中,自动注入及绑定配置项必须用这种方式@Autowiredpublic void setDeviceListenerService(IDeviceLoginInfoService deviceListenerService) {WebSocketTunnel.deviceLoginInfoService = deviceListenerService;}@Value("${guacamole.ip}")public void setGuacIp(String guacIp) {WebSocketTunnel.guacIp = guacIp;}@Value("${guacamole.port}")public void setGuacPort(Integer guacPort) {WebSocketTunnel.guacPort = guacPort;}@Overrideprotected GuacamoleTunnel createTunnel(Session session, EndpointConfig endpointConfig) throws GuacamoleException {//从session中获取传入参数Map<String, List<String>> map = session.getRequestParameterMap();DeviceLoginInfoVo loginInfo = null;String did = map.get("did").get(0);tid = map.get("tid").get(0);tid = tid.toLowerCase();// 根据传入参数从数据库中查找连接信息loginInfo = deviceLoginInfoService.getDeviceLoginInfo(did, tid);if(loginInfo != null) {loginInfo.setPort(opsPort);}if(loginInfo != null) {//String wid = (map.get("width")==null) ? "1413" : map.get("width").get(0);//String hei = (map.get("height")==null) ? "925" : map.get("height").get(0);String wid = "1412";String hei = "924";GuacamoleConfiguration configuration = new GuacamoleConfiguration();configuration.setParameter("hostname", loginInfo.getIp());configuration.setParameter("port", loginInfo.getPort().toString());configuration.setParameter("username", loginInfo.getUser());configuration.setParameter("password", loginInfo.getPassword());if(tid.equals("ssh")) {configuration.setProtocol("ssh"); // 远程连接协议configuration.setParameter("width", wid);configuration.setParameter("height", hei);configuration.setParameter("color-scheme", "white-black");//configuration.setParameter("terminal-type", "xterm-256color");//configuration.setParameter("locale", "zh_CN.UTF-8");configuration.setParameter("font-name", "Courier New");configuration.setParameter("enable-sftp", "true");}else if(tid.equals("vnc")){configuration.setProtocol("vnc"); // 远程连接协议configuration.setParameter("width", wid);configuration.setParameter("height", hei);}else if(tid.equals("rdp")) {configuration.setProtocol("rdp"); // 远程连接协议configuration.setParameter("ignore-cert", "true");if(loginInfo.getDomain() !=null) {configuration.setParameter("domain", loginInfo.getDomain());}configuration.setParameter("width", wid);configuration.setParameter("height", hei);}GuacamoleClientInformation information = new GuacamoleClientInformation();information.setOptimalScreenHeight(Integer.parseInt(hei));information.setOptimalScreenWidth(Integer.parseInt(wid));GuacamoleSocket socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(guacIp, guacPort),configuration, information);GuacamoleTunnel tunnel = new SimpleGuacamoleTunnel(socket);guacamoleTunnel = tunnel;return tunnel;}return null;}
}

Servlet方式

从GuacamoleHTTPTunnelServlet类继承,重载doConnect方法

@WebServlet(urlPatterns = "/tunnel")
public class HttpTunnelServlet extends GuacamoleHTTPTunnelServlet {@ResourceIDeviceLoginInfoService deviceLoginInfoService;@Value("${guacamole.ip}")private String guacIp;@Value("${guacamole.port}")private Integer guacPort;@Overrideprotected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {//从HttpServletRequest获取请求参数String did = request.getParameter("did");String tid = request.getParameter("tid");tid = tid.toLowerCase();//根据参数从数据库中查找连接信息,主机ip、端口、用户名、密码等DeviceLoginInfoVo loginInfo = deviceLoginInfoService.getDeviceLoginInfo(did, tid);if(loginInfo != null) {GuacamoleConfiguration configuration = new GuacamoleConfiguration();configuration.setParameter("hostname", loginInfo.getIp());configuration.setParameter("port", loginInfo.getPort().toString());configuration.setParameter("username", loginInfo.getUser());configuration.setParameter("password", loginInfo.getPassword());if(tid.equals("ssh")) {configuration.setProtocol("ssh"); // 远程连接协议}else if(tid.equals("vnc")){configuration.setProtocol("vnc"); // 远程连接协议}else if(tid.equals("rdp")) {configuration.setProtocol("rdp"); // 远程连接协议configuration.setParameter("ignore-cert", "true");if(loginInfo.getDomain() != null) {configuration.setParameter("domain", loginInfo.getDomain());}configuration.setParameter("width", "1024");configuration.setParameter("height", "768");}GuacamoleSocket socket = new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(guacIp, guacPort),configuration);GuacamoleTunnel tunnel = new SimpleGuacamoleTunnel(socket);return tunnel;}return null;}
}

前端页面

我用的是最基本的html+js

<!DOCTYPE HTML>
<html>
<head><meta charset="UTF-8"><link rel="stylesheet" type="text/css" href="guacamole.css"/><title>guac</title><style></style>
</head>
<body>
<div id="mainapp"><!-- Display --><div id="display"></div>
</div><!-- Guacamole JavaScript API -->
<script type="text/javascript" src="guacamole-common-js/all.js"></script>
<script type="text/javascript">function getUrlParam(name) {var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");var r = window.location.search.substr(1).match(reg);if(r != null) {return decodeURI(r[2]);}return null;}var user = getUrlParam('user');var devid = getUrlParam('did');var typeid= getUrlParam('tid');// var width = getUrlParam('width');// var height = getUrlParam('height');// Get display div from documentvar display = document.getElementById("display");var uuid;var tunnel = new Guacamole.ChainedTunnel(new Guacamole.WebSocketTunnel("webSocket"));var guac = new Guacamole.Client(tunnel);// Add client to display divdisplay.appendChild(guac.getDisplay().getElement());tunnel.onuuid = function(id) {uuid = id;}// Connectguac.connect('did='+devid+'&tid='+typeid+'&user='+user);// Disconnect on closewindow.onunload = function() {guac.disconnect();}// Mousevar mouse = new Guacamole.Mouse(guac.getDisplay().getElement());mouse.onmousedown =mouse.onmousemove = function(mouseState) {guac.sendMouseState(mouseState);};mouse.onmouseup = function(mouseState) {vueapp.showfile = false;guac.sendMouseState(mouseState);};// Keyboardvar keyboard = new Guacamole.Keyboard(document);keyboard.onkeydown = function (keysym) {guac.sendKeyEvent(1, keysym);};keyboard.onkeyup = function (keysym) {guac.sendKeyEvent(0, keysym);};function setWin() {let width = window.document.body.clientWidth;let height = window.document.body.clientHeight;guac.sendSize(1412, 924);scaleWin();}function handleMouseEvent(event) {// Do not attempt to handle mouse state changes if the client// or display are not yet availableif(!guac || !guac.getDisplay())return;event.stopPropagation();event.preventDefault();// Send mouse state, show cursor if necessaryguac.getDisplay().showCursor(true);};// Forward all mouse interaction over Guacamole connectionmouse.onEach(['mousemove'], handleMouseEvent);// Hide software cursor when mouse leaves displaymouse.on('mouseout', function hideCursor() {guac.getDisplay().showCursor(false);display.style.cursor = 'initial';});guac.getDisplay().getElement().addEventListener('mouseenter', function (e) {display.style.cursor = 'none';});
</script>
</body>
</html>

将页面放在Springboot项目的resource下的static下,启动程序,通过地址

http://ip:8080?did=1&tid=ssh访问,可以打开远程桌面。

可以看出guacamole-common和guacamole-common-js已经做了很好的封装,对于SSH、VNC、RDP这几种远程方式,可以很简单的实现。

接下来,SFTP的实现较为复杂,需要对SFTP上传下载的流程及guacamole封装的协议有较好的了解,才能实现。另外对于录屏及录屏的播放,因为我们的项目中需要把guac和java后端分开两台服务器部署,所以也要有点工作要做。这两部分内容见下一篇博文。

开源堡垒机Guacamole二次开发记录之一相关推荐

  1. 开源堡垒机Guacamole二次开发记录之二

    这篇主要记录录屏和SFTP的实现. 录屏及视频播放 对于录屏及录屏的播放,因为我们的项目中需要把guacd和java后端分开两台服务器部署,而guacamole的录屏是通过guacd程序录制的.我的要 ...

  2. 关于开源堡垒机Jumpserver二次开发

    针对市场上商业堡垒机动辄七八十万的投入,一般屌丝公司都是伤不起,只能砸机兴叹,开源堡垒机自然是一个方案.是拿来即用吗,NO,那是作死,不说安全.性能.并发,高可用,光维护资产.用户账号密码.授权,就可 ...

  3. php开源堡垒机,开源堡垒机在开发环境中的使用方案-麒麟开源堡垒机

    一.部署说明: 开发环境主要使用开发人员的PC或笔记本终端进行开发,开发完成后,将代码交付相应的负责人,负责人编译测试后,将代码上传到CVS备份,将程序上传到生产环境使用.这种管理模式主要存在如下问题 ...

  4. 开源堡垒机 Jumpserver 入门教程

    背景 笔者最近想起此前公司使用过的堡垒机系统,觉得用的很方便,而现在的公司并没有搭建此类系统,想着以后说不定可以用上:而且最近也有点时间,因此来了搭建堡垒机系统的兴趣,在搭建过程中参考了比较多的文档, ...

  5. jumpserver开源堡垒机

    jumpserver开源堡垒机 jumpserver简介 官方对jumpserver的介绍 jumpserver的优点 jumpserver功能 jumpserver安装部署 安装方式有两种 jump ...

  6. jumpserver 使用教程_开源堡垒机 Jumpserver 入门教程

    原标题:开源堡垒机 Jumpserver 入门教程 背景 笔者最近想起此前公司使用过的堡垒机系统,觉得用的很方便,而现在的公司并没有搭建此类系统,想着以后说不定可以用上:而且最近也有点时间,因此来了搭 ...

  7. 为什么开源堡垒机不可取

    运维风险管理系统,行业又称堡垒机,是目前信息化程度和信息安全需求较高的行业应用较为普遍的最新的安全防护技术平台,但是中小企业出于成本考虑,往往无法承担硬件堡垒机动辄数十上百万的费用,那么有没有适合中小 ...

  8. 堡垒机、运维堡垒机、开源堡垒机、云堡垒机全面解析

    一.概述 1.0.数据丢失危机1.1.面临的挑战 复制代码 二.堡垒机的概念和种类 2.0.网关型堡垒机2.1.运维审计型堡垒机2.1.1.主要功能 复制代码 三.主流堡垒机解决方案 3.0.使用开源 ...

  9. 采购堡垒机时候,选择开源堡垒机还是云堡垒机?

    很多企业在办理等保业务时候,采购堡垒机的时候,都在纠结选择开源堡垒机还是云堡垒机?这里我们小编就给大家详细介绍一下什么是开源堡垒机,什么是云堡垒机?采购堡垒机的时候,选择开源堡垒机还是云堡垒机? 什么 ...

最新文章

  1. docker redis 多个实例
  2. 盛大文学难逃“垄断”嫌疑,完美文学虎口夺食
  3. linux 命令 跳过yes,Linux命令之yes
  4. STM32串口发送中断
  5. 微软企业库配置工具菜单Edit Enterprise Library V5 Configuration
  6. First Chapter--Getting Started With Testing
  7. Acwing 1089. 烽火传递
  8. 从文本分类问题中的特征词选择算法追踪如何将数学知识,数学理论迁移到实际工程中去...
  9. java工作流引擎Jflow流程事件和流程节点事件设置
  10. 程序员笔试网上查答案,HR吐槽,网友:你们公司断网开发吗?
  11. 【渝粤教育】广东开放大学 计算机硬件组装与维护 形成性考核 (38)
  12. LayoutInflater
  13. 关于OpenFOAM的一些学习资料
  14. DB2之CLOB对象用法
  15. 大数据处理技术与人工智能技术
  16. @media scree 手机移动端屏幕自适应
  17. 皮皮虾小视频怎么去水印
  18. BigDecimal加减乘除计算
  19. 磁盘被写保护无法使用怎么办?
  20. Python基础了解 python自带IDLE编译

热门文章

  1. 无符号整型 有符号整型 相加
  2. 2021你挣了多少外快?务实社海哥告诉你2022副业做什么好?
  3. 《Java工程师成神之路》深入理解Java核心技术(基础篇)
  4. 华邦存储器W25Q80, W25Q16, W25Q32系列的spi通讯
  5. 2022-2028全球与中国塑料加工助剂市场现状及未来发展趋势
  6. 网络图片异步加载(用到多线程(线程池),java回调机制,图片缓存,图片的动画)
  7. 机器学习结合大数据面试_数据科学面试机器学习
  8. 信号在自由空间传播损耗的变化--fspl
  9. 喜欢一个人和爱一个人的区别
  10. 适合日常养生的中药有哪些?恒修堂给你答案