中繼(relay)
在RTCPeeConnection中,使用ICE框架來保證RTCPeerConnection能實現NAT穿越
ICE,全名叫交互式連接建立(Interactive Connectivity Establishment),一種綜合性的NAT穿越技術,它是一種框架,可以整合各種NAT穿越技術如
STUN(Simple Traversal of UDP over NATs,NAT 的UDP簡單穿越);
、TURN(Traversal Using Relay NAT 中繼NAT實現的穿透)。ICE會先使用STUN,嘗試建立一個基於UDP的連接,如果失敗了,就會去TCP(先嘗試HTTP,然后嘗試HTTPS),如果依舊失敗ICE就會使用一個中繼的TURN服務器。
STUN協議可以很好的解決一般家用(NAT)路由器環境的打洞問題,但是對於大部分的企業的網絡環境就不是很好了.這時需要一個新的解決方案:TURN, TURN是一個Client-Server協議
ICE協議就是綜合前面2種協議的綜合性NAT穿越解決方案.
DataChannel是建立在PeerConnection上的,不能單獨使用
盡管WebRTC允許點對點的通信,但它
依然需要服務器
:
* 信令傳輸:建立點對點的連接需要傳輸一些媒體和網絡相關的元數據信息,需要通過服務器
* NAT和防火牆穿透:我們需要通過ICE框架來建立點與點之間的網絡路徑。可以使用STUN服務器(確定雙方的可公開訪問你的IP地址和端口)以及TURN服務器(如果直接連接失敗,就必須數據中繼了)
docker run -it --name apprtc -d -p 8080:8080 -p 8443:8443 -p 8081:80 -p 443:443 ubuntu /bin/bash
工具:
apt-get update
apt-get -y install autoconf automake build-essential libtool wget git vim unzip
解壓並設置環境變量即可:
設置環境變量
export PATH=$PATH:/root/google_appengine/
Node.js 就是運行在服務端的 JavaScript。
采用C++語言編寫而成,是一個Javascript的運行環境。
apt
-
get
install nodejs nodejs-legacy
npm python-webtest
-y
npm install -g npm
執行
npm update -g
npm
指令進行升級,查看版本
npm
-
v
安裝
npm install -g grunt-cli
java -version
ImportError: No module named requests
pip install requests
if you have pip installed
Grunt
.
基於Node.js的項目構建工具,可以自動運行你所設定的任務
JavaScript 世界的構建工具
房間服務器:
項目源碼地址:
https://github.com/webrtc/apprtc
git clone
https://github.com/webrtc/apprtc
cd apprtc
修改配置后編譯:
配置
apprtc/src/app_engine/constants
TURN_BASE_URL
//turn打洞服務器配置
TURN_URL_TEMPLATE
CEOD_KEY = 和coturn turnserver.conf static-auth-secret
一致
WSS_INSTANCES = [{
WSS_INSTANCE_HOST_KEY
: '192.168.214.129:
8089
',
WSS_INSTANCE_NAME_KEY: 'wsserver-std',
WSS_INSTANCE_ZONE_KEY: 'us-central1-a'
}, {
WSS_INSTANCE_HOST_KEY
: '192.168.214.129:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std-2',
WSS_INSTANCE_ZONE_KEY: 'us-central1-f'
}]
ICE_SERVER_BASE_URL
執行
npm install
命令安裝項目依賴的庫,解壓壓縮包到當前項目的node_modules目錄。
再執行下面命令編譯出apprtc
這個GAE app.
grunt build
編譯之后會多出out目錄
dev_appserver.py --host=0.0.0.0 /root/apprtc/out/app_engine/
錯誤:google appengine BindError: Unable to bind 8080
重啟
2,信令服務器
用GO語言編寫的基於websocket的信令服務器:Collider.
1,安裝go,參見我的安裝文檔
使用go version查看go環境是否配置正確
2,
mkdir -p /root/goWorkspace/src
export GOPATH=/root/goWorkspace/
存放Collider的go代碼程序。
cp -rf `pwd`/apprtc/src/collider/* $GOPATH/src
編輯collidermain/main.go,修改設置自己的房間服務器URL:
cd goWorkspace/src
- install dependencies
go get collidermain
- Install collidermain
go install collidermain
成功編譯后會在gopath目錄下生成bin和pkg目錄,執行文件在bin下
Testing
go test collider
編輯$GOPATH/src/collider/collider.go,
設置信令服務器所需要用的
HTTPS
的證書文件
/cert/cert.pem and /cert/key.pem作用
運行
$GOPATH/bin/collidermain -port=8089 -tls=true
可以設置下path= /root/goWorkspace/bin
打洞服務器
STUN/TURN/ICE服務器的搭建:
選擇有更豐富功能的coTurn作為我們的NAT穿越打洞服務器,該項目是一個C/C++語言的開源項目
主要是配置cert和pkey
turnserver.conf中的
cert和pkey配置的自簽名證書用Openssl命令生成:
openssl req -x509 -newkey rsa:2048 -keyout
/etc/turn_server_pkey.pem -out /etc/turn_server_cert.
pem -days 99999 -nodes
pkey應該是private key的縮寫吧
安裝
libevent:
Before installing and configuring the TURN server, the latest stable libevent library should be downloaded, built and installed:
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
As root, build and install the library with the usual:
tar xvfz libevent-2.0.21-stable.tar.gz cd libevent-2.0.21-stable ./configure make && make install
Download the TURN server from
https://github.com/coturn/coturn/wiki/Downloads
. Build and install with:
apt-get install libssl-dev
apt-get install libsqlite3 (or
sqlite3
):
apt-get install
libsqlite3-dev
(or sqlite3-dev)
apt-get install
sqlite3 libsqlite3-dev
apt-get install libevent-dev
tar xvfz turnserver-<...>.tar.gz ./configure make && make install
Check the INSTALL and README (有多個README文件)files with the server. They contain lots of useful information.

By default, coturn uses
SQLite
database for users and settings. That database is automatically created (empty) when the process turnserver starts the first time.
port
3478
(STUN) should be opened for TCP and UDP.
3478、3479:tcp&udp - coTurn 的監聽端口。
59000~65000:tcp&udp - coTurn 的服務端口。
min-port=
59000
max-port=
65000
- Change WSS_INSTANCE_HOST_KEY to the hostname and port Collider is listening too, e.g. localhost:8089 or otherHost:443.
mv /usr/local/share/examples/turnserver/etc/turnserver.conf /etc/turnserver.conf
turnserver -L <public_ip_address> -a -f -r <realm-name>
Or, start it as a daemon with:
$
turnserver
-L <public_ip_address>
-o
-a -f -r <realm-name>
-L, --listening-ip Listener IP address of relay server.
Multiple listeners can be specified, for example:
-L ip1 -L ip2 -L ip3
If no IP(s) specified, then all IPv4 and
IPv6 system IPs will be used for listening.
The same ip(s) can be used as both listening and relay ip(s).
-a, --lt-cred-mech Use long-term credentials mechanism (this one you need for WebRTC usage).
-f, --fingerprint Use fingerprints in the TURN messages. If an incoming request
contains a fingerprint, then TURN server will always add
fingerprints to the messages in this session, regardless of the
per-server setting.
-r, --realm The default realm to be used for the users when no explicit
origin/realm relationship was found in the database, or if the TURN
server is not using any database (just the commands-line settings
and the userdb file). Must be used with long-term credentials
mechanism or with TURN REST API.
-o, --daemon Run server as daemon.
參考:
user=jalon:0xa9f776c6470bab44567b2584863e7345 是通過
# $ turnadmin -k -u jalon -r north.gov -p jalon
# Output: 0xa9f776c6470bab44567b2584863e7345
生成的
參考:
需要自己實現coTurn連接信息接口,主要返回用戶名、密碼和turn配置信息,通常叫做TURN REST API,不實現這個接口的話AppRTCDemo連不上服務器,瀏覽器訪問的話可以正常訪問。
TURN REST API
標准參考文檔
返回json結果示例:
{"username":"1456904882:1231244","password":"jAph7EHMLuPJuxLLC1uRiI3kvq4=","ttl":86400,"uris":["turn:192.168.214.129:3478?transport=udp","turn:192.168.214.129:3478?transport=tcp","turn:192.168.214.129:3479?transport=udp","turn:192.168.214.129:3479?transport=tcp"]}