forked from paulhiggs/dvb-i-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsr.js
271 lines (242 loc) · 9.79 KB
/
csr.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import chalk from "chalk";
// express framework - https://expressjs.com/en/4x/api.html
import express from "express";
// morgan - https://www.npmjs.com/package/morgan
import morgan, { token } from "morgan";
// favourite icon - https://www.npmjs.com/package/serve-favicon
import favicon from "serve-favicon";
import { join } from "path";
const keyFilename = join(".", "selfsigned.key"),
certFilename = join(".", "selfsigned.crt");
import { createServer } from "https";
// command line arguments - https://www.npmjs.com/package/command-line-args
import commandLineArgs from "command-line-args";
import commandLineUsage from "command-line-usage";
import cluster from "cluster";
import { cpus } from "os";
import cors from "cors";
import process from "process";
import { Default_SLEPR, IANA_Subtag_Registry, ISO3166, TVA_ContentCS, TVA_FormatCS, DVBI_ContentSubject } from "./data-locations.js";
import { CORSlibrary, CORSmanual, CORSnone, CORSoptions, HTTPPort } from "./globals.js";
import { readmyfile } from "./utils.js";
const numCPUs = cpus().length;
// SLEPR == Service List Entry Point Registry
import SLEPR from "./slepr.js";
// command line options
const optionDefinitions = [
{ name: "urls", alias: "u", type: Boolean, defaultValue: false, description: "Load data files from network locations." },
{ name: "port", alias: "p", type: Number, defaultValue: HTTPPort.csr, typeLabel: "{underline ip-port}", description: `The HTTP port to listen on. Default: ${HTTPPort.csr}` },
{
name: "sport",
alias: "s",
type: Number,
defaultValue: HTTPPort.csr + 1,
typeLabel: "{underline ip-port}",
description: `The HTTPS port to listen on. Default: ${HTTPPort.csr + 1}`,
},
{ name: "file", alias: "f", type: String, defaultValue: Default_SLEPR.file, typeLabel: "{underline filename}", description: "local file name of master SLEPR file" },
{
name: "CORSmode",
alias: "c",
type: String,
defaultValue: "library",
typeLabel: "{underline mode}",
description: `type of CORS habdling "${CORSlibrary}" (default), "${CORSmanual}" or "${CORSnone}"`,
},
{ name: "help", alias: "h", type: Boolean, defaultValue: false, description: "This help" },
];
const commandLineHelp = [
{
header: "DVB Central Service Registry",
content: "An implementaion of a DVB-I Service List Registry",
},
{
header: "Synopsis",
content: "$ node csr <options>",
},
{
header: "Options",
optionList: optionDefinitions,
},
{
header: "Client Query",
content: "{underline <host>}:{underline <port>}/query[?{underline arg}={underline value}(&{underline arg}={underline value})*]",
},
{
content: [
{ header: "{underline arg}" },
{ name: "regulatorListFlag", summary: "Select only service lists that have the @regulatorListFlag set as specified (true|false)" },
{ name: "Delivery[]", summary: "Select only service lists that use the specified delivery system (dvb-t|dvb-dash|dvb-c|dvb-s|dvb-iptv)" },
{ name: "TargetCountry[]", summary: "Select only service lists that apply to the specified countries (form: {underline ISO3166 3-digit code})" },
{ name: "Language[]", summary: "Select only service lists that use the specified language (form: {underline IANA 2 digit language code})" },
{ name: "Genre[]", summary: "Select only service lists that match one of the given Genres" },
{ name: "Provider[]", summary: "Select only service lists that match one of the specified Provider names" },
],
},
{ content: "note that all query values except Provider are checked against constraints. An HTTP 400 response is returned with errors in the response body." },
{
header: "About",
content: "Project home: {underline https://github.com/paulhiggs/dvb-i-tools/}",
},
];
try {
var options = commandLineArgs(optionDefinitions);
} catch (err) {
console.log(commandLineUsage(commandLineHelp));
process.exit(1);
}
if (!CORSoptions.includes(options.CORSmode)) {
console.log(chalk.red(`CORSmode must be "${CORSnone}", "${CORSlibrary}" to use the Express cors() handler, or "${CORSmanual}" to have headers inserted manually`));
process.exit(1);
}
if (options.help) {
console.log(commandLineUsage(commandLineHelp));
process.exit(0);
}
if (options.urls && options.file == Default_SLEPR.file) options.file = Default_SLEPR.url;
import IANAlanguages from "./IANAlanguages.js";
var knownLanguages = new IANAlanguages();
knownLanguages.loadLanguages(options.urls ? { url: IANA_Subtag_Registry.url } : { file: IANA_Subtag_Registry.file });
import ISOcountries from "./ISOcountries.js";
var knownCountries = new ISOcountries(false, true);
knownCountries.loadCountries(options.urls ? { url: ISO3166.url } : { file: ISO3166.file });
import ClassificationScheme from "./ClassificationScheme.js";
let knownGenres = new ClassificationScheme();
knownGenres.loadCS(
options.urls ? { urls: [TVA_ContentCS.url, TVA_FormatCS.url, DVBI_ContentSubject.url] } : { files: [TVA_ContentCS.file, TVA_FormatCS.file, DVBI_ContentSubject.file] }
);
const RELOAD = "RELOAD",
UPDATE = "UPDATE",
INCR_REQUESTS = "REQUESTS++",
INCR_FAILURES = "FAILURES++",
STATS = "STATS";
if (cluster.isPrimary) {
console.log(chalk.orange(`Number of CPUs is ${numCPUs}`));
console.log(chalk.orange(`Primary ${process.pid} is running`));
var metrics = {
numRequests: 0,
numFailed: 0,
reloadRequests: 0,
};
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on("exit", (worker, /* eslint-disable no-unused-vars*/ code, signal /* eslint-enable */) => {
console.log(chalk.red(`worker ${worker.process.pid} died`));
console.log(chalk.red("Let's fork another worker!"));
cluster.fork();
});
cluster.on("message", (/* eslint-disable no-unused-vars*/ worker /* eslint-enable */, msg, /* eslint-disable no-unused-vars*/ handle /* eslint-enable */) => {
if (msg.topic)
switch (msg.topic) {
case RELOAD:
metrics.reloadRequests++;
for (const id in cluster.workers) {
// Here we notify each worker of the updated value
cluster.workers[id].send({ topic: UPDATE });
}
break;
case INCR_REQUESTS:
metrics.numRequests++;
break;
case INCR_FAILURES:
metrics.numFailed++;
break;
case STATS:
console.log(`knownLanguages.length=${knownLanguages.languagesList.length}`);
console.log(`knownCountries.length=${knownCountries.count()}`);
console.log(`requests=${metrics.numRequests} failed=${metrics.numFailed} reloads=${metrics.reloadRequests}`);
console.log(`SLEPR file=${options.file}`);
break;
}
});
} else {
var app = express();
app.use(cors());
token("pid", function getPID(/* eslint-disable no-unused-vars */ req /* eslint-enable */) {
return process.pid;
});
token("protocol", function getProtocol(req) {
return req.protocol;
});
token("parseErr", function getParseErr(req) {
if (req.parseErr?.length > 0) return `(query errors=${req.parseErr.length})`;
return "";
});
token("agent", function getAgent(req) {
return `(${req.headers["user-agent"]})`;
});
const SLEPR_query_route = "/query",
SLEPR_reload_route = "/reload",
SLEPR_stats_route = "/stats";
let manualCORS = function (res, req, next) {
next();
};
if (options.CORSmode == CORSlibrary) {
app.options("*", cors());
} else if (options.CORSmode == CORSmanual) {
manualCORS = function (req, res, next) {
let opts = res.getHeader("X-Frame-Options");
if (opts) {
if (!opts.includes("SAMEORIGIN")) opts.push("SAMEORIGIN");
} else opts = ["SAMEORIGIN"];
res.setHeader("X-Frame-Options", opts);
res.setHeader("Access-Control-Allow-Origin", "*");
next();
};
}
var csr = new SLEPR(options.urls);
csr.loadServiceListRegistry(options.file, knownLanguages, knownCountries, knownGenres);
app.use(morgan(":pid :remote-addr :protocol :method :url :status :res[content-length] - :response-time ms :agent :parseErr"));
app.use(favicon(join("phlib", "ph-icon.ico")));
if (options.CORSmode == CORSlibrary) app.options(SLEPR_query_route, cors());
else if (options.CORSmode == CORSmanual) app.options(SLEPR_query_route, manualCORS);
app.get(SLEPR_query_route, function (req, res) {
process.send({ topic: INCR_REQUESTS });
if (!csr.processServiceListRequest(req, res)) process.send({ topic: INCR_FAILURES });
res.end();
});
app.get(SLEPR_reload_route, function (req, res) {
process.send({ topic: RELOAD });
res.status(404).end();
});
app.get(SLEPR_stats_route, function (req, res) {
process.send({ topic: STATS });
res.status(404).end();
});
app.get("*", function (req, res) {
res.status(404).end();
});
process.on("message", (msg) => {
if (msg.topic)
switch (msg.topic) {
case UPDATE:
knownCountries.loadCountries(options.urls ? { url: ISO3166.url } : { file: ISO3166.filee });
knownLanguages.loadLanguages(options.urls ? { url: IANA_Subtag_Registry.url } : { file: IANA_Subtag_Registry.file });
knownGenres.loadCS(
options.urls ? { urls: [TVA_ContentCS.url, TVA_FormatCS.url, DVBI_ContentSubject.url] } : { files: [TVA_ContentCS.file, TVA_FormatCS.file, DVBI_ContentSubject.file] }
);
csr.loadDataFiles(options.urls, knownLanguages, knownCountries, knownGenres);
csr.loadServiceListRegistry(options.file);
break;
}
});
// start the HTTP server
var http_server = app.listen(options.port, function () {
console.log(chalk.cyan(`HTTP listening on port number ${http_server.address().port}, PID=${process.pid}`));
});
// start the HTTPS server
// sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout ./selfsigned.key -out selfsigned.crt
var https_options = {
key: readmyfile(keyFilename),
cert: readmyfile(certFilename),
};
if (https_options.key && https_options.cert) {
if (options.sport == options.port) options.sport = options.port + 1;
var https_server = createServer(https_options, app);
https_server.listen(options.sport, function () {
console.log(chalk.cyan(`HTTPS listening on port number ${https_server.address().port}, PID=${process.pid}`));
});
}
}