diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-11-03 01:33:53 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-11-03 01:33:53 +0100 |
commit | d1291f67551c58168af43698a359cb5ddfd266b0 (patch) | |
tree | 55a13ed29fe1915e3f42f1b1b7038dafa2e975a7 /node_modules/selenium-webdriver/net | |
parent | d0a0695fb5d34996850723f7d4b1b59c3df909c2 (diff) |
node_modules
Diffstat (limited to 'node_modules/selenium-webdriver/net')
-rw-r--r-- | node_modules/selenium-webdriver/net/index.js | 117 | ||||
-rw-r--r-- | node_modules/selenium-webdriver/net/portprober.js | 205 |
2 files changed, 322 insertions, 0 deletions
diff --git a/node_modules/selenium-webdriver/net/index.js b/node_modules/selenium-webdriver/net/index.js new file mode 100644 index 000000000..6c193db5c --- /dev/null +++ b/node_modules/selenium-webdriver/net/index.js @@ -0,0 +1,117 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +'use strict'; + +var os = require('os'); + + +function getLoInterface() { + var name; + if (process.platform === 'darwin') { + name = 'lo0'; + } else if (process.platform === 'linux') { + name = 'lo'; + } + return name ? os.networkInterfaces()[name] : null; +} + + +/** + * Queries the system network interfaces for an IP address. + * @param {boolean} loopback Whether to find a loopback address. + * @param {string=} opt_family The IP family (IPv4 or IPv6). Defaults to IPv4. + * @return {string} The located IP address or undefined. + */ +function getAddress(loopback, opt_family) { + var family = opt_family || 'IPv4'; + var addresses = []; + + var interfaces; + if (loopback) { + var lo = getLoInterface(); + interfaces = lo ? [lo] : null; + } + interfaces = interfaces || os.networkInterfaces(); + for (var key in interfaces) { + if (!interfaces.hasOwnProperty(key)) { + continue; + } + + interfaces[key].forEach(function(ipAddress) { + if (ipAddress.family === family && + ipAddress.internal === loopback) { + addresses.push(ipAddress.address); + } + }); + } + return addresses[0]; +} + + +// PUBLIC API + + +/** + * Retrieves the external IP address for this host. + * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4". + * @return {string} The IP address or undefined if not available. + */ +exports.getAddress = function(opt_family) { + return getAddress(false, opt_family); +}; + + +/** + * Retrieves a loopback address for this machine. + * @param {string=} opt_family The IP family to retrieve. Defaults to "IPv4". + * @return {string} The IP address or undefined if not available. + */ +exports.getLoopbackAddress = function(opt_family) { + return getAddress(true, opt_family); +}; + + +/** + * Splits a hostport string, e.g. "www.example.com:80", into its component + * parts. + * + * @param {string} hostport The string to split. + * @return {{host: string, port: ?number}} A host and port. If no port is + * present in the argument `hostport`, port is null. + */ +exports.splitHostAndPort = function(hostport) { + let lastIndex = hostport.lastIndexOf(':'); + if (lastIndex < 0) { + return {host: hostport, port: null}; + } + + let firstIndex = hostport.indexOf(':'); + if (firstIndex != lastIndex && !hostport.includes('[')) { + // Multiple colons but no brackets, so assume the string is an IPv6 address + // with no port (e.g. "1234:5678:9:0:1234:5678:9:0"). + return {host: hostport, port: null}; + } + + let host = hostport.slice(0, lastIndex); + if (host.startsWith('[') && host.endsWith(']')) { + host = host.slice(1, -1); + } + + let port = parseInt(hostport.slice(lastIndex + 1), 10); + return {host, port}; +}; diff --git a/node_modules/selenium-webdriver/net/portprober.js b/node_modules/selenium-webdriver/net/portprober.js new file mode 100644 index 000000000..3ea33b2b3 --- /dev/null +++ b/node_modules/selenium-webdriver/net/portprober.js @@ -0,0 +1,205 @@ +// Licensed to the Software Freedom Conservancy (SFC) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The SFC licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +'use strict'; + +var exec = require('child_process').exec, + fs = require('fs'), + net = require('net'); + + +/** + * The IANA suggested ephemeral port range. + * @type {{min: number, max: number}} + * @const + * @see http://en.wikipedia.org/wiki/Ephemeral_ports + */ +const DEFAULT_IANA_RANGE = {min: 49152, max: 65535}; + + +/** + * The epheremal port range for the current system. Lazily computed on first + * access. + * @type {Promise.<{min: number, max: number}>} + */ +var systemRange = null; + + +/** + * Computes the ephemeral port range for the current system. This is based on + * http://stackoverflow.com/a/924337. + * @return {!Promise<{min: number, max: number}>} A promise that will resolve to + * the ephemeral port range of the current system. + */ +function findSystemPortRange() { + if (systemRange) { + return systemRange; + } + var range = process.platform === 'win32' ? + findWindowsPortRange() : findUnixPortRange(); + return systemRange = range.catch(function() { + return DEFAULT_IANA_RANGE; + }); +} + + +/** + * Executes a command and returns its output if it succeeds. + * @param {string} cmd The command to execute. + * @return {!Promise<string>} A promise that will resolve with the command's + * stdout data. + */ +function execute(cmd) { + return new Promise((resolve, reject) => { + exec(cmd, function(err, stdout) { + if (err) { + reject(err); + } else { + resolve(stdout); + } + }); + }); +} + + +/** + * Computes the ephemeral port range for a Unix-like system. + * @return {!Promise<{min: number, max: number}>} A promise that will resolve + * with the ephemeral port range on the current system. + */ +function findUnixPortRange() { + var cmd; + if (process.platform === 'sunos') { + cmd = + '/usr/sbin/ndd /dev/tcp tcp_smallest_anon_port tcp_largest_anon_port'; + } else if (fs.existsSync('/proc/sys/net/ipv4/ip_local_port_range')) { + // Linux + cmd = 'cat /proc/sys/net/ipv4/ip_local_port_range'; + } else { + cmd = 'sysctl net.inet.ip.portrange.first net.inet.ip.portrange.last' + + ' | sed -e "s/.*:\\s*//"'; + } + + return execute(cmd).then(function(stdout) { + if (!stdout || !stdout.length) return DEFAULT_IANA_RANGE; + var range = stdout.trim().split(/\s+/).map(Number); + if (range.some(isNaN)) return DEFAULT_IANA_RANGE; + return {min: range[0], max: range[1]}; + }); +} + + +/** + * Computes the ephemeral port range for a Windows system. + * @return {!Promise<{min: number, max: number}>} A promise that will resolve + * with the ephemeral port range on the current system. + */ +function findWindowsPortRange() { + // First, check if we're running on XP. If this initial command fails, + // we just fallback on the default IANA range. + return execute('cmd.exe /c ver').then(function(stdout) { + if (/Windows XP/.test(stdout)) { + // TODO: Try to read these values from the registry. + return {min: 1025, max: 5000}; + } else { + return execute('netsh int ipv4 show dynamicport tcp'). + then(function(stdout) { + /* > netsh int ipv4 show dynamicport tcp + Protocol tcp Dynamic Port Range + --------------------------------- + Start Port : 49152 + Number of Ports : 16384 + */ + var range = stdout.split(/\n/).filter(function(line) { + return /.*:\s*\d+/.test(line); + }).map(function(line) { + return Number(line.split(/:\s*/)[1]); + }); + + return { + min: range[0], + max: range[0] + range[1] + }; + }); + } + }); +} + + +/** + * Tests if a port is free. + * @param {number} port The port to test. + * @param {string=} opt_host The bound host to test the {@code port} against. + * Defaults to {@code INADDR_ANY}. + * @return {!Promise<boolean>} A promise that will resolve with whether the port + * is free. + */ +function isFree(port, opt_host) { + return new Promise((resolve, reject) => { + let server = net.createServer().on('error', function(e) { + if (e.code === 'EADDRINUSE') { + resolve(false); + } else { + reject(e); + } + }); + + server.listen(port, opt_host, function() { + server.close(() => resolve(true)); + }); + }); +} + + +/** + * @param {string=} opt_host The bound host to test the {@code port} against. + * Defaults to {@code INADDR_ANY}. + * @return {!Promise<number>} A promise that will resolve to a free port. If a + * port cannot be found, the promise will be rejected. + */ +function findFreePort(opt_host) { + return findSystemPortRange().then(function(range) { + var attempts = 0; + return new Promise((resolve, reject) => { + findPort(); + + function findPort() { + attempts += 1; + if (attempts > 10) { + reject(Error('Unable to find a free port')); + } + + var port = Math.floor( + Math.random() * (range.max - range.min) + range.min); + isFree(port, opt_host).then(function(isFree) { + if (isFree) { + resolve(port); + } else { + findPort(); + } + }); + } + }); + }); +} + + +// PUBLIC API + + +exports.findFreePort = findFreePort; +exports.isFree = isFree; |