如何让WEBRTC 支持P2P RELAY
一:下载
libevent下载
http://libevent.org/
turnserver下载
https://github.com/coturn/coturn new
https://github.com/freewebsys/rfc5766-turn-server old
二:配置
添加用户密码
1. 直接修改 turnuserdb.conf
添加一行 sjz:111111
2. turnadmin 工具生成 会提示生成到哪个文件
turnadmin -a -u baoyu -r by -p 111111
生成用户和秘钥
hxy:0xcb39d7e60e8b6e4e98b12a93f2f32bf1
注意: 0x 为秘钥 没有则为密码
WEBRTC中只支持密码
eg:
webrtc::PeerConnectionInterface::RTCConfiguration config;
webrtc::PeerConnectionInterface::IceServer server;
server.urls.push_back("stun:192.168.1.116");
server.urls.push_back("turn:192.168.1.116");
server.username = "baoyu";
server.password = "111111";
config.servers.push_back(server);
列车所有用户
turnadmin -l
服务器启动
turnserver -v -r by (-r 的参数必须与之前turnadmin 中的 -r 参数一致)
也可在命令行中传递用户和密码
./turnserver -v --user=hxy:111111 --user=sjz:111111 -r by
三:配置WEBRTC 强制RELAY
google WebRTC - how to set always to use TURN server?
http://stackoverflow.com/questions/22130311/webrtc-how-to-set-always-to-use-turn-server
修改参数
webrtc::PeerConnectionInterface::RTCConfiguration config;
webrtc::PeerConnectionInterface::IceServer server;
server.urls.push_back("stun:192.168.1.116");
server.urls.push_back("turn:192.168.1.116");
server.username = "baoyu";
server.password = "111111";
config.type = webrtc::PeerConnectionInterface::kRelay;
config.servers.push_back(server);
========================================运行日志===============================
turnuser.db
hxy:0xcb39d7e60e8b6e4e98b12a93f2f32bf1
baoyu:111111
root@ubuntu:~# ./turnserver -v -r by
0: log file opened: /var/log/turn_24305_2016-07-14.log
0:
RFC 3489/5389/5766/5780/6062/6156 STUN/TURN Server
Version Citrix-3.2.5.9 'Marshal West'
0:
Max number of open files/sockets allowed for this process: 65536
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: Redis is not supported
0: PostgreSQL is not supported
0: MySQL is not supported
0: OpenSSL compile-time version 0x1000207f: fresh enough
0: Default Net Engine version: 3 (UDP thread per CPU core)
=====================================================
0: Config file found: /root/turnserver.conf
0: Config file found: /root/turnserver.conf
0: Config file found: /root/turnuserdb.conf
0:
CONFIGURATION ALERT: you specified long-term user accounts, (-u option)
but you did not specify the long-term credentials option
(-a or --lt-cred-mech option).
I am turning --lt-cred-mech ON for you, but double-check your configuration.
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.1.116
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.1.116
0: Relay address to use: ::1
0: =====================================================
0: Total: 2 relay addresses discovered
0: =====================================================
0: pid file created: /var/run/turnserver.pid
0: IO method (main listener thread): epoll (with changelist)
0: Wait for relay ports initialization...
0: relay 192.168.1.116 initialization...
0: relay 192.168.1.116 initialization done
0: relay ::1 initialization...
0: relay ::1 initialization done
0: Relay ports initialization done
0: IO method (general relay thread): epoll (with changelist)
0: turn server id=0 created
0: IPv4. TCP listener opened on : 127.0.0.1:3478
0: IPv4. TCP listener opened on : 127.0.0.1:3479
0: IPv4. TCP listener opened on : 192.168.1.116:3478
0: IPv4. TCP listener opened on : 192.168.1.116:3479
0: IPv6. TCP listener opened on : ::1:3478
0: IPv6. TCP listener opened on : ::1:3479
0: IO method (general relay thread): epoll (with changelist)
0: turn server id=1 created
0: IPv4. UDP listener opened on: 127.0.0.1:3478
0: IPv4. TCP listener opened on : 127.0.0.1:3478
0: IPv4. UDP listener opened on: 127.0.0.1:3479
0: IPv4. UDP listener opened on: 192.168.1.116:3478
0: IPv4. UDP listener opened on: 192.168.1.116:3479
0: IPv6. UDP listener opened on: ::1:3478
0: IPv4. TCP listener opened on : 127.0.0.1:3479
0: IPv4. TCP listener opened on : 192.168.1.116:3478
0: IPv4. TCP listener opened on : 192.168.1.116:3479
0: IPv6. TCP listener opened on : ::1:3478
0: IPv6. TCP listener opened on : ::1:3479
0: IPv6. UDP listener opened on: ::1:3479
0: Total UDP servers: 0
0: Total General servers: 2
0: IO method (auth thread): epoll (with changelist)
0: IO method (cli thread): epoll (with changelist)
0: IPv4. CLI listener opened on : 127.0.0.1:5766
3: handle_udp_packet: New UDP endpoint: local addr 192.168.1.116:3478, remote addr 192.168.1.113:53129
3: session 001000000000000001: user <>: incoming packet BINDING processed, success
3: handle_udp_packet: New UDP endpoint: local addr 192.168.1.116:3478, remote addr 192.168.1.113:53131
3: session 001000000000000002: user <>: incoming packet BINDING processed, success
3: handle_udp_packet: New UDP endpoint: local addr 192.168.1.116:3478, remote addr 192.168.1.113:53133
3: session 001000000000000003: user <>: incoming packet BINDING processed, success
3: handle_udp_packet: New UDP endpoint: local addr 192.168.1.116:3478, remote addr 192.168.1.113:53135
3: session 001000000000000004: user <>: incoming packet BINDING processed, success
4: session 001000000000000001: user <>: incoming packet message processed, error 401: Unauthorised
4: session 001000000000000002: user <>: incoming packet message processed, error 401: Unauthorised
4: session 001000000000000003: user <>: incoming packet message processed, error 401: Unauthorised
4: session 001000000000000004: user <>: incoming packet message processed, error 401: Unauthorised
4: IPv4. Local relay addr: 192.168.1.116:63300
4: session 001000000000000001: new, username=
4: session 001000000000000001: user
4: IPv4. Local relay addr: 192.168.1.116:53067
4: session 001000000000000002: new, username=
4: session 001000000000000002: user
4: IPv4. Local relay addr: 192.168.1.116:49434
4: session 001000000000000003: new, username=
4: session 001000000000000003: user
4: IPv4. Local relay addr: 192.168.1.116:63580
4: session 001000000000000004: new, username=
4: session 001000000000000004: user
4: handle_udp_packet: New UDP endpoint: local addr 192.168.1.116:3478, remote addr 192.168.1.226:59724
4: session 000000000000000001: user <>: incoming packet BINDING processed, success
4: session 001000000000000004: refreshed, username=
4: session 001000000000000004: user
4: session 001000000000000003: refreshed, username=
4: session 001000000000000003: user
4: session 001000000000000002: refreshed, username=
4: session 001000000000000002: user
4: session 000000000000000001: user <>: incoming packet message processed, error 401: Unauthorised
4: IPv4. Local relay addr: 192.168.1.116:63258
4: session 000000000000000001: new, username=
4: session 000000000000000001: user
4: session 000000000000000001: peer 192.168.1.116 lifetime updated: 300
4: session 000000000000000001: user
4: session 001000000000000001: peer 192.168.1.116 lifetime updated: 300
4: session 001000000000000001: user
4: session 001000000000000001: peer 192.168.1.116 lifetime updated: 600
4: session 001000000000000001: user
4: session 000000000000000001: peer 192.168.1.116 lifetime updated: 600
4: session 000000000000000001: user
5: session 001000000000000004: closed (2nd stage), user
5: session 001000000000000003: closed (2nd stage), user
5: session 001000000000000002: closed (2nd stage), user
5: session 000000000000000001: usage: username=
5: session 001000000000000001: usage: username=
6: session 000000000000000001: usage: username=
6: session 001000000000000001: usage: username=
7: session 001000000000000001: usage: username=
7: session 000000000000000001: usage: username=
8: session 001000000000000001: usage: username=
8: session 000000000000000001: usage: username=
9: session 000000000000000001: usage: username=
9: session 001000000000000001: usage: username=