1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.findLoopbackAddress = findLoopbackAddress;
|
4 | exports.getOSLoopbackAddress = getOSLoopbackAddress;
|
5 | exports.getOSLoopbackAddressIfAvailable = getOSLoopbackAddressIfAvailable;
|
6 | const tslib_1 = require("tslib");
|
7 | const os_1 = tslib_1.__importDefault(require("os"));
|
8 |
|
9 |
|
10 |
|
11 | function findLoopbackAddress() {
|
12 | let ipv6 = undefined;
|
13 | let ipv6LinkLocal = undefined;
|
14 | let ipv4 = undefined;
|
15 | for (const [name, infos] of Object.entries(os_1.default.networkInterfaces())) {
|
16 | let internal = false;
|
17 | if (infos) {
|
18 | for (const info of infos) {
|
19 | if (!info.internal) {
|
20 | continue;
|
21 | }
|
22 | internal = true;
|
23 |
|
24 | if (info.family === "IPv4" || info.family === 4) {
|
25 | if (!ipv4) {
|
26 | ipv4 = info.address;
|
27 | }
|
28 |
|
29 | }
|
30 | else if (info.family === "IPv6" || info.family === 6) {
|
31 | if (info.scopeid) {
|
32 | if (!ipv6LinkLocal) {
|
33 | ipv6LinkLocal = info.address + "%" + name;
|
34 | }
|
35 | }
|
36 | else if (!ipv6) {
|
37 | ipv6 = info.address;
|
38 | }
|
39 | }
|
40 | }
|
41 | }
|
42 | if (internal) {
|
43 | break;
|
44 | }
|
45 | }
|
46 | const address = ipv4 || ipv6 || ipv6LinkLocal;
|
47 | if (!address) {
|
48 | throw new Error("Could not find a valid loopback address on the platform!");
|
49 | }
|
50 | return address;
|
51 | }
|
52 | let loopbackAddress = undefined;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | function getOSLoopbackAddress() {
|
62 | return loopbackAddress ?? (loopbackAddress = findLoopbackAddress());
|
63 | }
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function getOSLoopbackAddressIfAvailable() {
|
71 | try {
|
72 | return loopbackAddress ?? (loopbackAddress = findLoopbackAddress());
|
73 | }
|
74 | catch (error) {
|
75 | console.log(error.stack);
|
76 | return undefined;
|
77 | }
|
78 | }
|
79 |
|
\ | No newline at end of file |