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

news2024/9/20 21:46:48

简介

项目中需要用到堡垒机功能,调研了一大圈,发现了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: 8080
  servlet:
    context-path: /

spring:
  servlet:
    multipart:
      enabled: false
      max-file-size: 1024MB
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/guac?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

mybatis-plus:
  mapper-locations: classpath:mapper/*.xml

guacamole:
  ip: 192.168.110.2
  port: 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中,自动注入及绑定配置项必须用这种方式
    @Autowired
    public 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;
    }

    @Override
    protected 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 {
    @Resource
    IDeviceLoginInfoService deviceLoginInfoService;

    @Value("${guacamole.ip}")
    private String guacIp;
    @Value("${guacamole.port}")
    private Integer guacPort;

	@Override
	protected 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 document
    var 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 div
    display.appendChild(guac.getDisplay().getElement());

    tunnel.onuuid = function(id) {
        uuid = id;
    }

    // Connect
    guac.connect('did='+devid+'&tid='+typeid+'&user='+user);

    // Disconnect on close
    window.onunload = function() {
        guac.disconnect();
    }

    // Mouse
    var 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);
    };

    // Keyboard
    var 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 available
        if(!guac || !guac.getDisplay())
            return;

        event.stopPropagation();
        event.preventDefault();
        // Send mouse state, show cursor if necessary
        guac.getDisplay().showCursor(true);
    };

    // Forward all mouse interaction over Guacamole connection
    mouse.onEach(['mousemove'], handleMouseEvent);
    // Hide software cursor when mouse leaves display
    mouse.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后端分开两台服务器部署,所以也要有点工作要做。这两部分内容见下一篇博文。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/740227.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

LabVIEW-模拟传感器采集数据并预测数据

一、题目 已知某传感器过去的一段时间内采集的数据为d1,d2,d3,......,dn&#xff0c;现欲以m点的数据宽度&#xff0c;预测 tao 步后的数据值&#xff0c;即将一维的时间序列数据重构为如下m1列的形式&#xff1a; d(1) d(2 ) ....... d(m), d(mtao) d(2) d(…

Python第一天学习之Python数据类型

1.数据类型介绍 2.数据转换 money 50 money "giao" print(money)Python会进行自动的转换&#xff0c;但是&#xff0c;运算就错误,在赋值时可以直接转换&#xff0c;但是在计算时无法直接转换。 money 50 money "giao" print(money1)数据类型转换 …

OpenSSL安装使用(四):DES加解密功能测试

OpenSSL是一个开放源代码的安全套接字层密码库&#xff0c;它主要用于互联网安全协议的实现&#xff0c;具有加密&#xff0c;认证和安全访问等功能。OpenSSL由Eric Young和Tim Hudson共同开发&#xff0c;源自SSLeay开放源代码密码库&#xff0c;后来和内容安全管理密码库&…

探索基于VSCode的远程开发插件,进行远程指令和本地指令的运行

需求 最近在研究VSCode的插件的时候&#xff0c;使用了VSCode的远程开发套件&#xff0c;Remote - SSH可以在本地的VSCode上登录远程机器&#xff0c;打开远程机器的某个文件夹进行开发。并且在开发过程中&#xff0c;能够使用几乎所有的VSCode插件。 当你使用这个插件链接到远…

Python(三):Python开发环境搭建

❤️ 专栏简介&#xff1a;本专栏记录了我个人从零开始学习Python编程的过程。在这个专栏中&#xff0c;我将分享我在学习Python的过程中的学习笔记、学习路线以及各个知识点。 ☀️ 专栏适用人群 &#xff1a;本专栏适用于希望学习Python编程的初学者和有一定编程基础的人。无…

mybatis 注解方式操作 sql

前言:注解的方式在某些查询的时候还是比较方便的 mybatis注解配置 mapUnderscoreToCamelCase 配置Select 注解Insert 注解Delete 注解 和 Update 注解Provider 注解 mapUnderscoreToCamelCase 配置 别名设置&#xff0c;mapUnderscoreToCamelCase 配置 配置可以将 带下划线 sq…

python车牌识别

识别结果 蓝牌 绿牌 黄牌 环境 python:3.9\opencv:4.5.1 环境安装 pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple pip3 install hyperlpr -i https://pypi.tuna.tsinghua.edu.cn/simple 修改 cd /Library/Frameworks/Python.framework/Versi…

巧用word 邮件合并批量输出报告

在实际调查中&#xff0c;往往遇到很多统计信息要单独生成调查报告。word 邮件合并就能很好的帮助我们快速实现批量产出报告。 具体案例如下&#xff1a; 目前入河排污口调查正在如火如荼开展&#xff0c;我们排查收集了大量信息&#xff0c;整理为表格。 要将这些表格输出为…

算法训练营第三十四天||1005.K次取反后最大化的数组和 ● 134. 加油站● 135. 分发糖果

1005.K次取反后最大化的数组和 自己思路&#xff1a;自己想的就是把数组按从小到大排序&#xff0c;然后把前k小的数字都取反&#xff0c;然后相加起来&#xff0c;这个思路没有考虑到前k个小的数字中不全是负数的情况&#xff0c;比如这个数组全大于0的数&#xff0c;这种情况…

Redis熟悉到精通:开篇

文章目录 要点使用缓存技术的目的需要缓存机制的数据种类Redis学习资料 要点 掌握数据结构和缓存的基本使用方法; 掌握支撑Redis实现高可靠、高性能的技术; 高可靠 Redis之所以可以实现高可靠、高性能&#xff0c;和它的持久化机制、主从复制机制、哨兵、故障自动恢复、切片集…

模拟对讲机会被数字对讲机取代吗?

经常在网上看到有网友讨论&#xff0c;模拟对讲机是不是快被淘汰了&#xff0c;要被数字对讲机取代了。其实不管是模拟还是数字对讲机&#xff0c;都有其各自的优势&#xff0c;数字对讲要想全面取代模拟对讲&#xff0c;还是有些为时尚早。 传统的模拟对讲机主要是将语音、信…

用Python监控并分析城市空气质量

大家好&#xff0c;同为发展中国家&#xff0c;印度也受到空气质量问题的困扰&#xff0c;本文就以印度的城市为例进行数据分析。使用简单的Python代码&#xff0c;分析城市空气质量及其每天在全国范围内&#xff08;即印度水平&#xff09;的排名。 在开始之前&#xff0c;先介…

(转载)极限学习机(extreme learning machine, ELM)的回归拟合及分类(matlab实现)

单隐含层前馈神经网络(single-hidden layer feedforward neural network,SLFN)以其良好的学习能力在许多领域中得到了广泛的应用。然而&#xff0c;传统的学习算法(如BP算法等)固有的一些缺点&#xff0c;成为制约其发展的主要瓶颈。前馈神经网络大多采用梯度下降方法&#xff…

阿里云服务器安装mysql并用idea连接

文章目录 前言一.购置阿里云服务器——不定时二.在服务器安装mysql——用时5分钟三.打开服务器mysql的端口——用时2分钟1.找到安全组2.打开默认mysql的3306端口 三.打开idea连接数据库——5分钟四.总用时大约20分钟 前言 记录第一次通过idea连接安装在服务器上的数据库——排错…

在vite创建的vue3项目中加载Cesium世界街道地图的底图

在vite创建的vue3项目中加载Cesium世界街道地图的底图 使用vite创建vue3项目 npm create vitelatestcd到创建的项目文件夹中 npm install安装Cesium npm i cesium vite-plugin-cesium vite -D配置 vite.config.js文件 import { defineConfig } from vite import vue from vitej…

ETHERCAT转PROFINET协议网关连接ethercat网线接口定义

大家好&#xff0c;今天我要给大家介绍一款神奇的产品&#xff0c;YC-PN-ECT&#xff0c;它是一款 PROFINET 从站功能的通讯网关&#xff0c;可以将 PROFINET 网络和 ETHERCAT 网络连接起来&#xff0c;让不同厂家的 PLC 能够互相通信&#xff0c;真是太酷了&#xff01; PEO…

T100新增栏位,配置ACC作业并提供开窗作业维护

需求分析:将xxxx作业中的一个界面新增一个栏位,并提供开窗功能进行资料的维护。 一、ACC自适应配置文档开发 1.1 azzi650 注册应用分类码【ACC】 新增一个资料 应用分类码【数字】说明 填写文字说明作业编号:这个时候是空的作业名称:也是空的1.2 azzi910 作业基本维护 这个…

做爬虫如何选择Python和C语言

目录 优劣势分析 Python 进行爬虫的优势&#xff1a; Python 进行爬虫的劣势&#xff1a; C进行爬虫的优势&#xff1a; C进行爬虫的劣势&#xff1a; 示例代码说明 Python 示例代码&#xff1a; C语言 示例代码&#xff1a; 怎么选择 优劣势分析 Python 进行爬虫的优…

Java设计模式之创建型-原型模式(UML类图+案例分析)

一、基础概念 通过复制已有对象作为原型&#xff0c;通过复制该原型来返回一个新对象&#xff0c;而不是新建对象&#xff0c;说白了就是不断复制相同的对象罢了。 二、UML类图 三、角色分析 角色描述抽象原型类规定了具体的原型对象必须实现的clone()方法具体原型类实现抽象…

倒计时1天!LeaTech全球CTO领导力峰会TVP四周年庆典即将开幕

引言 3 月 4 日&#xff0c;上海扬子江丽笙精选酒店&#xff0c;LeaTech 全球 CTO 领导力峰会暨腾讯云 TVP 四周年、CTO 训练营校友联合庆典即将开幕。本次 LeaTech 全球 CTO 领导力峰会以“寻光之旅”为主题&#xff0c;腾讯云 TVP 携手 51CTO&#xff0c;联合邀请业内资深领袖…