cfpilot 0.9.2 → 0.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -87,7 +87,7 @@ var VERSION, NAME;
87
87
  var init_version = __esm({
88
88
  "src/shared/version.ts"() {
89
89
  "use strict";
90
- VERSION = "0.9.2";
90
+ VERSION = "0.9.4";
91
91
  NAME = "cfpilot";
92
92
  }
93
93
  });
@@ -3505,118 +3505,6 @@ function probeTlsHandshake(ip, port, host, timeoutMs = 4e3) {
3505
3505
  socket.on("error", (err) => finish({ error: err.message }));
3506
3506
  });
3507
3507
  }
3508
- function measureDownload(ip, port, host, bytes, timeoutMs = 12e3, path = "/__edge_probe") {
3509
- return new Promise((resolve2) => {
3510
- const started = Date.now();
3511
- let headerEnd = -1;
3512
- let received = 0;
3513
- let settled = false;
3514
- let headerBuf = Buffer.alloc(0);
3515
- let status2 = 0;
3516
- const minPayload = Math.min(bytes, 32 * 1024);
3517
- const finish = (result) => {
3518
- if (settled) return;
3519
- settled = true;
3520
- clearTimeout(timer);
3521
- try {
3522
- socket.destroy();
3523
- } catch {
3524
- }
3525
- resolve2({ ...result, ms: Date.now() - started });
3526
- };
3527
- const judge = () => {
3528
- if (status2 !== 200) {
3529
- return { ok: false, error: status2 === 0 ? "\u672A\u6536\u5230\u54CD\u5E94\u5934" : `HTTP ${status2}` };
3530
- }
3531
- if (received < minPayload) {
3532
- return { ok: false, error: `\u8F7D\u8377\u4E0D\u8DB3\uFF08${received}/${minPayload} \u5B57\u8282\uFF09` };
3533
- }
3534
- return { ok: true };
3535
- };
3536
- const request = [
3537
- `GET ${path}?bytes=${bytes} HTTP/1.1`,
3538
- `Host: ${host}`,
3539
- `User-Agent: ${USER_AGENT}`,
3540
- "Accept: */*",
3541
- "Accept-Encoding: identity",
3542
- "",
3543
- ""
3544
- ].join("\r\n");
3545
- const socket = tls.connect(
3546
- {
3547
- host: ip,
3548
- port,
3549
- servername: host,
3550
- rejectUnauthorized: false,
3551
- ALPNProtocols: ["http/1.1"]
3552
- },
3553
- () => {
3554
- socket.write(request);
3555
- }
3556
- );
3557
- const timer = setTimeout(() => {
3558
- const seconds = Math.max(1e-3, (Date.now() - started) / 1e3);
3559
- const v = judge();
3560
- finish({
3561
- ok: v.ok,
3562
- bytes: received,
3563
- speedMBps: v.ok ? received / 1024 / 1024 / seconds : 0,
3564
- error: v.ok ? void 0 : v.error ?? "\u4E0B\u8F7D\u8D85\u65F6"
3565
- });
3566
- }, timeoutMs);
3567
- socket.setTimeout(timeoutMs, () => {
3568
- const seconds = Math.max(1e-3, (Date.now() - started) / 1e3);
3569
- const v = judge();
3570
- finish({
3571
- ok: v.ok,
3572
- bytes: received,
3573
- speedMBps: v.ok ? received / 1024 / 1024 / seconds : 0,
3574
- error: v.ok ? void 0 : v.error ?? "\u4E0B\u8F7D\u8D85\u65F6"
3575
- });
3576
- });
3577
- socket.on("error", (err) => {
3578
- finish({ ok: false, bytes: received, speedMBps: 0, error: err.message });
3579
- });
3580
- const settle = () => {
3581
- if (settled) return;
3582
- const seconds = Math.max(1e-3, (Date.now() - started) / 1e3);
3583
- const v = judge();
3584
- finish({
3585
- ok: v.ok,
3586
- bytes: received,
3587
- speedMBps: v.ok ? received / 1024 / 1024 / seconds : 0,
3588
- error: v.ok ? void 0 : v.error ?? "\u672A\u6536\u5230\u5B8C\u6574\u54CD\u5E94"
3589
- });
3590
- };
3591
- socket.on("data", (chunk) => {
3592
- if (headerEnd < 0) {
3593
- headerBuf = headerBuf.length === 0 ? chunk : Buffer.concat([headerBuf, chunk]);
3594
- const idx = headerBuf.indexOf("\r\n\r\n");
3595
- if (idx < 0) {
3596
- if (headerBuf.length > 8192) finish({ ok: false, bytes: 0, speedMBps: 0, error: "\u54CD\u5E94\u5934\u5F02\u5E38" });
3597
- return;
3598
- }
3599
- const head = headerBuf.subarray(0, idx).toString("latin1");
3600
- const code = /^HTTP\/1\.[01]\s+(\d{3})/.exec(head)?.[1];
3601
- status2 = code ? Number(code) : 0;
3602
- if (status2 !== 200) {
3603
- finish({ ok: false, bytes: 0, speedMBps: 0, error: `HTTP ${status2 || "?"}` });
3604
- return;
3605
- }
3606
- headerEnd = idx + 4;
3607
- const body = headerBuf.subarray(headerEnd);
3608
- received += body.byteLength;
3609
- headerEnd = 0;
3610
- headerBuf = Buffer.alloc(0);
3611
- } else {
3612
- received += chunk.byteLength;
3613
- }
3614
- if (received >= bytes) settle();
3615
- });
3616
- socket.on("end", settle);
3617
- socket.on("close", settle);
3618
- });
3619
- }
3620
3508
  function measureThroughput(options) {
3621
3509
  const {
3622
3510
  ip,
@@ -3628,18 +3516,18 @@ function measureThroughput(options) {
3628
3516
  warmupMs = 1e3,
3629
3517
  windowMs = 400,
3630
3518
  connections = 3,
3631
- timeoutMs = 2e4
3519
+ timeoutMs = 2e4,
3520
+ requiredHeader,
3521
+ minBytes = 32 * 1024
3632
3522
  } = options;
3633
3523
  const connCount = Math.max(1, Math.min(8, connections));
3634
3524
  return new Promise((resolve2) => {
3635
3525
  const started = Date.now();
3636
3526
  let settled = false;
3637
- let totalBytes = 0;
3638
- let windowBytes = 0;
3639
- let windowStart = started;
3640
- let peak = 0;
3527
+ const meter = new WindowMeter({ startedAt: started, warmupMs, windowMs });
3641
3528
  let openedCount = 0;
3642
3529
  let lastError = "";
3530
+ let headerMismatch = false;
3643
3531
  const sockets = [];
3644
3532
  const finish = () => {
3645
3533
  if (settled) return;
@@ -3652,23 +3540,26 @@ function measureThroughput(options) {
3652
3540
  } catch {
3653
3541
  }
3654
3542
  }
3655
- const elapsedNow = Date.now() - started;
3656
- if (elapsedNow >= warmupMs) {
3657
- const dt = (Date.now() - windowStart) / 1e3;
3658
- if (dt > 0) {
3659
- const speed = windowBytes / dt / 1024 / 1024;
3660
- if (speed > peak) peak = speed;
3661
- }
3543
+ const now = Date.now();
3544
+ const peakMBps = meter.finish(now);
3545
+ const totalBytes = meter.totalBytes;
3546
+ const elapsed = Math.max(1e-3, (now - started) / 1e3);
3547
+ const ok = !headerMismatch && totalBytes >= minBytes;
3548
+ let error;
3549
+ if (!ok) {
3550
+ if (headerMismatch) error = lastError || "\u76EE\u6807\u4E0D\u652F\u6301\u63A2\u9488\u6D4B\u901F";
3551
+ else if (totalBytes === 0) error = lastError || "\u672A\u6536\u5230\u6570\u636E";
3552
+ else error = lastError || `\u8F7D\u8377\u4E0D\u8DB3\uFF08${totalBytes}/${minBytes} \u5B57\u8282\uFF09`;
3662
3553
  }
3663
- const elapsed = Math.max(1e-3, (Date.now() - started) / 1e3);
3664
3554
  resolve2({
3665
- ok: totalBytes > 0,
3555
+ ok,
3666
3556
  bytes: totalBytes,
3667
- ms: Date.now() - started,
3668
- peakMBps: Number(peak.toFixed(2)),
3557
+ ms: now - started,
3558
+ steadyMBps: Number(meter.steadyMBps.toFixed(2)),
3559
+ peakMBps: Number(peakMBps.toFixed(2)),
3669
3560
  avgMBps: Number((totalBytes / 1024 / 1024 / elapsed).toFixed(2)),
3670
3561
  connections: openedCount,
3671
- error: totalBytes > 0 ? void 0 : lastError || "\u672A\u6536\u5230\u6570\u636E"
3562
+ error
3672
3563
  });
3673
3564
  };
3674
3565
  const request = [
@@ -3725,14 +3616,19 @@ function measureThroughput(options) {
3725
3616
  socket.destroy();
3726
3617
  return;
3727
3618
  }
3619
+ if (requiredHeader && !head.toLowerCase().includes(`${requiredHeader.toLowerCase()}:`)) {
3620
+ headerMismatch = true;
3621
+ lastError = `\u76EE\u6807\u4E0D\u652F\u6301\u63A2\u9488\u6D4B\u901F\uFF08\u54CD\u5E94\u5934\u7F3A\u5C11 ${requiredHeader}\uFF0C\u53EF\u6539\u7528 --speed-mode url\uFF09`;
3622
+ socket.destroy();
3623
+ finish();
3624
+ return;
3625
+ }
3728
3626
  headerDone = true;
3729
3627
  const body = headerBuffer.subarray(idx + 4);
3730
- totalBytes += body.byteLength;
3731
- windowBytes += body.byteLength;
3628
+ meter.add(body.byteLength);
3732
3629
  headerBuffer = Buffer.alloc(0);
3733
3630
  } else {
3734
- totalBytes += chunk.byteLength;
3735
- windowBytes += chunk.byteLength;
3631
+ meter.add(chunk.byteLength);
3736
3632
  }
3737
3633
  if (Date.now() - started > durationMs) finish();
3738
3634
  });
@@ -3744,15 +3640,7 @@ function measureThroughput(options) {
3744
3640
  });
3745
3641
  };
3746
3642
  const ticker = setInterval(() => {
3747
- const now = Date.now();
3748
- const dt = (now - windowStart) / 1e3;
3749
- if (dt <= 0) return;
3750
- const bytes = windowBytes;
3751
- windowBytes = 0;
3752
- windowStart = now;
3753
- if (now - started < warmupMs) return;
3754
- const speed = bytes / dt / 1024 / 1024;
3755
- if (speed > peak) peak = speed;
3643
+ meter.tick(Date.now());
3756
3644
  }, windowMs);
3757
3645
  const timer = setTimeout(finish, durationMs + 4e3);
3758
3646
  for (let i = 0; i < connCount; i++) {
@@ -3760,10 +3648,101 @@ function measureThroughput(options) {
3760
3648
  }
3761
3649
  });
3762
3650
  }
3763
- var USER_AGENT;
3651
+ async function measureProbeThroughput(options) {
3652
+ const rawPath = options.path && options.path.length > 0 ? options.path : "/__edge_probe";
3653
+ const separator = rawPath.includes("?") ? "&" : "?";
3654
+ return await measureThroughput({
3655
+ ip: options.ip,
3656
+ port: options.port,
3657
+ sniHost: options.host,
3658
+ requestHost: options.host,
3659
+ path: `${rawPath}${separator}bytes=${options.bytes}`,
3660
+ connections: options.connections,
3661
+ durationMs: options.durationMs,
3662
+ warmupMs: options.warmupMs,
3663
+ windowMs: options.windowMs,
3664
+ timeoutMs: options.timeoutMs,
3665
+ // /__edge_probe 的独有标记:少了它就不是「我们自己的探针端点」
3666
+ requiredHeader: "x-probe-bytes",
3667
+ minBytes: 32 * 1024
3668
+ });
3669
+ }
3670
+ var WindowMeter, USER_AGENT;
3764
3671
  var init_true_probe = __esm({
3765
3672
  "src/scan/true-probe.ts"() {
3766
3673
  "use strict";
3674
+ WindowMeter = class {
3675
+ startedAt;
3676
+ warmupMs;
3677
+ windowMs;
3678
+ windowBytes = 0;
3679
+ windowStart;
3680
+ peak = 0;
3681
+ total = 0;
3682
+ /** 预热之后累计的字节与**秒数**(注意 settle 里的 dt 已经是秒) */
3683
+ steadyBytes = 0;
3684
+ steadySeconds = 0;
3685
+ constructor(options) {
3686
+ this.startedAt = options.startedAt;
3687
+ this.warmupMs = options.warmupMs ?? 1e3;
3688
+ this.windowMs = options.windowMs ?? 500;
3689
+ this.windowStart = options.startedAt;
3690
+ }
3691
+ /** 累计收到的字节到当前窗口 */
3692
+ add(bytes) {
3693
+ if (bytes <= 0) return;
3694
+ this.windowBytes += bytes;
3695
+ this.total += bytes;
3696
+ }
3697
+ /** 结算当前窗口(由定时器按 windowMs 调用);返回当前峰值(MB/s) */
3698
+ tick(now) {
3699
+ this.settle(now);
3700
+ return this.peak;
3701
+ }
3702
+ /**
3703
+ * 收尾:结算最后一个不完整窗口,并处理「链路很快、没到预热期就下完预算」的兜底。
3704
+ */
3705
+ finish(now) {
3706
+ this.settle(now);
3707
+ const elapsed = (now - this.startedAt) / 1e3;
3708
+ if (this.peak === 0 && this.total > 0 && elapsed > 0) {
3709
+ this.peak = this.total / 1024 / 1024 / elapsed;
3710
+ }
3711
+ this.windowBytes = 0;
3712
+ this.windowStart = now;
3713
+ return this.peak;
3714
+ }
3715
+ /** 结算一个窗口:慢启动期内只丢弃,不记峰值/稳态 */
3716
+ settle(now) {
3717
+ const dt = (now - this.windowStart) / 1e3;
3718
+ if (dt <= 0) return;
3719
+ const bytes = this.windowBytes;
3720
+ this.windowBytes = 0;
3721
+ this.windowStart = now;
3722
+ if (now - this.startedAt < this.warmupMs) return;
3723
+ this.steadyBytes += bytes;
3724
+ this.steadySeconds += dt;
3725
+ const speed = bytes / dt / 1024 / 1024;
3726
+ if (speed > this.peak) this.peak = speed;
3727
+ }
3728
+ /** 已接收总字节 */
3729
+ get totalBytes() {
3730
+ return this.total;
3731
+ }
3732
+ /** 当前峰值(MB/s) */
3733
+ get peakMBps() {
3734
+ return this.peak;
3735
+ }
3736
+ /**
3737
+ * 稳态吞吐(MB/s):预热之后按时长加权的平均。
3738
+ *
3739
+ * 若整段都在预热期(链路极快),退回整段平均 —— 否则会报 0。
3740
+ */
3741
+ get steadyMBps() {
3742
+ if (this.steadySeconds > 0) return this.steadyBytes / 1024 / 1024 / this.steadySeconds;
3743
+ return this.peak;
3744
+ }
3745
+ };
3767
3746
  USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36";
3768
3747
  }
3769
3748
  });
@@ -3977,7 +3956,7 @@ async function runScan(options) {
3977
3956
  const probeConcurrency = options.probeConcurrency ?? 24;
3978
3957
  const probeTimeoutMs = options.probeTimeoutMs ?? 3e3;
3979
3958
  const speedLimit = options.speedLimit ?? 30;
3980
- const speedConcurrency = options.speedConcurrency ?? 4;
3959
+ const speedConcurrency = options.speedConcurrency ?? 1;
3981
3960
  const speedTimeoutMs = options.speedTimeoutMs ?? 12e3;
3982
3961
  const speedBytes = options.speedBytes ?? 5 * 1024 * 1024;
3983
3962
  const top = options.top ?? 8;
@@ -4134,31 +4113,33 @@ async function runScan(options) {
4134
4113
  stage: 3,
4135
4114
  done: speedDone,
4136
4115
  total: stage2.length,
4137
- message: `\u6D4B\u901F ${speedDone}/${stage2.length} ${result.peakMBps.toFixed(1)} MB/s`
4116
+ message: `\u6D4B\u901F ${speedDone}/${stage2.length} ${result.steadyMBps.toFixed(1)} MB/s`
4138
4117
  });
4139
- return { ...item, speed: result.ok ? result.peakMBps : 0 };
4118
+ return { ...item, speed: result.ok ? result.steadyMBps : 0 };
4140
4119
  });
4141
4120
  } else {
4142
- report({ type: "stage", stage: 3, name: "\u771F\u5B9E\u4E0B\u884C\u901F\u5EA6\u6D4B\u8BD5\uFF08\u9879\u76EE\u63A2\u9488\u7AEF\u70B9\uFF09", total: stage2.length });
4121
+ report({ type: "stage", stage: 3, name: "\u771F\u5B9E\u4E0B\u884C\u901F\u5EA6\u6D4B\u8BD5\uFF08\u9879\u76EE\u63A2\u9488\u7AEF\u70B9 \xB7 \u591A\u6D41\u7A33\u6001\uFF09", total: stage2.length });
4143
4122
  let speedDone = 0;
4144
4123
  speedResults = await pool(stage2, speedConcurrency, async (item) => {
4145
- const result = await measureDownload(
4146
- item.ip,
4147
- options.port,
4148
- options.host,
4149
- speedBytes,
4150
- speedTimeoutMs,
4151
- strictProbePath
4152
- );
4124
+ const result = await measureProbeThroughput({
4125
+ ip: item.ip,
4126
+ port: options.port,
4127
+ host: options.host,
4128
+ bytes: speedBytes,
4129
+ path: strictProbePath,
4130
+ connections: options.speedConnections ?? 3,
4131
+ durationMs: options.speedDurationMs ?? 6e3,
4132
+ timeoutMs: speedTimeoutMs
4133
+ });
4153
4134
  speedDone++;
4154
4135
  report({
4155
4136
  type: "progress",
4156
4137
  stage: 3,
4157
4138
  done: speedDone,
4158
4139
  total: stage2.length,
4159
- message: `\u6D4B\u901F ${speedDone}/${stage2.length} ${result.speedMBps.toFixed(1)} MB/s`
4140
+ message: `\u6D4B\u901F ${speedDone}/${stage2.length} ${result.steadyMBps.toFixed(1)} MB/s`
4160
4141
  });
4161
- return { ...item, speed: result.ok ? result.speedMBps : 0 };
4142
+ return { ...item, speed: result.ok ? result.steadyMBps : 0 };
4162
4143
  });
4163
4144
  }
4164
4145
  const entries = speedResults.filter((r) => options.skipSpeed ? true : Boolean(r.speed && r.speed > 0)).map((r) => ({
@@ -5272,6 +5253,15 @@ scan \u9009\u9879:
5272
5253
  --speed-conns <n> \u6D4B\u901F\u5E76\u53D1\u8FDE\u63A5\u6570\uFF0C\u9ED8\u8BA4 3
5273
5254
  \u5355\u6761 TCP \u8FDE\u63A5\u53D7\u62E5\u585E\u7A97\u53E3\u9650\u5236\uFF0C\u591A\u8FDE\u63A5\u66F4\u63A5\u8FD1\u771F\u5B9E\u5E26\u5BBD
5274
5255
  --speed-duration <ms> \u6D4B\u901F\u65F6\u957F\uFF0C\u9ED8\u8BA4 6000
5256
+ \u524D 1 \u79D2\u7684\u6162\u542F\u52A8\u6837\u672C\u4F1A\u88AB\u4E22\u5F03\uFF0C\u53EA\u7EDF\u8BA1\u4E4B\u540E\u7684\u7A33\u6001\u541E\u5410
5257
+ \u26A0\uFE0F \u6D41\u91CF\u5F00\u9500 \u2248 \u65F6\u957F \xD7 \u94FE\u8DEF\u901F\u7387 \xD7 \u8FDE\u63A5\u6570\uFF1A
5258
+ 3 \u6761\u8FDE\u63A5\u30016 \u79D2\u30011MB/s \u94FE\u8DEF\u4E0A\u7EA6 15MB/\u5019\u9009
5259
+ \uFF0830 \u4E2A\u5019\u9009 \u2248 450MB\uFF09\u3002\u6D41\u91CF\u654F\u611F\u65F6\u8C03\u5C0F\u5B83\u6216 --speed-limit
5260
+ --speed-concurrency <n>
5261
+ \u6D4B\u901F\u9636\u6BB5\u7684\u5019\u9009\u5E76\u884C\u6570\uFF0C\u9ED8\u8BA4 1\uFF08\u4E32\u884C\uFF09
5262
+ \u5E26\u5BBD\u7531\u6574\u6761\u94FE\u8DEF\u5171\u4EAB\uFF1A\u5E76\u884C\u6D4B\u591A\u4E2A\u5019\u9009\u4F1A\u4E92\u76F8\u62A2\u5E26\u5BBD\uFF0C
5263
+ \u6BCF\u4E2A\u90FD\u53EA\u80FD\u6D4B\u5230 1/N\uFF0C\u6570\u503C\u666E\u904D\u504F\u4F4E\uFF08\u8001\u7248\u672C\u62A5\u51FA
5264
+ 0.1 MB/s \u7684\u4E3B\u56E0\uFF09\u3002>1 \u53EA\u5728\u5343\u5146\u4EE5\u4E0A\u94FE\u8DEF\u624D\u6709\u610F\u4E49
5275
5265
  --retries <n> \u6BCF\u4E2A\u63A2\u6D4B\u7684\u5C1D\u8BD5\u6B21\u6570\uFF08\u542B\u9996\u6B21\uFF09\uFF0C\u9ED8\u8BA4 2
5276
5266
  \u771F\u673A\u5076\u53D1\u5931\u8D25\u7387 3~5%\uFF0C\u91CD\u8BD5\u53D6\u7B2C\u4E00\u6B21\u6210\u529F\u7684\u7ED3\u679C
5277
5267
  --no-verify \u9636\u6BB5\u4E8C\u53EA\u505A 101 \u63E1\u624B\uFF0C\u8DF3\u8FC7\u54CD\u5E94\u4F53\u6821\u9A8C
@@ -5607,6 +5597,8 @@ ${NAME} v${VERSION}
5607
5597
  speedBytes: flagNumber(args, "speed-bytes", 5 * 1024 * 1024),
5608
5598
  speedMode,
5609
5599
  speedUrl,
5600
+ // 默认串行:带宽是共享资源,并行测多个候选会互相抢带宽(见 scan.ts 的 speedConcurrency 注释)
5601
+ speedConcurrency: flagNumber(args, "speed-concurrency", 1),
5610
5602
  speedConnections: flagNumber(args, "speed-conns", 3),
5611
5603
  speedDurationMs: flagNumber(args, "speed-duration", 6e3),
5612
5604
  top,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cfpilot",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "CFPilot —— Cloudflare Pages 代理部署与优选工具(交互菜单 + 子命令)",
5
5
  "type": "module",
6
6
  "license": "MIT",