Product SiteDocumentation Site

11.8. 实时通信服务

实时通信(Real-Time Communication,RTC)服务包括语音、视频/网络摄像头即时通信(instant messaging,IM)和共享桌面。这章对需要操作 RTC 的三种服务给出了简要介绍,包括 TURN 服务器、 SIP 服务器和 XMPP 服务器。如何规划、安装并管理这些服务的深入细节可以在《实时通信的快速开始指南》中得到,它包括 Debian 特定的一些示例。
SIP 和 XMPP 可以提供相同的功能。SIP 在语音和视频上略微知名一些,而 XMPP 在传统上被认为是 IM 协议。实际上,它们都可以用于这些之中的任何目的。为了使连接选项最大化,推荐并行运行它们。
这些服务在认证和保密目的上都依赖于 X.509 证书。更多信息请参见 第 10.2 节 “X.509 认证”

11.8.1. RTC 服务的 DNS 设置

RTC services require DNS SRV and NAPTR records. A sample configuration that can be placed in the zone file for falcot.com (see also 例 10.13 “Excerpt of /etc/bind/db.falcot.com:
; the server where everything will run
server1            IN     A      198.51.100.19
server1            IN     AAAA   2001:DB8:1000:2000::19

; IPv4 only for TURN for now, some clients are buggy with IPv6
turn-server        IN     A      198.51.100.19

; IPv4 and IPv6 addresses for SIP
sip-proxy          IN     A      198.51.100.19
sip-proxy          IN     AAAA   2001:DB8:1000:2000::19

; IPv4 and IPv6 addresses for XMPP
xmpp-gw            IN     A      198.51.100.19
xmpp-gw            IN     AAAA   2001:DB8:1000:2000::19

; DNS SRV and NAPTR for STUN / TURN
_stun._udp  IN SRV    0 1 3467 turn-server.falcot.com.
_turn._udp  IN SRV    0 1 3467 turn-server.falcot.com.
@           IN NAPTR  10 0 "s" "RELAY:turn.udp" "" _turn._udp.falcot.com.

; DNS SRV and NAPTR records for SIP
_sips._tcp  IN SRV    0 1 5061 sip-proxy.falcot.com.
@           IN NAPTR  10 0 "s" "SIPS+D2T" "" _sips._tcp.falcot.com.

; DNS SRV records for XMPP Server and Client modes:
_xmpp-client._tcp  IN     SRV    5 0 5222 xmpp-gw.falcot.com.
_xmpp-server._tcp  IN     SRV    5 0 5269 xmpp-gw.falcot.com.

11.8.2. TURN 服务器

TURN 是一种服务,它帮助 NAT 路由器和防火墙后面的客户端发现最有效的方式,来与其他客户端通信,并且如果没有发现直接媒体路径的话会依赖媒体流。强烈推荐在其它 RTC 服务提供给终端用户之前安装 TURN 服务器。
TURN 和相关的 ICE 协议是开放的标准。为了从这些协议中获益,将连接最大化并将用户的挫败感最小化,重要的是确保所有的客户端软件支持 ICE 和 TURN。
对于有效工作的 ICE 算法,服务器必须具有两个公共的 IPV4 地址。
安装 coturn 软件包并编辑 /etc/turnserver.conf 配置文件。SQLite 数据库默认在 /var/db/turndb 中配置用户账户设置,而如果需要的话可以替代设置 PostgreSQL、MySQL 或 Redis。 要做的最重要的事情是插入服务器的 IP 地址。
The server can be started running turnserver from the coturn package. We want the server to be an automatically started system service. This is the default behavior using systemd. Only when using the older SysVinit you have to edit the /etc/default/coturn file like this:
#
# Uncomment it if you want to have the turnserver running as
# an automatic system service daemon
#
TURNSERVER_ENABLED=1
TURN 服务器默认使用匿名访问。我们必须添加我们想要使用的用户:
# turnadmin -a -u roland -p secret_password -r falcot.com
# turnadmin -A -u admin -p secret_password
我们使用 -a 参数来添加普通用户,而用 -A 来添加管理用户。

11.8.3. SIP 代理服务器

SIP 代理服务器管理其他组织、SIP 中继提供商、SIP PBXes 如 Asterisk、SIP 电话、基于 SIP 的软件电话与 WebRTC应用之间的到来的与外发的 SIP 链接。
强烈推荐在尝试设置 SIP PBX 之前安装并配置 SIP 代理。SIP 代理使到达 PBX 的大量流量正常化,并提供更大的连接与恢复能力。

11.8.3.1. 安装 SIP 代理

Install the kamailio package and the package for the database backend. The Falcot administrators chose MySQL, so they install kamailio-mysql-modules and mariadb-server. /etc/kamailio/kamctlrc is the configuration file for the control tools kamctl and kamdbctl. You need to edit and set the SIP_DOMAIN to your SIP service domain and set the DBENGINE to MySQL, another database backend can be used.
[...]
## your SIP domain
SIP_DOMAIN=sip.falcot.com

## chrooted directory
# CHROOT_DIR="/path/to/chrooted/directory"

## database type: MYSQL, PGSQL, ORACLE, DB_BERKELEY, DBTEXT, or SQLITE
# by default none is loaded
#
# If you want to setup a database with kamdbctl, you must at least specify
# this parameter.
DBENGINE=MYSQL
[...]
现在我们聚焦在配置文件 /etc/kamailio/kamailio.cfg 上。Falcot 公司需要用户认证和持续的用户位置,所以他们在文件的顶部添加了后面的指令 #!define
#!KAMAILIO
#
# Kamailio (OpenSER) SIP Server v5.2 - default configuration script
#     - web: https://www.kamailio.org
#     - git: https://github.com/kamailio/kamailio
#!define WITH_MYSQL
#!define WITH_AUTH
#!define WITH_USRLOCDB
[...]
Kamailio needs a database structure that we can create running kamdbctl create as root. Finally, we can add some users with kamctl.
# kamdbctl create
[...]
# kamctl add roland secret_password
Once everything is properly configured you can start or restart the service with systemctl restart kamailio, you can connect with a SIP client providing the IP address and the port (5090 is the default port). The users have the following id: roland@sip.falcot.com, and they can login using a client (see 第 13.9 节 “即时通讯软件”).

11.8.4. XMPP 服务器

XMPP 服务器管理本地 XMPP 用户与公共因特网上其它域的XMPP 用户之间的连接。
prosody is a popular XMPP server that operates reliably on Debian servers.

11.8.4.1. 安装 XMPP 服务器

安装 prosody 软件包。
复查 /etc/prosody/prosody.cfg.lua 配置文件。要做的最重要的事情是插入被允许管理这台服务器的用户的 JID。
admins = { "joe@falcot.com" }
每个域还需要独立的配置文件。复制来自 /etc/prosody/conf.avail/example.com.cfg.lua 的示例,并用它作为起点,这里是 falcot.com.cfg.lua
VirtualHost "falcot.com"
        enabled = true
        ssl = {
                key = "/etc/ssl/private/falcot.com.key";
                certificate = "/etc/ssl/certs/falcot.com.pem";
                }

-- Set up a MUC (multi-user chat) room server on conference.example.com:
Component "conference.falcot.com" "muc"
为了启用域,必须有个来自 /etc/prosody/conf.d/ 的符号链接。以这样的方式来创建:
# ln -s /etc/prosody/conf.avail/falcot.com.cfg.lua /etc/prosody/conf.d/
重新启动服务来使用新的配置。

11.8.4.2. 管理 XMPP 服务器

Some management operations can be performed using the prosodyctl command line utility. For example, to add the administrator account specified in /etc/prosody/prosody.cfg.lua:
# prosodyctl adduser joe@falcot.com
关于如何定制配置的更多细节请参见 Prosody online documentation

11.8.5. 在端口 443 运行服务

一些管理员倾向在端口 443 运行所有的 RTC 服务。这帮助用户从饭店或机场那样的远程位置连接,在那里其它端口可能会被阻塞,或者因特网通信量会经由 HTTP 代理服务器。
为了使用这个策略,每项服务(SIP、XMPP 和 TURN)需要不同的 IP 地址。所有的服务仍然可以在相同的主机上,因为 Linux 在单一主机上支持多个 IP 地址。对于每个进程的配置文件中,还要在 DNS SRV 记录中指定端口号,即443。

11.8.6. 添加 WebRTC

Falcot 公司想要让客户直接从网站上打电话。Falcot 公司的管理员还想要使用 WebRTC 作为其灾难恢复计划的一部分,这样员工可以在家使用 web 浏览器登录公司电话系统上,并在紧急情况下正常工作。
WebRTC 是快速进化的技术,它本质上使用来自 Testing(测试) 发布版本的软件包。其他选项用于编译软件。
WebRTC 使用简单的 API 来提供带有 RTC 的浏览器和移动应用,它是自由软件,正在由 Google 开发。
A very flexible approach is using GStreamer's WebRTC implementation. It enables pipeline-based multimedia applications, which allows developing interesting and highly efficient applications. A good starting point is the following demo by Centricular, the main company that is developing it:
更先进的点击呼叫网站典型地使用服务器一侧的脚本,来动态生成 config.js 文件。DruCall 源代码说明了如何使用 PHP 来实现。
这一章只举了部分可获得的服务器软件的示例;但描述了最常用的网络服务。现在到了更技术性的章节了:我们将进入一些概念更深入的细节,描述大量部署和虚拟化。