1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5import 'dart:async'; 6 7import 'base/platform.dart'; 8import 'doctor.dart'; 9 10class 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) => 22 platform.environment[key.toLowerCase()]?.trim() ?? 23 platform.environment[key.toUpperCase()]?.trim() ?? 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')); 37 for (String host in const <String>['127.0.0.1', 'localhost']) { 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} 56