WebRTC学习笔记(1):在阿里云基于coturn搭建STUN\TURN服务器
- WebRTC
- NAT
- 为何要进行NAT穿越
- coturn服务器搭建
- 下载
- 安装
- 配置
- 启动服务
- 测试
- 关闭coturn服务
WebRTC
WebRTC,名称源自网页即时通信(英语:Web Real-Time Communication)的缩写,是一个支持网页浏览器进行实时语音对话或视频对话的API。提供了视频会议的核心技术,包括音视频的采集、编解码、网络传输、显示等功能,并且还支持跨平台:windows,linux,mac,android。
NAT
NAT(Network Address Translation,网络地址转换),是一种在 IP 数据包通过路由器或防火墙时重写来源 IP 地址或目的 IP 地址的技术。这种技术被普遍使用在有多台主机但只通过一个公有 IP 地址访问因特网的私有网络中。它是一个方便且得到了广泛应用的技术。当然,NAT 也让主机之间的通信变得复杂,导致了通信效率的降低。NAT 是作为一种解决 IPv4 地址短缺以避免保留 IP 地址困难的方案而流行起来的。
为何要进行NAT穿越
端到端通信的一个主要问题是,在许多情况下,这些端点并不在公共互联网中,而是位于网络(和端口)地址转换器(NAT)后面的专用地址空间中。NAT将端点的真实IP地址隐藏于世界其他地方,这使得端点之间建立端到端直接连接变得困难。这就是协助框架—包括STUN和TURN(或使用中继NAT穿越)—派上用场的地方。
coturn服务器搭建
coturn是一个开源的STUN/TURN服务器。
操作环境:阿里云服务器
操作系统:Centos7.8.2003(64位)
coturn部署需要使用openssl和openssl-devel
1 2 | yum -y install openssl yum -y install openssl-devel |
下载
- 使用Git下载安装
1 | git clone https://github.com/coturn/coturn.git |
- 下载压缩包安装
1 | wget https://coturn.net/turnserver/v4.5.1.2/turnserver-4.5.1.2.tar.gz |
这里使用的下载压缩包安装的方式,下载完成之后,执行解压操作。
1 | tar -zxvf turnserver-4.5.1.2.tar.gz |
安装
安装过程中可能出现权限相关问题,我这里直接以root用户执行。
1 2 3 4 | cd turnserver-4.5.1.2 ./configure --prefix=/usr/local/turnserver make make install |
指定安装到/usr/local/turnserver目录下。
环境变量可根据个人所需进行配置,自行搜索。
配置
1 2 3 | cd /usr/local/turnserver/etc/ cp turnserver.conf.default turnserver.conf vim turnserver.conf |
编辑turnserver.conf文件,添加一下内容,其他参数可使用默认配置
1 2 3 4 | listening-port=3478 #监听端口,默认3478 external-ip=39.96.xxx.xxx #外网IP user=test:123456 #用户名:密码,可配置多个 #realm=mycompany.org #域名,可选 |
启动服务
由于没有配置环境变量,启动服务需要到安装目录下。
1 2 | cd /usr/local/turnserver/ ./bin/turnserver -v -r 39.96.xxx.xxx -a -o -c ./etc/turnserver.conf |
注意:一定要打开相关端口,配置UDP、TCP。
我这边是阿里云服务器,在安全组里配置,其他服务器自行搜索。

测试
- 官方测试地址

先通过Add Server添加STUN、TURN地址,选中其中一个服务,点击Gather candidates进行测试,看看有没有返回relay。
这边使用Firefox浏览器,Chrome浏览器测试提示有错误,如知原因,还请告知。 - coturn自带工具测试
- 手写测试工具,感谢测试coturn服务器是否可用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <script type="text/javascript"> function checkTURNServer(turnConfig, timeout) { return new Promise(function(resolve, reject) { setTimeout(function() { if (promiseResolved) return; resolve(false); promiseResolved = true; }, timeout || 5000); var promiseResolved = false, myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection //compatibility for firefox and chrome , pc = new myPeerConnection(turnConfig), noop = function() {}; pc.createDataChannel(""); //create a bogus data channel pc.createOffer(function(sdp) { console.log(Math.random()+"createOffer:"+JSON.stringify(sdp)) if (sdp.sdp.indexOf('typ relay') > -1) { // sometimes sdp contains the ice candidates... promiseResolved = true; resolve(true); } pc.setLocalDescription(sdp, noop, noop); }, noop); // create offer and set local description pc.onicecandidate = function(ice) { //listen for candidate events console.log("onicecandidate:"+ice.candidate.candidate) if (promiseResolved || !ice || !ice.candidate || !ice.candidate.candidate || !(ice.candidate.candidate.indexOf( 'typ relay') > -1)) return; promiseResolved = true; resolve(true); }; }); } function testtrun() { checkTURNServer({ 'iceServers': [ { 'urls': 'stun:39.96.xxx.xxx:3478' }, { 'urls': 'turn:39.96.xxx.xxx:3478', 'credential': "123456", 'username': "test" } ] }).then(function(bool) { var resTxt = bool ? 'yes' : 'no'; document.getElementById('spRes1').innerHTML = resTxt; console.log('is my TURN server active? ', resTxt); }).catch(console.error.bind(console)); } </script> |
关闭coturn服务
1 2 | ps -ef | grep turnserver kill -9 pid |
