Install libevent
Because the libevent version installed by apt is not satisfied by default.
wget https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
tar zxvf libevent-2.1.12-stable.tar.gz
cd libevent-2.1.12-stable
./configure
mkdir build
cd build
cmake ..
make && make install
如果出现:
configure: error: openssl is a must but can not be found. You should add the directory containing ‘openssl.pc’ to the ‘PKG_CONFIG_PATH’ environment variable, or set ‘CFLAGS’ and ‘LDFLAGS’ directly for openssl, or use `–disable-openssl’ to disable support for openssl encryption
则先安装:sudo apt-get install libssl-dev。
如果已经安装还报错,则按上文提示处理
wget https://coturn.net/turnserver/v4.5.0.8/turnserver-4.5.0.8.tar.gz
tar -zxvf turnserver-4.5.0.8.tar.gz
cd turnserver-4.5.0.8/
./configure --prefix=/usr/local/turnserver # 指定安装的目录
make && make install
vim ~/.bashrc
export turnserver_home=/usr/local/turnserver
export PATH=
P
A
T
H
:
PATH:
PATH:turnserver_home/bin
source ~/.bashrc
配置 turnserver
创建配置文件
touch /etc/turnserver.conf
vim /etc/turnserver.conf
# TURN server name and realm
realm=mytest
server-name=turnserver
# Use fingerprint in TURN message
fingerprint
# IPs the TURN server listens to
listening-ip=0.0.0.0
# External IP-Address of the TURN server
external-ip=IP_ADDRESS
# Main listening port
listening-port=3478
# Further ports that are open for communication
min-port=10000
max-port=20000
# Log file path
log-file=/var/log/turnserver.log
# Enable verbose logging
verbose
# Specify the user for the TURN authentification
user=test:test123
# Enable long-term credential mechanism
Lt-cred-mech
通过配置文件启动 turnserver
turnserver -v -r mytest -a -o -c /etc/turnserver.conf
/usr/local/turnserver/bin/turnserver -v -r mytest -a -o -c /etc/turnserver.conf
Test:
Test URL:
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
If you test a STUN server, it works if you can gather a candidate with type “srflx”. If you test a TURN server, it works if you can gather a candidate with type “relay”.
/usr/local/turnserver/bin/turnutils_uclient -u test -w test123 -y sg.mytest.hk
一定要加上 -y 参数,client-to-client 才可以看到:
leon@v-server:~/src/turnserver-4.5.0.8$ /usr/local/turnserver/bin/turnutils_uclient -u test -w test123 -y sg.mytest.hk
0: Total connect time is 0
1: start_mclient: msz=4, tot_send_msgs=0, tot_recv_msgs=0, tot_send_bytes ~ 0, tot_recv_bytes ~ 0
2: start_mclient: msz=4, tot_send_msgs=3, tot_recv_msgs=3, tot_send_bytes ~ 300, tot_recv_bytes ~ 300
3: start_mclient: msz=4, tot_send_msgs=10, tot_recv_msgs=10, tot_send_bytes ~ 1000, tot_recv_bytes ~ 1000
4: start_mclient: msz=4, tot_send_msgs=15, tot_recv_msgs=15, tot_send_bytes ~ 1500, tot_recv_bytes ~ 1500
5: start_mclient: msz=4, tot_send_msgs=15, tot_recv_msgs=15, tot_send_bytes ~ 1500, tot_recv_bytes ~ 1500
5: start_mclient: tot_send_msgs=20, tot_recv_msgs=20
5: start_mclient: tot_send_bytes ~ 2000, tot_recv_bytes ~ 2000
5: Total transmit time is 5
5: Total lost packets 0 (0.000000%), total send dropped 0 (0.000000%)
5: Average round trip delay 0.000000 ms; min = 0 ms, max = 0 ms
5: Average jitter 0.450000 ms; min = 0 ms, max = 4 ms
DEBUG
If got errors below, try it withou VPN:
The server stun:sg.mytest.hk:3478 returned an error with code=701:
STUN host lookup received error.
The server turn:sg.mytest.hk:3478?transport=udp returned an error with code=701:
TURN host lookup received error.
USAGE:
let iceServer = {
iceServers: [
{
url: "stun:xxx:3478"
},
{
urls: "turn:xxxx:3478",
username: "xxxx",
credential: "123456",
},
],
};
Golang:
var (
defaultRTCConfiguration = webrtc.RTCConfiguration{
IceServers: []webrtc.RTCIceServer{
{
URLs: []string{
"turn:sg.mytest.hk:3478",
},
Username: "test",
Credential: "test123",
CredentialType: 1,
},
},
}
)
安装服务:
sudo vi /etc/systemd/system/turnserver.service
[Unit]
Description=coturn
Documentation=man:coturn(1) man:turnadmin(1) man:turnserver(1)
After=syslog.target network.target
[Service]
Type=forking
PIDFile=/var/run/turnserver.pid
ExecStart=/usr/local/turnserver/bin/turnserver --daemon --pidfile /var/run/turnserver.pid -c /etc/turnserver.conf
ExecStopPost=/usr/bin/rm -f /var/run/turnserver.pid
Restart=on-abort
LimitCORE=infinity
LimitNOFILE=999999
LimitNPROC=60000
LimitRTPRIO=infinity
LimitRTTIME=7000000
CPUSchedulingPolicy=other
sudo systemctl enable turnserver
sudo systemctl start turnserver
参考:
https://yuanchieh.page/posts/2020-09-21_aws-coturn-server-%E6%9E%B6%E8%A8%AD%E6%95%99%E5%AD%B8/
https://blog.csdn.net/kyl282889543/article/details/106816517#t13