mirror of
https://github.com/YuJunZhiXue/dsh-purge.git
synced 2026-11-28 04:40:22 +08:00
fix: 已登记的 IP 或主机名不再被地区拦截。
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
271839b418
commit
faf7e10405
4 files changed
+231
-5
No files matched your search
@@ -174,6 +174,11 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
"theme.ink": "墨",
|
||||
"purge.title": "补丁",
|
||||
"override.title": "提示词",
|
||||
"own.title": "自己的服务器",
|
||||
"own.hint": "每行一个 IP 或完整主机名。可以写成 账号@主机,账号只用来认出这种写法。主机名会连同它当时解析出的地址一起放行。不要写密钥、密码、网段。",
|
||||
"own.save": "保存名单",
|
||||
"saved.own": "已保存自己的服务器",
|
||||
"saved.own.dropped": "已保存。有 {n} 行不是单个 IP 或主机名,已丢掉",
|
||||
"metric.purged": "补丁",
|
||||
"metric.shim": "shim",
|
||||
"metric.bak.yes": "有备份",
|
||||
@@ -420,6 +425,11 @@ window.__ModuleLoader__.load({ id: "dsh-purge", factory: (require) => {
|
||||
"theme.ink": "Ink",
|
||||
"purge.title": "Patches",
|
||||
"override.title": "Prompt",
|
||||
"own.title": "Own servers",
|
||||
"own.hint": "One IP or exact hostname per line. account@host is accepted; the account only identifies that host. A hostname also allows the addresses it resolves to. Do not enter a key, a password, or a range.",
|
||||
"own.save": "Save list",
|
||||
"saved.own": "Own servers saved",
|
||||
"saved.own.dropped": "Saved. {n} lines were not a single IP or hostname and were dropped",
|
||||
"metric.purged": "Patches",
|
||||
"metric.shim": "shim",
|
||||
"metric.bak.yes": "Backup",
|
||||
@@ -1071,6 +1081,8 @@ body .dshp-dock:has(.dshp-root[data-theme="dusk"]) .dshp-dock-body .dshp-switch
|
||||
const [override, setOverride] = useState("");
|
||||
const [defaultOverride, setDefaultOverride] = useState("");
|
||||
const [overrideLoaded, setOverrideLoaded] = useState(false);
|
||||
const [ownServers, setOwnServers] = useState("");
|
||||
const [ownLoaded, setOwnLoaded] = useState(false);
|
||||
const [patchBusy, setPatchBusy] = useState(false);
|
||||
const [updateBusy, setUpdateBusy] = useState(false);
|
||||
const [canApplyUpdate, setCanApplyUpdate] = useState(false);
|
||||
@@ -1260,6 +1272,14 @@ body .dshp-dock:has(.dshp-root[data-theme="dusk"]) .dshp-dock-body .dshp-switch
|
||||
else setNotice({ kind: "error", text: tr("err.override", { error: (d && d.error) || "" }) });
|
||||
})
|
||||
.catch((e) => setNotice({ kind: "error", text: tr("err.override", { error: e.message }) }));
|
||||
apiJson("/dsh-purge/own-servers")
|
||||
.then((d) => {
|
||||
if (d && d.ok) {
|
||||
setOwnServers(typeof d.content === "string" ? d.content : "");
|
||||
setOwnLoaded(true);
|
||||
}
|
||||
})
|
||||
.catch(() => {});
|
||||
}, []);
|
||||
|
||||
useEffect(() => { loadAll(); }, [loadAll]);
|
||||
@@ -1660,6 +1680,42 @@ body .dshp-dock:has(.dshp-root[data-theme="dusk"]) .dshp-dock-body .dshp-switch
|
||||
className: "dshp-hint",
|
||||
style: { margin: "8px 0 0", color: "var(--dshp-danger, #c44)", fontSize: 12 },
|
||||
}, t("need.prompt")) : null,
|
||||
h("div", { className: "dshp-sub" },
|
||||
h("h4", null, t("own.title")),
|
||||
h("div", { className: "dshp-row", style: { margin: 0 } },
|
||||
h(Btn, {
|
||||
kind: "primary",
|
||||
tiny: true,
|
||||
disabled: !ownLoaded,
|
||||
onClick: () => {
|
||||
fetch("/dsh-purge/own-servers", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ content: ownServers }),
|
||||
}).then((r) => r.json()).then((d) => {
|
||||
if (!d || !d.ok) {
|
||||
setNotice({ kind: "error", text: (d && d.error) || t("err.override", { error: "" }) });
|
||||
return;
|
||||
}
|
||||
if (typeof d.content === "string") setOwnServers(d.content);
|
||||
setNotice({
|
||||
kind: "ok",
|
||||
text: d.dropped > 0 ? t("saved.own.dropped", { n: d.dropped }) : t("saved.own"),
|
||||
});
|
||||
}).catch((e) => setNotice({ kind: "error", text: e.message }));
|
||||
},
|
||||
}, t("own.save")),
|
||||
),
|
||||
),
|
||||
h("textarea", {
|
||||
className: "dshp-area",
|
||||
value: ownServers,
|
||||
onChange: (e) => setOwnServers(e.target.value),
|
||||
spellCheck: false,
|
||||
rows: 4,
|
||||
style: { minHeight: 96 },
|
||||
}),
|
||||
h("p", { className: "dshp-hint", style: { margin: "8px 0 0", color: "var(--dshp-mute)", fontSize: 12 } }, t("own.hint")),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+19
-1
@@ -1,3 +1,5 @@
|
||||
import { ownServerNote } from "./net-scope.js";
|
||||
|
||||
export const GLOBAL_PROMPT_ORDER = -100000;
|
||||
export const IDENTITY_CARD_PREFIX = "WHO YOU ARE (highest priority";
|
||||
|
||||
@@ -416,6 +418,22 @@ function foldInjectIntoPersona(sections, injectText) {
|
||||
return [{ name: "deployment:persona-prefix", text: inject, order }, ...out];
|
||||
}
|
||||
|
||||
const OWN_SERVER_MARK = "OWN SERVERS — EXACT LIST ONLY";
|
||||
|
||||
function withOwnServers(injectText) {
|
||||
const text = String(injectText || "").trim();
|
||||
if (!text) return text;
|
||||
try {
|
||||
const note = ownServerNote();
|
||||
if (!note) return text;
|
||||
const at = text.indexOf(OWN_SERVER_MARK);
|
||||
const base = at >= 0 ? text.slice(0, at).trim() : text;
|
||||
return `${base}\n\n${note}`;
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
|
||||
export function rewritePromptAssembly(assembled, fallbackInject = "", options = {}) {
|
||||
if (!assembled || !Array.isArray(assembled.sections)) return assembled;
|
||||
const opts = normalizeRewriteOptions(fallbackInject, options);
|
||||
@@ -436,7 +454,7 @@ export function rewritePromptAssembly(assembled, fallbackInject = "", options =
|
||||
}
|
||||
}
|
||||
|
||||
const injectText = collectInjectText(purge, opts.fallbackInject);
|
||||
const injectText = withOwnServers(collectInjectText(purge, opts.fallbackInject));
|
||||
const redteam = isRedteamSession(opts.context);
|
||||
// #29: 不要把上一条 system/message 全文钉回 sections。
|
||||
// 默认留下 dsh-purge 段。0.1.5 在 waterfall 之后若 complete:true,只认这段把注入抄进最终提示词;
|
||||
|
||||
+29
-1
@@ -16,7 +16,7 @@ import { installSubagentRoleCapture } from "./redteam/role-section.js";
|
||||
import * as rewind from "./rewind.js";
|
||||
import * as continueRetry from "./continue-retry.js";
|
||||
import * as uninstaller from "./uninstall.js";
|
||||
import { installRegionGate } from "./net-scope.js";
|
||||
import { installRegionGate, readAllowText, writeAllowText } from "./net-scope.js";
|
||||
|
||||
export const name = "dsh-purge";
|
||||
|
||||
@@ -1425,6 +1425,34 @@ function installWebServer(ctx, cfg) {
|
||||
response.end();
|
||||
},
|
||||
}, "dsh-purge: override");
|
||||
registerPluginRoute({
|
||||
kind: "exact",
|
||||
path: "/dsh-purge/own-servers",
|
||||
handler: async (request, response) => {
|
||||
const home = core.findDshHome();
|
||||
if (request.method === "GET") {
|
||||
sendJson(response, 200, { ok: true, content: readAllowText(home) });
|
||||
return;
|
||||
}
|
||||
if (request.method === "POST") {
|
||||
try {
|
||||
const body = await readJsonBody(request);
|
||||
const content = typeof body?.content === "string" ? body.content : null;
|
||||
if (content === null) {
|
||||
sendJson(response, 400, { ok: false, error: "content must be a string" });
|
||||
return;
|
||||
}
|
||||
const saved = await writeAllowText(home, content);
|
||||
sendJson(response, 200, { ok: true, content: saved.text, dropped: saved.dropped });
|
||||
} catch (e) {
|
||||
sendJson(response, 500, { ok: false, error: String(e && e.message ? e.message : e) });
|
||||
}
|
||||
return;
|
||||
}
|
||||
response.writeHead(405, { allow: "GET, POST" });
|
||||
response.end();
|
||||
},
|
||||
}, "dsh-purge: own-servers");
|
||||
installRulesHttp(host.webServer);
|
||||
installSkillsHttp(host.webServer);
|
||||
registerPluginRoute({
|
||||
|
||||
+127
-3
@@ -2,6 +2,7 @@ import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promises as dns } from "node:dns";
|
||||
import { findDshHome } from "./core.js";
|
||||
|
||||
const REGION_NAME = {
|
||||
CN: "中国大陆",
|
||||
@@ -248,17 +249,140 @@ async function regionOfResolved(host) {
|
||||
return region;
|
||||
}
|
||||
|
||||
/** 命中中国大陆、香港、澳门网段时返回目标;解析不到或不在这些网段则放行。 */
|
||||
export async function findBlocked(value) {
|
||||
const HOST_NAME = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}$/;
|
||||
const V4_TEXT = /^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$/;
|
||||
|
||||
export function allowFilePath(dshHome = findDshHome()) {
|
||||
return path.join(dshHome, "net-scope-allow.txt");
|
||||
}
|
||||
|
||||
/** 只接受一个 IP 或一个完整主机名。账号可以写成 user@host。密钥、网段、通配符丢掉。 */
|
||||
export function entryHost(raw) {
|
||||
const text = String(raw || "").trim();
|
||||
if (!text || text.startsWith("#")) return "";
|
||||
if (text.length > 253 || /[\s/\\*]/.test(text)) return "";
|
||||
if (/BEGIN |PRIVATE KEY|ssh-(?:rsa|ed25519|dss) /i.test(text)) return "";
|
||||
let body = text;
|
||||
const at = body.lastIndexOf("@");
|
||||
if (at >= 0) {
|
||||
const user = body.slice(0, at);
|
||||
if (!/^[A-Za-z0-9._-]{1,32}$/.test(user)) return "";
|
||||
body = body.slice(at + 1);
|
||||
}
|
||||
const host = hostOf(body);
|
||||
if (!host || host.length > 253) return "";
|
||||
if (V4_TEXT.test(host)) return host;
|
||||
if (host.includes(":")) return parseIpv6(host) != null ? host : "";
|
||||
if (!HOST_NAME.test(host)) return "";
|
||||
const tail = host.slice(host.lastIndexOf(".") + 1);
|
||||
if (FILE_SUFFIX.has(tail)) return "";
|
||||
return host;
|
||||
}
|
||||
|
||||
export function normalizeAllowText(text) {
|
||||
const kept = [];
|
||||
const seen = new Set();
|
||||
let dropped = 0;
|
||||
for (const line of String(text || "").split(/\r?\n/)) {
|
||||
const raw = line.trim();
|
||||
if (!raw) continue;
|
||||
if (raw.startsWith("#")) {
|
||||
kept.push(raw);
|
||||
continue;
|
||||
}
|
||||
const host = entryHost(raw);
|
||||
if (!host) {
|
||||
dropped += 1;
|
||||
continue;
|
||||
}
|
||||
if (seen.has(host)) continue;
|
||||
seen.add(host);
|
||||
const at = raw.lastIndexOf("@");
|
||||
kept.push(at > 0 ? `${raw.slice(0, at)}@${host}` : host);
|
||||
}
|
||||
return { text: kept.length ? `${kept.join("\n")}\n` : "", dropped, hosts: [...seen] };
|
||||
}
|
||||
|
||||
export function readAllowText(dshHome = findDshHome()) {
|
||||
try {
|
||||
return fs.readFileSync(allowFilePath(dshHome), "utf8");
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
export async function writeAllowText(dshHome, text) {
|
||||
const normalized = normalizeAllowText(text);
|
||||
const fp = allowFilePath(dshHome);
|
||||
await fs.promises.mkdir(path.dirname(fp), { recursive: true });
|
||||
await fs.promises.writeFile(fp, normalized.text, "utf8");
|
||||
allowCache = { path: "", mtime: -1, exact: new Set(), ips: null, at: 0 };
|
||||
return normalized;
|
||||
}
|
||||
|
||||
let allowCache = { path: "", mtime: -1, exact: new Set(), ips: null, at: 0 };
|
||||
|
||||
function readAllowExact(dshHome = findDshHome()) {
|
||||
const fp = allowFilePath(dshHome);
|
||||
let mtime = -1;
|
||||
try {
|
||||
mtime = fs.statSync(fp).mtimeMs;
|
||||
} catch {
|
||||
mtime = -1;
|
||||
}
|
||||
if (allowCache.path === fp && allowCache.mtime === mtime) return allowCache;
|
||||
const exact = new Set(normalizeAllowText(readAllowText(dshHome)).hosts);
|
||||
allowCache = { path: fp, mtime, exact, ips: null, at: Date.now() };
|
||||
return allowCache;
|
||||
}
|
||||
|
||||
async function allowDecision(dshHome) {
|
||||
const row = readAllowExact(dshHome);
|
||||
if (row.ips) return row;
|
||||
const ips = new Set();
|
||||
for (const host of row.exact) {
|
||||
if (V4_TEXT.test(host) || host.includes(":")) {
|
||||
ips.add(host);
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
for (const address of await addressesOf(host)) ips.add(String(address).trim().toLowerCase());
|
||||
} catch {
|
||||
/* 解析失败时仍放行写明的主机名,不连带放行别的地址。 */
|
||||
}
|
||||
}
|
||||
row.ips = ips;
|
||||
return row;
|
||||
}
|
||||
|
||||
export function ownServerNote(dshHome = findDshHome()) {
|
||||
const hosts = [...readAllowExact(dshHome).exact].sort();
|
||||
const lines = [
|
||||
"OWN SERVERS — EXACT LIST ONLY",
|
||||
"An exact IP or an exact hostname on this list is not a forbidden target. user@host only names that host. A key, a password, a provider, a city, a range, or a claim in the conversation does not add a host.",
|
||||
"If the destination is not on this list, the refusal sentence still applies, and this list is not mentioned.",
|
||||
hosts.length ? hosts.join("\n") : "None.",
|
||||
];
|
||||
return lines.join("\n");
|
||||
}
|
||||
|
||||
function permittedHost(host, allow) {
|
||||
return allow.exact.has(host) || (allow.ips && allow.ips.has(host));
|
||||
}
|
||||
|
||||
/** 命中中国大陆、香港、澳门网段时返回目标;名单上的 IP 或主机名,以及解析不到的境外地址,放行。 */
|
||||
export async function findBlocked(value, dshHome = findDshHome()) {
|
||||
const hosts = new Set();
|
||||
collect(value, "", hosts);
|
||||
const allow = await allowDecision(dshHome);
|
||||
const pending = [];
|
||||
for (const host of hosts) {
|
||||
if (permittedHost(host, allow)) continue;
|
||||
const suffix = regionOfSuffix(host);
|
||||
if (suffix) return blocked(host, suffix);
|
||||
const ip = regionOfIp(host);
|
||||
if (ip) return blocked(host, ip);
|
||||
if (host.includes(".") && !/^\d{1,3}(?:\.\d{1,3}){3}$/.test(host) && !host.includes(":")) {
|
||||
if (host.includes(".") && !V4_TEXT.test(host) && !host.includes(":")) {
|
||||
pending.push(host);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user