-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
124 lines (95 loc) · 3.2 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
This is a straight-through proxy, to the V2 legacy api on the V3 system. This
example is driven via the V2 client SDK - as such it elaborates none of the new V3 features.
*/
var request = require("request")
var express = require("express")
var https = require('https')
var http = require('http')
var url = require('url')
var fs = require('fs')
var bodyParser = require('body-parser');
function get_config() {
var config_file = process.argv[2]
if (!config_file) {
console.log("Please provide config file name as argument")
process.exit(1)
}
if (!fs.existsSync(config_file)) {
console.log(config_file + " does not exist")
process.exit(1)
}
return JSON.parse(fs.readFileSync(config_file))
}
var conf = get_config()
function is_cell() {
return conf.gateway == "NEURON"
}
function neuron() {
return process.env.NEURON_PORT_4006_TCP_ADDR
}
function get_gateway() {
return (is_cell()) ? neuron() + ":4003" : conf.gateway
}
function get_mount_point(app) {
var mountPoint;
if (is_cell()) {
// a cell requires to be on a sub-url as nginx knows about "/xsdk", mounting is a good way to achieve this
mountPoint = express()
app.use("/xsdk", mountPoint)
console.log("mounting on /xsdk")
} else {
mountPoint = app
}
mountPoint.use(bodyParser.json())
mountPoint.use(bodyParser.urlencoded({ extended: true }))
mountPoint.use(express.static('./public'));
return mountPoint
}
var app = express();
var mountPoint = get_mount_point(app),
gw = conf.protocol + "://" + get_gateway()
if (conf.protocol == "https") {
var opts = {
key: fs.readFileSync(conf.key_file),
cert: fs.readFileSync(conf.cert_file)
};
https.createServer(opts, app).listen(conf.port)
} else {
http.createServer(app).listen(conf.port)
}
//Returns Secure token to connect to the service.
mountPoint.post('/signal/token', function(req, res) {
body = req.body
body["ident"] = conf.ident
body["secret"] = conf.secret
var url = gw + "/signal/token"
request.post({url:gw + "/signal/token",json: true, form: body }).pipe(res)
})
//Returns List of valid signaling servers that the clients can connect to.
mountPoint.get('/signal/list', function(req, res) {
request.get({ url: gw + "/signal/list", json: true }).pipe(res)
})
//Returns a Valid ICE server setup to handle the WebRTC handshake and TURN connection if needed.
mountPoint.post('/ice', function(req, res) {
body = req.body
body["ident"] = conf.ident
body["secret"] = conf.secret
request.post({ url: gw + "/ice", json: true, form: body }).pipe(res)
});
// an interesting tweak on serving different xirsys_connects for various purposes ...
mountPoint.get('/xirsys_connect.js', function(req, res) {
res.writeHead(200, { 'Content-Type': 'application/javascript' })
var xirsysConnect = {
secureTokenRetrieval: true,
server: '',
data: {
domain: 'www.xirsys.com',
application: 'default',
room: 'default',
secure: (conf.protocol == "https") ? 1 : 0
}
}
var xc = "var xirsysConnect=" + JSON.stringify(xirsysConnect) + ";\n"
res.end(xc)
});