NoVNC—以Web方式交付VNC远程连接

https://github.com/novnc/noVNC

一、noVNC是什么

noVNC是一个 HTML5 VNC 客户端,采用 HTML 5 WebSockets, Canvas 和 JavaScript 实现,noVNC 被普遍用在各大云计算、虚拟机控制面板中,比如 OpenStack Dashboard 和 OpenNebula Sunstone 都用的是 noVNC。
noVNC采用WebSockets实现,但是目前大多数VNC服务器都不支持 WebSockets,所以noVNC是不能直接连接 VNC 服务器的,需要一个代理来做WebSockets和TCP sockets 之间的转换。这个代理在noVNC的目录里,叫做websockify 。

目标:通过浏览器远程访问Windows桌面。

原理:浏览器不支持VNC,所以不能直接连接VNC,但是可以使用代理,使用noVNC通过WebSocket建立连接,而VNC Server不支持WebSocket,所以需要开启Websockify代理来做WebSocket和TCP Socket之间的转换。

二、CentOS 7 安装novnc

1. 环境

1
2
3
4
5
6
7
[root@novnc ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@novnc ~]# ifconfig eth0|awk 'NR==2{print $2}'
10.0.0.60
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

2. 安装配置

  • 安装桌面环境

如何在CentOS7上安装桌面环境

  • 安装tigervnc
1
2
3
4
#安装依赖软件包
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y git tigervnc-server -y
  • 启动VNC服务并输入密码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@novnc ~]# vncserver :1

You will require a password to access your desktops.

Password:
Verify:
Would you like to enter a view-only password (y/n)? y
Password:
Verify:

New 'novnc:1 (root)' desktop is novnc:1

Creating default startup script /root/.vnc/xstartup
Creating default config /root/.vnc/config
Starting applications specified in /root/.vnc/xstartup
Log file is /root/.vnc/novnc:1.log

#当执行vncserver :1设置为1时,下面要运行VNC是的端口号应该是5900+1,那就是5901,VNC的默认端口是5900。

#写入开机自启动
chmod +x /etc/rc.d/rc.local
echo '/usr/bin/vncserver :1' >>/etc/rc.d/rc.local

  • 查看日志
1
 cat /root/.vnc/novnc:1.log
  • 查看端口
1
2
3
[root@novnc utils]# netstat -lntup|grep 59
tcp        0      0 0.0.0.0:5901            0.0.0.0:*               LISTEN      8380/Xvnc          
tcp6       0      0 :::5901                 :::*                    LISTEN      8380/Xvnc
  • 安装noVNC
1
git clone https://github.com/novnc/noVNC.git
  • 创建安全连接(一路回车)

VNC的默认会话不是安全的,我们需要创建一个安全的VNC连接,会发现提示需要输入内容,这些字段我们并不需要都进行填写,当启动noVNC时,websockify将自动装载证书。

1
2
3
#要将生成的self.pem文件放到noVNC/utils底下
cd ./noVNC/utils/
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem

  • 运行noVNC
1
2
3
[root@novnc noVNC]# pwd
/root/noVNC
[root@novnc noVNC]# ./utils/launch.sh --vnc localhost:5901

  • 测试进行访问连接
1
http://ip:6080/vnc.html

image.png

  • 设置开机自启动
1
echo './root/noVNC/utils/launch.sh --vnc localhost:5901 &' >>/etc/rc.d/rc.local
  • 安装numpy,解决连接速度慢:

https://sourceforge.net/projects/numpy/files/

1
2
3
4
5
6
#安装python依赖
yum install python-dev python-devel -y
#上传压缩包解压
unzip numpy-1.11.2.zip
cd numpy-1.11.2/
python setup.py install

3. 一键安装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@novnc scripts]# cat novnc_install.sh
#!/bin/bash
##############################################################
# File Name: novnc_install.sh
# Version: V1.0
# Author: lcx
# Organization: www.in365robot.com
##############################################################
# 环境优化
setenforce 0
systemctl stop firewalld
systemctl disable firewalld

# install vncserver && git
yum install -y epel*
yum install tigervnc-server git -y

#启动VNC服务并输入密码
echo '请输入密码:'
vncserver :1

# download noVNC
git clone git://github.com/kanaka/noVNC
# 创建安全连接
cd ./noVNC/utils/
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
# run noVNC
cd ../
./utils/launch.sh --vnc localhost:5901 &
echo '请访问 http://ip:6080/vnc.html '
# 自启动
chmod +x /etc/rc.d/rc.local
echo '/usr/bin/vncserver :1' >>/etc/rc.d/rc.local
echo './root/noVNC/utils/launch.sh --vnc localhost:5901 &' >>/etc/rc.d/rc.local

1
2
3
[root@novnc scripts]# chmod +x /server/scripts/novnc_install.sh
[root@novnc scripts]# ll /server/scripts/novnc_install.sh
-rwxr-xr-x. 1 root root 903 Dec 24 18:25 /server/scripts/novnc_install.sh

三、Windows 安装novnc

实现目标:通过浏览器远程访问Windows桌面

  • 准备一台Windows7 32位的虚拟机

1. 环境

  • UtralVNC:

    Windows环境下的VNC Server,在你需要访问的目标机器上安装。

    此处提示:生产需求为Windows7 32位,在下载UtralVNC软件时请下载之前的较旧版本,最新版会不兼容。

  • Node.js:

    用于执行Websockify.js。Websockify还有Python版本的,不过在Windows下不可以成功。

  • noVNC

    noVNC是一个HTML5 VNC客户端

  • websockify-js

    noVNC是通过websockt建立链接,而VNC server不支持websocket,所以需要开启websockify代理来做 WebSockets 和 TCP sockets 之间的转换。


2. 安装utralNVC server

将UltraVNC软件复制到需要远程协助的电脑上双击打开安装。

在【Select Components】界面按需要选择上需要的组件,这里将【UltraVNC Server】和【UltraVNC Viewer】选择上。当需要远程协助时安装的电脑必须选择上【UltraVNC Server】,【UltraVNC Viewer】是用来远程协助的工具。点击【next】进行下一步安装。

安装完成后桌面就会有快捷方式,天蓝色的是服务的快捷方式,浅绿色是远程连接的工具。同时系统托盘上会有一个天蓝色的眼睛图标的程序,这个就是vnc server。

右键小眼睛图标打开菜单,点击【Start Service】并重启电脑。

点击【Admin Properties】打开配置连接的密码,连接的密码分为可操作的密码跟只看的密码。

3. 安装Node.js

选择npm package manager

node.js安装完成后,需要安装ws、optimist模块(执行websockify.js文件所需)

1
npm install ws

1
npm install optimist

1
npm install mime-types

安装完ws和optimist后会在C:UsersAdministrator下生成node_modules目录

4. 安装noVNC和websockify

把noVNC.zip解压到node_modules目录下,再把websockify-master.zip解压到noVNC目录下。

5. 执行websockify.js

转发9000端口的http链接到5900端口(UltraVNC Server的默认端口为5900)

1
2
3
4
5
6
7
C:Users
oot
ode_modules
oVNCwebsockify-js-masterwebsockify>node websockify.js --web C:Users
oot
ode_modules
oVNC 9000 localhost:5900

在浏览器访问http://192.168.1.163:9000/会出现这样的提示

需要把websockify.js中的filename += ‘/index.html’改成filename += ‘/vnc.html’;

点击链接输入UltraVNC设置的密码 完成。

6. 防火墙新建入站规则

如果被禁止访问,需要允许访问的9000端口进入

7. Windows开机自启动设置

windows自启动bat脚本链接

1
2
3
4
5
6
7
8
9
#1. 桌面新建自启动批处理文件start.bat
@echo off
start "cmd" "cd C:Users
oot
ode_modules
oVNCwebsockify-js-masterwebsockify" & node websockify.js --web C:Users
oot
ode_modules
oVNC 9000 localhost:5900"

打开运行,输入shell:startup 回车。将start.bat启动文件放入文件夹