본문 바로가기
WebRTC

클라우드 서버에 turn 서버 띄우기 (feat. Google Cloud Platform, CoTurn)

by Playdev 2021. 12. 12.
728x90

이전 게시글 (https://playdev.tistory.com/13) 을 통해 Mac 에서 간단하게 Turn 서버를 띄우는 기록을 남겼었다.

이후 잠깐의 테스트는 진행했으나 스타벅스와 같은 카페에서 로컬&외부 기기 테스트 (LTE <> Wifi) 시 원할하지 않아 더는 진행을 못하고 있었다.

 

이번 기회에 Google Cloud Platform (GCP) 에 신규 인스턴스를 생성하여 세팅했던 기록을 간략히 남겨두려 한다.

여기서는 인스턴스 생성, 도메인, 인증서 발급 등은 생략하고 오로지 Coturn 서버를 세팅하는 데에만 기록을 남긴다.

 

 

1. 인스턴스를 하나 생성한다.

- 적절하게 스펙을 정하고, 여기서는 테스트용도로 f1-micro (vCPU 1, 614MB 메모리) 를 사용하였다. (월 약 6$)

- 네트워크 방화벽은 turn 으로 하나 생성하여 3478, 5349(tls) tcp, udp 모두 개방해준다.

[Compute Engine 생성]

 

2. 도메인의 각 A 레코드를 설정한다.

- 여기서는 turn.xxxxxx.com 과 stun.xxxxx.com 과 같은 서브 도메인을 사용하였다.

[도메인 DNS 설정]

 

3. Coturn 설치 및 설정

- 기본 설치 및 중지

sudo apt-get -y update

sudo apt-get install coturn

sudo systemctl stop coturn

 

- 아래와 같이 주석처리 되어있는 'TURNSERVER_ENABLED' 를 활성화 시킨다.

sudo vi /etc/default/coturn

# /etc/default/coturn
TURNSERVER_ENABLED=1

 

- turnserver.conf 를 수정한다. 여기서는 일부 정보를 XXXXX, YYYYY, ZZZZZ 로 표기 했고 상황에 맞게 지정하면 된다.

sudo vi /etc/turnserver.conf

# /etc/turnserver.conf
# STUN server port is 3478 for UDP and TCP, and 5349 for TLS.
# Allow connection on the UDP port 3478
listening-port=3478
# and 5349 for TLS (secure)
tls-listening-port=5349
# Require authentication
fingerprint
lt-cred-mech
# We will use the longterm authentication mechanism, but if
# you want to use the auth-secret mechanism, comment lt-cred-mech and 
# uncomment use-auth-secret
# Check: https://github.com/coturn/coturn/issues/180#issuecomment-364363272
#The static auth secret needs to be changed, in this tutorial
# we'll generate a token using OpenSSL
# use-auth-secret
# static-auth-secret=replace-this-secret
# ----
# If you decide to use use-auth-secret, After saving the changes, change the auth-secret 
using the following command:
# sed -i "s/replace-this-secret/$(openssl rand -hex 32)/" /etc/turnserver.conf
# This will replace the replace-this-secret text on the file with the generated token usi
ng openssl. 
# Specify the server name and the realm that will be used
# if is your first time configuring, just use the domain as name
server-name=XXXXX.com
realm=ZZZZZ
# Important: 
# Create a test user if you want
# You can remove this user after testing
user=XXXXX:YYYYY
total-quota=100
stale-nonce=600
# Path to the SSL certificate and private key. In this example we will use
# the letsencrypt generated certificate files.
cert=/etc/letsencrypt/live/turn.XXXXX.com/fullchain.pem
pkey=/etc/letsencrypt/live/turn.XXXXX.com/privkey.pem
# Specify the allowed OpenSSL cipher list for TLS/DTLS connections
cipher-list="ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-S
HA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384"
# Specify the process user and group
proc-user=turnserver
proc-group=turnserver

 

- turnadmin 을 활용하여 long term user 생성

# sudo turnadmin -a -u USERNAME -r REALM -p PASSWORD

sudo turnadmin -a -u XXXXX -r ZZZZZ -p YYYYY

 

4. Coturn 시작 및 상태 확인

- 시작

sudo systemctl start coturn

 

- 상태 확인

sudo systemctl status coturn

[Coturn 서비스 상태]

 

5. 테스트

아래 링크를 통해 테스트를 진행할 수 있다.

https://webrtc.github.io/samples/src/content/peerconnection/trickle-ice/

 

Trickle ICE

 

webrtc.github.io

[Turn ICE 테스트]

 

 

로그는 아래 와 같이 확인이 가능하다.

tail -f /var/log/turn_*.log

 

 

아래 영상은 위에서 간단하게 구축해본 Turn 서버를 통해 실제 기기 <> 에뮬레이터 간 스트리밍을 해본 예시

 

[LTE - Wifi 간 WebRTC 연결]

 

추후엔 아래와 같은 형태로 다중 Turn 서버를 운영해볼 수도 있을 것 같다.

https://github.com/ant-media/Ant-Media-Server/wiki/Coturn-Cluster

728x90