Self-built APPRTC

Source: Internet
Author: User

Self-built APPRTCwords 3158 Read 1718 comments 2 likes 2

APPRTC is a demo of WEBRTC.
Self-built APPRTC can be bitter their mind to work their bones hungry body skin, more importantly, can learn to WEBRTC server building process ...

Part of the APPRTC is this:
1, APPRTC-room server. Github
2. Collider-Signaling server. The above Github project comes in, under Src/collider.
3, Coturn-hole (intranet penetration) server. Google Code
4, also need to implement a Coturn connection information (mainly the user name, password configuration) to obtain the interface, often called the TURN REST API.

0. Preparatory work
0, in order to avoid any other operating system urine problems, the operating system is recommended to use Ubuntu 14.04.
1, APPRTC relies on grunt and Google appengine SDK for Python offline version.
The GAE SDK installation is simple: Download, unzip, add to the PATH environment variable to complete.
Grunt, is a set of task execution system under node. js, through Gruntfile.js configuration, can do a lot of things. First install node. js. Using NVM makes it easy to install and set up node. js for your Linux account. (Then, you can choose to install CNPM, so you can use the domestic cache node, NPM Install command will be much faster, if you only use this time grunt, then do not install this is also possible.) Next, you'll need to perform a NPM install-g grunt-cli to install the grunt.
2, Collider relies on Golang. The problem with the urine comes: the wall ... Almost all of the packages required to install Golang and future use of Golang are FQ. So after this problem is solved, we can finish our preparations by using GVM to install Golang (current version go1.4.2).

I. Coturn, hole server, installation requires sudo
0, this goods is a basic dependence, and do not need to change the code, as long as the installation, and then change the configuration file, and then start the service, open the port on the line.
1, download, go here, download the latest version, turnserver-*-debian-wheezy-ubuntu-mint-x86-64bits.tar.gz
2, create a directory, and then, tar xzf < Download the file name >-c < directory name >.
3. Enter the directory, cat install, view the installation instructions, install the required software according to the instructions, and then install the Deb package in the directory.
4, after the installation is complete, modify the configuration file/etc/turnserver.conf into the following content:

# Not much to say
Listening-device=eth0
listening-port=3478
Relay-device=eth0
# Remember to open the firewall
min-port=59000
max-port=65000
# more complex output logs
Verbose
# WebRTC will use it in the news.
Fingerprint
# WebRTC Certification Required
Lt-cred-mech
# REST API certification requires
Use-auth-secret
# REST API Encryption required KEY
# Here we use the "static" Key,google.
static-auth-secret=4080218913
# User logon domain
realm=< fill in your own domain name >
# provides more secure access to the TURN service
Stale-nonce
# SSL needs to be used to generate the command:
# sudo OpenSSL req-x509-newkey rsa:2048-keyout/etc/turn_server_pkey.pem-out/etc/turn_server_cert.pem-days 99999-no Des
Cert=/etc/turn_server_cert.pem
Pkey=/etc/turn_server_pkey.pem
# shielded loopback, multicast IP address relay
No-loopback-peers
No-multicast-peers
# Enable Mobility ICE support (I don't know what to do)
Mobility
# Disable local telnet CLI management interface
No-cli

After the configuration file is saved successfully, use the command service Coturn start to start the Coturn service.
If the server is in the network, you also need to do port mapping: 3478,3479:TCP&UDP, plus the 59000~65000:TCP&UDP in the configuration file.

Two. Coturn connection Information Interface (TURN REST API)
0, because Coturn did not bring the interface, so we need to implement the code of the farmers themselves. For details on how this interface is implemented, please refer to the Coturn original document. I only do a brief description here:

* The TURN server itself does not need to implement the interface, so the service provider needs to implement and deploy itself.
* This TURN REST API has standard reference documentation, including request parameters and return content, as defined. The Code farmer needs to implement the interface as described in the document.
* The contents mentioned in the document are:
*① request information, way GET:
* Need to include: Service ("turn"), username, key
* For example:/turn?username=392938130&key=4080218913
*② response information, content format JSON Dictionary:
* Required inclusions: username, password, TTL (optional, in seconds. Recommended 86400), URIs (array)
For example

{"username": "1433065916:392938130", "Password": "***base64 encoded string***", "ttl": 86400, "URIs": ["Turn : 104.155.31.139:3478?transport=udp "," turn:104.155.31.139:3478?transport=tcp "," turn:104.155.31.139:3479? " Transport=udp "," Turn:104.155.31.139:3479?transport=tcp "]}

This is the basic form. What needs to be mentioned are:
1. The username field of the response needs to be output in the form of "timestamp +": "+ username". Note: Here timestamp is the UNIX timestamp of the password failure , which is "current timestamp + TTL time", while username is the username submitted at the request, unchanged; Use half-width colon for string connection (default setting is This way, you can change it yourself, but it hurts a lot ... )。 For the sake of later narration, the username field in the response content here is called Turn-username.
2. The password field of the response needs to be calculated with the HMAC-SHA1 algorithm, the formula is: "Base64_encode (HMAC (Key, Turn-username)"). The key here is the "Static-auth-secret" value in the Coturn server configuration. With key as the keys of the HMAC algorithm, Turn-username is the computed content, and after the HMAC Digest is obtained, the final password is obtained by Base64 encoding.
3, that's all. URIs generally based on the hypothetical size of the Coturn server from the background configuration, and then directly output to the foreground. I use node. js to write a simple interface code, there is a need to refer to:

var express = require (' Express ');
var crypto = require (' crypto ');
var app = Express ();

var HMAC = function (key, content) {
var method = Crypto.createhmac (' SHA1 ', key);
Method.setencoding (' base64 ');
Method.write (content);
Method.end ();
return Method.read ();
};

App.get ('/turn ', function (req, resp) {

var query = Req.query;
var key = ' 4080218913 '; The key here is set in advance, we see him as a constant light, so we do not read from the HTTP request parameters

if (!query[' username ']) {
Return Resp.send ({' Error ': ' Apperror ', ' message ': ' must provide username. '});
} else {
var time_to_live = 600;
var timestamp = Math.floor (Date.now ()/+) + time_to_live;
var turn_username = timestamp + ': ' + query[' username '];
var password = HMAC (key, Turn_username);

Return Resp.send ({
Username:turn_username,
Password:password,
Ttl:time_to_live,
"URIs": [
"Turn:turn.server.ip.address:3478?transport=udp",
"Turn:turn.server.ip.address:3478?transport=tcp",
"Turn:turn.server.ip.address:3479?transport=udp",
"Turn:turn.server.ip.address:3479?transport=tcp"
]
});
}

});

App.listen (' 3033 ', function () {
Console.log (' server started ');
});

The code does not deal with cross-domain issues, so although the above code listens to port 3033, but I made an anti-generation with nginx, in order to be able to use the same domain name. There is no cross-domain problem.

Three. Collider Signaling Server
0, now can put FQ pose, if do not know how to explain you are not ready ...
1, first, may be because of the wall or I fq posture is not right, I get Collider in the dependency of the library when I encountered a bit of trouble, but changed the source code is good:

# $PWD = apprtc/src/collider/collider/
Go to this directory above
# $FILES = [' Collider.go ', ' collider_test.go ']
Take this line from the two files above:
"Code.google.com/p/go.net/websocket"
Replace with:
"Golang.org/x/net/websocket"
Remember to save

2, then you can refer to the steps inside the SRC/COLLIDER/README.MD executed:

......

# $PWD = apprtc/src/collider/
3. Connect the following 3 directories with the soft link to the $GOPATH/src/
Note that you should first check if your Golang is installed and check that the $GOPATH is not empty and that the path $GOPATH/src/already exists.
Hypothesis: Now it's under apprtc/src/collider/
Command: Ln-s $PWD/{collider,collidermain,collidertest} $GOPATH/src/
Result: The 3 directories will be soft-linked to the $GOPATH/src/

4. After the soft link is created, execute:
Command: Go get collidermain && go install collidermain

3, after successful execution of the above command, the path $GOPATH/bin/collidermain will be an executable file.
You can append the--help parameter to see all available command-line arguments. Here we use such a configuration:

# $GOPATH/bin/collidermain-port=8089-room-server= ' http://apprtc.domain.com:8083 '-tls=false

# We haven't been exposed to APPRTC's construction yet, so for the time being we don't know what the value of the Room-server parameter is.
# This command should be done after a good apprtc, and then come back.

-port = The port that represents the collider listener. We use 8089 here. If there is a firewall, remember to open, if in the intranet, remember to do mapping on the router.
-room-server = Represents a URL that APPRTC can access. If you are using non-standard ports (80, 443), be sure to write the port number.
-tls = False indicates that TLS encryption is turned off. Because this need to configure the certificate ("/cert/cert.pem", "/cert/key.pem"), so the purpose of their own research, closed is the most convenient ...

4, here 8089 is the service port, if you in the intranet and need to provide services, need to map 8089:tcp out of the network.

Four. APPRTC Room Server
0, this is the last step, only need to change a few configuration can be opened to run. This assumes that your path is the root directory of the APPRTC project.
1, because our Collider used the TLS = False parameter, and for the convenience of access, the following modifications are required:

# $FILE = apprtc/src/app_engine/apprtc.py

Search for "WSS:" and "https:" (note colon)
Can be found in the method get_wss_parameters, here you need to replace the WSS: to WS:, the https: replace the http:, Save the exit, you can.

2, edit the configuration file, the file is located in apprtc/src/app_engine/constants.py.

① Search Turn_base_url
Replace the string after the equal sign with the address that APPRTC can access, such as: ' http://apprtc.domain.com:8083 '

② Search Wss_instances
As you can see, this is configured for an array, but we only have a single server. So delete the other elements of the array first, leaving only one.
In the preserved element, we only modify the value of the wss_instance_host_key corresponding.
Change it to the accessible address of the Collider server above. For example: apprtc.domain.com:8089. No protocol required, no URI.

After changing the above two places, you can save the exit

3. If you use a nonstandard port, or use port mappings on the router to provide testing and services for the WAN, you will also need to modify some code on the Web front end. Otherwise, you will encounter a cross-domain access problem with your browser (mainly pushstate will produce an error), so change:

# $FILE = apprtc/src/web_app/js/appcontroller.js

There are two of these functions in this file:
AppController.prototype.pushCallNavigation_
AppController.prototype.displaySharingInfo_

At the beginning of each of these two lines of functions, add the following code:
Roomlink = Roomlink.replace (/apprtc\.domain\.com\//, ' apprtc.domain.com:8083/');

Save exit

4. Now reconfirm that the current directory is the root of the APPRTC project, and then execute:

$ grunt build; dev_appserver.py--host=0.0.0.0./out/app_engine

# because it is for the public network, or LAN other people to provide services, so here host=0.0.0.0, in order to avoid only monitoring the status of 127.0.0.1.

5. It is important to note that the Dev_appserver listens to 8080 ports by default. But the configuration is full of 8083, because the last step is poor ...

Five. Nginx, used to unify the HTTP external service port, and the most important anti-generational 3033 TURN REST API service.
0. Install nginx:apt-get install nginx-y first.
1. The default configuration file is/etc/nginx/sites-enabled/default. When opened, add:

server {
Listen 8083;
server_name apprtc.domain.com;
Location/{
Proxy_pass http://ip.address.of.apprtc:8080;
Proxy_set_header Host $host;
}
Location/turn {
Proxy_pass http://ip.address.of.turn-rest-api:3033;
Proxy_set_header Host $host;
}
}

# exit after saving

2. Use the service Nginx reload to reload the configuration file.
3, if necessary, you can do port mapping on the router: 8083:tcp.
4. At this point, the URL of the APPRTC service that can be accessed externally has been determined, and you can check and execute the commands in "three (3)". Note If you run this command earlier, you do not need to run it again.

Six. Open the browser to access http://apprtc.domain.com:8083/. Enter the room number, and the room URL to your good friends, you can face the base of ...

--------------------------------------------------------------

Since I built the entire environment in the intranet virtual machine operation, so the article will appear everywhere "set port map on the router" such sentences.
Here, let's do it in a unified way:

8083:TCP-APPRTC Port, which provides HTTP services. The default is to use 8080:TCP, I changed it.
8089:tcp-collider service port, providing WebSocket service.
3478, 3479:tcp&udp-coturn the listening port.
The 59000~65000:tcp&udp-coturn service port.

The port of the TURN REST API (3033) is not required to do port mapping alone due to the Nginx reverse proxy.

That's it, EOF.

Self-built APPRTC

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.