Ian Hickson | 449f4a6 | 2019-11-27 15:04:02 -0800 | [diff] [blame] | 1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
Danny Tuppeny | b16ef48 | 2019-01-10 07:47:29 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | import 'dart:async'; |
| 6 | |
Danny Tuppeny | b16ef48 | 2019-01-10 07:47:29 +0000 | [diff] [blame] | 7 | import 'doctor.dart'; |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 8 | import 'globals.dart' as globals; |
Danny Tuppeny | b16ef48 | 2019-01-10 07:47:29 +0000 | [diff] [blame] | 9 | |
| 10 | class ProxyValidator extends DoctorValidator { |
| 11 | ProxyValidator() : super('Proxy Configuration'); |
| 12 | |
| 13 | static bool get shouldShow => _getEnv('HTTP_PROXY').isNotEmpty; |
| 14 | |
| 15 | final String _httpProxy = _getEnv('HTTP_PROXY'); |
| 16 | final String _noProxy = _getEnv('NO_PROXY'); |
| 17 | |
| 18 | /// Gets a trimmed, non-null environment variable. If the variable is not set |
| 19 | /// an empty string will be returned. Checks for the lowercase version of the |
| 20 | /// environment variable first, then uppercase to match Dart's HTTP implementation. |
| 21 | static String _getEnv(String key) => |
Jonah Williams | ee7a37f | 2020-01-06 11:04:20 -0800 | [diff] [blame] | 22 | globals.platform.environment[key.toLowerCase()]?.trim() ?? |
| 23 | globals.platform.environment[key.toUpperCase()]?.trim() ?? |
Danny Tuppeny | b16ef48 | 2019-01-10 07:47:29 +0000 | [diff] [blame] | 24 | ''; |
| 25 | |
| 26 | @override |
| 27 | Future<ValidationResult> validate() async { |
| 28 | final List<ValidationMessage> messages = <ValidationMessage>[]; |
| 29 | |
| 30 | if (_httpProxy.isNotEmpty) { |
| 31 | messages.add(ValidationMessage('HTTP_PROXY is set')); |
| 32 | |
| 33 | if (_noProxy.isEmpty) { |
| 34 | messages.add(ValidationMessage.hint('NO_PROXY is not set')); |
| 35 | } else { |
| 36 | messages.add(ValidationMessage('NO_PROXY is $_noProxy')); |
Alexandre Ardhuin | 4f9b6cf | 2020-01-07 16:32:04 +0100 | [diff] [blame] | 37 | for (final String host in const <String>['127.0.0.1', 'localhost']) { |
Danny Tuppeny | b16ef48 | 2019-01-10 07:47:29 +0000 | [diff] [blame] | 38 | final ValidationMessage msg = _noProxy.contains(host) |
| 39 | ? ValidationMessage('NO_PROXY contains $host') |
| 40 | : ValidationMessage.hint('NO_PROXY does not contain $host'); |
| 41 | |
| 42 | messages.add(msg); |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | final bool hasIssues = |
| 48 | messages.any((ValidationMessage msg) => msg.isHint || msg.isHint); |
| 49 | |
| 50 | return ValidationResult( |
| 51 | hasIssues ? ValidationType.partial : ValidationType.installed, |
| 52 | messages, |
| 53 | ); |
| 54 | } |
| 55 | } |