cfpilot 0.8.0 → 0.9.0
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/assets/control.js +9 -9
- package/assets/dataplane.js +4 -4
- package/assets/worker.js +11 -11
- package/dist/cli.js +267 -76
- package/package.json +1 -1
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.
|
|
90
|
+
VERSION = "0.9.0";
|
|
91
91
|
NAME = "cfpilot";
|
|
92
92
|
}
|
|
93
93
|
});
|
|
@@ -453,13 +453,21 @@ var init_prompt = __esm({
|
|
|
453
453
|
import { homedir } from "node:os";
|
|
454
454
|
import { join } from "node:path";
|
|
455
455
|
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
|
|
456
|
+
function configDir() {
|
|
457
|
+
const override = process.env.CFPILOT_CONFIG_DIR?.trim();
|
|
458
|
+
return override ? override : join(homedir(), ".cfpilot");
|
|
459
|
+
}
|
|
460
|
+
function configFile() {
|
|
461
|
+
return join(configDir(), "config.json");
|
|
462
|
+
}
|
|
456
463
|
function configPath() {
|
|
457
|
-
return
|
|
464
|
+
return configFile();
|
|
458
465
|
}
|
|
459
466
|
function loadConfig() {
|
|
460
|
-
|
|
467
|
+
const file = configFile();
|
|
468
|
+
if (!existsSync(file)) return {};
|
|
461
469
|
try {
|
|
462
|
-
return JSON.parse(readFileSync(
|
|
470
|
+
return JSON.parse(readFileSync(file, "utf8"));
|
|
463
471
|
} catch {
|
|
464
472
|
return {};
|
|
465
473
|
}
|
|
@@ -470,11 +478,12 @@ function saveConfig(patch) {
|
|
|
470
478
|
for (const k of Object.keys(next)) {
|
|
471
479
|
if (next[k] === "" || next[k] === void 0) delete next[k];
|
|
472
480
|
}
|
|
473
|
-
mkdirSync(
|
|
474
|
-
writeFileSync(
|
|
481
|
+
mkdirSync(configDir(), { recursive: true });
|
|
482
|
+
writeFileSync(configFile(), JSON.stringify(next, null, 2) + "\n", "utf8");
|
|
475
483
|
}
|
|
476
484
|
function clearConfig() {
|
|
477
|
-
|
|
485
|
+
const file = configFile();
|
|
486
|
+
if (existsSync(file)) writeFileSync(file, "{}\n", "utf8");
|
|
478
487
|
}
|
|
479
488
|
function resolveToken(fromArgs) {
|
|
480
489
|
return (fromArgs || "").trim() || (process.env.CLOUDFLARE_API_TOKEN || "").trim() || loadConfig().token || "";
|
|
@@ -482,12 +491,9 @@ function resolveToken(fromArgs) {
|
|
|
482
491
|
function isConfigured(cfg = loadConfig()) {
|
|
483
492
|
return Boolean(cfg.token);
|
|
484
493
|
}
|
|
485
|
-
var CONFIG_DIR, CONFIG_FILE;
|
|
486
494
|
var init_store = __esm({
|
|
487
495
|
"src/menu/store.ts"() {
|
|
488
496
|
"use strict";
|
|
489
|
-
CONFIG_DIR = join(homedir(), ".cfpilot");
|
|
490
|
-
CONFIG_FILE = join(CONFIG_DIR, "config.json");
|
|
491
497
|
}
|
|
492
498
|
});
|
|
493
499
|
|
|
@@ -628,22 +634,41 @@ var init_cf_api = __esm({
|
|
|
628
634
|
* 读取 KV 里的一个键值(不存在时返回 null)。
|
|
629
635
|
*
|
|
630
636
|
* 用于部署前检测「KV 已有配置会覆盖环境变量」的情况。
|
|
637
|
+
*
|
|
638
|
+
* ## ⚠️ 「读不到」与「不存在」必须区分
|
|
639
|
+
*
|
|
640
|
+
* 之前这里对 **403 / 500 / 网络异常**统统返回 `null`,
|
|
641
|
+
* 与「键不存在」混为一谈。
|
|
642
|
+
*
|
|
643
|
+
* 后果:KV 归属判定(`inspectKvOwnership`)里
|
|
644
|
+
* 「清单存在」「小写 config 存在」两个判据在
|
|
645
|
+
* 「KV 属于别的账号」「Token 没权限」「网络断了」时**全为 false**
|
|
646
|
+
* → verdict 落到 `empty`(**不阻断**)
|
|
647
|
+
* → 于是会去建项目、写 KV,产出一堆孤儿项目与半成品实例。
|
|
648
|
+
*
|
|
649
|
+
* 现在只有 **404(键不存在)** 才返回 null,
|
|
650
|
+
* 其它错误一律抛出,由调用方按「无法确认 → 不可放行」处理。
|
|
631
651
|
*/
|
|
632
652
|
async getKvValue(accountId, namespaceId, key) {
|
|
653
|
+
let response;
|
|
633
654
|
try {
|
|
634
|
-
|
|
655
|
+
response = await fetch(
|
|
635
656
|
`${this.apiBase}/accounts/${accountId}/storage/kv/namespaces/${namespaceId}/values/${encodeURIComponent(key)}`,
|
|
636
657
|
{
|
|
637
658
|
headers: { Authorization: `Bearer ${this.token}` },
|
|
638
659
|
signal: AbortSignal.timeout(this.timeoutMs)
|
|
639
660
|
}
|
|
640
661
|
);
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
662
|
+
} catch (err) {
|
|
663
|
+
throw new Error(
|
|
664
|
+
`\u8BFB\u53D6 KV \u5931\u8D25\uFF08\u65E0\u6CD5\u786E\u8BA4\u5F52\u5C5E\uFF09\uFF1A${err instanceof Error ? err.message : String(err)}`
|
|
665
|
+
);
|
|
666
|
+
}
|
|
667
|
+
if (response.status === 404) return null;
|
|
668
|
+
if (!response.ok) {
|
|
669
|
+
throw new Error(`\u8BFB\u53D6 KV \u5931\u8D25\uFF08\u65E0\u6CD5\u786E\u8BA4\u5F52\u5C5E\uFF09\uFF1AHTTP ${response.status}`);
|
|
646
670
|
}
|
|
671
|
+
return await response.text();
|
|
647
672
|
}
|
|
648
673
|
/**
|
|
649
674
|
* 写入 KV 键值。
|
|
@@ -1088,7 +1113,7 @@ function renderTokenCheck(result) {
|
|
|
1088
1113
|
}
|
|
1089
1114
|
lines.push("");
|
|
1090
1115
|
lines.push("\u8BF4\u660E\uFF1A\u5217\u8868\u7C7B\u63A5\u53E3\u901A\u8FC7\u53EA\u4EE3\u8868\u5177\u5907\u8BFB\u53D6\u6743\u9650\uFF1B\u521B\u5EFA / \u4FEE\u6539\u7C7B\u64CD\u4F5C\u4ECD\u7136\u9700\u8981 Edit \u6743\u9650\u3002");
|
|
1091
|
-
lines.push("\u63D0\u793A\
|
|
1116
|
+
lines.push("\u63D0\u793A\uFF1AZone / DNS / Routes \u6743\u9650\u53EA\u5F71\u54CD**\u8DE8\u8D26\u53F7**\u81EA\u5B9A\u4E49\u57DF\u540D\uFF1B\u540C\u8D26\u53F7\u57DF\u540D\u7531 Cloudflare \u81EA\u52A8\u5EFA DNS \u8BB0\u5F55\u3002");
|
|
1092
1117
|
return `${lines.join("\n")}
|
|
1093
1118
|
`;
|
|
1094
1119
|
}
|
|
@@ -1184,7 +1209,7 @@ async function checkTokenPermissions(options) {
|
|
|
1184
1209
|
let zoneRow = makeRow(
|
|
1185
1210
|
"\u57DF\u540D\u5217\u8868\uFF08Zone\uFF09",
|
|
1186
1211
|
expectCustomDomain,
|
|
1187
|
-
expectCustomDomain ? "\u65E0\u6CD5\u9009\u62E9\u57DF\u540D\u3001\u65E0\u6CD5\u7ED1\u5B9A\u81EA\u5B9A\u4E49\u57DF\u540D\uFF0C\u53EA\u80FD\u4F7F\u7528 workers.dev \u5730\u5740" : "\u81EA\u5B9A\u4E49\u57DF\u540D\
|
|
1212
|
+
expectCustomDomain ? "\u65E0\u6CD5\u9009\u62E9\u57DF\u540D\u3001\u65E0\u6CD5\u7ED1\u5B9A\u81EA\u5B9A\u4E49\u57DF\u540D\uFF0C\u53EA\u80FD\u4F7F\u7528 workers.dev \u5730\u5740" : "\u65E0\u6CD5\u6258\u7BA1\u81EA\u5B9A\u4E49\u57DF\u540D\uFF08Pages \u9ED8\u8BA4\u7684 *.pages.dev \u57DF\u540D\u4E0D\u53D7\u5F71\u54CD\uFF09",
|
|
1188
1213
|
"Token \u4E2D\u6DFB\u52A0 Zone / Zone / Read",
|
|
1189
1214
|
zoneProbe
|
|
1190
1215
|
);
|
|
@@ -1205,7 +1230,7 @@ async function checkTokenPermissions(options) {
|
|
|
1205
1230
|
"Workers KV Storage",
|
|
1206
1231
|
true,
|
|
1207
1232
|
"\u65E0\u6CD5\u521B\u5EFA / \u7ED1\u5B9A KV \u547D\u540D\u7A7A\u95F4\uFF0C\u90E8\u7F72\u4F1A\u4E2D\u65AD",
|
|
1208
|
-
"Token \u4E2D\u6DFB\u52A0
|
|
1233
|
+
"Token \u4E2D\u6DFB\u52A0 Account / Workers KV Storage / Write",
|
|
1209
1234
|
kvProbe
|
|
1210
1235
|
)
|
|
1211
1236
|
);
|
|
@@ -1216,9 +1241,9 @@ async function checkTokenPermissions(options) {
|
|
|
1216
1241
|
rows.push(
|
|
1217
1242
|
makeRow(
|
|
1218
1243
|
"Workers \u811A\u672C",
|
|
1219
|
-
|
|
1220
|
-
"\
|
|
1221
|
-
"
|
|
1244
|
+
false,
|
|
1245
|
+
"\u4E0D\u5F71\u54CD Pages \u90E8\u7F72\uFF08\u5185\u6838\u8D70 Pages \u90E8\u7F72\u63A5\u53E3\u4E0A\u4F20\uFF0C\u4E0D\u7ECF\u8FC7 Workers \u811A\u672C API\uFF09",
|
|
1246
|
+
"\u4EC5\u5F53\u4F60\u8981\u7528\u5F52\u6863\u7684\u5355\u9879\u76EE Workers \u5F62\u6001\u65F6\u624D\u9700\u8981",
|
|
1222
1247
|
scriptsProbe
|
|
1223
1248
|
)
|
|
1224
1249
|
);
|
|
@@ -1229,9 +1254,9 @@ async function checkTokenPermissions(options) {
|
|
|
1229
1254
|
rows.push(
|
|
1230
1255
|
makeRow(
|
|
1231
1256
|
"workers.dev \u5B50\u57DF\u8BFB\u53D6",
|
|
1232
|
-
|
|
1233
|
-
"\
|
|
1234
|
-
"
|
|
1257
|
+
false,
|
|
1258
|
+
"\u4E0D\u5F71\u54CD Pages \u90E8\u7F72\uFF08Pages \u9879\u76EE\u7528\u81EA\u5DF1\u7684 *.pages.dev \u57DF\u540D\uFF09",
|
|
1259
|
+
"\u4EC5\u5F53\u4F60\u8981\u7528\u5F52\u6863\u7684\u5355\u9879\u76EE Workers \u5F62\u6001\u65F6\u624D\u9700\u8981",
|
|
1235
1260
|
subdomainProbe
|
|
1236
1261
|
)
|
|
1237
1262
|
);
|
|
@@ -1256,9 +1281,9 @@ async function checkTokenPermissions(options) {
|
|
|
1256
1281
|
rows.push(
|
|
1257
1282
|
makeRow(
|
|
1258
1283
|
"workers.dev \u8DEF\u7531\u5F00\u5173",
|
|
1259
|
-
|
|
1260
|
-
"\u90E8\u7F72\
|
|
1261
|
-
"
|
|
1284
|
+
false,
|
|
1285
|
+
"\u4E0D\u5F71\u54CD Pages \u90E8\u7F72\uFF08Pages \u9879\u76EE\u7528\u81EA\u5DF1\u7684 *.pages.dev \u57DF\u540D\uFF09",
|
|
1286
|
+
"\u4EC5\u5F53\u4F60\u8981\u7528\u5F52\u6863\u7684\u5355\u9879\u76EE Workers \u5F62\u6001\u65F6\u624D\u9700\u8981",
|
|
1262
1287
|
subdomainRouteProbe
|
|
1263
1288
|
)
|
|
1264
1289
|
);
|
|
@@ -1271,7 +1296,7 @@ async function checkTokenPermissions(options) {
|
|
|
1271
1296
|
"Pages \u9879\u76EE",
|
|
1272
1297
|
true,
|
|
1273
1298
|
"pages-split \u65E0\u6CD5\u521B\u5EFA\u9879\u76EE\u3001\u914D\u7F6E\u53D8\u91CF\u4E0E\u89E6\u53D1\u90E8\u7F72",
|
|
1274
|
-
"Token \u4E2D\u6DFB\u52A0
|
|
1299
|
+
"Token \u4E2D\u6DFB\u52A0 Account / Pages / Write",
|
|
1275
1300
|
pagesProbe
|
|
1276
1301
|
)
|
|
1277
1302
|
);
|
|
@@ -1282,7 +1307,7 @@ async function checkTokenPermissions(options) {
|
|
|
1282
1307
|
makeRow(
|
|
1283
1308
|
"DNS \u8BB0\u5F55",
|
|
1284
1309
|
expectCustomDomain,
|
|
1285
|
-
"\u65E0\u6CD5\
|
|
1310
|
+
"\u65E0\u6CD5\u81EA\u884C\u521B\u5EFA CNAME \u8BB0\u5F55\uFF08\u8DE8\u8D26\u53F7\u81EA\u5B9A\u4E49\u57DF\u540D\u9700\u624B\u52A8\u52A0\uFF1B\u540C\u8D26\u53F7\u7531 Cloudflare \u81EA\u52A8\u5EFA\uFF09",
|
|
1286
1311
|
"Token \u4E2D\u6DFB\u52A0 Zone / DNS / Edit",
|
|
1287
1312
|
{ status: "unknown", detail }
|
|
1288
1313
|
)
|
|
@@ -1291,7 +1316,7 @@ async function checkTokenPermissions(options) {
|
|
|
1291
1316
|
makeRow(
|
|
1292
1317
|
"Worker Routes",
|
|
1293
1318
|
expectCustomDomain,
|
|
1294
|
-
"\u81EA\u5B9A\u4E49\u57DF\u540D\
|
|
1319
|
+
"Pages \u81EA\u5B9A\u4E49\u57DF\u540D\u65E0\u6CD5\u964D\u7EA7\u5230 Route + CNAME \u65B9\u6848",
|
|
1295
1320
|
"Token \u4E2D\u6DFB\u52A0 Zone / Workers Routes / Edit",
|
|
1296
1321
|
{ status: "unknown", detail }
|
|
1297
1322
|
)
|
|
@@ -1305,7 +1330,7 @@ async function checkTokenPermissions(options) {
|
|
|
1305
1330
|
makeRow(
|
|
1306
1331
|
"DNS \u8BB0\u5F55",
|
|
1307
1332
|
expectCustomDomain,
|
|
1308
|
-
"\u65E0\u6CD5\
|
|
1333
|
+
"\u65E0\u6CD5\u81EA\u884C\u521B\u5EFA CNAME \u8BB0\u5F55\uFF08\u8DE8\u8D26\u53F7\u81EA\u5B9A\u4E49\u57DF\u540D\u9700\u624B\u52A8\u52A0\uFF1B\u540C\u8D26\u53F7\u7531 Cloudflare \u81EA\u52A8\u5EFA\uFF09",
|
|
1309
1334
|
"Token \u4E2D\u6DFB\u52A0 Zone / DNS / Edit",
|
|
1310
1335
|
dnsProbe
|
|
1311
1336
|
)
|
|
@@ -1318,7 +1343,7 @@ async function checkTokenPermissions(options) {
|
|
|
1318
1343
|
makeRow(
|
|
1319
1344
|
"Worker Routes",
|
|
1320
1345
|
expectCustomDomain,
|
|
1321
|
-
"\u81EA\u5B9A\u4E49\u57DF\u540D\
|
|
1346
|
+
"Pages \u81EA\u5B9A\u4E49\u57DF\u540D\u65E0\u6CD5\u964D\u7EA7\u5230 Route + CNAME \u65B9\u6848",
|
|
1322
1347
|
"Token \u4E2D\u6DFB\u52A0 Zone / Workers Routes / Edit",
|
|
1323
1348
|
routesProbe
|
|
1324
1349
|
)
|
|
@@ -1427,6 +1452,59 @@ var init_version_util = __esm({
|
|
|
1427
1452
|
}
|
|
1428
1453
|
});
|
|
1429
1454
|
|
|
1455
|
+
// src/shared/github-mirrors.ts
|
|
1456
|
+
function downloadSources(url) {
|
|
1457
|
+
const sources = [{ name: "GitHub", url }];
|
|
1458
|
+
for (const m of MIRRORS) {
|
|
1459
|
+
sources.push({ name: m.name, url: m.prefix + url });
|
|
1460
|
+
}
|
|
1461
|
+
return sources;
|
|
1462
|
+
}
|
|
1463
|
+
function apiSources(apiUrl) {
|
|
1464
|
+
const sources = [{ name: "GitHub API", url: apiUrl }];
|
|
1465
|
+
for (const m of MIRRORS) {
|
|
1466
|
+
sources.push({ name: `${m.name} API`, url: m.prefix + apiUrl });
|
|
1467
|
+
}
|
|
1468
|
+
return sources;
|
|
1469
|
+
}
|
|
1470
|
+
async function fetchLatestVersionWithoutApi(timeoutMs) {
|
|
1471
|
+
const url = `${RELEASES_WEB}/latest`;
|
|
1472
|
+
for (const source of downloadSources(url)) {
|
|
1473
|
+
try {
|
|
1474
|
+
const response = await fetch(source.url, {
|
|
1475
|
+
headers: { "User-Agent": "CFPilot-CLI" },
|
|
1476
|
+
redirect: "manual",
|
|
1477
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1478
|
+
});
|
|
1479
|
+
const location = response.headers.get("location") ?? "";
|
|
1480
|
+
const version = versionFromReleaseUrl(location);
|
|
1481
|
+
if (version) return version;
|
|
1482
|
+
} catch {
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
return "";
|
|
1486
|
+
}
|
|
1487
|
+
function versionFromReleaseUrl(url) {
|
|
1488
|
+
if (!url) return "";
|
|
1489
|
+
const m = url.match(/\/releases\/(?:tag|download)\/v?([0-9]+(?:\.[0-9]+)*)/i);
|
|
1490
|
+
return m ? m[1] : "";
|
|
1491
|
+
}
|
|
1492
|
+
var MIRRORS, GITHUB_OWNER, GITHUB_REPO, RELEASES_API, RELEASES_WEB;
|
|
1493
|
+
var init_github_mirrors = __esm({
|
|
1494
|
+
"src/shared/github-mirrors.ts"() {
|
|
1495
|
+
"use strict";
|
|
1496
|
+
MIRRORS = [
|
|
1497
|
+
{ name: "ghfast.top", prefix: "https://ghfast.top/" },
|
|
1498
|
+
{ name: "ghproxy.net", prefix: "https://ghproxy.net/" },
|
|
1499
|
+
{ name: "gh-proxy.com", prefix: "https://gh-proxy.com/" }
|
|
1500
|
+
];
|
|
1501
|
+
GITHUB_OWNER = "xmwengxing";
|
|
1502
|
+
GITHUB_REPO = "CFPilot";
|
|
1503
|
+
RELEASES_API = `https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases`;
|
|
1504
|
+
RELEASES_WEB = `https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/releases`;
|
|
1505
|
+
}
|
|
1506
|
+
});
|
|
1507
|
+
|
|
1430
1508
|
// src/deploy/kernel-remote.ts
|
|
1431
1509
|
import { mkdir, readFile, rename, writeFile } from "node:fs/promises";
|
|
1432
1510
|
import { homedir as homedir2 } from "node:os";
|
|
@@ -1444,20 +1522,8 @@ function versionFromTag(tag) {
|
|
|
1444
1522
|
}
|
|
1445
1523
|
async function fetchRemoteKernelInfo(options = {}) {
|
|
1446
1524
|
if (isRemoteDisabled()) return null;
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
const response = await fetch(`${RELEASES_API}?per_page=20`, {
|
|
1450
|
-
headers: {
|
|
1451
|
-
Accept: "application/vnd.github+json",
|
|
1452
|
-
"User-Agent": "CFPilot-CLI"
|
|
1453
|
-
},
|
|
1454
|
-
signal: AbortSignal.timeout(options.timeoutMs ?? DEFAULT_TIMEOUT_MS)
|
|
1455
|
-
});
|
|
1456
|
-
if (!response.ok) return null;
|
|
1457
|
-
const payload = await response.json();
|
|
1458
|
-
if (!Array.isArray(payload)) return null;
|
|
1459
|
-
releases = payload.filter(isGitHubRelease);
|
|
1460
|
-
} catch {
|
|
1525
|
+
const releases = await fetchReleasesWithFallback(options.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
1526
|
+
if (releases.length === 0) {
|
|
1461
1527
|
return null;
|
|
1462
1528
|
}
|
|
1463
1529
|
const sorted = releases.filter((release) => !release.draft && !release.prerelease).map((release) => ({ release, version: versionFromTag(release.tag_name) })).filter((item) => item.version !== "").sort((a, b) => compareSemver(b.version, a.version));
|
|
@@ -1471,6 +1537,40 @@ async function fetchRemoteKernelInfo(options = {}) {
|
|
|
1471
1537
|
}
|
|
1472
1538
|
return null;
|
|
1473
1539
|
}
|
|
1540
|
+
async function fetchReleasesWithFallback(timeoutMs) {
|
|
1541
|
+
const apiUrl = `${RELEASES_API2}?per_page=20`;
|
|
1542
|
+
for (const source of apiSources(apiUrl)) {
|
|
1543
|
+
try {
|
|
1544
|
+
const response = await fetch(source.url, {
|
|
1545
|
+
headers: {
|
|
1546
|
+
Accept: "application/vnd.github+json",
|
|
1547
|
+
"User-Agent": "CFPilot-CLI"
|
|
1548
|
+
},
|
|
1549
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1550
|
+
});
|
|
1551
|
+
if (!response.ok) continue;
|
|
1552
|
+
const payload = await response.json();
|
|
1553
|
+
if (!Array.isArray(payload)) continue;
|
|
1554
|
+
const releases = payload.filter(isGitHubRelease);
|
|
1555
|
+
if (releases.length > 0) return releases;
|
|
1556
|
+
} catch {
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
const version = await fetchLatestVersionWithoutApi(timeoutMs);
|
|
1560
|
+
if (!version) return [];
|
|
1561
|
+
const controlUrl = `${RELEASES_WEB}/latest/download/${CONTROL_ASSET_NAME}`;
|
|
1562
|
+
const dataUrl = `${RELEASES_WEB}/latest/download/${DATA_ASSET_NAME}`;
|
|
1563
|
+
const pseudo = {
|
|
1564
|
+
tag_name: `v${version}`,
|
|
1565
|
+
draft: false,
|
|
1566
|
+
prerelease: false,
|
|
1567
|
+
assets: [
|
|
1568
|
+
{ name: CONTROL_ASSET_NAME, browser_download_url: controlUrl },
|
|
1569
|
+
{ name: DATA_ASSET_NAME, browser_download_url: dataUrl }
|
|
1570
|
+
]
|
|
1571
|
+
};
|
|
1572
|
+
return [pseudo];
|
|
1573
|
+
}
|
|
1474
1574
|
function findAssetUrl(assets, name) {
|
|
1475
1575
|
for (const asset of assets) {
|
|
1476
1576
|
if (asset.name.trim().toLowerCase() === name) return asset.browser_download_url;
|
|
@@ -1491,21 +1591,31 @@ function isGitHubRelease(value) {
|
|
|
1491
1591
|
}
|
|
1492
1592
|
async function downloadAsset(url, timeoutMs) {
|
|
1493
1593
|
if (!url.trim()) throw new Error("\u4E0B\u8F7D\u5730\u5740\u4E3A\u7A7A");
|
|
1494
|
-
const
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1594
|
+
const errors = [];
|
|
1595
|
+
for (const source of downloadSources(url)) {
|
|
1596
|
+
try {
|
|
1597
|
+
const response = await fetch(source.url, {
|
|
1598
|
+
headers: { "User-Agent": "CFPilot-CLI" },
|
|
1599
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
1600
|
+
});
|
|
1601
|
+
if (!response.ok) {
|
|
1602
|
+
errors.push(`${source.name}: HTTP ${response.status}`);
|
|
1603
|
+
continue;
|
|
1604
|
+
}
|
|
1605
|
+
const declared = response.headers.get("content-length");
|
|
1606
|
+
if (declared) {
|
|
1607
|
+
const size = Number(declared);
|
|
1608
|
+
if (Number.isFinite(size) && size > MAX_ASSET_BYTES) {
|
|
1609
|
+
errors.push(`${source.name}: \u8D85\u8FC7 ${MAX_ASSET_BYTES} \u5B57\u8282\u4E0A\u9650`);
|
|
1610
|
+
continue;
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
return await readBodyWithLimit(response);
|
|
1614
|
+
} catch (err) {
|
|
1615
|
+
errors.push(`${source.name}: ${err instanceof Error ? err.message : String(err)}`);
|
|
1506
1616
|
}
|
|
1507
1617
|
}
|
|
1508
|
-
|
|
1618
|
+
throw new Error(`\u6240\u6709\u4E0B\u8F7D\u6E90\u5747\u5931\u8D25 \u2014\u2014 ${errors.join("\uFF1B")}`);
|
|
1509
1619
|
}
|
|
1510
1620
|
async function readBodyWithLimit(response) {
|
|
1511
1621
|
const body = response.body;
|
|
@@ -1646,15 +1756,16 @@ async function fetchRemoteKernel(info, timeoutMs) {
|
|
|
1646
1756
|
await writeCached(dataPath, data).catch(() => void 0);
|
|
1647
1757
|
return { control, data };
|
|
1648
1758
|
}
|
|
1649
|
-
var CONTROL_ASSET_NAME, DATA_ASSET_NAME, REPO,
|
|
1759
|
+
var CONTROL_ASSET_NAME, DATA_ASSET_NAME, REPO, RELEASES_API2, DEFAULT_TIMEOUT_MS, MAX_ASSET_BYTES, CACHE_DIR_ENV, LOCAL_DIR_ENV, REMOTE_OFF_ENV, BUILD_RE, BUILD_NUMERIC_RE;
|
|
1650
1760
|
var init_kernel_remote = __esm({
|
|
1651
1761
|
"src/deploy/kernel-remote.ts"() {
|
|
1652
1762
|
"use strict";
|
|
1653
1763
|
init_version_util();
|
|
1764
|
+
init_github_mirrors();
|
|
1654
1765
|
CONTROL_ASSET_NAME = "cfpilot-kernel-control.js";
|
|
1655
1766
|
DATA_ASSET_NAME = "cfpilot-kernel-dataplane.js";
|
|
1656
1767
|
REPO = "xmwengxing/CFPilot";
|
|
1657
|
-
|
|
1768
|
+
RELEASES_API2 = process.env.CFPILOT_KERNEL_API_BASE ?? `https://api.github.com/repos/${REPO}/releases`;
|
|
1658
1769
|
DEFAULT_TIMEOUT_MS = 2e4;
|
|
1659
1770
|
MAX_ASSET_BYTES = 10 * 1024 * 1024;
|
|
1660
1771
|
CACHE_DIR_ENV = "CFPILOT_KERNEL_CACHE_DIR";
|
|
@@ -1702,7 +1813,22 @@ async function writeManifest(client, accountId, namespaceId, manifest) {
|
|
|
1702
1813
|
}
|
|
1703
1814
|
}
|
|
1704
1815
|
async function inspectKvOwnership(client, accountId, namespaceId, controlProject) {
|
|
1705
|
-
|
|
1816
|
+
let manifest;
|
|
1817
|
+
let legacyConfig;
|
|
1818
|
+
let cfpilotConfig;
|
|
1819
|
+
try {
|
|
1820
|
+
manifest = await readManifest(client, accountId, namespaceId);
|
|
1821
|
+
legacyConfig = await client.getKvValue(accountId, namespaceId, "config");
|
|
1822
|
+
cfpilotConfig = await client.getKvValue(accountId, namespaceId, "CONFIG");
|
|
1823
|
+
} catch (err) {
|
|
1824
|
+
const reason = err instanceof Error ? err.message : String(err);
|
|
1825
|
+
return {
|
|
1826
|
+
verdict: "occupied",
|
|
1827
|
+
manifest: null,
|
|
1828
|
+
detail: `\u65E0\u6CD5\u786E\u8BA4 KV \u5F52\u5C5E\uFF08${reason}\uFF09\u2014\u2014 \u8BF7\u68C0\u67E5 Token \u6743\u9650\u4E0E\u7F51\u7EDC\u540E\u91CD\u8BD5`,
|
|
1829
|
+
blocking: true
|
|
1830
|
+
};
|
|
1831
|
+
}
|
|
1706
1832
|
if (manifest) {
|
|
1707
1833
|
if (manifest.controlProject === controlProject) {
|
|
1708
1834
|
return {
|
|
@@ -1719,7 +1845,6 @@ async function inspectKvOwnership(client, accountId, namespaceId, controlProject
|
|
|
1719
1845
|
blocking: true
|
|
1720
1846
|
};
|
|
1721
1847
|
}
|
|
1722
|
-
const legacyConfig = await client.getKvValue(accountId, namespaceId, "config");
|
|
1723
1848
|
if (legacyConfig !== null) {
|
|
1724
1849
|
return {
|
|
1725
1850
|
verdict: "foreign",
|
|
@@ -1731,7 +1856,6 @@ async function inspectKvOwnership(client, accountId, namespaceId, controlProject
|
|
|
1731
1856
|
blocking: true
|
|
1732
1857
|
};
|
|
1733
1858
|
}
|
|
1734
|
-
const cfpilotConfig = await client.getKvValue(accountId, namespaceId, "CONFIG");
|
|
1735
1859
|
if (cfpilotConfig !== null) {
|
|
1736
1860
|
return {
|
|
1737
1861
|
verdict: "stale",
|
|
@@ -2441,9 +2565,32 @@ async function runDeployCommand(args) {
|
|
|
2441
2565
|
process.exitCode = 1;
|
|
2442
2566
|
return;
|
|
2443
2567
|
}
|
|
2444
|
-
|
|
2445
|
-
|
|
2568
|
+
if (kvId) {
|
|
2569
|
+
let found = "";
|
|
2570
|
+
for (const acc2 of accounts) {
|
|
2571
|
+
const namespaces = await client.listKvNamespaces(acc2.id);
|
|
2572
|
+
if (namespaces.some((ns) => ns.id === kvId)) {
|
|
2573
|
+
found = acc2.id;
|
|
2574
|
+
break;
|
|
2575
|
+
}
|
|
2576
|
+
}
|
|
2577
|
+
if (!found) {
|
|
2578
|
+
process.stderr.write(
|
|
2579
|
+
`\u2717 KV ${kvId} \u4E0D\u5C5E\u4E8E\u8BE5 Token \u7684\u4EFB\u4F55\u8D26\u53F7\uFF08${accounts.map((a) => a.name).join("\u3001")}\uFF09\u2014\u2014 \u8BF7\u7528 --account \u6307\u5B9A\u6B63\u786E\u7684\u8D26\u53F7
|
|
2580
|
+
`
|
|
2581
|
+
);
|
|
2582
|
+
process.exitCode = 1;
|
|
2583
|
+
return;
|
|
2584
|
+
}
|
|
2585
|
+
resolvedAccount = found;
|
|
2586
|
+
const acc = accounts.find((a) => a.id === found);
|
|
2587
|
+
process.stdout.write(`\u4F7F\u7528\u8D26\u53F7\uFF1A${acc.name}\uFF08${acc.id}\uFF0C\u7531 --kv \u53CD\u67E5\uFF09
|
|
2588
|
+
`);
|
|
2589
|
+
} else {
|
|
2590
|
+
resolvedAccount = accounts[0].id;
|
|
2591
|
+
process.stdout.write(`\u4F7F\u7528\u8D26\u53F7\uFF1A${accounts[0].name}\uFF08${accounts[0].id}\uFF09
|
|
2446
2592
|
`);
|
|
2593
|
+
}
|
|
2447
2594
|
}
|
|
2448
2595
|
if (flagBool(args, "list-kv")) {
|
|
2449
2596
|
await listKvInstances(client, resolvedAccount);
|
|
@@ -2625,6 +2772,7 @@ async function menuDeploy(mode) {
|
|
|
2625
2772
|
if (mode === "fresh") flags["new-kv"] = true;
|
|
2626
2773
|
if (panelPassword) flags["panel-password"] = panelPassword;
|
|
2627
2774
|
if (cfg.kvNamespaceId && mode === "update") flags["kv"] = cfg.kvNamespaceId;
|
|
2775
|
+
if (cfg.accountId) flags["account"] = cfg.accountId;
|
|
2628
2776
|
const { runDeployCommand: runDeployCommand2 } = await Promise.resolve().then(() => (init_command(), command_exports));
|
|
2629
2777
|
await runDeployCommand2(makeArgs(flags));
|
|
2630
2778
|
}
|
|
@@ -3555,6 +3703,24 @@ async function pool(items, concurrency, worker, shouldStop) {
|
|
|
3555
3703
|
await Promise.all(runners);
|
|
3556
3704
|
return out;
|
|
3557
3705
|
}
|
|
3706
|
+
async function poolAdaptive(items, concurrency, worker, batchSize = 200) {
|
|
3707
|
+
const out = [];
|
|
3708
|
+
const maxConcurrency = Math.max(1, concurrency);
|
|
3709
|
+
let current = maxConcurrency;
|
|
3710
|
+
for (let start = 0; start < items.length; start += batchSize) {
|
|
3711
|
+
const batch = items.slice(start, start + batchSize);
|
|
3712
|
+
const results = await pool(batch, current, worker);
|
|
3713
|
+
out.push(...results);
|
|
3714
|
+
const okCount = results.reduce((n, r) => n + (r.ok ? 1 : 0), 0);
|
|
3715
|
+
const rate = results.length > 0 ? okCount / results.length : 1;
|
|
3716
|
+
if (rate < 0.4) {
|
|
3717
|
+
current = Math.max(8, Math.floor(current / 2));
|
|
3718
|
+
} else if (rate > 0.85) {
|
|
3719
|
+
current = Math.min(maxConcurrency, current * 2);
|
|
3720
|
+
}
|
|
3721
|
+
}
|
|
3722
|
+
return out;
|
|
3723
|
+
}
|
|
3558
3724
|
function isTimeoutError(error) {
|
|
3559
3725
|
if (!error) return false;
|
|
3560
3726
|
return /超时|timeout|timed out|ETIMEDOUT/i.test(error);
|
|
@@ -3584,7 +3750,7 @@ async function runScan(options) {
|
|
|
3584
3750
|
const speedConcurrency = options.speedConcurrency ?? 4;
|
|
3585
3751
|
const speedTimeoutMs = options.speedTimeoutMs ?? 12e3;
|
|
3586
3752
|
const speedBytes = options.speedBytes ?? 5 * 1024 * 1024;
|
|
3587
|
-
const top = options.top ??
|
|
3753
|
+
const top = options.top ?? 8;
|
|
3588
3754
|
const countryFilter = normalizeCountryFilter(options.countries ?? []);
|
|
3589
3755
|
const hasCountryFilter = countryFilter.length > 0;
|
|
3590
3756
|
const effectiveProbeLimit = hasCountryFilter ? Math.max(probeLimit * 4, 500) : probeLimit;
|
|
@@ -3593,7 +3759,7 @@ async function runScan(options) {
|
|
|
3593
3759
|
});
|
|
3594
3760
|
report({ type: "stage", stage: 1, name: "TCP \u63E1\u624B\u521D\u7B5B", total: candidates.length });
|
|
3595
3761
|
let done = 0;
|
|
3596
|
-
const tcpResults = await
|
|
3762
|
+
const tcpResults = await poolAdaptive(candidates, concurrency, async (ip) => {
|
|
3597
3763
|
const result = await withRetry(retries, () => tcpPing(ip, options.port, tcpTimeoutMs));
|
|
3598
3764
|
done++;
|
|
3599
3765
|
if (done % 500 === 0 || done === candidates.length) {
|
|
@@ -3613,7 +3779,7 @@ async function runScan(options) {
|
|
|
3613
3779
|
const tlsInput = filtered.slice().sort((a, b) => a.latency - b.latency).slice(0, tlsCandidateLimit);
|
|
3614
3780
|
report({ type: "stage", stage: 1, name: "TLS \u63E1\u624B\u7B5B\u9009", total: tlsInput.length });
|
|
3615
3781
|
let tlsDone = 0;
|
|
3616
|
-
const tlsResults = await
|
|
3782
|
+
const tlsResults = await poolAdaptive(tlsInput, tlsConcurrency, async (item) => {
|
|
3617
3783
|
const probe = await withRetry(
|
|
3618
3784
|
retries,
|
|
3619
3785
|
() => probeTlsHandshake(item.ip, options.port, options.host, tlsTimeoutMs),
|
|
@@ -4741,7 +4907,7 @@ async function handleRequest(req, res) {
|
|
|
4741
4907
|
candidates: plan.candidates,
|
|
4742
4908
|
// 国家过滤(可多选):不传 = 全量;无效代码在 normalizeCountryFilter 里被丢弃
|
|
4743
4909
|
countries: Array.isArray(body.countries) ? body.countries : void 0,
|
|
4744
|
-
top: body.top ??
|
|
4910
|
+
top: body.top ?? 8,
|
|
4745
4911
|
// ⚠️ 必须透传这些参数,否则桥接服务永远用默认值:
|
|
4746
4912
|
// wsPath / probePath —— 对接第三方项目(路径不是 /__edge_probe)时,
|
|
4747
4913
|
// 阶段二会硬连默认探针路径 → 全军覆没
|
|
@@ -4882,7 +5048,7 @@ scan \u9009\u9879:
|
|
|
4882
5048
|
\uFF08\u9ED8\u8BA4\u6821\u9A8C\uFF1A\u53D1 PING \u5E76\u9A8C\u8BC1\u6570\u636E\u9762\u56DE {"pong":true}\uFF09
|
|
4883
5049
|
\u76EE\u6807\u6CA1\u6709\u63A2\u9488\u7AEF\u70B9\u65F6\u4F1A\u81EA\u52A8\u964D\u7EA7\u4E3A\u7EAF 101 \u6821\u9A8C
|
|
4884
5050
|
--no-speed \u8DF3\u8FC7\u6D4B\u901F\uFF0C\u53EA\u6309\u5EF6\u8FDF\u6392\u5E8F
|
|
4885
|
-
--top <n> \u8FD4\u56DE\u6761\u6570\uFF0C\u9ED8\u8BA4
|
|
5051
|
+
--top <n> \u8FD4\u56DE\u6761\u6570\uFF0C\u9ED8\u8BA4 8
|
|
4886
5052
|
--concurrency <n> \u9636\u6BB5\u4E00 TCP \u5E76\u53D1\uFF0C\u9ED8\u8BA4 300
|
|
4887
5053
|
--tls-concurrency <n> \u9636\u6BB5\u4E00 TLS \u63E1\u624B\u5E76\u53D1\uFF0C\u9ED8\u8BA4 64
|
|
4888
5054
|
TLS \u63E1\u624B\u662F CPU \u5BC6\u96C6\u578B\u64CD\u4F5C\uFF0C\u8FC7\u9AD8\u4F1A\u8BA9\u672C\u673A\u6210\u4E3A\u74F6\u9888
|
|
@@ -5059,7 +5225,7 @@ async function runScanCommand(args) {
|
|
|
5059
5225
|
process.exit(1);
|
|
5060
5226
|
}
|
|
5061
5227
|
const port = flagNumber(args, "port", 443);
|
|
5062
|
-
const top = flagNumber(args, "top",
|
|
5228
|
+
const top = flagNumber(args, "top", 8);
|
|
5063
5229
|
let token = flagString(args, "token");
|
|
5064
5230
|
const noCallback = flagBool(args, "no-callback");
|
|
5065
5231
|
const askCallback = flagBool(args, "ask-callback");
|
|
@@ -5493,6 +5659,14 @@ __export(config_exports, {
|
|
|
5493
5659
|
syncAccount: () => syncAccount
|
|
5494
5660
|
});
|
|
5495
5661
|
async function syncFromCloud(token) {
|
|
5662
|
+
saveConfig({
|
|
5663
|
+
accountId: "",
|
|
5664
|
+
accountName: "",
|
|
5665
|
+
kvNamespaceId: "",
|
|
5666
|
+
panelHost: "",
|
|
5667
|
+
dataHost: "",
|
|
5668
|
+
wsPath: ""
|
|
5669
|
+
});
|
|
5496
5670
|
try {
|
|
5497
5671
|
const client = new CloudflareClient({ token });
|
|
5498
5672
|
const accounts = await client.listAccounts();
|
|
@@ -5512,6 +5686,12 @@ async function syncFromCloud(token) {
|
|
|
5512
5686
|
status("ok", `\u8D26\u53F7\uFF1A${account.name}`);
|
|
5513
5687
|
const namespaces = await client.listKvNamespaces(account.id);
|
|
5514
5688
|
if (namespaces.length === 0) {
|
|
5689
|
+
saveConfig({
|
|
5690
|
+
kvNamespaceId: "",
|
|
5691
|
+
panelHost: "",
|
|
5692
|
+
dataHost: "",
|
|
5693
|
+
wsPath: ""
|
|
5694
|
+
});
|
|
5515
5695
|
status("info", "\u8BE5\u8D26\u53F7\u4E0B\u8FD8\u6CA1\u6709 KV\uFF08\u9996\u6B21\u90E8\u7F72\u65F6\u4F1A\u81EA\u52A8\u521B\u5EFA\uFF09");
|
|
5516
5696
|
return true;
|
|
5517
5697
|
}
|
|
@@ -5532,7 +5712,8 @@ async function syncFromCloud(token) {
|
|
|
5532
5712
|
saveConfig({
|
|
5533
5713
|
kvNamespaceId: chosen.id,
|
|
5534
5714
|
panelHost: chosen.manifest.controlSubdomain,
|
|
5535
|
-
dataHost: chosen.manifest.dataSubdomain
|
|
5715
|
+
dataHost: chosen.manifest.dataSubdomain,
|
|
5716
|
+
wsPath: chosen.manifest.credentials?.wsPath ?? ""
|
|
5536
5717
|
});
|
|
5537
5718
|
status("ok", `\u5B9E\u4F8B\uFF1A${chosen.manifest.controlProject} / ${chosen.manifest.dataProject}`);
|
|
5538
5719
|
status("info", `\u63A7\u5236\u9762\uFF1A${chosen.manifest.controlSubdomain}`);
|
|
@@ -5540,7 +5721,12 @@ async function syncFromCloud(token) {
|
|
|
5540
5721
|
return true;
|
|
5541
5722
|
}
|
|
5542
5723
|
if (namespaces.length === 1) {
|
|
5543
|
-
saveConfig({
|
|
5724
|
+
saveConfig({
|
|
5725
|
+
kvNamespaceId: namespaces[0].id,
|
|
5726
|
+
panelHost: "",
|
|
5727
|
+
dataHost: "",
|
|
5728
|
+
wsPath: ""
|
|
5729
|
+
});
|
|
5544
5730
|
status("info", `KV\uFF1A${namespaces[0].title}\uFF08\u5C1A\u65E0\u90E8\u7F72\uFF09`);
|
|
5545
5731
|
} else {
|
|
5546
5732
|
const picked = await select(
|
|
@@ -5555,7 +5741,12 @@ async function syncFromCloud(token) {
|
|
|
5555
5741
|
]
|
|
5556
5742
|
);
|
|
5557
5743
|
if (picked) {
|
|
5558
|
-
saveConfig({
|
|
5744
|
+
saveConfig({
|
|
5745
|
+
kvNamespaceId: picked,
|
|
5746
|
+
panelHost: "",
|
|
5747
|
+
dataHost: "",
|
|
5748
|
+
wsPath: ""
|
|
5749
|
+
});
|
|
5559
5750
|
status("ok", "KV \u5DF2\u4FDD\u5B58");
|
|
5560
5751
|
}
|
|
5561
5752
|
}
|