Centos7安装Guacamole1.0.0以及自定义客户端 原

1、概述

    Guacamole大致流程如下图所示,本文目的是快速完成安装部署,如需深入了解参阅官方文档或其他博客。

    准备两台机器,一个是代理机一个是目标机器。

2、安装依赖

    基础依赖:

  • jdk1.8+
  • tomcat8+

    系统依赖: 

yum install -y cairo-devel libjpeg-turbo-devel libpng-devel uuid-devel
yum install -y freerdp-devel pango-devel libssh2-devel libvncserver-devel pulseaudio-libs-devel openssl-devel libvorbis-devel libwebp-devel

3、部署

3.1、guacamole

    访问http://guacamole.apache.org/releases/1.0.0/下载server包,然后上传到代理机目录下,如/opt/guacamole。接着解压,cd到目录执行

./configure --with-init-dir=/etc/init.d
make && make install
ldconfig

    执行完成后,编译或下载war包,这里是war包下载地址http://mirror.bit.edu.cn/apache/guacamole/1.0.0/binary/,放入到tomcat的webapp下,然后在/opt/guacamole目录下创建三个文件:

guacamole.properties

guacd-hostname: 192.168.1.96
guacd-port:     4822

user-mapping.xml 

<user-mapping>
    <!-- 登陆账号密码 -->
    <authorize username="admin" password="1">
        <connection name="win">
            <protocol>rdp</protocol>
            <param name="hostname">192.168.4.181</param>
            <param name="port">3389</param>
            <!-- 需要链接的服务器的账号密码 -->
            <param name="username">shy</param>
            <param name="password">1</param>
            <!-- 大小自动变化 -->
            <param name="resize-method">display-update</param>
            <!-- 分辨率 -->
            <param name="dpi">100</param>
        </connection>
        <connection name="linux_cmd">
            <protocol>ssh</protocol>
            <param name="hostname">192.168.4.181</param>
            <param name="port">22</param>
            <!-- 需要链接的服务器的账号密码 -->
            <param name="username">shy</param>
            <param name="password">cmgplex!@#</param>
        </connection>
        <connection name="linux_win">
            <protocol>rdp</protocol>
            <param name="hostname">192.168.4.181</param>
            <param name="port">1</param>
            <!-- 需要链接的服务器的账号密码 -->
            <param name="username">shy</param>
            <param name="password">1</param>
        </connection>
    </authorize>
</user-mapping>

logback.xml

<configuration>
 <appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>/opt/guacamole/guacamole.log</file>
   <encoder>
     <pattern>%msg%n</pattern>
   </encoder>
 </appender>
 <root level="info">
   <appender-ref ref="FILE" />
 </root>
</configuration>

    需要注意的是,如果guacamole.properties配置不起作用,代理会始终绑定在127.0.0.1上,导致其他服务器无法连接,这时可以手动启动加参数绑定IP即可

guacd -l 192.168.43.1

    准备完成后配置环境变量

export CATALINA_HOME=/opt/guacamole/apache-tomcat-8.5.43
export CATALINA_BASE=/opt/guacamole/apache-tomcat-8.5.43
export GUACAMOLE_HOME=/opt/guacamole

    最后启动服务

service guacd start  #启动guacamole
./startup.sh  #启动tomcat

3.2目标机器

    如果目标是windows,或者命令行linux直接配置用户名密码等即可,如果目标是linux桌面,那么需要在目标机器上安装vnc或者rdp服务,以rdp为例,部署过程如下:

i /etc/yum.repos.d/xrdp.repo

[xrdp]
name=xrdp
baseurl=http://li.nux.ro/download/nux/dextop/el7/x86_64/
enabled=1
gpgcheck=0

yum -y install xrdp tigervnc-server

#启动服务:
systemctl start xrdp.service
systemctl enable xrdp.service

    最后打开http://localhost:8080/guacamole,即可看到页面

三、自定义客户端

    有时候需要集成到自己的系统,而不是使用自带的client,Guacamole支持多种客户端API,以java为例,基于springboot流程如下:

    添加依赖

<!-- guacamole -->
		<dependency>
			<groupId>org.apache.guacamole</groupId>
			<artifactId>guacamole-common</artifactId>
			<version>1.0.0</version>
		</dependency>

    Application增加注解@ServletComponentScan,复制官方demo的servlet并稍作修改如下:

    DummyGuacamoleTunnelServlet.java

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.InetGuacamoleSocket;
import org.apache.guacamole.net.SimpleGuacamoleTunnel;
import org.apache.guacamole.protocol.ConfiguredGuacamoleSocket;
import org.apache.guacamole.protocol.GuacamoleConfiguration;
import org.apache.guacamole.servlet.GuacamoleHTTPTunnelServlet;
import org.springframework.beans.factory.annotation.Value;

/**
 * Simple tunnel example with hard-coded configuration parameters.
 */
@WebServlet(urlPatterns = "/tunnel")
public class DummyGuacamoleTunnelServlet extends GuacamoleHTTPTunnelServlet {
	@Value("${guacamole.guacd.host}")
	private String guacdHost;
	@Value("${guacamole.guacd.port}")
	private Integer guacdPort;
	@Value("${guacamole.target.protocol}")
	private String targetProtocol;
	@Value("${guacamole.target.host}")
	private String targetHost;
	@Value("${guacamole.target.port}")
	private String targetPort;
	@Value("${guacamole.target.username}")
	private String targetUsername;
	@Value("${guacamole.target.password}")
	private String targetPassword;

	private static final long serialVersionUID = 1126569778799758654L;

	@Override
	protected GuacamoleTunnel doConnect(HttpServletRequest request) throws GuacamoleException {
		GuacamoleConfiguration config = new GuacamoleConfiguration();
		config.setProtocol(this.targetProtocol);
		config.setParameter("hostname", this.targetHost);
		config.setParameter("port", this.targetPort);
		config.setParameter("username", this.targetUsername);
		config.setParameter("password", this.targetPassword);
		return new SimpleGuacamoleTunnel(
				new ConfiguredGuacamoleSocket(new InetGuacamoleSocket(this.guacdHost, this.guacdPort), config));

	}

}

    application.properties增加如下配置

guacamole.guacd.host=xxxx
guacamole.guacd.port=4822
guacamole.target.protocol=rdp
guacamole.target.host=192.168.1.1
guacamole.target.port=3389
guacamole.target.username=ad
guacamole.target.password=1

    最后增加页面,页面很简单注意引用js即可

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
-->
<html>

    <head>
        <link rel="stylesheet" type="text/css" href="/media/css/guacamole.css"/>
        <!-- Guacamole JavaScript API -->
        <script type="text/javascript" src="/media/js/all.min.js"></script>
        <link rel="shortcut icon" href="/media/image/favicon.ico" />
    </head>

    <body>

        <!-- Display -->
        <div id="display"></div>
        <!-- Init -->
        <script type="text/javascript"> /* <![CDATA[ */

            // Get display div from document
            var display = document.getElementById("display");

            // Instantiate client, using an HTTP tunnel for communications.
            var guac = new Guacamole.Client(
                new Guacamole.HTTPTunnel("/tunnel")
            );

            // Add client to display div
            display.appendChild(guac.getDisplay().getElement());
            
            // Error handler
            guac.onerror = function(error) {
                alert(error);
            };

            // Connect
            guac.connect();

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

            // Mouse
            var mouse = new Guacamole.Mouse(guac.getDisplay().getElement());

            mouse.onmousedown = 
            mouse.onmouseup   =
            mouse.onmousemove = function(mouseState) {
                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);
            };

        /* ]]> */ </script>

    </body>

</html>

    js可以在官方demo里找到。

    这里展示的是比较简单的自定义客户端,根据自身业务可以做到更强大的功能。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏Java编程指南

Java常见面试题及答案

Java 虚拟机是一个可以执行 Java 字节码的虚拟机进程。Java 源文件被编译成能被 Java 虚拟机执行的字节码文件。 Java 被设计成允许应用程序...

9620
来自专栏DotNet程序园

WTM 构建DotNetCore开源生态,坐而论道不如起而行之

我记得大约是18、9岁的时候接触的java,比dotnet更早,毕竟java更早出来的。但是后来有了C#之后,我喜新厌旧了,原因很简单,好比两个姑娘,C#更美,...

12330
来自专栏Java编程指南

深入理解 Java中的 流 (Stream)

最近在看《Hadoop:The Definitive Guide》,对其分布式文件系统HDFS的Streaming data access不能理解。基于流的数据...

7210
来自专栏Java编程指南

一篇文章让你弄懂SpringMVC 拦截器

第一种方式是要定义的Interceptor类要实现了Spring的HandlerInterceptor 接口

6820
来自专栏Java编程指南

java基础教程|java定时器

在我们工作当中总有需要按时间计划执行简单任务的情况,例如定时进行商品的抢购,闹钟功能等。下面我们来讲解一下JAVA自带的入门级定时器 Timer是...

14240
来自专栏Java编程指南

一次JDBC与MySQL因“CST”时区协商误解导致时间差了13或14个小时

注意到这里 this.session.getDefaultTimeZone() 得到的是刚才那个 CST -0600

24750
来自专栏Java编程指南

Java 如何抛出异常、自定义异常

1、定义 : 一个方法不处理这个异常,而是调用层次向上传递,谁调用这个方法,这个异常就由谁来处理。

9630
来自专栏Java编程指南

IDEA 配置优化 提高开发效率

如何去掉呢? 打开IDEA 的preferences|Editor|Code Style, 去掉下图中的两个勾选:

10420
来自专栏Java编程指南

SpringBoot整合ActiveMQ

MQ的作用在前面文章中我已经介绍过了,那么今天就教大家学习整合ActiveMQ,篇幅有点长希望大家能看完。

7210
来自专栏Java编程指南

java核心数据结构总结

 JDK提供了一组主要的数据结构的实现,如List、Set、Map等常用结构,这些结构都继承自java.util.collection接口。

9520

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励