diff --git a/package.json b/package.json index 4a10c69b207cd..7a9f9978ba10b 100644 --- a/package.json +++ b/package.json @@ -156,8 +156,7 @@ "validate-npm-package-name": "2.2.2", "webpack": "1.12.15", "webpack-directory-name-as-main": "1.0.0", - "whatwg-fetch": "0.9.0", - "wreck": "6.2.0" + "whatwg-fetch": "0.9.0" }, "devDependencies": { "@elastic/eslint-config-kibana": "0.0.3", diff --git a/src/cli_plugin/install/download.js b/src/cli_plugin/install/download.js index 871b170628fe8..8e67978c9bf96 100644 --- a/src/cli_plugin/install/download.js +++ b/src/cli_plugin/install/download.js @@ -10,7 +10,7 @@ export function _downloadSingle(settings, logger, sourceUrl) { if (/^file/.test(urlInfo.protocol)) { downloadPromise = downloadLocalFile(logger, decodeURI(urlInfo.path), settings.tempArchiveFile); } else if (/^https?/.test(urlInfo.protocol)) { - downloadPromise = downloadHttpFile(logger, sourceUrl, settings.tempArchiveFile, settings.timeout); + downloadPromise = downloadHttpFile(logger, sourceUrl, settings.tempArchiveFile, settings.timeout, settings.proxy); } else { downloadPromise = Promise.reject(new UnsupportedProtocolError()); } diff --git a/src/cli_plugin/install/downloaders/http.js b/src/cli_plugin/install/downloaders/http.js index 40069c4cd063e..8b5e413c00006 100644 --- a/src/cli_plugin/install/downloaders/http.js +++ b/src/cli_plugin/install/downloaders/http.js @@ -1,25 +1,24 @@ -import Wreck from 'wreck'; import Progress from '../progress'; import { fromNode as fn } from 'bluebird'; +import Request from 'request'; import { createWriteStream, unlinkSync } from 'fs'; -function sendRequest({ sourceUrl, timeout }) { - const maxRedirects = 11; //Because this one goes to 11. - return fn(cb => { - const req = Wreck.request('GET', sourceUrl, { timeout, redirects: maxRedirects }, (err, resp) => { - if (err) { - if (err.code === 'ECONNREFUSED') { - err = new Error('ENOTFOUND'); - } - - return cb(err); - } +function sendRequest({ sourceUrl, reqOptions }) { + return new Promise((resolve, reject) => { + const req = Request.get(sourceUrl, reqOptions); + req.on('response', (resp) => { if (resp.statusCode >= 400) { - return cb(new Error('ENOTFOUND')); + return reject(new Error('ENOTFOUND')); } + return resolve({ req, resp }); + }); - cb(null, { req, resp }); + req.on('error', (err) => { + if (err.code === 'ECONNREFUSED') { + err = new Error('ENOTFOUND'); + } + return reject(err); }); }); } @@ -48,9 +47,20 @@ function downloadResponse({ resp, targetPath, progress }) { /* Responsible for managing http transfers */ -export default async function downloadUrl(logger, sourceUrl, targetPath, timeout) { +export default async function downloadUrl(logger, sourceUrl, targetPath, timeout, proxy) { + const reqOptions = { + 'timeout': timeout, + 'maxRedirects': 11, //Because this one goes to 11. + 'encoding': null + }; + + if (proxy) { + reqOptions.proxy = proxy; + logger.log(`Attempting to use the following proxy: ${proxy}`); + } + try { - const { req, resp } = await sendRequest({ sourceUrl, timeout }); + const { req, resp } = await sendRequest({ sourceUrl, reqOptions }); try { let totalSize = parseFloat(resp.headers['content-length']) || 0; diff --git a/src/cli_plugin/install/index.js b/src/cli_plugin/install/index.js index 78547d1db006b..6a956c576f52c 100644 --- a/src/cli_plugin/install/index.js +++ b/src/cli_plugin/install/index.js @@ -4,7 +4,7 @@ import install from './install'; import Logger from '../lib/logger'; import pkg from '../../utils/package_json'; import { getConfig } from '../../server/path'; -import { parse, parseMilliseconds } from './settings'; +import { parse, parseMilliseconds, parseProxy } from './settings'; import { find } from 'lodash'; function processCommand(command, options) { @@ -41,6 +41,11 @@ export default function pluginInstall(program) { 'path to the directory where plugins are stored', fromRoot('plugins') ) + .option( + '--proxy ', + 'proxy url to use', + parseProxy + ) .description('install a plugin', `Common examples: install x-pack diff --git a/src/cli_plugin/install/settings.js b/src/cli_plugin/install/settings.js index fd87342d5dae3..b02e0e4381362 100644 --- a/src/cli_plugin/install/settings.js +++ b/src/cli_plugin/install/settings.js @@ -23,6 +23,16 @@ export function parseMilliseconds(val) { return result; }; +export function parseProxy(val) { + const result = val.trim(); + const regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; + if (!regexp.test(result)) { + throw new Error(`Invalid proxy name ${result}`); + } + + return result; +} + export function parse(command, options, kbnPackage) { const settings = { timeout: options.timeout || 0, @@ -31,7 +41,8 @@ export function parse(command, options, kbnPackage) { config: options.config || '', plugin: command, version: kbnPackage.version, - pluginDir: options.pluginDir || '' + pluginDir: options.pluginDir || '', + proxy: options.proxy || '' }; settings.urls = generateUrls(settings); diff --git a/tasks/build/download_node_builds.js b/tasks/build/download_node_builds.js index cb74b55cec446..d05c488962fea 100644 --- a/tasks/build/download_node_builds.js +++ b/tasks/build/download_node_builds.js @@ -6,7 +6,7 @@ module.exports = function (grunt) { let { createGunzip } = require('zlib'); let { Extract } = require('tar'); let { rename } = require('fs'); - let wreck = require('wreck'); + let request = require('request'); let platforms = grunt.config.get('platforms'); let activeDownloads = []; @@ -21,7 +21,7 @@ module.exports = function (grunt) { } let resp = await fromNode(cb => { - let req = wreck.request('GET', platform.nodeUrl, null, function (err, resp) { + let req = request.get(platform.nodeUrl, function (err, resp) { if (err) { return cb(err); }