runDiagnostic method

Future<ProxyDiagnosticResult> runDiagnostic({
  1. bool testAllSources = true,
  2. bool validateProxies = true,
  3. String? testUrl,
  4. int timeout = 10000,
})

Runs a comprehensive diagnostic on the proxy system

Implementation

Future<ProxyDiagnosticResult> runDiagnostic({
  bool testAllSources = true,
  bool validateProxies = true,
  String? testUrl,
  int timeout = 10000,
}) async {
  final result = ProxyDiagnosticResult();

  try {
    // Step 1: Check current proxy lists
    result.currentProxies = _proxyManager.proxies.length;
    result.currentValidatedProxies = _proxyManager.validatedProxies.length;

    // Step 2: Test fetching from all sources if requested
    if (testAllSources) {
      final sourceResults = await _testAllSources();
      result.sourceResults = sourceResults;
    }

    // Step 3: Test proxy validation
    if (validateProxies && result.currentProxies > 0) {
      final validationResults = await _testProxyValidation(
        testUrl: testUrl,
        timeout: timeout,
      );
      result.validationResults = validationResults;
    }

    // Step 4: Try to fetch and validate new proxies
    try {
      final startTime = DateTime.now();
      final proxies = await _proxyManager.fetchValidatedProxies(
        options: ProxyFilterOptions(count: 10, onlyHttps: true),
        onProgress: (completed, total) {
          if (kDebugMode) {
            print('Validated $completed of $total proxies');
          }
        },
      );
      final endTime = DateTime.now();

      result.fetchValidatedProxiesSuccess = true;
      result.fetchValidatedProxiesCount = proxies.length;
      result.fetchValidatedProxiesTime =
          endTime.difference(startTime).inMilliseconds;
    } catch (e) {
      result.fetchValidatedProxiesSuccess = false;
      result.fetchValidatedProxiesError = e.toString();
    }

    // Step 5: Test getting a proxy
    try {
      final proxy = _proxyManager.getNextProxy(validated: true);
      result.getNextProxySuccess = true;
      result.proxyDetails = proxy.toString();
    } catch (e) {
      result.getNextProxySuccess = false;
      result.getNextProxyError = e.toString();
    }
  } catch (e) {
    result.overallError = e.toString();
  }

  return result;
}