cfpilot 0.9.6 → 0.9.8

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.6";
90
+ VERSION = "0.9.8";
91
91
  NAME = "cfpilot";
92
92
  }
93
93
  });
@@ -3671,6 +3671,103 @@ async function measureProbeThroughput(options) {
3671
3671
  minBytes: 32 * 1024
3672
3672
  });
3673
3673
  }
3674
+ function buildVlessTunnelPayload(uuid, targetHost, targetPort, request) {
3675
+ const clean = uuid.replace(/-/g, "");
3676
+ if (!/^[0-9a-fA-F]{32}$/.test(clean)) return null;
3677
+ const hostBuf = Buffer.from(targetHost, "utf8");
3678
+ return Buffer.concat([
3679
+ Buffer.from([0]),
3680
+ Buffer.from(clean, "hex"),
3681
+ Buffer.from([0, 1, targetPort >> 8 & 255, targetPort & 255, 2, hostBuf.length]),
3682
+ hostBuf,
3683
+ Buffer.from(request, "utf8")
3684
+ ]);
3685
+ }
3686
+ function buildTrojanTunnelPayload(password, targetHost, targetPort, request) {
3687
+ const digest = crypto.createHash("sha224").update(password).digest("hex");
3688
+ const hostBuf = Buffer.from(targetHost, "utf8");
3689
+ return Buffer.concat([
3690
+ Buffer.from(digest + "\r\n", "ascii"),
3691
+ Buffer.from([1, 3, hostBuf.length]),
3692
+ hostBuf,
3693
+ Buffer.from([targetPort >> 8 & 255, targetPort & 255]),
3694
+ Buffer.from("\r\n", "ascii"),
3695
+ Buffer.from(request, "utf8")
3696
+ ]);
3697
+ }
3698
+ function tunnelUpgradeRequest(host, wsPath, key) {
3699
+ return ["GET " + wsPath + " HTTP/1.1", "Host: " + host, "Upgrade: websocket", "Connection: Upgrade", "Sec-WebSocket-Key: " + key, "Sec-WebSocket-Version: 13", "User-Agent: " + USER_AGENT, "", ""].join("\r\n");
3700
+ }
3701
+ function tunnelTargetRequest(targetHost) {
3702
+ return "GET / HTTP/1.1\r\nHost: " + targetHost + "\r\nUser-Agent: " + USER_AGENT + "\r\nConnection: close\r\n\r\n";
3703
+ }
3704
+ function verifyTunnel(options) {
3705
+ const { ip, port, host, wsPath, uuid, trojanPassword, targetHost = "baidu.com", targetPort = 80, timeoutMs = 8e3 } = options;
3706
+ const payload = uuid ? buildVlessTunnelPayload(uuid, targetHost, targetPort, tunnelTargetRequest(targetHost)) : trojanPassword ? buildTrojanTunnelPayload(trojanPassword, targetHost, targetPort, tunnelTargetRequest(targetHost)) : null;
3707
+ if (!payload) return Promise.resolve({ ok: false, stage: "protocol", latencyMs: 0, error: "\u7F3A\u5C11 uuid / trojanPassword" });
3708
+ return new Promise((resolve2) => {
3709
+ const started = Date.now();
3710
+ const key = crypto.randomBytes(16).toString("base64");
3711
+ let settled = false;
3712
+ let handshakeDone = false;
3713
+ let buf = Buffer.alloc(0);
3714
+ const finish = (r) => {
3715
+ if (settled) return;
3716
+ settled = true;
3717
+ clearTimeout(timer);
3718
+ try {
3719
+ socket.destroy();
3720
+ } catch {
3721
+ }
3722
+ resolve2({ ...r, latencyMs: Date.now() - started });
3723
+ };
3724
+ const socket = tls.connect({ host: ip, port, servername: host, rejectUnauthorized: false, ALPNProtocols: ["http/1.1"] }, () => {
3725
+ socket.write(tunnelUpgradeRequest(host, wsPath, key));
3726
+ });
3727
+ const timer = setTimeout(() => {
3728
+ finish({ ok: false, stage: handshakeDone ? "data" : "handshake", error: handshakeDone ? "\u534F\u8BAE\u5DF2\u53D1\u51FA\u4F46\u6CA1\u6709\u56DE\u5305" : "WS \u63E1\u624B\u8D85\u65F6" });
3729
+ }, timeoutMs);
3730
+ socket.on("error", (err) => finish({ ok: false, stage: handshakeDone ? "data" : "handshake", error: err.message }));
3731
+ socket.setTimeout(timeoutMs, () => finish({ ok: false, stage: handshakeDone ? "data" : "handshake", error: "\u8FDE\u63A5\u8D85\u65F6" }));
3732
+ socket.on("data", (chunk) => {
3733
+ buf = Buffer.concat([buf, chunk]);
3734
+ if (!handshakeDone) {
3735
+ const lineEnd = buf.indexOf("\r\n\r\n");
3736
+ if (lineEnd < 0) {
3737
+ if (buf.length > 8192) finish({ ok: false, stage: "handshake", error: "\u54CD\u5E94\u5934\u5F02\u5E38" });
3738
+ return;
3739
+ }
3740
+ const head = buf.slice(0, lineEnd).toString("latin1");
3741
+ const status2 = Number((/^HTTP\/1\.[01]\s+(\d{3})/.exec(head.split("\r\n")[0] ?? "") ?? [])[1] ?? 0);
3742
+ buf = buf.subarray(lineEnd + 4);
3743
+ if (status2 !== 101) {
3744
+ finish({ ok: false, stage: "handshake", error: "HTTP " + status2 });
3745
+ return;
3746
+ }
3747
+ handshakeDone = true;
3748
+ socket.write(encodeWsFrame(payload, 2));
3749
+ }
3750
+ for (; ; ) {
3751
+ const decoded = decodeWsFrame(buf);
3752
+ if (!decoded) break;
3753
+ buf = decoded.rest;
3754
+ const { opcode, payload: data } = decoded.frame;
3755
+ if (opcode === 8) {
3756
+ const code = data.length >= 2 ? data.readUInt16BE(0) : 1005;
3757
+ finish({ ok: false, stage: "protocol", error: "WS \u5173\u95ED\uFF08code " + code + "\uFF09" });
3758
+ return;
3759
+ }
3760
+ if ((opcode === 1 || opcode === 2) && data.length > 0) {
3761
+ const body = uuid && data.length >= 2 && data[0] === 0 && data[1] === 0 ? data.subarray(2) : data;
3762
+ if (body.length > 0) {
3763
+ finish({ ok: true, firstBytes: body.subarray(0, 16).toString("latin1") });
3764
+ return;
3765
+ }
3766
+ }
3767
+ }
3768
+ });
3769
+ });
3770
+ }
3674
3771
  var WindowMeter, USER_AGENT;
3675
3772
  var init_true_probe = __esm({
3676
3773
  "src/scan/true-probe.ts"() {
@@ -4027,6 +4124,7 @@ async function runScan(options) {
4027
4124
  });
4028
4125
  let probeDone = 0;
4029
4126
  let verifiedCount = 0;
4127
+ let pathFailures = 0;
4030
4128
  const earlyExitTarget = hasCountryFilter ? Math.ceil(speedLimit * 1.5) : Infinity;
4031
4129
  let matchedCount = 0;
4032
4130
  let stoppedEarly = false;
@@ -4040,6 +4138,12 @@ async function runScan(options) {
4040
4138
  retries,
4041
4139
  () => probeFn(item.ip, options.port, options.host, probeTimeoutMs, probePath)
4042
4140
  );
4141
+ let pathOk;
4142
+ if (result.ok && explicitWsPath && explicitWsPath !== probePath && options.verifyWsPath !== false) {
4143
+ const pathProbe = await probeWs101(item.ip, options.port, options.host, probeTimeoutMs, explicitWsPath);
4144
+ pathOk = pathProbe.ok;
4145
+ if (!pathOk) pathFailures++;
4146
+ }
4043
4147
  probeDone++;
4044
4148
  if (result.pongVerified) verifiedCount++;
4045
4149
  if (hasCountryFilter && result.ok && countryFilter.includes(countryOfColo(result.colo || ""))) {
@@ -4052,7 +4156,7 @@ async function runScan(options) {
4052
4156
  total: stage1.length,
4053
4157
  message: strictProbe ? `101+PING \u6821\u9A8C ${probeDone}/${stage1.length}${result.ok ? ` \u2713 ${result.colo}` : result.pongVerified === false ? " \u2717 \u65E0\u54CD\u5E94" : ""}` : `101 \u6821\u9A8C ${probeDone}/${stage1.length}${result.ok ? ` \u2713 ${result.colo}` : ""}`
4054
4158
  });
4055
- return { ...item, probe: result };
4159
+ return { ...item, probe: result, pathOk };
4056
4160
  },
4057
4161
  () => {
4058
4162
  if (matchedCount >= earlyExitTarget) {
@@ -4062,7 +4166,37 @@ async function runScan(options) {
4062
4166
  return false;
4063
4167
  }
4064
4168
  );
4065
- const wsOk = strictProbe ? probeResults.filter((r) => r.probe.ok && r.probe.pongVerified === true) : probeResults.filter((r) => r.probe.ok);
4169
+ const wsOk = (strictProbe ? probeResults.filter((r) => r.probe.ok && r.probe.pongVerified === true) : probeResults.filter((r) => r.probe.ok)).filter((r) => r.pathOk !== false);
4170
+ let tunnelOk = wsOk;
4171
+ let tunnelFailed = 0;
4172
+ const tunnelCredential = options.uuid || options.trojanPassword;
4173
+ if (tunnelCredential && options.wsPath) {
4174
+ const candidates2 = wsOk.slice(0, speedLimit);
4175
+ const verified = await pool(candidates2, 4, async (item) => {
4176
+ const result = await verifyTunnel({
4177
+ ip: item.ip,
4178
+ port: options.port,
4179
+ host: options.host,
4180
+ wsPath: options.wsPath,
4181
+ uuid: options.uuid,
4182
+ trojanPassword: options.trojanPassword,
4183
+ targetHost: options.verifyTargetHost,
4184
+ targetPort: options.verifyTargetPort
4185
+ });
4186
+ if (!result.ok) tunnelFailed++;
4187
+ report({
4188
+ type: "progress",
4189
+ stage: 2,
4190
+ done: candidates2.length,
4191
+ total: candidates2.length,
4192
+ message: `\u771F\u5B9E\u96A7\u9053\u6821\u9A8C ${result.ok ? "\u2713" : "\u2717 " + (result.error ?? "")}`
4193
+ });
4194
+ return { item, ok: result.ok };
4195
+ });
4196
+ const passed = new Set(verified.filter((v) => v.ok).map((v) => v.item.ip));
4197
+ tunnelOk = wsOk.filter((item) => passed.has(item.ip));
4198
+ if (tunnelOk.length === 0) tunnelOk = wsOk;
4199
+ }
4066
4200
  let wsFiltered = wsOk;
4067
4201
  let appliedCountries = [];
4068
4202
  let countryFilterRelaxed = false;
@@ -4101,61 +4235,82 @@ async function runScan(options) {
4101
4235
  const requestPath = withBytes(parsedUrl, options.speedBytes ?? 104857600);
4102
4236
  let speedDone = 0;
4103
4237
  speedResults = await pool(stage2, speedConcurrency, async (item) => {
4104
- const result = await measureThroughput({
4105
- ip: item.ip,
4106
- port: options.port,
4107
- sniHost: parsedUrl.host,
4108
- requestHost: parsedUrl.host,
4109
- path: requestPath,
4110
- connections: options.speedConnections ?? 8,
4111
- durationMs: options.speedDurationMs ?? 6e3,
4112
- timeoutMs: options.speedTimeoutMs ?? 2e4
4113
- });
4238
+ const repeat = Math.max(1, options.speedRepeat ?? 1);
4239
+ const samples = [];
4240
+ for (let r = 0; r < repeat; r++) {
4241
+ const one = await measureThroughput({
4242
+ ip: item.ip,
4243
+ port: options.port,
4244
+ sniHost: parsedUrl.host,
4245
+ requestHost: parsedUrl.host,
4246
+ path: requestPath,
4247
+ connections: options.speedConnections ?? 8,
4248
+ durationMs: options.speedDurationMs ?? 6e3,
4249
+ timeoutMs: options.speedTimeoutMs ?? 2e4
4250
+ });
4251
+ if (one.ok) samples.push(one.steadyMBps);
4252
+ }
4253
+ samples.sort((a, b) => a - b);
4254
+ const median = samples.length === 0 ? 0 : samples[Math.floor(samples.length / 2)];
4255
+ const jitter = samples.length < 2 ? 0 : samples[samples.length - 1] - samples[0];
4256
+ const result = { ok: samples.length > 0, steadyMBps: median };
4114
4257
  speedDone++;
4115
4258
  report({
4116
4259
  type: "progress",
4117
4260
  stage: 3,
4118
4261
  done: speedDone,
4119
4262
  total: stage2.length,
4120
- message: `\u6D4B\u901F ${speedDone}/${stage2.length} [${item.probe.colo || "UNK"}] ${result.steadyMBps.toFixed(1)} MB/s`
4263
+ message: `\u6D4B\u901F ${speedDone}/${stage2.length} [${item.probe.colo || "UNK"}] ${result.steadyMBps.toFixed(1)} MB/s${jitter > 0 ? ` \xB1${jitter.toFixed(1)}` : ""}`
4121
4264
  });
4122
- return { ...item, speed: result.ok ? result.steadyMBps : 0 };
4265
+ return { ...item, speed: result.ok ? result.steadyMBps : 0, jitter };
4123
4266
  });
4124
4267
  } else {
4125
4268
  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 });
4126
4269
  let speedDone = 0;
4127
4270
  speedResults = await pool(stage2, speedConcurrency, async (item) => {
4128
- const result = await measureProbeThroughput({
4129
- ip: item.ip,
4130
- port: options.port,
4131
- host: options.host,
4132
- bytes: speedBytes,
4133
- path: strictProbePath,
4134
- connections: options.speedConnections ?? 8,
4135
- durationMs: options.speedDurationMs ?? 6e3,
4136
- timeoutMs: speedTimeoutMs
4137
- });
4271
+ const repeat = Math.max(1, options.speedRepeat ?? 1);
4272
+ const samples = [];
4273
+ for (let r = 0; r < repeat; r++) {
4274
+ const one = await measureProbeThroughput({
4275
+ ip: item.ip,
4276
+ port: options.port,
4277
+ host: options.host,
4278
+ bytes: speedBytes,
4279
+ path: strictProbePath,
4280
+ connections: options.speedConnections ?? 8,
4281
+ durationMs: options.speedDurationMs ?? 6e3,
4282
+ timeoutMs: speedTimeoutMs,
4283
+ token: options.uuid
4284
+ });
4285
+ if (one.ok) samples.push(one.steadyMBps);
4286
+ }
4287
+ samples.sort((a, b) => a - b);
4288
+ const median = samples.length === 0 ? 0 : samples[Math.floor(samples.length / 2)];
4289
+ const jitter = samples.length < 2 ? 0 : samples[samples.length - 1] - samples[0];
4290
+ const result = { ok: samples.length > 0, steadyMBps: median };
4138
4291
  speedDone++;
4139
4292
  report({
4140
4293
  type: "progress",
4141
4294
  stage: 3,
4142
4295
  done: speedDone,
4143
4296
  total: stage2.length,
4144
- message: `\u6D4B\u901F ${speedDone}/${stage2.length} [${item.probe.colo || "UNK"}] ${result.steadyMBps.toFixed(1)} MB/s`
4297
+ message: `\u6D4B\u901F ${speedDone}/${stage2.length} [${item.probe.colo || "UNK"}] ${result.steadyMBps.toFixed(1)} MB/s${jitter > 0 ? ` \xB1${jitter.toFixed(1)}` : ""}`
4145
4298
  });
4146
4299
  return { ...item, speed: result.ok ? result.steadyMBps : 0 };
4147
4300
  });
4148
4301
  }
4149
- const entries = speedResults.filter((r) => options.skipSpeed ? true : Boolean(r.speed && r.speed > 0)).map((r) => ({
4302
+ const ranked = speedResults.filter((r) => options.skipSpeed ? true : Boolean(r.speed && r.speed > 0)).map((r) => ({
4150
4303
  ip: r.ip,
4151
4304
  port: options.port,
4152
4305
  colo: r.probe.colo || "UNK",
4153
4306
  latency: r.latency,
4154
4307
  tlsMs: r.tls.tlsMs,
4155
- speed: r.speed ? Number(r.speed.toFixed(2)) : 0
4308
+ speed: r.speed ? Number(r.speed.toFixed(2)) : 0,
4309
+ speedJitter: r.jitter ? Number(r.jitter.toFixed(2)) : 0
4156
4310
  })).sort(
4157
4311
  (a, b) => options.skipSpeed ? a.tlsMs - b.tlsMs || a.latency - b.latency : b.speed - a.speed || a.tlsMs - b.tlsMs || a.latency - b.latency
4158
- ).slice(0, top);
4312
+ );
4313
+ const entries = pickWithDiversity(ranked, top);
4159
4314
  for (const entry of entries) {
4160
4315
  report({ type: "result", entry });
4161
4316
  }
@@ -4191,6 +4346,33 @@ function formatEntries(entries) {
4191
4346
  (e) => `${formatHost(e.ip)}:${e.port}#${e.colo}_${e.latency}_${e.speed}`
4192
4347
  );
4193
4348
  }
4349
+ function pickWithDiversity(ranked, top) {
4350
+ if (top <= 0) return [];
4351
+ const buckets = /* @__PURE__ */ new Map();
4352
+ for (const item of ranked) {
4353
+ const parts = item.ip.split(".");
4354
+ const key = parts.length >= 2 ? `${parts[0]}.${parts[1]}` : item.ip;
4355
+ const list = buckets.get(key);
4356
+ if (list) list.push(item);
4357
+ else buckets.set(key, [item]);
4358
+ }
4359
+ if (buckets.size <= 1) return ranked.slice(0, top);
4360
+ const groups = [...buckets.values()];
4361
+ const out = [];
4362
+ for (let round = 0; out.length < top; round++) {
4363
+ let pickedAny = false;
4364
+ for (const group of groups) {
4365
+ if (out.length >= top) break;
4366
+ const item = group[round];
4367
+ if (item) {
4368
+ out.push(item);
4369
+ pickedAny = true;
4370
+ }
4371
+ }
4372
+ if (!pickedAny) break;
4373
+ }
4374
+ return out;
4375
+ }
4194
4376
  var init_scan = __esm({
4195
4377
  "src/scan/scan.ts"() {
4196
4378
  "use strict";
@@ -5307,6 +5489,11 @@ scan \u9009\u9879:
5307
5489
  --speed-limit <n> \u8FDB\u5165\u6D4B\u901F\u7684\u5019\u9009\u6570\uFF0C\u9ED8\u8BA4 30
5308
5490
  --speed-bytes <n> \u6D4B\u901F\u5B57\u8282\u6570\uFF0C\u9ED8\u8BA4 5242880
5309
5491
  --timeout <ms> \u9636\u6BB5\u4E00\u8D85\u65F6\uFF0C\u9ED8\u8BA4 1200
5492
+ --speed-repeat <n> \u6BCF\u4E2A\u5019\u9009\u91CD\u590D\u6D4B\u901F\u6B21\u6570\uFF0C\u9ED8\u8BA4 1\uFF08>1 \u65F6\u53D6\u4E2D\u4F4D\u6570\u5E76\u663E\u793A \xB1\u6296\u52A8\uFF09
5493
+ --uuid <uuid> \u771F\u5B9E\u96A7\u9053\u6821\u9A8C\u7528\u7684 VLESS UUID\uFF08\u7ED9\u4E86\u624D\u4F1A\u8DD1\u9636\u6BB5\u4E8C\u70B9\u4E94\uFF09
5494
+ --trojan-password <pw> \u771F\u5B9E\u96A7\u9053\u6821\u9A8C\u7528\u7684 Trojan \u53E3\u4EE4\uFF08\u4E0E --uuid \u4E8C\u9009\u4E00\uFF09
5495
+ --verify-target <host> \u96A7\u9053\u6821\u9A8C\u8BBF\u95EE\u7684\u76EE\u6807\u57DF\u540D\uFF0C\u9ED8\u8BA4 baidu.com
5496
+ --no-verify-ws-path \u8DF3\u8FC7\u300C\u771F\u5B9E WS \u8DEF\u5F84 101\u300D\u8FD9\u9879\u989D\u5916\u6821\u9A8C
5310
5497
  --seed <n> \u91C7\u6837\u968F\u673A\u79CD\u5B50\u3002\u9ED8\u8BA4\u6BCF\u6B21\u968F\u673A \u2014\u2014 \u4FDD\u8BC1\u6BCF\u6B21\u63A2\u7D22\u4E0D\u540C\u7684 IP \u5B50\u96C6
5311
5498
  \uFF08\u5199\u6B7B\u79CD\u5B50\u4F1A\u8BA9\u6BCF\u6B21\u8DD1\u90FD\u6D4B\u540C\u4E00\u6279 IP\uFF0C\u62BD\u5230\u52A3\u8D28\u6BB5\u5C31\u6C38\u8FDC\u6D4B\u4E0D\u51FA\u597D IP\uFF09
5312
5499
  \u663E\u5F0F\u6307\u5B9A\u5219\u56FA\u5B9A\u3001\u7ED3\u679C\u53EF\u590D\u73B0\uFF08\u6392\u67E5 / \u5BF9\u6BD4\u65F6\u7528\uFF09
@@ -5624,6 +5811,13 @@ ${NAME} v${VERSION}
5624
5811
  // 用 3 条会系统性低估 2~4 倍。默认 8 条,贴近浏览器/客户端的真实多连接行为。
5625
5812
  speedConnections: flagNumber(args, "speed-conns", 8),
5626
5813
  speedDurationMs: flagNumber(args, "speed-duration", 6e3),
5814
+ // 真实隧道校验(可选):给了凭据才会跑
5815
+ uuid: flagString(args, "uuid"),
5816
+ trojanPassword: flagString(args, "trojan-password"),
5817
+ verifyTargetHost: flagString(args, "verify-target"),
5818
+ verifyTargetPort: flagNumber(args, "verify-target-port", 80),
5819
+ speedRepeat: flagNumber(args, "speed-repeat", 1),
5820
+ verifyWsPath: args.flags["no-verify-ws-path"] === void 0,
5627
5821
  top,
5628
5822
  onProgress: (event) => printProgress(event, quiet)
5629
5823
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cfpilot",
3
- "version": "0.9.6",
3
+ "version": "0.9.8",
4
4
  "description": "CFPilot —— Cloudflare Pages 代理部署与优选工具(交互菜单 + 子命令)",
5
5
  "type": "module",
6
6
  "license": "MIT",