内网 coturn(TURN Server)联通性测试与验证 | 8月更文挑战

5,176 阅读3分钟

使用 Trickle ICE 验证 TURN Server 是否可以正常工作时,Trickle ICE 和 coturn 的 log 都有错误打印,对于配置是否正确不敢确认,搭建了完整的测试环境验证后通过抓包才最终放心,解除了心中的疑虑。

安装与配置

网上介绍 TURN Server 安装与配置的文章挺多的,我是在 CentOS 上使用,参考的是 Centos7 安装coturn部署一套 STUN/TURN 服务 webRTC打洞服务器

按照文章的步骤一步一步操作基本上就 OK 了,碰到一个问题需要注意一下,4.5.2 版本在使用 CentOS 7 系统下无法正常工作,回退版本可以了,当时以为配置文件的问题查了很久,最后看到下面两个 Issue 才知道。

使用 Trickle ICE 测试

参考网上的文章使用 Trickle ICE 测试 TURN Server ,说是看到下面的 Done 就可以了。

但是看到下面的错误提示,对于是否可以正常工作还是很大疑虑。

The server turn:192.168.0.221:3478?transport=udp returned an error with code=701: TURN allocate request timed out.

image.png

通过查看 Trickle ICE 的源码发现指定 iceTransportPolicy 可以让 ICE 直接使用 TURN Server 进行转发而不是使用默认的优先级,这样可以进行抓包观察验证 TURN Server 是否正常的进行转发了。

image.png

从图中可以看到,ICE 获取到了三个 UDP 的主机候选项和三个 TCP 的主机候选项,以及一个中继候选项,因为是在同一个局域网中,获取不到反射候选项。

TimeComponentTypeFoundationProtocolAddressPortPriority
0.020rtphost3598251130udp172.31.112.150126126 | 32542 | 255
0.021rtphost6840418udp192.168.0.2350127126 | 32286 | 255
0.021rtphost1553261686udp192.168.30.2350128126 | 32030 | 255
0.072rtprelay4222569549udp192.168.0.221204122 | 32286 | 255
0.122rtphost2566588554tcp172.31.112.1990 | 32542 | 255
0.122rtphost1324063890tcp192.168.0.23990 | 32286 | 255
0.122rtphost303503494tcp192.168.30.23990 | 32030 | 255
39.867Done
39.871

Type 中的 host 是主机候选,relay 是中继候选项

搭建环境验证

1. 网络环境

  • 网段一:192.168.0.xxx
  • 网段二:192.168.100.xxx

网段一是主网络,模拟公网环境,TURN Server 和 Janus 的服务器在此网络中,两个服务都在一台设备上,IP 地址是 192.168.0.221.

网段二是自网络,模拟局域网,互动的电脑在此网络中,子网段路由器的 WAN IP 是 192.168.0.251。

2. 使用 Trickle ICE 测试 STUN 和 TURN

对比之前,多了服务器反射候选项,说明 STUN 正常工作

image.png

3. 使用 Janus 测试抓包

修改 RTCPeerConnection 初始化配置,将 iceTransportPolicy 设置为 relay,然后开始互动并使用 WireShark 进行抓包。

    const pc = new RTCPeerConnection({
      iceServers: [
        {
          urls: ['turn:192.168.0.221:3478'],
          username: 'test',
          credential: '123456',
          credentialType: 'password',
        },
      ],
      iceTransportPolicy: 'relay',
      bundlePolicy: 'max-bundle',
      rtcpMuxPolicy: 'require',
      // certificates: '',
      iceCandidatePoolSize: '0',
      sdpSemantics: 'unified-plan',
      tcpCandidatePolicy: 'disable',
      IceTransportsType: 'nohost',
    });

开始互动后 publish 流成功了,观察 Janus 消息,浏览器上报的 candidate 里面只有 relay 地址,基本可以确定 TURN Server 可以正常工作。

image.png

通过 wireshark 的过滤可以看到,所有发送给 192.168.0.221 的 udp 包目标端口都是 3478,可以确定流是通过 TURN Server 转发到 Janus 的。

image.png

image.png

配置文件参考

测试时候的配置,有需要可以参考下

# TURN server name and realm
realm=192.168.0.221
server-name=turnserver

# Use fingerprint in TURN message
fingerprint

# IPs the TURN server listens to
# listening-ip=192.168.0.221

# External IP-Address of the TURN server
#external-ip=121.199.22.135

# Main listening port
listening-port=3478

# Further ports that are open for communication
min-port=20000
max-port=22000

# Enable verbose logging
verbose

# Specify the user for the TURN authentification
user=test:123456

# Enable long-term credential mechanism
lt-cred-mech

参考资料: