Path: blob/trunk/javascript/selenium-webdriver/net/index.js
3220 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617'use strict'1819const os = require('node:os')2021function getLoInterface() {22let name23if (process.platform === 'darwin') {24name = 'lo0'25} else if (process.platform === 'linux') {26name = 'lo'27}28return name ? os.networkInterfaces()[name] : null29}3031/**32* Queries the system network interfaces for an IP address.33* @param {boolean} loopback Whether to find a loopback address.34* @param {string} family The IP family (IPv4 or IPv6). Defaults to IPv4.35* @return {(string|undefined)} The located IP address or undefined.36*/37function getIPAddress(loopback, family) {38let interfaces39if (loopback) {40const lo = getLoInterface()41interfaces = lo ? [lo] : null42}43interfaces = interfaces || os.networkInterfaces()44for (let key in interfaces) {45if (!Object.prototype.hasOwnProperty.call(interfaces, key)) {46continue47}4849for (let ipAddress of interfaces[key]) {50if ((ipAddress.family === family || `IPv${ipAddress.family}` === family) && ipAddress.internal === loopback) {51return ipAddress.address52}53}54}55return undefined56}5758// PUBLIC API5960/**61* Retrieves the external IP address for this host.62* @param {string=} family The IP family to retrieve. Defaults to "IPv4".63* @return {(string|undefined)} The IP address or undefined if not available.64*/65function getAddress(family = 'IPv4') {66return getIPAddress(false, family)67}6869/**70* Retrieves a loopback address for this machine.71* @param {string=} family The IP family to retrieve. Defaults to "IPv4".72* @return {(string|undefined)} The IP address or undefined if not available.73*/74function getLoopbackAddress(family = 'IPv4') {75return getIPAddress(true, family)76}7778/**79* Splits a hostport string, e.g. "www.example.com:80", into its component80* parts.81*82* @param {string} hostport The string to split.83* @return {{host: string, port: ?number}} A host and port. If no port is84* present in the argument `hostport`, port is null.85*/86function splitHostAndPort(hostport) {87let lastIndex = hostport.lastIndexOf(':')88if (lastIndex < 0) {89return { host: hostport, port: null }90}9192let firstIndex = hostport.indexOf(':')93if (firstIndex != lastIndex && !hostport.includes('[')) {94// Multiple colons but no brackets, so assume the string is an IPv6 address95// with no port (e.g. "1234:5678:9:0:1234:5678:9:0").96return { host: hostport, port: null }97}9899let host = hostport.slice(0, lastIndex)100if (host.startsWith('[') && host.endsWith(']')) {101host = host.slice(1, -1)102}103104let port = parseInt(hostport.slice(lastIndex + 1), 10)105return { host, port }106}107108// PUBLIC API109module.exports = {110splitHostAndPort,111getLoopbackAddress,112getAddress,113}114115116