@trim21/personal-pi-extensions 0.1.437 → 0.1.439
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trim21/personal-pi-extensions",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.439",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Custom pi coding-agent extensions: bwrap sandbox, workspace guard, opencode edit, and more",
|
|
6
6
|
"keywords": [
|
|
@@ -23,7 +23,6 @@
|
|
|
23
23
|
"format": "prettier --write .",
|
|
24
24
|
"test": "vitest run",
|
|
25
25
|
"test:coverage": "vitest run --coverage",
|
|
26
|
-
"build:holder": "esbuild src/bwrap/holder.ts --format=esm --target=node24 --outfile=src/bwrap/holder.js",
|
|
27
26
|
"prepare": "husky"
|
|
28
27
|
},
|
|
29
28
|
"peerDependencies": {
|
|
@@ -42,7 +41,6 @@
|
|
|
42
41
|
"@types/turndown": "^5.0.6",
|
|
43
42
|
"@typescript/native": "npm:typescript@^7.0.2",
|
|
44
43
|
"@vitest/coverage-v8": "^4.1.10",
|
|
45
|
-
"esbuild": "^0.28.2",
|
|
46
44
|
"eslint": "^10.8.1",
|
|
47
45
|
"eslint-config-prettier": "10.1.8",
|
|
48
46
|
"eslint-plugin-erasable-syntax-only": "0.7.1",
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const FAKEIP_RANGE = "198.18.0.1/16";
|
|
2
|
-
|
|
2
|
+
/** TUN 与 slirp4netns tap0 共用;不对齐时大包会在 slirp NAT 后 PMTU blackhole。 */
|
|
3
|
+
export const TUN_MTU = 1500;
|
|
3
4
|
|
|
4
5
|
export interface MihomoConfigOptions {
|
|
5
6
|
/** 允许直连的条目列表(域名 / IP / CIDR,可带 :port;空列表 = 默认拒绝一切出网)。 */
|
|
@@ -158,6 +159,7 @@ function buildRules(allowlist: readonly string[]): BuiltRules {
|
|
|
158
159
|
*
|
|
159
160
|
* - auto-detect-interface 让 mihomo 出站绑定 slirp4netns 的 tap0,否则它自己的
|
|
160
161
|
* DNS 查询会被 auto_route 送回 TUN 形成环;
|
|
162
|
+
* - TUN mtu 与 slirp4netns `--mtu` 共用 TUN_MTU,避免依赖各自默认值;
|
|
161
163
|
* - allowlist 域名走 fake-ip-filter 真实解析(见 buildRules 注释)。
|
|
162
164
|
*/
|
|
163
165
|
export function generateMihomoConfig(options: MihomoConfigOptions): MihomoConfig {
|
|
@@ -4,7 +4,7 @@ import { tmpdir } from "node:os";
|
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { fileURLToPath } from "node:url";
|
|
6
6
|
|
|
7
|
-
import { generateMihomoConfig } from "./mihomo-config.js";
|
|
7
|
+
import { generateMihomoConfig, TUN_MTU } from "./mihomo-config.js";
|
|
8
8
|
|
|
9
9
|
/** 命令超时错误:name=TimeoutError(对齐标准错误分类),message 保留 timeout:N 格式。 */
|
|
10
10
|
export class TimeoutError extends Error {
|
|
@@ -37,7 +37,7 @@ export interface NetworkStackExecOptions {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
const NAMESERVER_PATTERN = /^\s*nameserver\s+(\S+)/;
|
|
40
|
-
const HOLDER_PATH = fileURLToPath(new URL("holder.
|
|
40
|
+
const HOLDER_PATH = fileURLToPath(new URL("holder.ts", import.meta.url));
|
|
41
41
|
// ip 通常位于 /usr/sbin 或 /sbin,进程默认 PATH 不含它们;node 则依赖宿主完整 PATH
|
|
42
42
|
const SBIN_PATH_SUFFIX = "/usr/local/sbin:/usr/sbin:/sbin";
|
|
43
43
|
|
|
@@ -169,6 +169,7 @@ export async function startNetworkStack(options: NetworkStackOptions): Promise<N
|
|
|
169
169
|
slirp4netnsPath,
|
|
170
170
|
[
|
|
171
171
|
"-c",
|
|
172
|
+
`--mtu=${TUN_MTU}`,
|
|
172
173
|
`--userns-path=/proc/${holderPid}/ns/user`,
|
|
173
174
|
"--netns-type=pid",
|
|
174
175
|
String(holderPid),
|
package/src/bwrap/runtime.ts
CHANGED
|
@@ -226,7 +226,6 @@ class BashOutput {
|
|
|
226
226
|
});
|
|
227
227
|
}
|
|
228
228
|
|
|
229
|
-
// eslint-disable-next-line unicorn/no-nonstandard-builtin-properties -- ES2024 标准(Node 24 原生支持)
|
|
230
229
|
async [Symbol.asyncDispose](): Promise<void> {
|
|
231
230
|
await this.close();
|
|
232
231
|
}
|
|
@@ -394,7 +393,6 @@ export class BwrapRuntime {
|
|
|
394
393
|
// 每次命令现建网络栈(启动约 140ms),作用域结束自动停栈;allowlist 变化即时生效
|
|
395
394
|
const networkStack = local ? undefined : await createNetworkStack(runtime);
|
|
396
395
|
await using _stack = {
|
|
397
|
-
// eslint-disable-next-line unicorn/no-nonstandard-builtin-properties -- ES2024 标准(Node 24 原生支持)
|
|
398
396
|
async [Symbol.asyncDispose]() {
|
|
399
397
|
try {
|
|
400
398
|
await networkStack?.stop();
|
package/src/bwrap/holder.js
DELETED
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { spawn } from "node:child_process";
|
|
2
|
-
import { dirname } from "node:path";
|
|
3
|
-
import { setTimeout as sleep } from "node:timers/promises";
|
|
4
|
-
const args = process.argv.slice(2);
|
|
5
|
-
const configPath = args[0];
|
|
6
|
-
const mihomoPath = args[1];
|
|
7
|
-
if (!configPath || !mihomoPath) {
|
|
8
|
-
process.exit(2);
|
|
9
|
-
}
|
|
10
|
-
function tap0Exists() {
|
|
11
|
-
return new Promise((resolve) => {
|
|
12
|
-
const child = spawn("ip", ["link", "show", "tap0"], { stdio: "ignore" });
|
|
13
|
-
child.once("exit", (code) => resolve(code === 0));
|
|
14
|
-
child.once("error", () => resolve(false));
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
async function waitFor(attempts, delayMs, predicate) {
|
|
18
|
-
for (let i = 0; i < attempts; i++) {
|
|
19
|
-
if (await predicate()) return true;
|
|
20
|
-
await sleep(delayMs);
|
|
21
|
-
}
|
|
22
|
-
return false;
|
|
23
|
-
}
|
|
24
|
-
async function main() {
|
|
25
|
-
await waitFor(100, 100, tap0Exists);
|
|
26
|
-
const mihomo = spawn(mihomoPath, ["-d", dirname(configPath), "-f", configPath], {
|
|
27
|
-
stdio: ["ignore", "inherit", "inherit"]
|
|
28
|
-
});
|
|
29
|
-
const stop = () => {
|
|
30
|
-
mihomo.kill("SIGTERM");
|
|
31
|
-
};
|
|
32
|
-
process.on("SIGTERM", () => {
|
|
33
|
-
stop();
|
|
34
|
-
process.exit(0);
|
|
35
|
-
});
|
|
36
|
-
process.on("SIGINT", () => {
|
|
37
|
-
stop();
|
|
38
|
-
process.exit(0);
|
|
39
|
-
});
|
|
40
|
-
await new Promise((resolve) => mihomo.once("exit", () => resolve()));
|
|
41
|
-
}
|
|
42
|
-
try {
|
|
43
|
-
await main();
|
|
44
|
-
} catch (error) {
|
|
45
|
-
console.error(error);
|
|
46
|
-
process.exit(1);
|
|
47
|
-
}
|