mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 04:20:22 +08:00
Revert "fix: 发布 1.1.29,更新下载不再把 GitHub 302 当成失败"
This reverts commit 5b1040c65c.
This commit is contained in:
1 parent
5b1040c65c
commit
cf91838538
5 files changed
+43
-172
No files matched your search
@@ -892,7 +892,7 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
const next = { stable: "", beta: "" };
|
||||
for (const id of ["stable", "beta"]) {
|
||||
const list = versions.filter((item) => item.channel === id && keepListedVersion(item, ceiling));
|
||||
const hit = preferListed(list, d && d.localVersion, d && d.pin);
|
||||
const hit = preferListed(list, d && d.localVersion);
|
||||
next[id] = (hit && hit.ref) || "";
|
||||
}
|
||||
setPick(next);
|
||||
@@ -1279,7 +1279,7 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
const ceiling = listedCeiling(versions, localVer || (updateInfo && updateInfo.localVersion));
|
||||
const list = versions.filter((item) => item.channel === id && keepListedVersion(item, ceiling));
|
||||
const selectedRef = pick[id] || "";
|
||||
const hit = list.find((item) => item.ref === selectedRef) || preferListed(list, localVer, updateInfo && updateInfo.pin);
|
||||
const hit = list.find((item) => item.ref === selectedRef) || preferListed(list, localVer);
|
||||
const onLane = channelNow === id;
|
||||
const noBeta = id === "beta" && Array.isArray(updateInfo && updateInfo.versions) && !list.length;
|
||||
const pickedOther = Boolean(hit && selectedRef && !hit.current);
|
||||
@@ -1721,9 +1721,7 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
return listedVersionNewer(ver, ceiling);
|
||||
}
|
||||
|
||||
function preferListed(list, localVer, pin) {
|
||||
const pinned = pin ? (list || []).find((item) => item.ref === pin) : null;
|
||||
if (pinned) return pinned;
|
||||
function preferListed(list, localVer) {
|
||||
const ver = String(localVer || "").replace(/^v/i, "");
|
||||
return (list || []).find((item) => item.current)
|
||||
|| (list || []).find((item) => ver && String(item.version || "").replace(/^v/i, "") === ver)
|
||||
|
||||
+1
-2
@@ -24,8 +24,7 @@ let bootFullQuit = false;
|
||||
const DEFAULTS = {
|
||||
enabled: true,
|
||||
autoApplyOnStart: true,
|
||||
// 默认关闭开机自动更新:曾因 stamp/SHA 误判触发 dsh add --force 无限重启。
|
||||
autoUpdateOnStart: false,
|
||||
autoUpdateOnStart: true,
|
||||
autoRevertOnMissing: false,
|
||||
injectOnce: false,
|
||||
stripMnemon: true,
|
||||
|
||||
+38
-146
@@ -1,8 +1,6 @@
|
||||
import { execFileSync, spawnSync } from "node:child_process";
|
||||
import fs from "node:fs";
|
||||
import { promises as fsp } from "node:fs";
|
||||
import http from "node:http";
|
||||
import https from "node:https";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
@@ -64,34 +62,6 @@ function stampPath() {
|
||||
return path.join(findDshHome(), "dsh-purge", "installed-rev");
|
||||
}
|
||||
|
||||
function autoUpdateGuardPath() {
|
||||
return path.join(findDshHome(), "dsh-purge", "auto-update-guard.json");
|
||||
}
|
||||
|
||||
/** 自动更新冷却:防止 dsh add 触发重启后 stamp/版本未对齐又立刻再更一轮。 */
|
||||
function autoUpdateGuardBlocks(ref) {
|
||||
try {
|
||||
const raw = JSON.parse(fs.readFileSync(autoUpdateGuardPath(), "utf8"));
|
||||
const at = Date.parse(String(raw?.at || "")) || 0;
|
||||
if (!at || Date.now() - at > 15 * 60 * 1000) return false;
|
||||
const target = String(raw?.ref || "");
|
||||
if (target && ref && target !== String(ref)) return false;
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function markAutoUpdateGuard(ref, sha = "") {
|
||||
const fp = autoUpdateGuardPath();
|
||||
fs.mkdirSync(path.dirname(fp), { recursive: true });
|
||||
fs.writeFileSync(fp, `${JSON.stringify({
|
||||
ref: String(ref || ""),
|
||||
sha: String(sha || ""),
|
||||
at: new Date().toISOString(),
|
||||
}, null, 2)}\n`, "utf8");
|
||||
}
|
||||
|
||||
export function normalizeChannel(value) {
|
||||
return String(value || "").toLowerCase() === "beta" ? "beta" : "stable";
|
||||
}
|
||||
@@ -438,98 +408,43 @@ function detectVia() {
|
||||
|
||||
function isAbortError(e) {
|
||||
const msg = String((e && e.message) || e || "");
|
||||
return (e && (e.name === "AbortError" || e.name === "TimeoutError")) || /aborted|abort|超时/i.test(msg);
|
||||
}
|
||||
|
||||
/** github.com/archive 会 302 到 codeload。宿主 fetch 经常把这次跳转原样抛成 http 302,这里直接改写成 codeload。 */
|
||||
function rewriteDownloadUrl(url) {
|
||||
const value = String(url || "");
|
||||
let match = value.match(/^https:\/\/github\.com\/([^/]+\/[^/]+)\/archive\/refs\/(tags|heads)\/(.+)\.(zip|tar\.gz)$/i);
|
||||
if (match) {
|
||||
const kind = match[4].toLowerCase() === "zip" ? "zip" : "tar.gz";
|
||||
return `https://codeload.github.com/${match[1]}/${kind}/refs/${match[2]}/${decodeURIComponent(match[3])}`;
|
||||
}
|
||||
match = value.match(/^https:\/\/github\.com\/([^/]+\/[^/]+)\/archive\/([0-9a-f]{7,40})\.(zip|tar\.gz)$/i);
|
||||
if (match) {
|
||||
const kind = match[3].toLowerCase() === "zip" ? "zip" : "tar.gz";
|
||||
return `https://codeload.github.com/${match[1]}/${kind}/${match[2]}`;
|
||||
}
|
||||
match = value.match(/^https:\/\/api\.github\.com\/repos\/([^/]+\/[^/]+)\/zipball\/(.+)$/i);
|
||||
if (match) return `https://codeload.github.com/${match[1]}/zip/refs/tags/${decodeURIComponent(match[2])}`;
|
||||
return "";
|
||||
}
|
||||
|
||||
function requestBuffer(url, ms, headers, redirectsLeft = 5) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let parsed;
|
||||
try {
|
||||
parsed = new URL(url);
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
return;
|
||||
}
|
||||
const lib = parsed.protocol === "http:" ? http : https;
|
||||
const req = lib.request(parsed, {
|
||||
method: "GET",
|
||||
headers: { "user-agent": "dsh-purge-update", accept: "*/*", ...headers },
|
||||
}, (res) => {
|
||||
const status = res.statusCode || 0;
|
||||
if ([301, 302, 303, 307, 308].includes(status)) {
|
||||
res.resume();
|
||||
if (redirectsLeft <= 0) {
|
||||
reject(new Error("http redirect loop"));
|
||||
return;
|
||||
}
|
||||
const loc = res.headers.location;
|
||||
const next = loc ? new URL(loc, url).href : rewriteDownloadUrl(url);
|
||||
if (!next || next === url) {
|
||||
reject(new Error(`http ${status}`));
|
||||
return;
|
||||
}
|
||||
requestBuffer(next, ms, headers, redirectsLeft - 1).then(resolve, reject);
|
||||
return;
|
||||
}
|
||||
if (status < 200 || status >= 300) {
|
||||
res.resume();
|
||||
const rewritten = rewriteDownloadUrl(url);
|
||||
if (rewritten && rewritten !== url && redirectsLeft > 0) {
|
||||
requestBuffer(rewritten, ms, headers, redirectsLeft - 1).then(resolve, reject);
|
||||
return;
|
||||
}
|
||||
reject(new Error(`http ${status}`));
|
||||
return;
|
||||
}
|
||||
const chunks = [];
|
||||
res.on("data", (chunk) => chunks.push(chunk));
|
||||
res.on("end", () => resolve({ status, body: Buffer.concat(chunks) }));
|
||||
res.on("error", reject);
|
||||
});
|
||||
req.on("error", reject);
|
||||
req.setTimeout(ms, () => {
|
||||
req.destroy(new Error("GitHub 超时"));
|
||||
});
|
||||
req.end();
|
||||
});
|
||||
return (e && (e.name === "AbortError" || e.name === "TimeoutError")) || /aborted|abort/i.test(msg);
|
||||
}
|
||||
|
||||
async function fetchRes(url, ms = 20000, headers = {}) {
|
||||
const started = rewriteDownloadUrl(url) || url;
|
||||
const ctrl = new AbortController();
|
||||
const timer = setTimeout(() => {
|
||||
try {
|
||||
ctrl.abort("timeout");
|
||||
} catch {
|
||||
ctrl.abort();
|
||||
}
|
||||
}, ms);
|
||||
try {
|
||||
const result = await requestBuffer(started, ms, headers);
|
||||
return {
|
||||
ok: true,
|
||||
status: result.status,
|
||||
async text() {
|
||||
return result.body.toString("utf8");
|
||||
},
|
||||
async arrayBuffer() {
|
||||
const copy = Buffer.from(result.body);
|
||||
return copy.buffer.slice(copy.byteOffset, copy.byteOffset + copy.byteLength);
|
||||
},
|
||||
};
|
||||
let current = String(url || "");
|
||||
for (let hop = 0; hop < 6; hop += 1) {
|
||||
const res = await fetch(current, {
|
||||
headers: { "user-agent": "dsh-purge-update", accept: "*/*", ...headers },
|
||||
// 手动跟跳转:部分宿主对 github.com → codeload 跨域 302 不会自动 follow,会直接抛 http 302。
|
||||
redirect: "manual",
|
||||
cache: "no-store",
|
||||
signal: ctrl.signal,
|
||||
});
|
||||
if ([301, 302, 303, 307, 308].includes(res.status)) {
|
||||
const loc = res.headers.get("location");
|
||||
if (!loc) throw new Error(`http ${res.status}`);
|
||||
current = new URL(loc, current).href;
|
||||
continue;
|
||||
}
|
||||
if (!res.ok) throw new Error(`http ${res.status}`);
|
||||
return res;
|
||||
}
|
||||
throw new Error("http redirect loop");
|
||||
} catch (e) {
|
||||
if (isAbortError(e) || /GitHub 超时/.test(String(e && e.message))) throw new Error("GitHub 超时");
|
||||
if (isAbortError(e)) throw new Error("GitHub 超时");
|
||||
throw e;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -626,9 +541,9 @@ export function markCurrent(versions, state, localVer, localSha) {
|
||||
&& String(item.version || "").replace(/^v/i, "") === ver
|
||||
));
|
||||
return rows.map((item) => {
|
||||
const shaHit = Boolean(!pin && localSha && item.sha && sameRev(localSha, item.sha));
|
||||
const shaHit = Boolean(localSha && item.sha && sameRev(localSha, item.sha));
|
||||
const pinHit = Boolean(pin && item.ref === pin);
|
||||
// 钉住某个标签时只标那一条,避免 master 的提交号也显示成「当前」。
|
||||
// 提交号对不上时,只把本通道里和本机版本号相同的那一条标成当前。
|
||||
const headHit = Boolean(
|
||||
!pin
|
||||
&& !shaMatched
|
||||
@@ -640,7 +555,7 @@ export function markCurrent(versions, state, localVer, localSha) {
|
||||
return {
|
||||
...item,
|
||||
shaShort: item.shaShort || (item.sha ? String(item.sha).slice(0, 7) : ""),
|
||||
current: pinHit || shaHit || headHit,
|
||||
current: shaHit || pinHit || headHit,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -798,12 +713,10 @@ function describeLane(id, listed, state, localVer, localRev) {
|
||||
const current = matchesHead || versions.some((item) => item.current && (item.latest || (state.pin && item.ref === state.pin)));
|
||||
let hasUpdate = false;
|
||||
if (onLane && state.pin) {
|
||||
// 钉住某标签时:只看是否已装到该提交。
|
||||
hasUpdate = Boolean(sha && localRev && !sameRev(localRev, sha));
|
||||
} else if (onLane && sha && localRev) {
|
||||
hasUpdate = !sameRev(localRev, sha) || versionIsNewer(version, localVer);
|
||||
} else if (onLane && versionIsNewer(version, localVer)) {
|
||||
// 正常通道:只按版本号升级,避免 zip/dsh 安装后 stamp 未写完就因 SHA 漂移反复 add --force 重启。
|
||||
hasUpdate = true;
|
||||
} else if (onLane && !localVer && sha && localRev && !sameRev(localRev, sha)) {
|
||||
hasUpdate = true;
|
||||
}
|
||||
return {
|
||||
@@ -1040,10 +953,6 @@ export async function applyUpdate(jobs = planUpdates(), options = {}) {
|
||||
const ref = safeUpdateRef(options.ref || currentChannelRef(state) || STABLE_REF);
|
||||
if (!ref) throw new Error("未知版本");
|
||||
const force = options.force !== false;
|
||||
// 先写入目标 sha:dsh plugin add --force 可能当场重启宿主,若 stamp 写在后面会永远对不上 → 开机无限更新重启。
|
||||
const plannedSha = (await remoteHeadSha(ref).catch(() => "")) || "";
|
||||
if (plannedSha) writeStamp(plannedSha);
|
||||
if (options.auto === true) markAutoUpdateGuard(ref, plannedSha);
|
||||
const zipDirs = [];
|
||||
const vias = new Set();
|
||||
for (const job of jobs) {
|
||||
@@ -1074,7 +983,7 @@ export async function applyUpdate(jobs = planUpdates(), options = {}) {
|
||||
await overlayFromZip(zipDirs, ref);
|
||||
vias.add("zip");
|
||||
}
|
||||
const sha = (gitOk() ? gitHead(PLUGIN_ROOT) : "") || plannedSha || (await remoteHeadSha(ref).catch(() => ""));
|
||||
const sha = (gitOk() ? gitHead(PLUGIN_ROOT) : "") || (await remoteHeadSha(ref).catch(() => ""));
|
||||
if (sha) writeStamp(sha);
|
||||
const via = vias.size > 1 ? "mixed" : [...vias][0] || detectVia();
|
||||
const pin = options.pin === false ? "" : (options.pin || state.pin || "");
|
||||
@@ -1194,26 +1103,10 @@ export async function autoUpdateIfNeeded() {
|
||||
try {
|
||||
const check = await checkUpdate();
|
||||
if (check.pinned) return rememberUpdate({ ...check, auto: true, skipped: "pinned" });
|
||||
const lane = check.lanes?.[check.channel] || check.lanes?.stable || {};
|
||||
const remoteVer = String(lane.version || "").replace(/^v/i, "");
|
||||
const localVer = String(check.localVersion || "").replace(/^v/i, "");
|
||||
// 自动更新只跟版本号,不追 master SHA,避免 stamp 未写完时反复 add --force 重启。
|
||||
if (!versionIsNewer(remoteVer, localVer)) {
|
||||
return rememberUpdate({ ...check, auto: true, hasUpdate: false, skipped: "latest" });
|
||||
}
|
||||
if (!check.hasUpdate) return rememberUpdate({ ...check, auto: true, skipped: "latest" });
|
||||
if (check.updateTarget === BETA_REF && readChannelState().channel !== "beta") {
|
||||
return rememberUpdate({ ...check, auto: true, skipped: "beta" });
|
||||
}
|
||||
const target = currentChannelRef();
|
||||
if (autoUpdateGuardBlocks(target)) {
|
||||
return rememberUpdate({
|
||||
...check,
|
||||
auto: true,
|
||||
skipped: "cooldown",
|
||||
error: "刚刚自动更新过,跳过本轮以免重启循环",
|
||||
});
|
||||
}
|
||||
const jobs = planUpdates();
|
||||
const dirtyGit = jobs.filter((j) => j.kind === "git" && gitTrackedDirty(j.dir));
|
||||
const runnable = jobs.filter((j) => !(j.kind === "git" && gitTrackedDirty(j.dir)));
|
||||
@@ -1226,11 +1119,10 @@ export async function autoUpdateIfNeeded() {
|
||||
});
|
||||
}
|
||||
const applied = await applyUpdate(runnable, {
|
||||
ref: target,
|
||||
ref: currentChannelRef(),
|
||||
channel: readChannelState().channel,
|
||||
pin: false,
|
||||
force: false,
|
||||
auto: true,
|
||||
});
|
||||
return rememberUpdate({ ...applied, auto: true });
|
||||
} catch (e) {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dsh-purge",
|
||||
"version": "1.1.29",
|
||||
"version": "1.1.28",
|
||||
"description": "DeepSeek Harness 网络安全红队工具:本机官方 Harness 受控评测与清洗,不同模型可换不同提示词;默认提示词面向国模「小码酱」。求 Star ⭐",
|
||||
"type": "module",
|
||||
"main": "lib/index.js",
|
||||
|
||||
@@ -1,21 +1,3 @@
|
||||
# 1.1.29
|
||||
|
||||
## 中文
|
||||
|
||||
- **修复选版本/重装后无限重启**:`dsh plugin add --force` 会先杀掉宿主,若 `installed-rev` 写在后面,下次启动又判定要更新,形成死循环。
|
||||
- 自动更新改为**只跟版本号**,不再因 master SHA 漂移反复安装;更新前先写 stamp,并加 15 分钟冷却。
|
||||
- 默认关闭 `autoUpdateOnStart`(可在配置里手动打开)。
|
||||
- **修复更新仍报 `http 302`**:下载改用 Node `https`,`github.com/archive` 即使没有 Location 也改写成 `codeload`,不再走宿主 fetch。
|
||||
- **修复切换版本后仍显示旧版本**:钉住的标签只标那一条为当前。
|
||||
|
||||
## English
|
||||
|
||||
- **Fix infinite restart after switching versions / reinstall**: `dsh plugin add --force` can kill the host before `installed-rev` is written, so the next boot thinks an update is still needed.
|
||||
- Auto-update follows **version numbers only** (not floating master SHA); write the stamp before install and add a 15-minute cooldown.
|
||||
- `autoUpdateOnStart` defaults to off (can still be enabled in config).
|
||||
- **Fix update still failing with `http 302`**: download via Node `https`, and rewrite `github.com/archive` to `codeload` even when Location is missing.
|
||||
- **Fix switch still showing the old version**: a pinned tag is the only row marked current.
|
||||
|
||||
# 1.1.28
|
||||
|
||||
## 中文
|
||||
|
||||
Reference in new issue
Block a user