0Pricing
Real-Time Streaming Systems (WebRTC + Live Data) · 课时

部署并保护您自己的 TURN 服务器

学习使用 coturn 自托管 TURN 服务器,使用有时限的令牌安全配置凭据,并决定采用自托管还是托管 TURN 服务。

部署并保护您自己的 TURN 服务器 是 CoddyKit 上的免费 Real-Time Streaming Systems (WebRTC + Live Data) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Real-Time Streaming Systems (WebRTC + Live Data) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

From Theory to Operation

You understand NAT challenges, STUN, and what TURN does. Now you will actually run a TURN server, secure it, and connect WebRTC to it. The most common open-source choice is coturn.

Why Self-Host TURN

Public STUN is free, but TURN relays media and consumes bandwidth, so it is rarely free. Running your own TURN server gives you control over capacity, cost, and privacy.

Installing coturn

On a Linux server you install coturn from the package manager. It runs as a background service.

sudo apt-get update
sudo apt-get install -y coturn
sudo systemctl enable coturn

Basic Configuration

coturn reads /etc/turnserver.conf. A minimal config sets the realm and listening ports.

listening-port=3478
tls-listening-port=5349
realm=turn.example.com
fingerprint

The Credential Problem

TURN requires authentication or anyone could relay traffic through your server at your expense. Hardcoding a static username and password is risky because clients can leak them.

Time-Limited Credentials

The secure approach is the REST/ephemeral credential mechanism. Your server generates short-lived usernames and passwords derived from a shared secret, so leaked credentials expire quickly.

use-auth-secret
static-auth-secret=your_long_shared_secret

Generating a Credential

Your backend creates a username as an expiry timestamp and signs it with HMAC-SHA1 using the shared secret. The signature becomes the password.

const crypto = require('crypto');
function turnCredential(secret, ttl) {
  const username = String(Math.floor(Date.now() / 1000) + ttl);
  const hmac = crypto.createHmac('sha1', secret);
  hmac.update(username);
  const password = hmac.digest('base64');
  return { username, password };
}

Wiring It Into WebRTC

Pass the TURN URL and ephemeral credentials into the peer connection's ICE server list. WebRTC uses them when direct paths fail.

const pc = new RTCPeerConnection({
  iceServers: [{
    urls: 'turn:turn.example.com:3478',
    username: cred.username,
    credential: cred.password
  }]
});

Use TLS and TCP Fallback

Some restrictive networks block UDP entirely. Offer turns: over TCP on port 443 so media can tunnel through firewalls that only allow HTTPS traffic.

// add a TLS/TCP TURN entry alongside the UDP one
urls: 'turns:turn.example.com:443?transport=tcp'

Self-Host vs Managed

Self-hosting coturn is cheaper at scale but means you handle uptime, bandwidth, and security. Managed TURN providers cost more per GB but remove operational burden. Pick based on your team and traffic.

Operating Responsibly

Monitor bandwidth, rotate the shared secret periodically, restrict relay to authenticated users, and place the server geographically near your users to minimize latency. A well-run TURN server is the safety net that makes calls connect everywhere.

Quick Check

Test your understanding of TURN deployment.

Recap

You learned to deploy and secure TURN:

  • Install and configure coturn with a realm and ports
  • Use use-auth-secret with HMAC-based ephemeral credentials
  • Wire credentials into the ICE server list
  • Offer TLS/TCP on 443 for restrictive networks
  • Weigh self-hosting against managed services

A secure TURN server ensures calls connect even behind tough NATs.

常见问题解答

「部署并保护您自己的 TURN 服务器」课时是免费的吗?

是的 — 「部署并保护您自己的 TURN 服务器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Real-Time Streaming Systems (WebRTC + Live Data) 课程的其余内容,请升级到 CoddyKit PRO。 Real-Time Streaming Systems (WebRTC + Live Data) 课程共包含 4 节课。

「部署并保护您自己的 TURN 服务器」这节课中我会学到什么?

学习使用 coturn 自托管 TURN 服务器,使用有时限的令牌安全配置凭据,并决定采用自托管还是托管 TURN 服务。 你通过在浏览器中直接运行的动手代码来练习 Real-Time Streaming Systems (WebRTC + Live Data),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Real-Time Streaming Systems (WebRTC + Live Data) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Real-Time Streaming Systems (WebRTC + Live Data) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「部署并保护您自己的 TURN 服务器」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Real-Time Streaming Systems (WebRTC + Live Data) 课中编写并运行代码吗?

能。每节 Real-Time Streaming Systems (WebRTC + Live Data) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. NAT 与防火墙带来的挑战
  2. STUN 服务器功能详解
  3. 用于中继连接的 TURN 服务器
  4. 部署并保护您自己的 TURN 服务器
← 返回 Real-Time Streaming Systems (WebRTC + Live Data)