stun-turn服务器搭建

目录



公共stun服务器

stun:stun1.l.google.com:19302// 273ms
stun:stun2.l.google.com:19302// 280ms
stun:stun3.l.google.com:19302// 262ms
stun:stun4.l.google.com:19302// 268ms

stun:stun.ideasip.com//240ms

stun:stun.schlund.de// 200ms
stun:stunserver.org// 260ms
stun:stun.voiparound.com// 215ms
stun:stun.voipbuster.com       // 225ms

响应速度很慢或者ping不通
stun.fwdnet.net
stun:stun.xten.com
stun.voxgratia.org
stun.voipstunt.com
stun.softjoys.com
stun:stun.rixtelecom.se
stun:stun.iptel.org
stun:stun.ekiga.net
stun:stun01.sipphone.com
stun:23.21.150.121

解析失败(unknown host)
stun:stun.fwdnet.net

stun/turn服务器验证

搭建stun服务器

安装依赖

sudo apt-get install g++
sudo apt-get install make
sudo apt-get install libboost-dev # For Boost
sudo apt-get install libssl-dev # For OpenSSL

下载安装stuntman

wget http://www.stunprotocol.org/stunserver-1.2.7.tgz
tar -zxvf stunserver-1.2.7.tgz
cd stunserver
make

make成功后, 文件夹下会出现三个新文件 stunclient, stunserverstuntestcode

文件名 说明
stunclient 测试客户端
stunserver server
stuntestcode 校验安装是否成功的

首先执行 ./stuntestcode 会出现以下打印

stuntestcode

执行 sudo ./stunserver 启动服务
默认是3478端口
执行 ./stunclient 127.0.0.1 3478 打印如下

stunclient



搭建turn服务器

安装依赖

sudo apt-get install libssl-dev
sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev

下载 libevent-2.0.21-stable.tar.gz (安装三部曲)(源码下载地址http://libevent.org)

注: 如果上面apt-get命令不能正常安装, 可尝试源码安装, 下面是源码下载地址
http://www.openssl.org
http://www.sqlite.org
http://www.postgresql.org
http://www.mysql.org
https://github.com/mongodb/mongo-c-driver
http://redis.io

安装turnserver

下载源码包 https://github.com/coturn/coturn/wiki/Downloads

tar xvfz turnserver-<...>.tar.gz
cd coturn # 解压后的文件夹, 可能不是这个名字
# 安装前可以先看看INSTALL文件
./configure
make
sudo make install

删除所有已安装的turn服务器文件

sudo make deinstall  // 在sudo make deinstall 之前不要使用 make clean, 这样将删除Makefile文件, 将不能卸载.
make clean

安装后会有以下命令

命令 说明
turnserver STUN/TURN 服务器
turnadmin 用于配置、管理账户
turnutils_stunclient 用于测试 STUN 服务
turnutils_uclient 用于测试 TURN 服务, 模拟多个UDP、TCP、TLS 或 DTLS 类型的客户端

配置文件

配置文件默认地址: /usr/local/etc/turnserver.conf.default

拷贝改名为真正使用的配置

cp /usr/local/etc/turnserver.conf.default /usr/local/etc/turnserver.conf
然后修改/usr/local/etc/turnserver.conf 配置
listening-device=eno1
relay-device=eno1
listening-ip=localhost
listening-port=3479
tls-listening-port=5352
#relay-ip=183.131.73.156
#external-ip=183.131.73.156
#external-ip=60.70.80.91/172.17.19.101
# 线程数量
relay-threads=15
# 端口范围
min-port=40000
max-port=45000
#static-auth-secret=demo
#user=demo:0xb8b7c50f71b482d8301784a66451c1fc
# 域
realm=webrtc.asounion.com
# turn的用户名和用户密码,   用户名:密码
user=demo:demo
#userdb=/usr/local/etc/turnuserdb.conf
#max-bps=102400
pidfile="/var/run/turnserver.pid"
#allow-loopback-peers
# 安全
no-multicast-peers
mobility
no-cli
# 指定cert和key
cert=/usr/local/etc/turn_server_cert.pem
pkey=/usr/local/etc/turn_server_pkey.pem
stale-nonce
#use-auth-secret
# 验证用, webrtc需要
lt-cred-mech
# 日志
Verbose
# 验证用 webrtc需要
fingerprint

turnserver启动命令

turnserver -L 183.131.73.156 -a -b /usr/local/etc/turnuserdb.conf -f -r demo

参数说明

  • -L listening-ip 中继服务器的侦听器IP地址。可以指定多个侦听器。如果未指定IP,则将使用所有IPv4和IPv6系统IP进行监听
  • -c 配置文件名(默认 - turnserver.conf)。

    配置文件的格式可以在提供的示例 /etc/turnserver.conf 示例文件中看到。选项的长名称用作文件中的配置项名称。如果不提供绝对路径,则在以下目录中搜索文件:
    current directory
    current directory etc/ subdirectory
    upper directory level etc/
    /etc/
    /usr/local/etc/

  • -b - db, - userdb
  • -r –realm 默认领域使用,为用户在没有明确的产地/境界关系数据库中找到。必须与长期凭证机制或TURN REST API一起使用。
  • -o 守护进程模式
  • -a lt-cred-mech使用长期凭证机制(这是WebRTC使用所需的机制)
  • -f 在TURN消息中使用指纹。如果传入请求包含指纹,则无论每服务器设置如何,TURN服务器将始终在此会话中的消息中添加指纹。

具体参数解释参考 https://github.com/coturn/coturn/wiki/turnserver

启动命令

turnserver -c /usr/local/etc/turnserver.conf
# turnserver -L 183.131.73.156 -a -c /usr/local/etc/turnserver.conf -f -r demo

添加turn服务器开机自启

vim /etc/rc.local 

在该文件里添加turnserver启动脚本.

开机自启


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 245292011@qq.com

文章标题:stun-turn服务器搭建

字数:1.1k

本文作者:常丁方

发布时间:2020-09-19, 15:36:36

最后更新:2021-08-25, 18:31:45

原始链接:http://changdingfang.com/2020/09/19/notes/linux/stun-turn/
×

喜欢就点赞,疼爱就打赏

资源