proxy.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @fileoverview Defines functions for configuring a webdriver proxy:
  19. *
  20. * const Capabilities = require('./capabilities').Capabilities;
  21. *
  22. * var capabilities = new Capabilities();
  23. * capabilities.setProxy(proxy.manual({http: 'host:1234'});
  24. */
  25. 'use strict';
  26. var ProxyConfig = require('./capabilities').ProxyConfig;
  27. // PUBLIC API
  28. /**
  29. * Configures WebDriver to bypass all browser proxies.
  30. * @return {!ProxyConfig} A new proxy configuration object.
  31. */
  32. exports.direct = function() {
  33. return {proxyType: 'direct'};
  34. };
  35. /**
  36. * Manually configures the browser proxy. The following options are
  37. * supported:
  38. *
  39. * - `ftp`: Proxy host to use for FTP requests
  40. * - `http`: Proxy host to use for HTTP requests
  41. * - `https`: Proxy host to use for HTTPS requests
  42. * - `bypass`: A list of hosts requests should directly connect to,
  43. * bypassing any other proxies for that request. May be specified as a
  44. * comma separated string, or a list of strings.
  45. *
  46. * Behavior is undefined for FTP, HTTP, and HTTPS requests if the
  47. * corresponding key is omitted from the configuration options.
  48. *
  49. * @param {{ftp: (string|undefined),
  50. * http: (string|undefined),
  51. * https: (string|undefined),
  52. * bypass: (string|!Array.<string>|undefined)}} options Proxy
  53. * configuration options.
  54. * @return {!ProxyConfig} A new proxy configuration object.
  55. */
  56. exports.manual = function(options) {
  57. // TODO(jleyba): Figure out why the Closure compiler does not think this is
  58. // a ProxyConfig record without the cast.
  59. return /** @type {!ProxyConfig} */({
  60. proxyType: 'manual',
  61. ftpProxy: options.ftp,
  62. httpProxy: options.http,
  63. sslProxy: options.https,
  64. noProxy: Array.isArray(options.bypass) ?
  65. options.bypass.join(',') : options.bypass
  66. });
  67. };
  68. /**
  69. * Creates a proxy configuration for a socks proxy.
  70. *
  71. * __Example:__
  72. *
  73. * const {Capabilities} = require('selenium-webdriver');
  74. * const proxy = require('selenium-webdriver/lib/proxy');
  75. *
  76. * let capabilities = new Capabilities();
  77. * capabilities.setProxy(proxy.socks('localhost:1234', 'bob', 'password'));
  78. *
  79. *
  80. * @param {string} host The proxy host, in the form `hostname:port`.
  81. * @param {string} username The user name to authenticate as.
  82. * @param {string} password The password to authenticate with.
  83. * @return {!ProxyConfig} A new proxy configuration object.
  84. * @see https://en.wikipedia.org/wiki/SOCKS
  85. */
  86. exports.socks = function(host, username, password) {
  87. return /** @type {!ProxyConfig} */({
  88. proxyType: 'manual',
  89. socksProxy: host,
  90. socksUsername: username,
  91. socksPassword: password
  92. });
  93. };
  94. /**
  95. * Configures WebDriver to configure the browser proxy using the PAC file at
  96. * the given URL.
  97. * @param {string} url URL for the PAC proxy to use.
  98. * @return {!ProxyConfig} A new proxy configuration object.
  99. */
  100. exports.pac = function(url) {
  101. return {
  102. proxyType: 'pac',
  103. proxyAutoconfigUrl: url
  104. };
  105. };
  106. /**
  107. * Configures WebDriver to use the current system's proxy.
  108. * @return {!ProxyConfig} A new proxy configuration object.
  109. */
  110. exports.system = function() {
  111. return {proxyType: 'system'};
  112. };