ぷろぐらま

Web、JavaEEの話題を中心に

AWS-EC2上にTURNサーバを構築する

EC2上にWebRTCで利用するTurnサーバを構築する方法。
OSはUbuntu、Turnサーバにはrfc5766-turn-serverを利用します。
https://code.google.com/p/rfc5766-turn-server/

●turnサーバのインストール

$ tar xvfz turnserver-3.2.5.7-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz
$ apt-get update 
$ apt-get install gdebi-core
$ gdebi rfc5766-turn-server_3.2.5.7-1_amd64.deb

●有効化(以下のコメントを外す)

$ sudo vi /etc/default/rfc5766-turn-server
#TURNSERVER_ENABLED=1

●各種設定(必須)
/etc/turnserver.confを以下のように編集

# アクセスを受け付けるIP
external-ip=xxx.xxx.xxx.xxxx   #ec2のPublic IPを指定
# フィンガープリントを有効化
fingerprint
# TURNではlong-term-credientialを利用する必要あり
lt-cred-mech
# long-term-credientialで利用するrealmの指定
realm=mylocal.domain   #任意の値でOK
# long-term-credientialで利用するユーザ管理ファイルを指定
userdb=/etc/turnuserdb.conf

●TURNを利用するためのユーザアカウントを作成
userdbで指定したファイルを以下のように変更。
ここではユーザ名「webrtc」、パスワード「mypassword」のアカウントを1つ追加

#This file can be used as user accounts storage for long-term credentials mechanism.
#
#username1:key1
#username2:key2
# OR:
#username1:password1
#username2:password2
webrtc:mypassword

ここで指定したユーザ名を利用して以下のようにアクセスする。

var pc_config = {"iceServers":[
  {"url":"stun:xxxdomain.com:3478"},
  {"url":"turn:xxxdomain.com:3478", "username":"webrtc", "credential":"mypassword"}
]};
var peer = new webkitRTCPeerConnection(pc_config);

●各種設定(任意)

listening-port=3478  #ポート番号の指定

●起動と終了

service rfc5766-turn-server start
service rfc5766-turn-server stop