推荐阅读
coturn 官方文档: turnserver 和 turnutils_uclient
轻量服务器不支持"安全组",所以放行是在"防火墙"
防火墙放行端口
TURN 在中继时需要很多端口,所以在这需要放行一批
此处放行的端口,还需要在后续的配置中体现,见后文
sudo apt update
sudo apt install coturn
vim /etc/turnserver.conf
配置说明如下
# === 基本网络设置 ===
listening-port=3478
tls-listening-port=5349
# 你的公网 ip
external-ip=xxx.xxx.xxx.xxx
# 下面这两个不能开启,让 coturn 自行获取
# https://github.com/coturn/coturn/issues/1082#issuecomment-1365539726
# listening-ip=0.0.0.0
# relay-ip=0.0.0.0
# 前文说的,放行端口范围,需要在此处指定
min-port=50000
max-port=51000
fingerprint
server-name=your.domain
realm=your.domain
# 静态用户认证
# lt-cred-mech
# user=username:password
# 建议使用动态用户认证
# use-auth-secret 和 lt-cred-mech 不能同时使用
use-auth-secret
static-auth-secret=your_secret
# === TLS 证书(可选)===
# 需要确保 coturn 能访问到
cert=/path/to/your/fullchain.cer
pkey=/path/to/your/cert.key
# === 安全性建议 ===
stale-nonce
no-multicast-peers
# gpt 会说配置 no-loopback-peers
# 但是最新的 coturn 的配置是 --allow-loopback-peers
# 默认禁止 loopback-peers,需要你明确允许,一般不需要允许
在服务器上运行 coturn 测试命令(-v
便于调试)
sudo turnserver -v
在我们本地也安装 coturn
, 就可以使用命令行工具 turnutils_uclient
来测试连接了。
然后在我们本地(不是服务器上)使用 ts 生成账号密码
import crypto from 'crypto'
export function getStunConfig() {
// 1小时后过期
const timestamp = Math.floor(Date.now() / 1000) + 3600
const username = `${timestamp}:user`
// TURN_SECRET 是上面的配置中 static-auth-secret 的值
const hmac = crypto.createHmac('sha1', TURN_SECRET)
hmac.update(username)
const password = hmac.digest('base64')
// turnutils_uclient 访问 your_turn_domain 时候不要带协议和端口
// 应当是 'turn.example.com' 或者 'example.com'
// 看你自己把哪个域名解析到 TURN 服务器
console.log(`turnutils_uclient -u ${username} -w ${password} -r your_realm your_turn_domain`)
return { username, password }
}
此时在本地执行 log 出来的命令 turnutils_uclient ...
,成功就表示 turn
服务没问题。
继续执行 openssl s_client -connect your_turn_domain:5349
,成功就表示 turns
没问题。
如果还失败,那你仔细看服务端的 coturn log 找找原因吧。
gpt 说:
大多数现代浏览器(尤其是 Chrome / Firefox)只信任 TLS TURN(即
turns:
协议)
所以建议为 TURN 服务配置 TLS,使用turns:test.cc:5349
如非特别声明,本站作品均为原创,遵循【自由转载-保持署名-非商用-非衍生 创意共享 3.0 许可证】。
对于转载作品,如需二次转载,请遵循原作许可。