当前位置:   article > 正文

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

guacamole-common

简介

项目中需要用到堡垒机功能,调研了一大圈,发现了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等。

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-websocket</artifactId>
  4. </dependency>
  5. <dependency>
  6. <groupId>javax.servlet</groupId>
  7. <artifactId>servlet-api</artifactId>
  8. <version>2.5</version>
  9. <scope>provided</scope>
  10. </dependency>
  11. <dependency>
  12. <groupId>javax.websocket</groupId>
  13. <artifactId>javax.websocket-api</artifactId>
  14. <version>1.0</version>
  15. <scope>provided</scope>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.apache.guacamole</groupId>
  19. <artifactId>guacamole-common</artifactId>
  20. <version>1.5.1</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.guacamole</groupId>
  24. <artifactId>guacamole-ext</artifactId>
  25. <version>1.5.1</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.apache.guacamole</groupId>
  29. <artifactId>guacamole-common-js</artifactId>
  30. <version>1.5.1</version>
  31. <type>zip</type>
  32. <scope>runtime</scope>
  33. </dependency>

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

配置文件application.yml

  1. server:
  2. port: 8080
  3. servlet:
  4. context-path: /
  5. spring:
  6. servlet:
  7. multipart:
  8. enabled: false
  9. max-file-size: 1024MB
  10. datasource:
  11. url: jdbc:mysql://127.0.0.1:3306/guac?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8
  12. username: root
  13. password: 123456
  14. driver-class-name: com.mysql.cj.jdbc.Driver
  15. mybatis-plus:
  16. mapper-locations: classpath:mapper/*.xml
  17. guacamole:
  18. ip: 192.168.110.2
  19. port: 4822

WebSocket方式

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

  1. @ServerEndpoint(value = "/webSocket", subprotocols = "guacamole")
  2. @Component
  3. public class WebSocketTunnel extends GuacamoleWebSocketTunnelEndpoint {
  4. private String uuid;
  5. private static IDeviceLoginInfoService deviceLoginInfoService;
  6. private static String guacIp;
  7. private static Integer guacPort;
  8. private GuacamoleTunnel guacamoleTunnel;
  9. // websocket中,自动注入及绑定配置项必须用这种方式
  10. @Autowired
  11. public void setDeviceListenerService(IDeviceLoginInfoService deviceListenerService) {
  12. WebSocketTunnel.deviceLoginInfoService = deviceListenerService;
  13. }
  14. @Value("${guacamole.ip}")
  15. public void setGuacIp(String guacIp) {
  16. WebSocketTunnel.guacIp = guacIp;
  17. }
  18. @Value("${guacamole.port}")
  19. public void setGuacPort(Integer guacPort) {
  20. WebSocketTunnel.guacPort = guacPort;
  21. }
  22. @Override
  23. protected GuacamoleTunnel createTunnel(Session session, EndpointConfig endpointConfig) throws GuacamoleException {
  24. //从session中获取传入参数
  25. Map<String, List<String>> map = session.getRequestParameterMap();
  26. DeviceLoginInfoVo loginInfo = null;
  27. String did = map.get("did").get(0);
  28. tid = map.get("tid").get(0);
  29. tid = tid.toLowerCase();
  30. // 根据传入参数从数据库中查找连接信息
  31. loginInfo = deviceLoginInfoService.getDeviceLoginInfo(did, tid);
  32. if(loginInfo != null) {
  33. loginInfo.setPort(opsPort);
  34. }
  35. if(loginInfo != null) {
  36. //String wid = (map.get("width")==null) ? "1413" : map.get("width").get(0);
  37. //String hei = (map.get("height")==null) ? "925" : map.get("height").get(0);
  38. String wid = "1412";
  39. String hei = "924";
  40. GuacamoleConfiguration configuration = new GuacamoleConfiguration();
  41. configuration.setParameter("hostname", loginInfo.getIp());
  42. configuration.setParameter("port", loginInfo.getPort().toString());
  43. configuration.setParameter("username", loginInfo.getUser());
  44. configuration.setParameter("password", loginInfo.getPassword());
  45. if(tid.equals("ssh")) {
  46. configuration.setProtocol("ssh"); // 远程连接协议
  47. configuration.setParameter("width", wid);
  48. configuration.setParameter("height", hei);
  49. configuration.setParameter("color-scheme", "white-black");
  50. //configuration.setParameter("terminal-type", "xterm-256color");
  51. //configuration.setParameter("locale", "zh_CN.UTF-8");
  52. configuration.setParameter("font-name", "Courier New");
  53. configuration.setParameter("enable-sftp", "true");
  54. }
  55. else if(tid.equals("vnc")){
  56. configuration.setProtocol("vnc"); // 远程连接协议
  57. configuration.setParameter("width", wid);
  58. configuration.setParameter("height", hei);
  59. }
  60. else if(tid.equals("rdp")) {
  61. configuration.setProtocol("rdp"); // 远程连接协议
  62. configuration.setParameter("ignore-cert", "true");
  63. if(loginInfo.getDomain() !=null) {
  64. configuration.setParameter("domain", loginInfo.getDomain());
  65. }
  66. configuration.setParameter("width", wid);
  67. configuration.setParameter("height", hei);
  68. }
  69. GuacamoleClientInformation information = new GuacamoleClientInformation();
  70. information.setOptimalScreenHeight(Integer.parseInt(hei));
  71. information.setOptimalScreenWidth(Integer.parseInt(wid));
  72. GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
  73. new InetGuacamoleSocket(guacIp, guacPort),
  74. configuration, information
  75. );
  76. GuacamoleTunnel tunnel = new SimpleGuacamoleTunnel(socket);
  77. guacamoleTunnel = tunnel;
  78. return tunnel;
  79. }
  80. return null;
  81. }
  82. }

Servlet方式

从GuacamoleHTTPTunnelServlet类继承,重载doConnect方法

  1. @WebServlet(urlPatterns = "/tunnel")
  2. public class HttpTunnelServlet extends GuacamoleHTTPTunnelServlet {
  3. @Resource
  4. IDeviceLoginInfoService deviceLoginInfoService;
  5. @Value("${guacamole.ip}")
  6. private String guacIp;
  7. @Value("${guacamole.port}")
  8. private Integer guacPort;
  9. @Override
  10. protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
  11. //从HttpServletRequest获取请求参数
  12. String did = request.getParameter("did");
  13. String tid = request.getParameter("tid");
  14. tid = tid.toLowerCase();
  15. //根据参数从数据库中查找连接信息,主机ip、端口、用户名、密码等
  16. DeviceLoginInfoVo loginInfo = deviceLoginInfoService.getDeviceLoginInfo(did, tid);
  17. if(loginInfo != null) {
  18. GuacamoleConfiguration configuration = new GuacamoleConfiguration();
  19. configuration.setParameter("hostname", loginInfo.getIp());
  20. configuration.setParameter("port", loginInfo.getPort().toString());
  21. configuration.setParameter("username", loginInfo.getUser());
  22. configuration.setParameter("password", loginInfo.getPassword());
  23. if(tid.equals("ssh")) {
  24. configuration.setProtocol("ssh"); // 远程连接协议
  25. }
  26. else if(tid.equals("vnc")){
  27. configuration.setProtocol("vnc"); // 远程连接协议
  28. }
  29. else if(tid.equals("rdp")) {
  30. configuration.setProtocol("rdp"); // 远程连接协议
  31. configuration.setParameter("ignore-cert", "true");
  32. if(loginInfo.getDomain() != null) {
  33. configuration.setParameter("domain", loginInfo.getDomain());
  34. }
  35. configuration.setParameter("width", "1024");
  36. configuration.setParameter("height", "768");
  37. }
  38. GuacamoleSocket socket = new ConfiguredGuacamoleSocket(
  39. new InetGuacamoleSocket(guacIp, guacPort),
  40. configuration
  41. );
  42. GuacamoleTunnel tunnel = new SimpleGuacamoleTunnel(socket);
  43. return tunnel;
  44. }
  45. return null;
  46. }
  47. }

前端页面

我用的是最基本的html+js

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <link rel="stylesheet" type="text/css" href="guacamole.css"/>
  6. <title>guac</title>
  7. <style>
  8. </style>
  9. </head>
  10. <body>
  11. <div id="mainapp">
  12. <!-- Display -->
  13. <div id="display"></div>
  14. </div>
  15. <!-- Guacamole JavaScript API -->
  16. <script type="text/javascript" src="guacamole-common-js/all.js"></script>
  17. <script type="text/javascript">
  18. function getUrlParam(name) {
  19. var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  20. var r = window.location.search.substr(1).match(reg);
  21. if(r != null) {
  22. return decodeURI(r[2]);
  23. }
  24. return null;
  25. }
  26. var user = getUrlParam('user');
  27. var devid = getUrlParam('did');
  28. var typeid= getUrlParam('tid');
  29. // var width = getUrlParam('width');
  30. // var height = getUrlParam('height');
  31. // Get display div from document
  32. var display = document.getElementById("display");
  33. var uuid;
  34. var tunnel = new Guacamole.ChainedTunnel(new Guacamole.WebSocketTunnel("webSocket"));
  35. var guac = new Guacamole.Client(tunnel);
  36. // Add client to display div
  37. display.appendChild(guac.getDisplay().getElement());
  38. tunnel.onuuid = function(id) {
  39. uuid = id;
  40. }
  41. // Connect
  42. guac.connect('did='+devid+'&tid='+typeid+'&user='+user);
  43. // Disconnect on close
  44. window.onunload = function() {
  45. guac.disconnect();
  46. }
  47. // Mouse
  48. var mouse = new Guacamole.Mouse(guac.getDisplay().getElement());
  49. mouse.onmousedown =
  50. mouse.onmousemove = function(mouseState) {
  51. guac.sendMouseState(mouseState);
  52. };
  53. mouse.onmouseup = function(mouseState) {
  54. vueapp.showfile = false;
  55. guac.sendMouseState(mouseState);
  56. };
  57. // Keyboard
  58. var keyboard = new Guacamole.Keyboard(document);
  59. keyboard.onkeydown = function (keysym) {
  60. guac.sendKeyEvent(1, keysym);
  61. };
  62. keyboard.onkeyup = function (keysym) {
  63. guac.sendKeyEvent(0, keysym);
  64. };
  65. function setWin() {
  66. let width = window.document.body.clientWidth;
  67. let height = window.document.body.clientHeight;
  68. guac.sendSize(1412, 924);
  69. scaleWin();
  70. }
  71. function handleMouseEvent(event) {
  72. // Do not attempt to handle mouse state changes if the client
  73. // or display are not yet available
  74. if(!guac || !guac.getDisplay())
  75. return;
  76. event.stopPropagation();
  77. event.preventDefault();
  78. // Send mouse state, show cursor if necessary
  79. guac.getDisplay().showCursor(true);
  80. };
  81. // Forward all mouse interaction over Guacamole connection
  82. mouse.onEach(['mousemove'], handleMouseEvent);
  83. // Hide software cursor when mouse leaves display
  84. mouse.on('mouseout', function hideCursor() {
  85. guac.getDisplay().showCursor(false);
  86. display.style.cursor = 'initial';
  87. });
  88. guac.getDisplay().getElement().addEventListener('mouseenter', function (e) {
  89. display.style.cursor = 'none';
  90. });
  91. </script>
  92. </body>
  93. </html>

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

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

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/550720
推荐阅读
相关标签
  

闽ICP备14008679号