文章目录
Windows搭建Turn服务
1.安装Cygwin(前置条件)
Cygwin简介
由于coturn目前并不支持windows服务器,所以这里需要安装Cgywin,以便达到在windows也能使用Linux命令的目的。Cygwin 提供一个UNIX 模拟 DLL 以及在其上层构建的多种可以在 Linux 系统中找到的软件包,在 Windows XP SP3 以上的版本提供良好的支持。它对于学习UNIX/Linux操作环境,或者从UNIX到Windows的应用程序移植,或者进行某些特殊的开发工作,尤其是使用GNU工具集在Windows上进行嵌入式系统开发,非常有用。
下载地址:https://cygwin.com/setup-x86_64.exe
安装步骤:
- 选择下载源,“从互联网安装”
- 选择根安装目录
D:/Develop/TurnService/Cygwin64
,以及本地软件包目录D:/Develop/TurnService/Cygwin64
- 选择连接类型,”使用系统代理设置“
- 选择下载站点,建议选择华为(https://mirrors.huaweicloud.com) 、腾讯(https://mirrors.tencent.com)、 阿里(https://mirrors.aliyun.com) 等一些国内镜像地址,下载速度会快很多,如果找不到镜像,可以手动加入此镜像地址
- 选择安装包,查看视图改为“Category / 类别”,软件包列表展开“All”,找到“Devel”分类的软件包,在“新增”一列将“默认”改为“安装”,否则后面编译时会缺各种依赖。
安装途中,可能因系统环境的不同,会报一两个动态库缺失的错误,暂未发现有什么影像,直接点确定跳过即可。
2.下载libevent2源码并编译
libevent的Github项目地址:https://github.com/libevent/libevent
到release页面找到最新版:https://github.com/libevent/libevent/releases
比如,截止目前最新版的下载地址为:https://github.com/libevent/libevent/releases/download/release-2.1.12-stable/libevent-2.1.12-stable.tar.gz
解压并编译安装:
- 装好Cgywin,打开目录下的“Cgywin.bat”,执行命令进入libevent2所在目录。比如,
cd D:/Develop/TurnService/libevent2
。注:系统区别,cd指令中路径的斜杠使用/
而不是\
- 执行命令
./configure
生成配置 - 成功后继续执行
make
命令来编译软件,并使用make install
命令安装软件,也可使用并行命令make && make install
3.下载coturn源码并编译
coturn的Github项目地址:https://github.com/coturn/coturn
到release页面找到最新版:https://github.com/coturn/coturn/releases
比如,截止目前最新版的下载地址为:https://github.com/coturn/coturn/archive/refs/tags/4.6.2.zip
解压并编译安装:
- 装好Cgywin,打开目录下的“Cgywin.bat”,执行命令进入coturn所在目录。比如,
cd D:/Develop/TurnService/coturn
。注:系统区别,cd指令中路径的斜杠使用/
而不是\
- 再执行命令
./configure
生成配置 - 成功后继续执行命令
make
编译软件,并使用make install
命令安装软件,也可使用并行命令make && make install
编译安装过程中,如果出现报错:src/apps/common/ns_turn_utils.c:53:10: 致命错误:sys/syscall.h: No such file or directory 53 | #include <sys/syscall.h>
则打开ns_turn_utils.c源码文件,找到52行,如下所示的代码块:
#if !defined(WINDOWS)
#include <sys/syscall.h>
#include <unistd.h>
#ifdef SYS_gettid
#define gettid() ((pid_t)syscall(SYS_gettid))
#endif
#endif
将其代码修改为:
#if !defined(WINDOWS)
#if __linux
#include <sys/syscall.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <windows.h> // Or something like it.
#endif
#include <unistd.h>
#ifdef SYS_gettid
#define gettid() ((pid_t)syscall(SYS_gettid))
#endif
#endif
保存修改后的源码文件,重新执行命令进行编译安装即可。
4.配置coturn
- 生成用户,启动Cgywin执行该命令:
turnadmin -a -u your_username -p your_password -r xxx.com
上面的命令,将创建用户your_username
,密码为your_password
,同时指定realm为xxx.com,大家根据实际情况修改。 - 切换到目录:
cd /usr/local/etc
里面有一个turnserver.conf.default
的文件 - 复制一份:
cp turnserver.conf.default turnserver.conf
- 打开该文件
turnserver.conf
,并在最后插入如下配置:
#与前ifconfig查到的网卡名称一致(如果无法运行ifconfig,可以在windows的cmd窗口中使用ipconfig,如果只有一块网卡,可以默认使用如下值)
relay-device=eth0
#内网IP(ipconfig查到的IP地址)
listening-ip=192.168.1.188
#公网IP(局域网可以使用与上面相同的ip地址)
external-ip=113.119.27.xx
#第1步创建的用户名密码,创建IceServer时用
user=your_username:your_password
#一般与turnadmin创建用户时指定的realm一致
realm=xxx.com
#端口号
listening-port=3478
#不开启会报CONFIG ERROR: Empty cli-password, and so telnet cli interface is disabled! Please set a non empty cli-password!错误
cli-password=qwerty
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
5.启动coturn服务
执行启动服务的命令:turnserver -a -f -r xxx.com
需要注意的是,-r参数后面是上一步的-r的值,然后ctrl+C退出。
使用 -o 参数后台启动:turnserver -a -o -f -r xxx.com
6.开放服务器相关端口
因coturn配置中使用了3478端口,所以需要放行服务器的3478端口,类型包括TCP、UDP。
7.测试turn服务的可用性
webrtc-samples官网提供了一个测试用的地址
https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/
- 进去后先点“Remove Server”按钮,删除原有服务器配置。
- 然后分别填写stun、turn服务器配置,然后点“Add Server”添加;stun和turn只是开始处不同,后面ip:端口,用户名,密码相同。
- 点下面的“Gather candidates”按钮测试,如下表中的“Address”出现服务器地址和“Type”有显示“Done”,即表示turn服务已成功启动。
Time | Type | Foundation | Protocol | Address | Port | Priority | URL (if present) | relayProtocol (if present) |
---|---|---|---|---|---|---|---|---|
0.014 | host | 2752541178 | udp | xxx.local | 54826 | 126 / 30 / 255 | ||
0.025 | srflx | 2758837020 | udp | xxx.xxx.xx.xx | 54826 | 100 / 30 / 255 | stun:xxx.com:3478 | |
0.104 | relay | 790236971 | udp | xxx.xx.xxx.xx | 55888 | 2 / 31 / 255 | turn:xxx.com:3478?transport=udp | udp |
0.137 | Done |
参考资料: