搭建STUN/TURN服务器
比较流行的STUN/TURN服务器是coturn。
获取coturn源码
git clone https://github.com/coturn/coturn.git
编译安装
cd coturn
./configure --prefix=/usr/local/coturn #--prefix指安装路径
sudo make -j 4 && make install
配置coturn
listening-port=3478 #指定监听端口
external-ip=xx.xx.xx.xx #公网ip
user=name:password #stun/turn服务的用户名和密码
realm=stun.xxx.cn #域名
cli-password=qwerty #不设置会出错
lt-cred-mech
min-port=60000
max-port=60010
fingerprint
将上述配置写入/usr/local/coturn/etc/turnserver.conf
原文件中只有一份turnserver.conf.default,将此文件复制一份并命名为turnserver.conf即可配置。
使用如下命令拷贝
cp turnserver.conf.default turnserver.conf
启动stun/turn服务
cd /usr/local/coturn/bin
turnserver -c ../etc/turnserver.conf
有时可能会出现错误
CONFIG ERROR: Empty cli-password, and so telnet cli interface is disabled! Please set a non empty cli-password!
需要在配置文件中添加
cli-password=qwerty
测试
使用Trickle ICE进行测试
STUN or TURN URI格式:turn:公网IP:port
测试成功截图:

失败原因
把需要的端口打开
没有正确创建数据库,turnadmin -l为空;
用户名密码输入错误;
realm错误:如果使用默认realm,它和turnadmin添加用户对应的realm不同,则无法通过认证。可以通过turnserver -r new.realm来覆盖默认设置;
证书问题?(未验证)
还有一种失败原因:
浏览器问题,换个浏览器
端口占用问题,
netstat -lnp|grep 3478 #查询该端口占用的进程
kill -9 pid #用上条命令得到的进程id,杀死进程
turnserver -c ../etc/turnserver.conf #重新启动