File size: 27,686 Bytes
c608dbf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
import axios from 'axios';

/**
 * 代理池类,用于管理和提供HTTP代理
 */
class ProxyPool {
  /**
   * 创建代理池实例
   * @param {Object} options - 配置选项
   * @param {number} options.targetCount - 目标代理数量,默认20
   * @param {number} options.batchSize - 每次获取的代理数量,默认20
   * @param {number} options.testTimeout - 测试代理超时时间(毫秒),默认3000
   * @param {number} options.requestTimeout - 请求目标网站超时时间(毫秒),默认5000
   * @param {string} options.targetUrl - 目标网站URL,默认'https://www.notion.so'
   * @param {number} options.concurrentRequests - 并发请求数量,默认15
   * @param {number} options.minThreshold - 可用代理数量低于此阈值时自动补充,默认5
   * @param {number} options.checkInterval - 检查代理池状态的时间间隔(毫秒),默认30000
   * @param {string} options.proxyProtocol - 代理协议,默认'http'
   * @param {number} options.maxRefillAttempts - 最大补充尝试次数,默认50
   * @param {number} options.retryDelay - 重试延迟(毫秒),默认500
   * @param {boolean} options.useCache - 是否使用缓存,默认true
   * @param {number} options.cacheExpiry - 缓存过期时间(毫秒),默认3600000 (1小时)
   * @param {string} options.logLevel - 日志级别,可选值:'debug', 'info', 'warn', 'error', 'none',默认'info'
   * @param {boolean} options.showProgressBar - 是否显示进度条,默认false
   */
  constructor(options = {}) {
    // 配置参数
    this.targetCount = options.targetCount || 20;
    this.batchSize = options.batchSize || 20;
    this.testTimeout = options.testTimeout || 3000; // 减少测试超时时间
    this.requestTimeout = options.requestTimeout || 5000; // 减少请求超时时间
    this.targetUrl = options.targetUrl || 'https://www.notion.so';
    this.concurrentRequests = options.concurrentRequests || 15; // 增加默认并发请求数
    this.minThreshold = options.minThreshold || 5;
    this.checkInterval = options.checkInterval || 30000; // 默认30秒检查一次
    this.proxyProtocol = options.proxyProtocol || 'http';
    this.maxRefillAttempts = options.maxRefillAttempts || 50; // 增加最大尝试次数
    this.retryDelay = options.retryDelay || 500; // 减少重试延迟
    this.useCache = options.useCache !== undefined ? options.useCache : true;
    this.cacheExpiry = options.cacheExpiry || 3600000; // 默认1小时
    this.logLevel = options.logLevel || 'info'; // 默认日志级别为info
    this.showProgressBar = options.showProgressBar !== undefined ? options.showProgressBar : false; // 是否显示进度条
    
    // 内部状态
    this.availableProxies = [];
    this.currentIndex = 0;
    this.isInitialized = false;
    this.isRefilling = false;
    this.checkTimer = null;
    this.proxyCache = new Map(); // 缓存验证过的代理
    this.proxyCountry = 'us';
    
    // 进度条状态
    this.progressTotal = 0;
    this.progressCurrent = 0;
    this.progressBarLength = 30; // 进度条长度
    this.lastProgressString = ''; // 上次显示的进度字符串
    
    // 日志级别权重
    this.logLevels = {
      'debug': 0,
      'info': 1,
      'warn': 2,
      'error': 3,
      'none': 4
    };
    
    // 绑定方法
    this.getProxy = this.getProxy.bind(this);
    this.removeProxy = this.removeProxy.bind(this);
    this.checkAndRefill = this.checkAndRefill.bind(this);
  }

  /**
   * 设置代理国家
   * @param {string} country - 代理国家 支持us uk jp de fr ca
   */
  setCountry(country) {
    this.proxyCountry = country;
  }
  
  /**
   * 根据设置的日志级别输出日志
   * @param {string} level - 日志级别
   * @param {...any} args - 日志参数
   */
  log(level, ...args) {
    if (this.logLevels[level] >= this.logLevels[this.logLevel]) {
      if (level === 'error') {
        console.error(...args);
      } else if (level === 'warn') {
        console.warn(...args);
      } else if (level === 'info' || level === 'debug') {
        console.log(...args);
      }
    }
  }
  
  /**
   * 显示进度条
   * @param {string} message - 进度条前的消息
   * @param {number} current - 当前进度
   * @param {number} total - 总进度
   * @param {boolean} [complete=false] - 是否完成
   */
  showProgress(message, current, total) {
    if (!this.showProgressBar) return;
    
    // 确保进度不超过总数
    current = Math.min(current, total);
    
    // 计算进度百分比
    const percent = total > 0 ? Math.floor((current / total) * 100) : 0;
    
    // 创建终端风格的箭头进度条
    let bar = '';
    let ProcessBarLength = 15;
    let CurrentProcessBarLength = Math.floor(ProcessBarLength*(percent/100));
    let ProcessBarRemainLength = ProcessBarLength-CurrentProcessBarLength;
    let ProcessBar = '-'.repeat(CurrentProcessBarLength)+'>';
    let ProcessBarRemain = '·'.repeat(ProcessBarRemainLength);
    if (percent === 100) {
      // 完成状态显示完整箭头
      bar = '['+'-'.repeat(ProcessBarLength)+'>'+']';
    } else {
      bar = '['+ProcessBar+ProcessBarRemain+']';
    }
    
    // 构建进度字符串,添加百分比
    const progressString = `${message} ${bar} ${current}/${total} (${percent}%)`;
    
    // 如果进度字符串与上次相同,则不重复显示
    if (progressString === this.lastProgressString) {
      return;
    }
    
    // 清除上一行
    if (this.lastProgressString) {
      process.stdout.clearLine(0);
      process.stdout.cursorTo(0);
    }
    
    // 显示进度条
    process.stdout.write(progressString);
    
    // 如果完成,换行
    if (percent === 100) {
      process.stdout.write('\n');
    }
    
    // 保存当前进度字符串
    this.lastProgressString = progressString;
  }
  
  /**
   * 初始化代理池
   * @returns {Promise<void>}
   */
  async initialize() {
    if (this.isInitialized) return;
    
    this.log('info', `初始化代理池,目标数量: ${this.targetCount}`);
    
    // 重置进度条状态
    this.progressTotal = this.targetCount;
    this.progressCurrent = 0;
    //this.showProgress('初始化代理池进度', this.progressCurrent, this.progressTotal);
    
    await this.refillProxies();
    
    // 设置定时检查
    this.checkTimer = setInterval(this.checkAndRefill, this.checkInterval);
    
    this.isInitialized = true;
    
    // 完成进度条
    this.showProgress('补充代理进度', this.availableProxies.length, this.progressTotal);
    this.log('info', `代理池初始化完成,当前可用代理数量: ${this.availableProxies.length}`);
  }
  
  /**
   * 停止代理池服务
   */
  stop() {
    if (this.checkTimer) {
      clearInterval(this.checkTimer);
      this.checkTimer = null;
    }
    this.log('info', '代理池服务已停止');
  }
  
  /**
   * 检查并补充代理
   */
  async checkAndRefill() {
    if (this.availableProxies.length <= this.minThreshold && !this.isRefilling) {
      this.log('info', `可用代理数量(${this.availableProxies.length})低于阈值(${this.minThreshold}),开始补充代理`);
      await this.refillProxies();
    }
  }
  
  /**
   * 补充代理到目标数量
   * @returns {Promise<void>}
   */
  async refillProxies() {
    if (this.isRefilling) return;
    
    this.isRefilling = true;
    this.log('info', `开始补充代理,当前数量: ${this.availableProxies.length},目标数量: ${this.targetCount}`);
    
    // 设置进度条状态
    this.progressTotal = this.targetCount;
    this.progressCurrent = this.availableProxies.length;
    this.showProgress('补充代理进度', this.progressCurrent, this.progressTotal);
    
    let attempts = 0;
    
    try {
      // 计算需要补充的代理数量
      const neededProxies = this.targetCount - this.availableProxies.length;
      
      // 优先检查缓存中的代理
      if (this.useCache && this.proxyCache.size > 0) {
        await this.tryUsingCachedProxies(neededProxies);
        
        // 更新进度条
        this.progressCurrent = this.availableProxies.length;
        this.showProgress('补充代理进度', this.progressCurrent, this.progressTotal);
      }
      
      // 如果缓存中的代理不足,继续获取新代理
      while (this.availableProxies.length < this.targetCount && attempts < this.maxRefillAttempts) {
        attempts++;
        
        this.log('debug', `补充尝试 #${attempts},当前可用代理: ${this.availableProxies.length}/${this.targetCount}`);
        
        // 计算本次需要获取的批次大小
        const remainingNeeded = this.targetCount - this.availableProxies.length;
        const batchSizeNeeded = Math.max(remainingNeeded * 2, 10); // 获取更多代理以提高成功率,至少10个
        
        // 获取代理
        const proxies = await this.getProxiesFromProvider(batchSizeNeeded);
        
        if (proxies.length === 0) {
          this.log('debug', `没有获取到代理,等待${this.retryDelay/1000}秒后重试...`);
          await new Promise(resolve => setTimeout(resolve, this.retryDelay));
          continue;
        }
        
        // 过滤掉已有的代理
        const newProxies = this.filterExistingProxies(proxies);
        
        if (newProxies.length === 0) {
          this.log('debug', '所有获取的代理都已存在,继续获取新代理...');
          // 减少等待时间,立即继续尝试
          continue;
        }
        
        // 保存当前进度,测试代理过程中会临时更新进度条
        const previousProgress = this.progressCurrent;
        
        // 测试代理
        const results = await this.testProxiesConcurrently(newProxies);
        
        // 添加可用代理
        this.addValidProxies(results);
        
        // 更新实际进度
        this.progressCurrent = this.availableProxies.length;
        this.showProgress('补充代理进度', this.progressCurrent, this.progressTotal);
        
        // 如果已经获取到足够的代理,提前结束
        if (this.availableProxies.length >= this.targetCount) {
          break;
        }
        
        // 如果还没补充到足够的代理,减少等待时间再继续
        if (this.availableProxies.length < this.targetCount) {
          await new Promise(resolve => setTimeout(resolve, 500)); // 减少等待时间到500毫秒
        }
      }
    } catch (error) {
      this.log('error', '补充代理过程中出错:', error);
    } finally {
      this.isRefilling = false;      
      if (this.availableProxies.length >= this.targetCount) {
        this.log('info', `代理补充完成,当前可用代理: ${this.availableProxies.length}/${this.targetCount}`);
      } else {
        this.log('info', `已达到最大尝试次数 ${this.maxRefillAttempts},当前可用代理: ${this.availableProxies.length}/${this.targetCount}`);
      }
    }
  }
  
  /**
   * 尝试使用缓存中的代理
   * @param {number} neededProxies - 需要的代理数量
   */
  async tryUsingCachedProxies(neededProxies) {
    const now = Date.now();
    const cachedProxies = [];
    
    // 筛选未过期的缓存代理
    for (const [proxyKey, data] of this.proxyCache.entries()) {
      if (now - data.timestamp < this.cacheExpiry && data.valid) {
        cachedProxies.push(proxyKey);
        
        if (cachedProxies.length >= neededProxies) {
          break;
        }
      }
    }
    
    if (cachedProxies.length > 0) {
      this.log('debug', `从缓存中找到 ${cachedProxies.length} 个可能可用的代理`);
      
      // 验证缓存的代理是否仍然可用
      const results = await this.testProxiesConcurrently(cachedProxies);
      this.addValidProxies(results);
    }
  }
  
  /**
   * 过滤掉已存在的代理
   * @param {Array<string>} proxies - 代理列表
   * @returns {Array<string>} - 新代理列表
   */
  filterExistingProxies(proxies) {
    return proxies.filter(proxy => {
      const proxyParts = proxy.split(':');
      const ip = proxyParts[0];
      const port = proxyParts[1];
      
      // 如果有用户名和密码,也需要比较这些信息
      if (proxyParts.length >= 4) {
        const username = proxyParts[2];
        const password = proxyParts[3];
        return !this.availableProxies.some(p => 
          p.ip === ip && 
          p.port === port && 
          p.username === username && 
          p.password === password
        );
      }
      
      // 没有用户名和密码时,只比较IP和端口
      return !this.availableProxies.some(p => p.ip === ip && p.port === port);
    });
  }
  
  /**
   * 添加有效的代理到代理池
   * @param {Array<{proxy: string, result: boolean}>} results - 测试结果
   */
  addValidProxies(results) {
    for (const { proxy, result } of results) {
      if (result) {
        const proxyParts = proxy.split(':');
        const ip = proxyParts[0];
        const port = proxyParts[1];
        
        // 检查是否已存在
        let exists = false;
        
        if (proxyParts.length >= 4) {
          // 有用户名和密码时,同时比较IP、端口、用户名和密码
          const username = proxyParts[2];
          const password = proxyParts[3];
          exists = this.availableProxies.some(p => 
            p.ip === ip && 
            p.port === port && 
            p.username === username && 
            p.password === password
          );
        } else {
          // 没有用户名和密码时,只比较IP和端口
          exists = this.availableProxies.some(p => p.ip === ip && p.port === port);
        }
        
        if (!exists) {
          const proxyObj = {
            ip,
            port,
            protocol: this.proxyProtocol,
            full: `${this.proxyProtocol}://${proxy}`,
            addedAt: new Date().toISOString()
          };
          
          // 如果有用户名和密码,添加到代理对象
          if (proxyParts.length >= 4) {
            proxyObj.username = proxyParts[2];
            proxyObj.password = proxyParts[3];
            proxyObj.full = `${this.proxyProtocol}://${proxyObj.username}:${proxyObj.password}@${ip}:${port}`;
          }
          
          this.availableProxies.push(proxyObj);
          
          // 添加到缓存
          if (this.useCache) {
            this.proxyCache.set(proxy, { 
              valid: true, 
              timestamp: Date.now() 
            });
          }
          
          this.log('debug', `成功添加代理: ${proxyObj.full},当前可用代理: ${this.availableProxies.length}/${this.targetCount}`);
          
          if (this.availableProxies.length >= this.targetCount) {
            break;
          }
        }
      } else if (this.useCache) {
        // 记录无效代理到缓存
        this.proxyCache.set(proxy, { 
          valid: false, 
          timestamp: Date.now() 
        });
      }
    }
  }
  
  /**
   * 从代理服务获取代理URL
   * @param {number} count - 请求的代理数量
   * @returns {Promise<Array<string>>} - 代理URL列表
   */
  async getProxiesFromProvider(count = null) {
    try {
      const requestCount = count || this.batchSize;
      // 限制单次请求数量最大为10
      const actualCount = Math.min(requestCount, 10);
      
      // 计算需要发送的请求数量,以获取足够的代理
      const requestsNeeded = Math.ceil(requestCount / 1); // 假设每次请求只返回1个代理
      const maxParallelRequests = 5; // 最大并行请求数
      const actualRequests = Math.min(requestsNeeded, maxParallelRequests);
      
      this.log('debug', `需要 ${requestCount} 个代理,将发送 ${actualRequests} 个并行请求`);
      
      // 并行发送多个请求
      const requestPromises = [];
      for (let i = 0; i < actualRequests; i++) {
        const url = `https://proxy.doudouzi.me/random/${this.proxyCountry}?number=${actualCount}&protocol=${this.proxyProtocol}&type=json`;
        requestPromises.push(
          axios.get(url, { 
            timeout: 10000,
            validateStatus: status => true
          })
        );
      }
      
      // 等待所有请求完成
      const responses = await Promise.all(requestPromises);
      
      // 处理所有响应,合并代理列表
      let proxies = [];
      
      for (const response of responses) {
        if (!response.data) continue;
        
        // 处理不同的返回格式
        if (typeof response.data === 'string') {
          // 如果返回的是字符串,尝试按行解析
          const proxyDataArray = response.data.trim().split('\n').filter(line => line.trim() !== '');
          
          for (const line of proxyDataArray) {
            try {
              const proxyData = JSON.parse(line);
              if (proxyData.ip && proxyData.port) {
                // 如果有用户名和密码,则使用认证格式
                if (proxyData.username && proxyData.password) {
                  proxies.push(`${proxyData.ip}:${proxyData.port}:${proxyData.username}:${proxyData.password}`);
                } else {
                  proxies.push(`${proxyData.ip}:${proxyData.port}`);
                }
              }
            } catch (err) {
              this.log('error', '解析代理数据出错:', err.message);
            }
          }
        } else if (typeof response.data === 'object') {
          // 如果返回的是对象
          if (Array.isArray(response.data)) {
            // 如果是数组,遍历处理
            for (const proxyData of response.data) {
              if (proxyData.ip && proxyData.port) {
                if (proxyData.username && proxyData.password) {
                  proxies.push(`${proxyData.ip}:${proxyData.port}:${proxyData.username}:${proxyData.password}`);
                } else {
                  proxies.push(`${proxyData.ip}:${proxyData.port}`);
                }
              }
            }
          } else {
            // 如果是单个对象
            if (response.data.ip && response.data.port) {
              if (response.data.username && response.data.password) {
                proxies.push(`${response.data.ip}:${response.data.port}:${response.data.username}:${response.data.password}`);
              } else {
                proxies.push(`${response.data.ip}:${response.data.port}`);
              }
            }
          }
        }
      }
      
      this.log('debug', `成功获取 ${proxies.length} 个代理`);
      return proxies;
    } catch (error) {
      this.log('error', '获取代理出错:', error.message);
      return [];
    }
  }
  
  /**
   * 并发测试多个代理
   * @param {Array<string>} proxies - 代理列表
   * @returns {Promise<Array<{proxy: string, result: boolean}>>} - 测试结果
   */
  async testProxiesConcurrently(proxies) {
    const results = [];
    const remainingNeeded = this.targetCount - this.availableProxies.length;
    
    // 增加并发数以加快处理速度
    const concurrentRequests = Math.min(this.concurrentRequests * 3, 30); // 增加到最多30个并发请求
    
    // 分批处理代理
    for (let i = 0; i < proxies.length; i += concurrentRequests) {
      const batch = proxies.slice(i, i + concurrentRequests);
      
      // 不再显示测试代理的单独进度条,而是更新补充代理的总体进度
      if (this.showProgressBar) {
        // 计算当前测试进度对应的总体进度
        const testedPercent = i / proxies.length;
        const progressStep = Math.min(1, testedPercent) * 0.2; // 测试部分占总体进度的20%
        const currentProgress = this.progressCurrent + progressStep * (this.targetCount - this.progressCurrent);
        this.showProgress('补充代理进度', Math.floor(currentProgress), this.progressTotal);
      }
      
      const promises = batch.map(proxy => {
        // 检查缓存中是否有近期验证过的结果
        if (this.useCache && this.proxyCache.has(proxy)) {
          const cachedResult = this.proxyCache.get(proxy);
          const isFresh = (Date.now() - cachedResult.timestamp) < this.cacheExpiry;
          
          if (isFresh) {
            // 使用缓存结果,避免重复测试
            return Promise.resolve({ proxy, result: cachedResult.valid });
          }
        }
        
        return this.testProxy(proxy)
          .then(result => ({ proxy, result }))
          .catch(() => ({ proxy, result: false }));
      });
      
      const batchResults = await Promise.all(promises);
      results.push(...batchResults);
      
      // 如果已经找到足够的代理,提前结束测试
      const successCount = results.filter(item => item.result).length;
      if (successCount >= remainingNeeded) {
        break;
      }
    }
    
    // 不再显示测试完成的进度条
    
    return results;
  }
  
  /**
   * 测试代理是否可用
   * @param {string} proxyUrl - 代理URL
   * @returns {Promise<boolean>} - 代理是否可用
   */
  async testProxy(proxyUrl) {
    try {
      // 创建代理配置
      const proxyParts = proxyUrl.split(':');
      const proxyConfig = {
        host: proxyParts[0],
        port: parseInt(proxyParts[1]),
        protocol: this.proxyProtocol
      };
      
      // 如果有用户名和密码,添加认证信息
      if (proxyParts.length >= 4) {
        proxyConfig.auth = {
          username: proxyParts[2],
          password: proxyParts[3]
        };
      }
      
      // 发送请求到目标网站
      const response = await axios.get(this.targetUrl, {
        proxy: proxyConfig,
        headers: {
          'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
          'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
          'Accept-Language': 'en-US,en;q=0.5',
          'Connection': 'keep-alive',
          'Upgrade-Insecure-Requests': '1'
        },
        timeout: Math.min(this.requestTimeout, 5000), // 减少超时时间,最多5秒
        validateStatus: status => true,
        maxRedirects: 5, // 减少最大重定向次数
        followRedirect: true
      });
      
      // 检查响应是否包含目标网站特有的内容
      const isTargetContent = response.data && 
                             (typeof response.data === 'string') && 
                             (response.data.includes('notion') || 
                              response.data.includes('Notion'));
      
      const isValid = response.status === 200 && isTargetContent;
      
      if (isValid) {
        this.log('debug', `代理 ${proxyUrl} 请求目标网站成功,状态码: ${response.status}`);
      } else {
        this.log('debug', `代理 ${proxyUrl} 请求目标网站失败,状态码: ${response.status}`);
      }
      
      return isValid;
    } catch (error) {
      this.log('debug', `代理 ${proxyUrl} 请求出错: ${error.message}`);
      return false;
    }
  }
  
  /**
   * 获取一个可用代理
   * @returns {Object|null} - 代理对象,如果没有可用代理则返回null
   */
  getProxy() {
    if (this.availableProxies.length === 0) {
      this.log('warn', '没有可用代理');
      return null;
    }
    
    // 轮询方式获取代理
    const proxy = this.availableProxies[this.currentIndex];
    this.currentIndex = (this.currentIndex + 1) % this.availableProxies.length;
    
    return proxy;
  }
  
  /**
   * 移除指定代理
   * @param {string} ip - 代理IP
   * @param {string|number} port - 代理端口
   * @returns {boolean} - 是否成功移除
   */
  removeProxy(ip, port) {
    const portStr = port.toString();
    const initialLength = this.availableProxies.length;
    
    // 找到要移除的代理
    const proxyToRemove = this.availableProxies.find(
      proxy => proxy.ip === ip && proxy.port === portStr
    );
    
    if (proxyToRemove) {
      // 更新缓存,标记为无效
      if (this.useCache) {
        const proxyKey = `${ip}:${portStr}`;
        this.proxyCache.set(proxyKey, { valid: false, timestamp: Date.now() });
      }
    }
    
    this.availableProxies = this.availableProxies.filter(
      proxy => !(proxy.ip === ip && proxy.port === portStr)
    );
    
    // 重置当前索引,确保不会越界
    if (this.currentIndex >= this.availableProxies.length && this.availableProxies.length > 0) {
      this.currentIndex = 0;
    }
    
    const removed = initialLength > this.availableProxies.length;
    
    if (removed) {
      this.log('debug', `已移除代理 ${ip}:${port},当前可用代理: ${this.availableProxies.length}`);
    } else {
      this.log('debug', `未找到要移除的代理 ${ip}:${port}`);
    }
    
    // 如果移除后代理数量低于阈值,触发补充
    this.checkAndRefill();
    
    return removed;
  }
  
  /**
   * 获取所有可用代理
   * @returns {Array<Object>} - 代理对象数组
   */
  getAllProxies() {
    return [...this.availableProxies];
  }
  
  /**
   * 获取可用代理数量
   * @returns {number} - 代理数量
   */
  getCount() {
    return this.availableProxies.length;
  }
  
  /**
   * 清理过期的缓存条目
   */
  cleanupCache() {
    if (!this.useCache) return;
    
    const now = Date.now();
    let cleanupCount = 0;
    
    for (const [key, data] of this.proxyCache.entries()) {
      if (now - data.timestamp > this.cacheExpiry) {
        this.proxyCache.delete(key);
        cleanupCount++;
      }
    }
    
    if (cleanupCount > 0) {
      this.log('debug', `清理了 ${cleanupCount} 个过期的缓存代理`);
    }
  }
}

// 使用示例
async function example() {
  // 创建代理池实例
  const proxyPool = new ProxyPool({
    targetCount: 10,           // 目标保持10个代理
    minThreshold: 3,           // 当可用代理少于3个时,自动补充
    checkInterval: 60000,      // 每60秒检查一次
    targetUrl: 'https://www.notion.so',
    concurrentRequests: 20,    // 增加并发请求数
    useCache: true,            // 启用缓存
    maxRefillAttempts: 50,     // 增加最大尝试次数
    retryDelay: 500,           // 减少重试延迟
    testTimeout: 3000,         // 减少测试超时时间
    requestTimeout: 5000,      // 减少请求超时时间
    logLevel: 'info',          // 设置日志级别
    showProgressBar: true      // 启用进度条
  });
  
  // 初始化代理池
  await proxyPool.initialize();
  
  // 获取一个代理
  const proxy = proxyPool.getProxy();
  console.log('获取到代理:', proxy);
  
  // 模拟使用一段时间后,移除一个代理
  setTimeout(() => {
    if (proxy) {
      proxyPool.removeProxy(proxy.ip, proxy.port);
    }
    
    // 获取所有代理
    const allProxies = proxyPool.getAllProxies();
    console.log(`当前所有代理(${allProxies.length}):`, allProxies);
    
    // 使用完毕后停止服务
    setTimeout(() => {
      proxyPool.stop();
      console.log('代理池示例运行完毕');
    }, 5000);
  }, 5000);
}

// 如果直接运行此文件,则执行示例
if (typeof require !== 'undefined' && require.main === module) {
  example().catch(err => console.error('示例运行出错:', err));
}

// 导出 ProxyPool 类和实例
export default ProxyPool;
export const proxyPool = new ProxyPool({
  logLevel: 'info', // 默认导出的实例使用info级别的日志
  showProgressBar: true // 启用进度条
});