1 | "use strict";
|
2 |
|
3 | let ports, services;
|
4 |
|
5 |
|
6 | module.exports.getService = (port, protocol = "tcp") => {
|
7 | if (typeof port !== "number") throw new Error("expected a 'number'");
|
8 | if (!ports) ports = require("./ports.json");
|
9 | return ports[`${port}/${protocol}`] || null;
|
10 | };
|
11 |
|
12 |
|
13 | module.exports.getPort = (service, protocol = "tcp") => {
|
14 | if (typeof service !== "string") throw new Error("expected a 'string'");
|
15 | if (!services) services = require("./services.json");
|
16 |
|
17 |
|
18 | const entry = services[service.toLowerCase()];
|
19 | if (!entry) return null;
|
20 |
|
21 |
|
22 | const port = entry.ports.filter(port => /\w+$/.exec(port)[0] === protocol)[0];
|
23 | if (!port) return null;
|
24 |
|
25 |
|
26 | return {
|
27 | port: Number(/^\d+/.exec(port)[0]),
|
28 | protocol: /\w+$/.exec(port)[0],
|
29 | description: entry.description
|
30 | };
|
31 | };
|