index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. 'use strict';
  18. var os = require('os');
  19. function getLoInterface() {
  20. var name;
  21. if (process.platform === 'darwin') {
  22. name = 'lo0';
  23. } else if (process.platform === 'linux') {
  24. name = 'lo';
  25. }
  26. return name ? os.networkInterfaces()[name] : null;
  27. }
  28. /**
  29. * Queries the system network interfaces for an IP address.
  30. * @param {boolean} loopback Whether to find a loopback address.
  31. * @param {string=} opt_family The IP family (IPv4 or IPv6). Defaults to IPv4.
  32. * @return {string} The located IP address or undefined.
  33. */
  34. function getAddress(loopback, opt_family) {
  35. var family = opt_family || 'IPv4';
  36. var addresses = [];
  37. var interfaces;
  38. if (loopback) {
  39. var lo = getLoInterface();
  40. interfaces = lo ? [lo] : null;
  41. }
  42. interfaces = interfaces || os.networkInterfaces();
  43. for (var key in interfaces) {
  44. if (!interfaces.hasOwnProperty(key)) {
  45. continue;
  46. }
  47. interfaces[key].forEach(function(ipAddress) {
  48. if (ipAddress.family === family &&
  49. ipAddress.internal === loopback) {
  50. addresses.push(ipAddress.address);
  51. }
  52. });
  53. }
  54. return addresses[0];
  55. }
  56. // PUBLIC API
  57. /**
  58. * Retrieves the external IP address for this host.
  59. * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
  60. * @return {string} The IP address or undefined if not available.
  61. */
  62. exports.getAddress = function(opt_family) {
  63. return getAddress(false, opt_family);
  64. };
  65. /**
  66. * Retrieves a loopback address for this machine.
  67. * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4".
  68. * @return {string} The IP address or undefined if not available.
  69. */
  70. exports.getLoopbackAddress = function(opt_family) {
  71. return getAddress(true, opt_family);
  72. };
  73. /**
  74. * Splits a hostport string, e.g. "www.example.com:80", into its component
  75. * parts.
  76. *
  77. * @param {string} hostport The string to split.
  78. * @return {{host: string, port: ?number}} A host and port. If no port is
  79. * present in the argument `hostport`, port is null.
  80. */
  81. exports.splitHostAndPort = function(hostport) {
  82. let lastIndex = hostport.lastIndexOf(':');
  83. if (lastIndex < 0) {
  84. return {host: hostport, port: null};
  85. }
  86. let firstIndex = hostport.indexOf(':');
  87. if (firstIndex != lastIndex && !hostport.includes('[')) {
  88. // Multiple colons but no brackets, so assume the string is an IPv6 address
  89. // with no port (e.g. "1234:5678:9:0:1234:5678:9:0").
  90. return {host: hostport, port: null};
  91. }
  92. let host = hostport.slice(0, lastIndex);
  93. if (host.startsWith('[') && host.endsWith(']')) {
  94. host = host.slice(1, -1);
  95. }
  96. let port = parseInt(hostport.slice(lastIndex + 1), 10);
  97. return {host, port};
  98. };