0. 前言html
很久以前写过一篇搭建WebRTC的文章,里面有简单的说到怎么搭建一个stun服务。但那时只是一笔带过。正好,这两天搭建stun服务,这篇博客就再复习一遍,并把搭建过程整理一下。git
1. 安装github
https://github.com/coturn/coturn 在这里git clone 下来而后编译安装,一切默认便可。编译后,也能够不用安装。在编译目录下bin文件夹下有turnserver turnutils_stunclient turnutils_uclient 这三个等一下会用到的软件。web
http://www.stunprotocol.org/ 这个stunserver 也是能够的。安全
2. 配置turnserver.conf服务器
能够建立 bin/turnserver.conf 文件,增长如下配置信息。网络
1 listening-ip=0.0.0.0 2 listening-port=3478 3 4 #relay-ip=0.0.0.0 5 external-ip=0.0.0.0 6 7 min-port=59000 8 max-port=65000 9 10 Verbose 11 fingerprint 12 13 no-stdout-log 14 syslog 15 16 cert=pem/turn_server_cert.pem 17 pkey=pem/turn_server_pkey.pem 18 19 user=demo:demo 20 21 no-tcp 22 no-tls 23 no-tcp-relay 24 stun-only 25 #secure-stun
简单说明,listening-ip 监听的IP,这里的IP必须是外网IP,若是服务器有外网IP网卡便可。最近(2018-04)购买的阿里云ECS虚拟主机,因为采用VPC专有网络架构。购买的ECS主机在主机内部是看不到外网网卡的,提工单问了,说是不行了,新的专有网络跟之前的经典网络不同了。如今的网络是在外面映射到虚拟主机的。这就很尴尬了,到时候若是要实际生产,那应该是要购买独立物理主机了。 好吧,我错了。是能够正常的运行的,是由于阿里云网络安全组的问题,相似与防火墙的东西。以前配了TCP,忘记还有UDP这回事了。道歉!!各位亲,要注意哦。我是在排查问题时,发现,全部的UDP包都处理不了,忽然想起的。架构
cert 和 pkey 这两个是配置若是要进行安全SSL链接用到的证书。TCP是SSL,UDP是DTLS链接。
no-tcp 和 no-tls 表示不要TCP链接了,由于NAT穿透,对TCP支持很弱,通常是使用UDP来进行穿透。
因为产品处于前期,这里只支持stun服务,对于turn的转发功能,暂时不考虑。tcp
3.启动turnserver服务工具
4.测试stun服务
(1) 使用 turnutils_stunclient 测试
可经过该工具进行查询,里面出现 The response is an error 401 (Unauthorized) 是若是服务器开启secure-stun选项时,那么就会出现401未受权错误。turnutils_stunclient 是不支持账号密码验证的。
(2) 使用 turnutils_uclient 测试
./turnutils_uclient -S -v -u demo -w demo -i pem/turn_client_cert.pem -k pem/turn_client_pkey.pem xxx.wunaozai.com -y
若是不用账号密码校验的能够不用 -u -w, 若是不用DTLS安全链接的能够不要-i -k.
(3) 使用 stun-client-0-96.exe 测试
(4)使用 NatTypeTester.exe 测试
使用 https://github.com/webrtc/samples 下 /src/content/peerconnection/trickle-ice/index.html 测试
5.参考资料
https://www.cnblogs.com/idignew/p/7446121.html
https://www.cnblogs.com/idignew/p/7440048.html
https://www.cnblogs.com/kakawater/p/7112925.html
https://www.cnblogs.com/mobilecard/p/6544731.html
https://www.cnblogs.com/lingdhox/p/4209659.html
本文地址: https://www.cnblogs.com/wunaozai/p/9071097.html