ubuntu18.04装coturn

  • 参考 科普
  • 在这里插入图片描述
    上图中的Relay server即为turn中继服务器,而STUN server的作用是通过收集NAT背后peer端(即:躲在路由器或交换机后的电脑)对外暴露出来的ip和端口,找到一条可穿透路由器的链路,俗称“打洞”。stun/turn服务器通常要部署在公网上,能被所有peer端访问到,coturn开源项目同时实现了stun和turn服务的功能,是webrtc应用的必备首选。

附带工具

這時就會有五個binary参考

turnserver: turn server相關操作
turnadmin: turn server的user authentication 相關操作

turnadmin -a -u test -r leifeng-elec.com-p test
turnutils_uclient 192.168.1.230 -u test -wtest

turnutils_uclient: 測試turn部分
turnutils_stunclient: 測試stun部分

直接apt安装


sudo apt install coturn

sudo systemctl status coturn


sudo systemctl enable --now coturn

sudo ufw allow Turnserver
编辑/etc/default/coturn文件:

TURNSERVER_ENABLED=1(必须)
  • 重启
service coturn restart

  • 我的日志
 cat /home/zhangbin/logs/coturn_2020-03-19.log 

用户

-加入数据库

root@ecs-x-medium-2-linux-20200223094431343:~# turnadmin -a -u zhangbin -p zhangbin -r rtc.betofly.cn
  • 查看
root@ecs-x-medium-2-linux-20200223094433143:~# turnadmin -l
zhangbin[rtc.zhangbin.cn]
root@ecs-x-medium-2-linux-20200224430933143:~# 
  • 其实这里我们指定的密码和域名暂时都是无关紧要的,因为后面我们在启动 turnserver 时,可以通过相关参数,给其指定固定的域名和密码;

我的配置


root@ecs-x-medium-2-linux-20200223305314:~# vi /etc/turnserver.conf

root@ecs-x-medium-2-linux-20200223093143:/home/zhangbin# turnserver -o -a -f -v -r rtc.zhangbin.cn
0: log file opened: /var/log/turn_29286_2020-03-19.log
0: 
RFC 3489/5389/5766/5780/6062/6156 STUN/TURN Server
Version Coturn-4.5.0.7 '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.0g  2 Nov 2017 (0x1010007f)
0: 
0: SQLite supported, default database location is /var/lib/turn/turndb
0: Redis 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: Config file found: /etc/turnserver.conf
0: log file opened: /home/zhangbin/logs/coturn_2020-03-19.log
0: Config file found: /etc/turnserver.conf
0: Domain name: 
0: Default realm: rtc.zhangbin.cn
0: ERROR: 
CONFIG ERROR: Empty cli-password, and so telnet cli interface is disabled! Please set a non empty cli-password!
0: WARNING: cannot find certificate file: turn_server_cert.pem (1)
0: WARNING: cannot start TLS and DTLS listeners because certificate file is not set properly
0: WARNING: cannot find private key file: turn_server_pkey.pem (1)
0: WARNING: cannot start TLS and DTLS listeners because private key file is not set properly
0: NO EXPLICIT LISTENER ADDRESS(ES) ARE CONFIGURED
0: ===========Discovering listener addresses: =========
0: Listener address to use: 127.0.0.1
0: Listener address to use: 192.168.0.181
0: Listener address to use: ::1
0: =====================================================
0: Total: 1 'real' addresses discovered
0: =====================================================
0: NO EXPLICIT RELAY ADDRESS(ES) ARE CONFIGURED
0: ===========Discovering relay addresses: =============
0: Relay address to use: 192.168.0.181
0: Relay address to use: ::1
0: =====================================================
0: Total: 2 relay addresses discovered
0: =====================================================
root@ecs-x-medium-2-linux-20200223093143:/home/zhangbin# 

证书

  • https://www.kancloud.cn/vim26/v1/578933
root@ecs-x-medium-2-linux-20200223093443143:/home/zhangbin# openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes
Can't load /root/.rnd into RNG
139673447518656:error:2406F079:random number generator:RAND_load_file:Cannot open file:../crypto/rand/randfile.c:88:Filename=/root/.rnd
Generating a RSA private key
.........+++++
............+++++
writing new private key to '/etc/turn_server_pkey.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CNNN
State or Province Name (full name) [Some-State]:GUANGDDD
Locality Name (eg, city) []:GUANGZHOU
Organization Name (eg, company) [Internet Widgits Pty Ltd]:EDU100
Organizational Unit Name (eg, section) []:WEBRTC
Common Name (e.g. server FQDN or YOUR name) []:ZHANGBIN
Email Address []:[email protected]
  • 生成了

root@ecs-x-medium-2-linux-2020022343093143:/home/zhangbin# ls /etc/t
terminfo/               turn_server_cert.pem    turn_server_pkey.pem
timezone                turnserver.conf         
tmpfiles.d/             turnserver.conf.origin  
root@ecs-x-medium-2-linux-20200243423093143:/home/zhangbin# 

命令配置

概述
Coturn 是webrtc,p2p视频通话必不少的,主要包含2个主要功能stun服务, turn服务
Coturn 的githup地址为 https://github.com/coturn/coturn/

安装
有两种方式安装,第一种是使用编译好的,第二种方式是自己编译
第一种编译好的下载地址为 http://turnserver.open-sys.org/downloads/
第二种方式是下载代码,自行编译.
sudo apt-get -y install gcc
sudo apt-get -y install libssl-dev
sudo apt-get -y install libevent-dev
sudo apt-get -y install libpq-dev
sudo apt-get -y install mysql-client
sudo apt-get -y install libmysqlclient-dev
sudo apt-get -y install libhiredis-dev
sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev

git clone https://github.com/coturn/coturn
cd coturn
./configure --prefix=/opt/coturn
make && make install
配置
这部分我认为是最重要的。
Coturn 主要的程序是以下几种
Turnadmin
Turnserver
Turnutils_uclient
主要使用的是这些, 由于现在是搭建p2p视频通话demo阶段,故coturn没有研究集群的搭建。等涉及到的时候再研究。
功能总术
Turnadmin:coturn的管理工具,主要是生成用户帐户的键值,列出数据库中的可用用户,在数据库中添加和删除用户。更新用户密码,为TURN REST API设置共享秘钥。
Turnserver:主要功能的程序
Turnutils_uclient:测试客户机,用于对coturn服务的测试工具。

Turnadmin使用
语法 turnadmin [command] [options]
Command
• -P, --generate-encrypted-password 生成秘钥
• -k, --key 生成用户帐户键值
• -a, --add 添加或更新用户
• -d, --delete 删除用户
• -l, --list 列出所有用户
• -s, --set-secret=…设置密码在数据库存储
• -S, --show-secret 展示在数据库存储的密码
• -X, --delete-secret 删除共离秘钥
• --delete-all-secrets 删除所有 REST API共享秘钥.
• -O, --add-origin 添加 origin-to-realm 关系
• -R, --del-origin 删除 origin-to-realm 关系
• -I, --list-origins 列出origin-to-realm 关系
• -g, --set-realm-option 设置reaml参数: max-bps, total-quota, user-quota.
• -G, --list-realm-options 列出reaml参数
Options:
• -b, --db, --userdb SQLite 数据库文件名
• -e, --psql-userdb, --sql-userdb PostgreSQL数据库连接字符串.
• -M, --mysql-userdb MySQL 数据库连接字符串
• -N, --redis-userdb Redis数据库连接字符串.
• -J, --mongo-userdb MongoDB 用户数据库连接字符串.
• -u, --user 用户名.
• -r, --realm Realm.
• -p, --password 密码.
• -o, --origin origin Fully-formed origin, for example https://crinna.org:443.不清楚用途
• --max-bps 设置限速.
• --total-quota Set value of realm’s total-quota parameter. Setting to zero value means removal of the option.
• --user-quota Set value of realm’s user-quota parameter. Setting to zero value means removal of the option.
• -h, --help Help.
示例
生成键值
turnadmin -k -u -r -p

生成的键值添加进数据库中
turnadmin -a [-b | -e | -M | -N ] -u -r -p
暂时使用以上两个命令就可以了。
Turnserver使用
语法:
turnserver [flags] [-n | -c ] [ --db= | --userdb= | --psql-userdb= | --mysql-userdb= | --mongo-userdb= | --redis-userdb= ] [options]
Flags:
• -v, --verbose ‘Moderate’ 详细模式
• -V, --Verbose 'Extra’详细模式,不推荐使用
• -o, --daemon后台运行.
• -f, --fingerprint在TURN消息中加入印记, 如果来的请求包含登记, 那么TURN服务器将总是添加印记到消息这那会话中。
• -a, --lt-cred-mech 使用凭证机制
• -z, --no-auth 不使用任何凭证机制, 允许匿名访问, 与-a或-A选项相反.
• --use-auth-secret TURN REST API 标志. 设置一个特殊WEBRTC身份验证基于authentication secret选项, 这个选项的用途在于支持TURN Server REST API, 配合 -a使用
• --oauth 支持 oAuth 验证
• --dh566 使用 566 位 DH TLS 密钥. 默认大小为 1066.
• --dh2066使用2066位DH TLS密钥.默认大小为1066.
• --no-tlsv1 不允许 TLSv1 协议.
• --no-tlsv1_1不允许TLSv1.1协议.
• --no-tlsv1_2不允许TLSv1.2协议.
• --no-udp 不监听udp
• --no-tcp 不监听tcp
• --no-tls 不监听tls
• --no-dtls 不监听dtls
• --no-udp-relay 不允许udp转发
• --no-tcp-relay 不允许tcp转发
• --stale-nonce用有生命周期的现时标志值来额外安全机制
• --no-stdout-log 不标准输出日志信息, 默认是日志同时标准输出和日志文件
• --syslog 标志所有信息重定向到系统日志 (syslog).
• --simple-log 简单日志
• --no-loopback-peers 不允许本地地址 (127.x.x.x and ::1).
• --no-multicast-peers 不允许广播地址 (224.0.0.0 and above, and FFXX:*).
• --udp-self-balance在aux端点之间平衡UDP通信. 结合 --aux-server 选项.
• --secure-stun需要验证STUN 绑定请求.
• -S, --stun-only 只做为STUN服务器,所有的TURN请求将被忽略.
• --no-stun Run as只做为TURN服务器,所有的STUN请求将被忽略.
• --mobility 支持移动ICE
• --no-cli I 不支持cli
• --server-relay 服务器中继.
• --check-origin-consistency 设置原点一致性检查的标志
配置文件设置
-n 不使用配置文件, 只使用命令行参数
-c 使用指定配置文件 搜索先后为
当前目录
当前目录下的 etc/子目录
上层目录的 etc/
/etc/
/usr/local/etc/

用户数据库设置
-b –db, --userdb sqlite数据库
-M –mysql-userdb mysql数据库
“host= dbname= user= password= connect_timeout=”
-N, --redis-userdb redis数据库
“ip= dbname= password= connect_timeout=”
-J, --mongo-userdb
“mongodb://username:password@host:port/database?options”.
Options:
• -d, --listening-device 监听网卡
• -L, --listening-ip 监听外网地址.
• -p, --listening-port TURN监听TCP/UDP端口,默认3478.
• --tls-listening-port TURN 监听TLS&DTLS端口,默认5349
• --alt-listening-port UDP/TCP替代端口
• --alt-tls-listening-port TLS&DTLS替代端口
• -E, --relay-ip 中转地址, 本地ip地址转发包给个人.
• -X, --external-ip [/private-ip] 外网IP
• -m, --relay-threads 处理外网连接线程数目
• --min-port 中继端点分配的UDP端口范围的下界。默认值为49152.
• --max-port 中继端点分配的UDP端口范围的上界。默认值为65535.
• -u, --user user:password or user:0xkey 用户帐户
• -r, --realm realm值
• -q, --user-quota 每个用户分配配额: 用户可以创建多少并发分配.
• -Q, --total-quota 总分配配额:并发分配的全局限制
• -s, --max-bps 限带宽数
• -B, --bps-capacity 服务器最大容量
• --static-auth-secret 静态身份秘钥值
• --server-name 用于支持oAuth身份验证, 默认值为realm.
• --cert 证书文件, PEM 格式… 如果同时If both --no-tls and --no-dtls 设置, 则该参数无用,该文件为openssl生成
• --pkey 私有密钥文件, PEM 格式.
• --pkey-pwd 私有密钥文件密码,当密钥被加秘的话.
• --CA-file CA 文件,促使将服务器验证客户端SSL证书.默认不开启
• --cipher-list 允许TLS/ DTLS连接使用OpenSSL密码表.
• --ec-curve-name Curve name for EC ciphers, if supported by OpenSSL library (TLS and DTLS). The default value is prime256v1, if pre-OpenSSL 1.0.2 is used. With OpenSSL 1.0.2+, an optimal curve will be automatically calculated, if not defined by this option.
• -l, --log-file or “stdout” or “syslog” 设置日志全路径
• --alternate-server ip:port选择设置“重定向”模式。这个选项的值是UDP和TCP服务的备用服务器的地址
• --tls-alternate-server ip:port选择设置“重定向”模式。这个选项的值是TLS和DTLS服务的备用服务器的地址[:]
• --max-allocate-timeout 最大时间,以秒为单位,允许完全分配建立,默认60s.
• --denied-peer-ip=<IPaddr[-IPaddr]>, --allowed-peer-ip=<IPaddr[-IPaddr]>黑白名单
• --pidfile 存储进程pid的文件
• --proc-user 进程刊用
• --proc-group 进程组
• --cli-ip用于CLI管理接口的本地系统IP地址.
• --no-cli.
• --cli-portCLI管理接口端口默认5766.
• --cli-password cli密码, 默认无密码.
turnutils_uclient 使用
turnutils_uclient [-tSvsyhcxg] [options]
Flags:
• -t 使用tcp ,默认udp
• -b 使用SCTP,默认udp
• -T使用TCP进行中继传输
• -P被动TCP.
• -S 安全连接: TLS for TCP, DTLS for UDP.
• -U安全加密连接(suite eNULL): SSL/TLS for TCP, DTLS for UDP.
• -v 详细.
• -s 使用send方法, 默认使用TURN 频道.
• -y使用客户机到客户机的连接:RTP/RTCP通道对到另一个RTP/RTCP通道对.
• -h在最后一个发送包之后无限期地等待.
• -c 不创建rtcp 连接.
• -x 请求 IPv6 中继地址 (RFC6156).
• -X 显式请求IPv4中继地址.
• -g 设置 DONT_FRAGMENT 参数 在TURN请求.
• -D强制通道填充
• -N负面的测试(some limited cases only).
• -R 负面协议测试.
• -O “DOS 攻击” 模式.
• -M 使用 TURN ICE.
• -I不设置转弯中继端点的权限
• -G 生成额外的请求
Options:
• -l 消息长度, 默认100字节
• -i 证书文件
• -k 私有秘钥文件.
• -E 服务器证书验证证书文件
• -p TURN 服务端口(默认: 3478 unsecure, 5349 secure).
• -n 发送消息数量,默认为5.
• -d 本地网卡.
• -L 本地IP地址.
• -m 客户端数量,默认1,2,4
• -e 前端地址.
• -r 前端端口 (default 3480).
• -z 每会话包间隔默认20ms.
• -u STUN/TURN 用户名.
• -w STUN/TURN 密码.
• -W TURN REST API 密钥.
• -o the ORIGIN STUN 属性值.
• -a 带宽用于分配中的带宽请求,默认为0.

有些身份验证部分还未深入研究,有些说不清, 不了解具体什么用及有什么坑,有这些大致可以使用。等之后涉及之后,再写博客记录

启动
生成证书
sudo openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes
生成用户信息
turnadmin -k -u <用户名> -r north.gov -p <密码>
./turnadmin -k -a -u liu -r north.gov -p 1234 -b /opt/coturn/et
c/turnuserdb.conf

vim /etc/turnuserdb.conf
liu:0xb4f2bb5729763806f0ff0fc5c0f954ba
配置文件
listening-device=eth0
listening-port=3478
tls-listening-port=5349
listening-ip=内网ip
relay-device=eth0
relay-ip=内网ip
external-ip=外网ip
relay-threads=50
min-port=49152
max-port=65535
fingerprint
lt-cred-mech
user=liu:0xb4f2bb5729763806f0ff0fc5c0f954ba
userdb=/etc/turnuserdb.cof
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
log-file=/var/tmp/turn.log
mobility
web-admin-ip=ip
web-admin-port=8888
启动./trunserver -c turn.conf

之前的配置trun时一直显示用户不正确 将user=liu:1234 就可以了,不明白为什么将编码的密码就不行, 显式写密码就行
测试网站
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
————————————————
版权声明:本文为CSDN博主「刘长福」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012618915/article/details/83150374

端口老是被占用

- Check that your process is not stuck

ps aux | grep turnserver
And check open ports and process

netstat -lnp


[root@webrtc-80 ~]# lsof -n -i4TCP:3478 | grep LISTEN
turnserve 18927 root 27u IPv4 31966 0t0 TCP 192.168.47.80:3478 (LISTEN)
[root@webrtc-80 ~]# lsof -n -i4TCP:5349 | grep LISTEN
turnserve 18927 root 28u IPv4 31967 0t0 TCP 192.168.47.80:5349 (LISTEN)

配置方法不对

  • extendip 填写外网ip
  • 其他都写自己的本地的局域网ip
  • 最简单的就像这样: 参考
配置coturn
使用turnadmin生成安全访问密码
turnadmin -k -u username -r north.gov -p password
/usr/local/etc/turnserver.conf配置
#listening-ip与relay-ip采用内网ip,external-ip是外网的ip

listening-port=444 #监听端口
external-ip=210.21.53.158 #外网IP
verbose
fingerprint
lt-cred-mech
realm=test
user=username:生成的加密密码    #修改成自己的
user=username:password       #修改成自己的
stale-nonce
no-loopback-peers
no-multicast-peers
mobility
no-cli

老是有授权错误401

  • 索性只使用用户名密码的方式
  • 去掉long term 认证
发布了693 篇原创文章 · 获赞 58 · 访问量 220万+

猜你喜欢

转载自blog.csdn.net/commshare/article/details/104957680