UNPKG

3.47 kBJavaScriptView Raw
1// Licensed to the Software Freedom Conservancy (SFC) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The SFC licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18'use strict';
19
20var os = require('os');
21
22
23function getLoInterface() {
24 var name;
25 if (process.platform === 'darwin') {
26 name = 'lo0';
27 } else if (process.platform === 'linux') {
28 name = 'lo';
29 }
30 return name ? os.networkInterfaces()[name] : null;
31}
32
33
34/**
35 * Queries the system network interfaces for an IP address.
36 * @param {boolean} loopback Whether to find a loopback address.
37 * @param {string=} opt_family The IP family (IPv4 or IPv6). Defaults to IPv4.
38 * @return {string} The located IP address or undefined.
39 */
40function getAddress(loopback, opt_family) {
41 var family = opt_family || 'IPv4';
42 var addresses = [];
43
44 var interfaces;
45 if (loopback) {
46 var lo = getLoInterface();
47 interfaces = lo ? [lo] : null;
48 }
49 interfaces = interfaces || os.networkInterfaces();
50 for (var key in interfaces) {
51 if (!interfaces.hasOwnProperty(key)) {
52 continue;
53 }
54
55 interfaces[key].forEach(function(ipAddress) {
56 if (ipAddress.family === family &&
57 ipAddress.internal === loopback) {
58 addresses.push(ipAddress.address);
59 }
60 });
61 }
62 return addresses[0];
63}
64
65
66// PUBLIC API
67
68
69/**
70 * Retrieves the external IP address for this host.
71 * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
72 * @return {string} The IP address or undefined if not available.
73 */
74exports.getAddress = function(opt_family) {
75 return getAddress(false, opt_family);
76};
77
78
79/**
80 * Retrieves a loopback address for this machine.
81 * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
82 * @return {string} The IP address or undefined if not available.
83 */
84exports.getLoopbackAddress = function(opt_family) {
85 return getAddress(true, opt_family);
86};
87
88
89/**
90 * Splits a hostport string, e.g. "www.example.com:80", into its component
91 * parts.
92 *
93 * @param {string} hostport The string to split.
94 * @return {{host: string, port: ?number}} A host and port. If no port is
95 * present in the argument `hostport`, port is null.
96 */
97exports.splitHostAndPort = function(hostport) {
98 let lastIndex = hostport.lastIndexOf(':');
99 if (lastIndex < 0) {
100 return {host: hostport, port: null};
101 }
102
103 let firstIndex = hostport.indexOf(':');
104 if (firstIndex != lastIndex && !hostport.includes('[')) {
105 // Multiple colons but no brackets, so assume the string is an IPv6 address
106 // with no port (e.g. "1234:5678:9:0:1234:5678:9:0").
107 return {host: hostport, port: null};
108 }
109
110 let host = hostport.slice(0, lastIndex);
111 if (host.startsWith('[') && host.endsWith(']')) {
112 host = host.slice(1, -1);
113 }
114
115 let port = parseInt(hostport.slice(lastIndex + 1), 10);
116 return {host, port};
117};