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

这篇具有很好参考价值的文章主要介绍了开源堡垒机Guacamole二次开发记录之一。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

简介

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

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

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

Guacamole项目的主页如下:

Apache Guacamole™

Guacamole项目的架构如下图:

guacamole堡垒机,Java,前端,springboot,ssh,VNC,RDP,Web远程桌面

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

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

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

guacamole堡垒机,Java,前端,springboot,ssh,VNC,RDP,Web远程桌面

 我们项目中有很多自己的业务需求和界面需求,所以,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后端分开两台服务器部署,所以也要有点工作要做。这两部分内容见下一篇博文。文章来源地址https://www.toymoban.com/news/detail-562611.html

到了这里,关于开源堡垒机Guacamole二次开发记录之一的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 开源项目 | 可二次开发的开源后台、支持支付系统通用的支付、对账、清算、账户管理、支付订单管理等功能

    在RuoYi项目基础上改造,通过多模块的方式整合其他经常被用到的功能模块,特别感谢RuoYi。基于SpringBoot2.0的开发的系统 易读易懂、界面简洁美观。具备支付系统通用的支付、对账、清算、账户管理、支付订单管理等功能;目前已接通微信支付渠道,应用微信公众号商城 在此

    2024年01月21日
    浏览(52)
  • [开源]一个低代码引擎,支持在线实时构建低码平台,支持二次开发

    TinyEngine低代码引擎使能开发者定制低代码平台,支持在线实时构建低码平台,支持二次开发或集成低码平台能力。 使用MIT开源协议 TinyEngine是一个低代码引擎,基于这个引擎可以构建或者开发出不同领域的低代码平台。 跨端跨框架前端组件 支持在线实时构建、支持二次开发

    2024年02月07日
    浏览(29)
  • UG\NX二次开发 使用录制功能录制操作记录时,如何设置默认的开发语言?

    文章作者:里海 来源网站:王牌飞行员_里海_里海NX二次开发3000例,CC++,Qt-CSDN博客 NX二次开发使用BlockUI设计对话框时,如何设置默认的代码语言?   依次打开“文件”-“实用工具”-“用户默认设置”-“用户界面”-“操作记录”-“C++”。       

    2024年02月11日
    浏览(30)
  • 开源堡垒机Jumpserver

    Jumpserver 是全球首款完全开源的堡垒机,使用 GNU GPL v2.0 开源协议,是符合 4A 的运维安全审计系统。 Jumpserver 使用 Python / Django 进行开发,遵循 Web 2.0 规范,配备了业界领先的 Web Terminal 解决方案,交互界面美观、用户体验好。 Jumpserver 采纳分布式架构,支持多机房跨区域部署

    2024年02月03日
    浏览(31)
  • JumpServer开源堡垒机安装配置

    JumpServer 堡垒机帮助企业以更安全的方式管控和登录各种类型的资产。 支持 官网地址:https://www.jumpserver.org/index.html JumpServer 采用分层架构,分别是负载层、接入层、核心层、数据层、存储层。 JumpServer 应用架构图如下: 2.1、下载 访问官网地址下载资源: https://community.fit2

    2024年02月14日
    浏览(33)
  • 开源堡垒机JumpServer的资产及用户配置说明

    很早之前就了解过开源堡垒机JumpServer,曾经部署并体验效果,感觉还不错,而且也用过某些客户的商业堡垒机,体验效果和这个开源堡垒机很相似。由于官方的资产及用户配置文档,我个人的理解思维方式感觉有点难,然后在此做记录配置资产及用户的步骤,主要是刚开始接

    2023年04月18日
    浏览(36)
  • Ubuntu 22.04.4+Jumpserver开源堡垒机安装部署

    JumpServer 是广受欢迎的开源堡垒机,是符合 4A 规范的专业运维安全审计系统。JumpServer 帮助企业以更安全的方式管控和登录所有类型的资产,实现事前授权、事中监察、事后审计,满足等保合规要求。 Jumpserver的官方文档非常齐全,可以直接参考官方文档,这里引用官方的ju

    2024年04月08日
    浏览(34)
  • 信雅纳网络测试的二次开发集成:XOA(Xena Open-Source Automation)开源自动化测试

    目录 XOA是什么 XOA CLI XOA Python API ​XOA Python Test Suite/测试套件 XOA Converter Source Code XOA(Xena Open-Source Automation) 是一个开源的测试自动化框架,追求“高效、易用、灵活”的跨操作系统的开发框架。能与Xena现有解决方案无缝配合,借助XOA可调用Xena(Z系列打流仪、E系列损伤仪)

    2024年04月17日
    浏览(30)
  • 飞致云旗下的jumpserver开源堡垒机的部署学习

    硬件配置: 2核,4G内存,50G磁盘(最低配置要求)   准备三台可以访问互联网的 64 位 Linux 主机 一个master 两个node(node节点内存可适当的放宽)#可以集群配置 基本配置: 最小化配置,ssh ,ntp  堡垒机的部署分为两种方式: 在线安装 离线安装 我们以v3版本来进行: 会拉取

    2024年03月20日
    浏览(37)
  • K8S系列文章之 开源的堡垒机 jumpserver

    一、jumpserver作为一款开源的堡垒机,不管是企业还是个人,我觉得都是比较合适的,而且使用也比较简单。 二、这里记录一下安装和使用过程。 1、安装,直接docker不是就行 目前没有看到3版本的,3版本的功能更加强大,可以官网学习一下。 2、使用 资产添加不用细讲,主要

    2024年02月14日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包