-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.js
More file actions
47 lines (41 loc) · 1.48 KB
/
Copy pathapp.js
File metadata and controls
47 lines (41 loc) · 1.48 KB
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
const express = require('express');
const proxy = require('http-proxy-middleware');
const cors = require('cors');
const processUrl = (originalUrl) => {
const urlRegExp = new RegExp(`^/([a-zA-Z0-9:.-]+.(?:opentok.com|tokbox.com|t|o)[:0-9]*)/(.*)$`);
const matches = originalUrl.match(urlRegExp);
if (!matches || matches.length < 3) {
return 'https://tokbox.com';
}
let domain = matches[1];
const splitter = matches[1].split(":");
if(domain.startsWith("eh.t")) domain = "enterprise.hlg.tokbox.com";
if(domain.startsWith("h.t")) domain = "hlg.tokbox.com";
if(domain.startsWith("as.o")) domain = "api-standard.opentok.com";
if(domain.startsWith("c.o")) domain = "config.opentok.com";
if(splitter.length > 1) {
domain = domain + ":" + splitter[1];
}
const returnUrl = `https://${domain}/${matches[2]}`;
console.log(`${originalUrl} -> ${returnUrl}`)
return returnUrl;
};
const proxyRouter = (req) => {
if (!req.originalUrl && !req.url) {
return '';
}
return processUrl(req.originalUrl || req.url);
};
const otProxy = proxy({
target: 'https://tokbox.com', // Not really used since we are changing the target with router function
router: proxyRouter,
changeOrigin: true,
ignorePath: true,
ws: true,
});
const app = express()
const PORT = process.env.PORT || 3000;
app.use('/', cors(), otProxy);
app.use('/proxy', cors(), otProxy);
const server = app.listen(PORT, () => console.log(`Reverse proxy started.`))
process.once('SIGINT', () => { server.close(); });