coturn穿透服务器安装部署

因为部署Janus的需要,把安装整个coturn服务器的过程记录下来备忘。

1、环境

服务器:阿里云ECS

系统:ubuntu16.04,采用root账号登录。

2、下载coturn版本

下载coturn官方版本:https://github.com/coturn/coturn

https://github.com/coturn/coturn/tags 选择合适的TAG版本下载。

我选择的是4.5.2版本。

3、安装依赖

先进行解压,tar -zxvf turnserver-4.5.2.tar.gz

然后进入相应目录,查看README 和 INSTALL 文件,非常重要(不要先入为主根据网上的资料直接进行编译安装,有些时候不同的版本,不同的需求,配置和编译的参数有差异)。

从INSTALL文件中,可以看到相关依赖:

OpenSSL, SQLite, libevent2, PostgreSQL, MySQL (or MariaDB) and Hiredis 
libraries can be downloaded from their web sites:
 - http://www.openssl.org (required);
 - http://www.libevent.org (required);
 - http://www.sqlite.org (optional);
 - http://www.postgresql.org (optional);
 - http://www.mysql.org (or http://mariadb.org) (optional);
 - https://github.com/mongodb/mongo-c-driver (optional);
 - http://redis.io (optional).
 
 Linux Ubuntu, Debian, Mint:
		$ sudo apt-get install libssl-dev
		$ sudo apt-get install libsqlite3 (or sqlite3)
		$ sudo apt-get install libsqlite3-dev (or sqlite3-dev)
		$ sudo apt-get install libevent-dev
		$ sudo apt-get install libpq-dev
		$ sudo apt-get install mysql-client
		$ sudo apt-get install libmysqlclient-dev
		$ sudo apt-get install libhiredis-dev

以上不是必须,因为在你选择的服务器上,可能已安装,也可以在后面 turnserver 编译时根据错误提示进行处理。

NOTE: If you do not install SQLite or PostgreSQL or MySQL or MongoDB or Redis,
then you will be limited to the command-line options for user database. 
It will work great for development setup, but for real runtime systems you 
will need SQLite or PostgreSQL or MySQL or MongoDB or Redis.

4、编译安装

编译前进行配置:

cd turnserver-4.5.2
./configure

默认目录如下:

    - /usr on Solaris.

    - /usr/pkg on NetBSD.

    - /usr/local everywhere else.

如果想调整,可采用:

PREFIX=/opt ./configure
# or
./configure --prefix=/opt 
# 甚至:
PREFIX=/opt BINDIR=/opt/bin64 CONFDIR=/opt/conf ./configure
# or
./configure --prefix=/opt --bindir=/opt/bin64 --confdir=/opt/conf

具体可以详见 INSTALL 文件。

接下来就可以编译和安装:

make
sudo make install

如果想从系统里移除 turnserver,则可以使用:

sudo make deinstall

然后进一步清理所有的编译文件和配置信息:

make clean

安装完毕,使用下面命令查看是否安装成功:

which turnserver
# 返回以下(默认)
/usr/local/bin/turnserver

5、配置文件

默认情况下的配置文件位置:/usr/local/etc/turnserver.conf,如果没有,则可以从turnserver.conf.default 拷贝一份。

在配置之前,还需要获取一些信息。

1)通过 ifconfig指令,得到服务器的内网地址及标识。

coturn穿透服务器安装部署_第1张图片

2)签名证书

在配置文件中需要cert和pkey,采用Openssl命令生成,生成的两个文件在/etc/目录下:

openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes

填写一些信息即可。则可以在 /etc 目录下看到两个文件:turn_server_cert.pem 和 turn_server_pkey.pem 。

3)编辑配置 turnserver.conf:

# Coturn TURN SERVER configuration file
listening-device=eth0
relay-device=eth0
listening-ip=172.31.xxx.xxx
listening-port=3478
tls-listening-port=5349
relay-ip=172.31.xxx.xxx
# 外网IP
external-ip=47.104.xxx.xxx/172.31.xxx.xxx
#relay-threads=50
# 域名
server-name=xxx.xxx.xx
lt-cred-mech
# 用于名/密码
static-auth-secret=jingzl
user=jingzl:jingzl.1q2
#userdb=/etc/turnuserdb.conf
min-port=49152
max-port=65535
#max-bps=102400
pidfile="/var/run/turnserver.pid"
#no-loopback-peers
allow-loopback-peers
no-multicast-peers
mobility
no-cli
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
stale-nonce
#use-auth-secret
Verbose
fingerprint

以上是目前的配置,还有更多的配置信息有待验证。详细可以参见 turnserver.conf.default 的解释。

6、运行

需要重点查看三个文件:README.turnadminREADME.turnserverREADME.turnutils

# 下面两种貌似都可以 -r 参数似乎可以随意设置
sudo turnserver -o -a -f --user=jingzl:jingzl.1q2 -r HB
sudo turnserver -o -a -f --user=jingzl:jingzl.1q2 -r api.xkstudio.cn

目前使用的几种参数解释如下,其他更详细的可以通过 turnserver --help 进行查看。

-o, --daemon					Start process as daemon (detach from current shell).
-f, --fingerprint				Use fingerprints in the TURN messages.
-a, --lt-cred-mech				Use the long-term credential mechanism.
-u, --user				User account, in form 'username:password', for long-term credentials.
						Cannot be used with TURN REST API.
-r, --realm					The default realm to be used for the users when no explicit
						origin/realm relationship was found in the database.
						Must be used with long-term credentials 
						mechanism or with TURN REST API.

启动后显示:

0: : Config file found: /usr/local/etc/turnserver.conf
0: : log file opened: /var/log/turn_15141_2021-06-26.log
0: : Listener address to use: 172.31.166.26
0: : Relay address to use: 172.31.166.26
0: : Whitelisting external-ip private part: 172.31.166.26
0: : Config file found: /usr/local/etc/turnserver.conf
0: : 
RFC 3489/5389/5766/5780/6062/6156 STUN/TURN Server
Version Coturn-4.5.2 'dan Eider'
0: : 
Max number of open files/sockets allowed for this process: 65535
0: : 
Due to the open files/sockets limitation,
max supported number of TURN Sessions possible is: 32500 (approximately)
0: : 

==== Show him the instruments, Practical Frost: ====

0: : TLS supported
0: : DTLS supported
0: : DTLS 1.2 supported
0: : TURN/STUN ALPN supported
0: : Third-party authorization (oAuth) supported
0: : GCM (AEAD) supported
0: : OpenSSL compile-time version: OpenSSL 1.1.1k  25 Mar 2021 (0x101010bf)
0: : 
0: : SQLite supported, default database location is /usr/local/var/db/turndb
0: : Redis is not supported
0: : PostgreSQL supported
0: : MySQL supported
0: : MongoDB is not supported
0: : 
0: : Default Net Engine version: 3 (UDP thread per CPU core)

=====================================================

0: : Domain name: 
0: : Default realm: HB
0: : 
CONFIGURATION ALERT: You specified --lt-cred-mech and --use-auth-secret in the same time.
Be aware that you could not mix the username/password and the shared secret based auth methods. 
Shared secret overrides username/password based auth method. Check your configuration!
0: : CONFIG WARNING: allow_loopback_peers opens a possible security vulnerability. Do not use in production!!
0: : SSL23: Certificate file found: /etc/turn_server_cert.pem
0: : SSL23: Private key file found: /etc/turn_server_pkey.pem
0: : TLS1.0: Certificate file found: /etc/turn_server_cert.pem
0: : TLS1.0: Private key file found: /etc/turn_server_pkey.pem
0: : TLS1.1: Certificate file found: /etc/turn_server_cert.pem
0: : TLS1.1: Private key file found: /etc/turn_server_pkey.pem
0: : TLS1.2: Certificate file found: /etc/turn_server_cert.pem
0: : TLS1.2: Private key file found: /etc/turn_server_pkey.pem
0: : TLS cipher suite: DEFAULT
0: : DTLS: Certificate file found: /etc/turn_server_cert.pem
0: : DTLS: Private key file found: /etc/turn_server_pkey.pem
0: : DTLS1.2: Certificate file found: /etc/turn_server_cert.pem
0: : DTLS1.2: Private key file found: /etc/turn_server_pkey.pem
0: : DTLS cipher suite: DEFAULT

7、测试验证

1)直接测试

网上有说直接用 http://域名或IP:3478 可以直接访问,我这边测试不行,无法访问,不知道是否是配置或者版本的原因。

2)用第三方ICE工具测试

测试地址:https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

使用firefox浏览器

coturn穿透服务器安装部署_第2张图片

可以看到本地的公网IP被返回。

本地公网IP查询网站:http://ip.chinaz.com/ 

8、运维

1)启动、停止

启动:

# 下面两种貌似都可以 -r 参数似乎可以随意设置
sudo turnserver -o -a -f --user=jingzl:jingzl.1q2 -r HB
sudo turnserver -o -a -f --user=jingzl:jingzl.1q2 -r api.xkstudio.cn

查看进程和关闭:

ps -ef|grep turnserver
kill -9 (pid)

2)日志

在启动turnserver时,会打印出来日志文件位置,当然你也可以自行进行配置。

coturn穿透服务器安装部署_第3张图片

可以使用 tail -f  -n 100 xxxx  来进行实时展示。

9、问题

1)在与Janus集成调试的过程中,经常会出现错误:

ERROR: check_stun_auth: Cannot find credentials of user

116: : session 003000000000000002: realm user : incoming packet message processed, error 401: Unauthorized

ICE failed for component 1 in stream 1, but we're still waiting for some info so we don't care... (trickle pending, answer received, alert not set)

(未完待续)

你可能感兴趣的:(Server,Linux,音视频服务器,ubuntu,coturn,NAT穿透,Janus)