Turn服务器搭建

2023-09-24 14:19:01 浏览数 (2)

Turn服务器搭建

1、下载并安装libevent-2.0

代码语言:javascript复制
wget https://github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
tar zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure
make && make install

2、下载编译安装coturn

代码语言:javascript复制
git clone https://github.com/coturn/coturn
cd coturn 
./configure 
make 
make install

3、查看是否安装成功

which turnserver

4、编辑配置文件

代码语言:javascript复制
#签名证书
yum install openssl

#生成的两个文件一般在/etc/目录下,如果不是就移动过去
openssl req -x509 -newkey rsa:2048 -keyout /etc/turn_server_pkey.pem -out /etc/turn_server_cert.pem -days 99999 -nodes

#修改配置信息
vim /etc/turnserver/turnserver.conf
代码语言:javascript复制
relay-device=eth0   #与前ifconfig查到的网卡名称一致
listening-ip=172.18.77.60    #内网IP
listening-port=57920
tls-listening-port=5349
relay-ip=172.18.77.60
external-ip=47.107.110.xxx    #公网IP
relay-threads=50
lt-cred-mech
cert=/etc/turn_server_cert.pem
pkey=/etc/turn_server_pkey.pem
pidfile=”/var/run/turnserver.pid”
min-port=49152
max-port=65535
user=admin:123456    #用户名密码,创建IceServer时用

5、启动服务

nohup turnserver -v -L 内网IP -a -u admin -p 123456 -f -r 外网IP &

6、配置 ICE REST API 服务

代码语言:javascript复制
#生成证书
openssl req -x509 -newkey rsa:2048 -keyout key.pem -out /etc/cert.pem -days 99999 -nodes

#移动至/cert目录
mkdir /cert
mv ./key.pem /cert/
mv /etc/cert.pem /cert/
编辑index.js
代码语言:javascript复制
var https = require('https')
var fs = require('fs')
var express = require('express')
var crypto = require('crypto')
var app = express()

var option = {
        key : fs.readFileSync('/cert/key.pem'),
        cert: fs.readFileSync('/cert/cert.pem'),
}

var hmac = function (key, content) {
  var method = crypto.createHmac('sha1', key)
  method.setEncoding('base64')
  method.write(content)
  method.end()
  return method.read()
}

app.get('/iceconfig', function (req, resp) {
  var query = req.query
  var key = '4080218913'
  var time_to_live = 3600*24
  var timestamp = Math.floor(Date.now() / 1000)   time_to_live
  var turn_username = "admin" //timestamp   ':'   'garrylea'
  var password = "123456" //hmac(key, turn_username)

  resp.header("Access-Control-Allow-Origin", "*");   //设置跨域访问

  return resp.send({
    iceServers: [
      {
        urls: [
          'turn:192.168.139.xxx:57920?transport=tcp',  //这四处修改为外网IP地址和端口
          'turn:192.168.139.xxx:57920?transport=udp',
          'turn:192.168.139.xxx:5349?transport=tcp',
          'turn:192.168.139.xxx:5349?transport=udp',
        ],
        username: turn_username,
        credential: password
      }
    ]
  })
})

var server = https.createServer(option, app);

server.listen('3033', function () {
  console.log('server started')
})

7、启动 ICE REST API 服务

npm install -g forever

forever start -l forever.log -a index.js

访问网址:https://IP:3033/iceconfig?key=none (IP为公网IP)

0 人点赞