proxylang 0.12.0 → 0.13.1
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/README.md +116 -374
- package/dist/build-B_eJXR4E.js +3619 -0
- package/dist/cli.js +216 -175
- package/dist/index.d.ts +829 -819
- package/dist/index.js +347 -178
- package/package.json +7 -5
- package/dist/profile-build-Cfs0OIt3.js +0 -4008
package/README.md
CHANGED
|
@@ -1,435 +1,177 @@
|
|
|
1
1
|
# proxylang
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
用 TypeScript 维护多台设备的代理配置,生成 Surge、sing-box、Mihomo 和 Stash 原生文件。共享节点与规则只写一次;每台设备明确选择自己的出口、客户端和设置。
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 从一份 Profile 开始
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
## 解决什么问题
|
|
10
|
-
|
|
11
|
-
代理配置越来越长后,难维护的往往是规则之间的关系:内网规则必须先于开发规则,工作与家庭设备共用一部分规则,同一个服务在不同设备上又需要走不同出口。复制配置和拼接 shell 脚本,会让这些关系散落在多个文件里。
|
|
12
|
-
|
|
13
|
-
proxylang 把这些关系直接写进代码:
|
|
14
|
-
|
|
15
|
-
- **组合规则**:把内网、工作、开发等规则拆成模块,按顺序组合;条件支持 `all`、`any`、`not`。
|
|
16
|
-
- **复用策略**:模板声明“需要一个代理出口”,使用时再绑定具体节点或组,同一份规则可以服务不同设备。
|
|
17
|
-
- **表达平台差异**:同一个出口在 Surge 使用 Smart,在 Mihomo 使用 URLTest,由作者显式选择。
|
|
18
|
-
- **提前发现问题**:按精确版本检查能力、引用与循环依赖;用请求案例检查首条命中规则,避免改动悄悄改变路由。
|
|
19
|
-
|
|
20
|
-
规则组合是核心,生成不同客户端的配置是它的出口。普通 TypeScript 的 `import`、函数和类型检查,负责把配置组织成可维护的项目。
|
|
21
|
-
|
|
22
|
-
## 快速开始
|
|
23
|
-
|
|
24
|
-
需要 **Node.js 24(最低 24.11.0)**。安装 npm 包:
|
|
25
|
-
|
|
26
|
-
```sh
|
|
27
|
-
npm install --save-exact proxylang@0.3.0
|
|
28
|
-
npx proxylang --help
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
将下面的 Usage 示例保存为 `proxylang.config.ts`,即可执行 `npx proxylang check`。
|
|
32
|
-
|
|
33
|
-
若要运行仓库里的完整示例,另需 **Vite+ 1.3.0**:
|
|
34
|
-
|
|
35
|
-
```sh
|
|
36
|
-
git clone https://github.com/CorrectRoadH/proxylang.git
|
|
37
|
-
cd proxylang
|
|
38
|
-
vp install --frozen-lockfile
|
|
39
|
-
vp run build
|
|
40
|
-
|
|
41
|
-
# 检查现成的四客户端示例,不读取密码
|
|
42
|
-
cd examples/clients
|
|
43
|
-
vp exec node ../../dist/cli.js check
|
|
44
|
-
|
|
45
|
-
# 用测试密码生成配置,不会启动代理
|
|
46
|
-
DEMO_PASSWORD=example-only vp exec node ../../dist/cli.js build
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
仓库开发统一使用 Vite+:`vp run dev` 监听构建,`vp run check` 检查源码,`vp run verify` 完成源码与隔离安装包验收。`vp add` / `vp remove` 管理依赖,提交更新后的 `package-lock.json`。
|
|
50
|
-
|
|
51
|
-
[四客户端示例](examples/clients/proxylang.config.ts)展示完整配置;下面从规则开始,逐步写出自己的版本。
|
|
52
|
-
|
|
53
|
-
## 直接组合设备配置
|
|
54
|
-
|
|
55
|
-
Profile 的 `proxies` 同时容纳普通代理、WireGuard 和 Tailscale。规则、代理组、Host 和 General 均可直接组合嵌套数组;General 按顺序浅合并,后者整键覆盖。
|
|
56
|
-
|
|
57
|
-
```ts
|
|
58
|
-
import { defineProfile, defineRoutes, outbound, proxy, secret, surge } from "proxylang";
|
|
59
|
-
|
|
60
|
-
const exit = proxy.trojan({
|
|
61
|
-
id: "edge",
|
|
62
|
-
server: "proxy.example.com",
|
|
63
|
-
port: 443,
|
|
64
|
-
password: secret("PASSWORD"),
|
|
65
|
-
});
|
|
66
|
-
const work = defineRoutes({ when: [surge.match.domainSuffix("github.com")], to: "edge" });
|
|
67
|
-
const local = defineRoutes({
|
|
68
|
-
when: [surge.match.domainSuffix("internal.example")],
|
|
69
|
-
to: outbound.direct,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
const laptop = defineProfile({
|
|
73
|
-
target: { engine: "surge", platform: "macos", version: "6.10.0" },
|
|
74
|
-
proxies: [exit],
|
|
75
|
-
groups: [],
|
|
76
|
-
routes: [local, work],
|
|
77
|
-
fallback: "edge",
|
|
78
|
-
surge: { general: [{ loglevel: "notify" }], hosts: [] },
|
|
79
|
-
});
|
|
80
|
-
```
|
|
81
|
-
|
|
82
|
-
字符串目的地是当前 Profile 的资源 ID,不是显示名称;缺失引用会阻止构建。平台专属代理仍须满足目标能力。完整语义见[Profile 组合契约](docs/feature/target-compilation/profile-composition.md)。
|
|
83
|
-
|
|
84
|
-
## Usage
|
|
85
|
-
|
|
86
|
-
将下面三个 TypeScript 代码块按顺序保存为项目中的 `proxylang.config.ts`。
|
|
87
|
-
|
|
88
|
-
### 1. 组合规则,给出口留一个位置
|
|
89
|
-
|
|
90
|
-
`rule(id, 条件, 动作)` 定义一条规则。`block` 把规则放在一起,还可以给整组规则添加共同条件;`policy` 把它们封装成可复用模板。
|
|
7
|
+
使用 Node 24(>=24.11.0)或 Node 26。安装 `proxylang`,在项目根目录创建 `proxylang.config.ts`:
|
|
91
8
|
|
|
92
9
|
```ts
|
|
93
10
|
import {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
11
|
+
defineBuild,
|
|
12
|
+
defineProfile,
|
|
13
|
+
defineProxyGroup,
|
|
14
|
+
defineRoutes,
|
|
15
|
+
defineSecrets,
|
|
97
16
|
group,
|
|
98
17
|
match,
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
rule,
|
|
103
|
-
secret,
|
|
104
|
-
sequence,
|
|
105
|
-
slot,
|
|
106
|
-
strategy,
|
|
107
|
-
surge,
|
|
108
|
-
use,
|
|
109
|
-
type Engine,
|
|
110
|
-
type Program,
|
|
18
|
+
outbound,
|
|
19
|
+
proxy,
|
|
20
|
+
type Exit,
|
|
111
21
|
} from "proxylang";
|
|
112
22
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
rule("lan", match.ipCidr("192.168.0.0/16"), direct()),
|
|
116
|
-
]);
|
|
117
|
-
|
|
118
|
-
const work = policy({
|
|
119
|
-
id: "work",
|
|
120
|
-
exits: ["proxy"],
|
|
121
|
-
rules: [
|
|
122
|
-
block(
|
|
123
|
-
"services",
|
|
124
|
-
[
|
|
125
|
-
rule("github", match.domainSuffix("github.com"), slot("proxy")),
|
|
126
|
-
rule("ssh", match.port(22), slot("proxy")),
|
|
127
|
-
],
|
|
128
|
-
match.network("tcp"),
|
|
129
|
-
),
|
|
130
|
-
],
|
|
23
|
+
const secrets = defineSecrets({
|
|
24
|
+
EDGE_PASSWORD: { env: "EDGE_PASSWORD", validation: "validation-edge" },
|
|
131
25
|
});
|
|
132
26
|
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
这里的顺序就是匹配顺序:先处理内网,再拦截广告,最后应用工作规则。`services` 中每条规则都同时受 TCP 条件约束。
|
|
141
|
-
|
|
142
|
-
`slot("proxy")` 让模板不依赖具体节点名称。换一台设备时,可以把同一个 `work` 模板绑定到别的出口;规则正文不用复制。`sequence` 保留原始顺序,不排序、不去重,最终未匹配的流量交给程序的 `fallback`。
|
|
143
|
-
|
|
144
|
-
`slot` 是普通 TypeScript 函数,表示模板的出口参数;`route` 引用具体节点或组。编辑器会检查字面量槽位名称、缺失或多余的绑定,并沿嵌套块与模板保留提示。`defineConfig` 也会检查目标与案例引用的程序名称。动态输入仍由 `check` 验证。
|
|
145
|
-
|
|
146
|
-
### 2. 一个出口,共享算法意图
|
|
147
|
-
|
|
148
|
-
自动择优可以只声明一次,让各平台选择实现:
|
|
149
|
-
|
|
150
|
-
```ts
|
|
151
|
-
const automatic = strategy({
|
|
152
|
-
id: "automatic",
|
|
153
|
-
members: ["edge"],
|
|
154
|
-
algorithm: group.auto(),
|
|
27
|
+
const edge = proxy.trojan({
|
|
28
|
+
id: "edge",
|
|
29
|
+
name: "Edge",
|
|
30
|
+
server: "192.0.2.10",
|
|
31
|
+
port: 443,
|
|
32
|
+
password: secrets.EDGE_PASSWORD,
|
|
155
33
|
});
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
原有显式平台配置仍然支持:
|
|
163
|
-
|
|
164
|
-
规则引用 `adaptive`,节点成员与出口名称可以共用,具体选路算法由每个平台独立声明。
|
|
165
|
-
|
|
166
|
-
```ts
|
|
167
|
-
const adaptive = strategy({
|
|
168
|
-
id: "adaptive",
|
|
169
|
-
members: ["edge"],
|
|
170
|
-
implementations: {
|
|
171
|
-
surge: surge.smart(),
|
|
172
|
-
"sing-box": group.urltest({ url: "https://www.gstatic.com/generate_204" }),
|
|
173
|
-
mihomo: group.urltest({ url: "https://www.gstatic.com/generate_204" }),
|
|
174
|
-
stash: group.select(),
|
|
175
|
-
},
|
|
34
|
+
const ai = defineProxyGroup({
|
|
35
|
+
id: "ai",
|
|
36
|
+
name: "AI",
|
|
37
|
+
algorithm: group.auto(),
|
|
38
|
+
members: [edge],
|
|
176
39
|
});
|
|
177
|
-
```
|
|
178
|
-
|
|
179
|
-
Smart、URLTest 和手动选择的运行行为不同。这段代码明确表达作者的选择,编译器不会自行降级或替换算法。缺少目标实现,或目标版本不支持所选算法时,会报告错误。
|
|
180
|
-
|
|
181
|
-
### 3. 声明节点与构建目标
|
|
182
40
|
|
|
183
|
-
|
|
41
|
+
const sharedRoutes = (exits: { ai: Exit }) => [
|
|
42
|
+
defineRoutes({ when: match.domainSuffix("internal.example"), to: outbound.direct }),
|
|
43
|
+
defineRoutes({ when: match.domainSuffix(["ai.example", "chat.example"]), to: exits.ai }),
|
|
44
|
+
];
|
|
45
|
+
const sharedCases = (exits: { ai: Exit }) => [
|
|
46
|
+
{ name: "AI traffic uses AI", request: { domain: "chat.example" }, expect: exits.ai },
|
|
47
|
+
];
|
|
184
48
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
server: "192.0.2.10",
|
|
193
|
-
port: 443,
|
|
194
|
-
serverName: "edge.example",
|
|
195
|
-
password: secret("PROXY_PASSWORD"),
|
|
196
|
-
},
|
|
197
|
-
],
|
|
198
|
-
groups: [adaptive],
|
|
199
|
-
routing,
|
|
200
|
-
fallback: direct(),
|
|
49
|
+
const laptop = defineProfile({
|
|
50
|
+
target: { engine: "surge", platform: "macos", version: "6.10.0" },
|
|
51
|
+
proxies: [edge],
|
|
52
|
+
groups: [ai],
|
|
53
|
+
routes: sharedRoutes({ ai }),
|
|
54
|
+
fallback: outbound.direct,
|
|
55
|
+
cases: sharedCases({ ai }),
|
|
201
56
|
});
|
|
202
57
|
|
|
203
|
-
export default
|
|
204
|
-
outDir: "out",
|
|
205
|
-
programs: {
|
|
206
|
-
desktop: program("surge"),
|
|
207
|
-
backup: program("sing-box"),
|
|
208
|
-
},
|
|
209
|
-
targets: [
|
|
210
|
-
{ id: "desktop", program: "desktop", engine: "surge", platform: "macos", version: "6.10.0" },
|
|
211
|
-
{ id: "backup", program: "backup", engine: "sing-box", platform: "linux", version: "1.12.0" },
|
|
212
|
-
],
|
|
213
|
-
cases: [
|
|
214
|
-
{
|
|
215
|
-
id: "github-via-proxy",
|
|
216
|
-
program: "backup",
|
|
217
|
-
request: { network: "tcp", domain: "github.com", port: 443 },
|
|
218
|
-
expect: { action: route("adaptive") },
|
|
219
|
-
},
|
|
220
|
-
],
|
|
221
|
-
});
|
|
58
|
+
export default defineBuild({ outDir: "generated", profiles: { laptop }, secrets });
|
|
222
59
|
```
|
|
223
60
|
|
|
224
|
-
示例节点使用文档保留地址,实际使用时替换为自己的服务器 IP 和 TLS 域名。`secret` 引用环境变量,检查配置时不读取密码,构建时才解析。`cases` 用来检查请求是否走向预期的逻辑出口。
|
|
225
|
-
|
|
226
|
-
### 4. 检查、解释、构建
|
|
227
|
-
|
|
228
61
|
```sh
|
|
229
|
-
|
|
62
|
+
npm install proxylang
|
|
230
63
|
npx proxylang check
|
|
231
|
-
|
|
232
|
-
# 查看请求命中了哪条规则,以及规则来自哪个模板
|
|
233
|
-
npx proxylang explain --target backup \
|
|
234
|
-
--request '{"network":"tcp","domain":"github.com","port":443}'
|
|
235
|
-
|
|
236
|
-
# 用测试密码构建;实际使用时由环境提供真实凭据
|
|
237
|
-
PROXY_PASSWORD=example-only npx proxylang build
|
|
238
|
-
```
|
|
239
|
-
|
|
240
|
-
`explain` 展示条件匹配与逻辑出口,不模拟节点测速或网络连通性。`build` 生成原生配置,整批成功后更新 `out/active.json`,其中列出各目标文件路径;一个目标失败不会替换上一批结果。
|
|
241
|
-
|
|
242
|
-
配置错误提供字段路径和约束提示,例如端口越界会定位到对应规则的条件字段。加载失败会说明是模块缺失、权限不足、语法错误还是执行失败;诊断不会回显原始异常或配置中的秘密。
|
|
243
|
-
|
|
244
|
-
默认输出是包含节点、策略组和规则的 **fragment**,需要与客户端的入口、DNS 等配置组合。可选 `standalone` 提供本机显式代理配置;生成文件不等于部署或系统接管。
|
|
245
|
-
|
|
246
|
-
### 从文件解释请求
|
|
247
|
-
|
|
248
|
-
把请求元数据保存为 UTF-8 JSON,避免 shell 转义:
|
|
249
|
-
|
|
250
|
-
```sh
|
|
251
|
-
npx proxylang explain --target backup --request-file request.json
|
|
252
|
-
npx proxylang build --help
|
|
64
|
+
npx proxylang build --validation
|
|
253
65
|
```
|
|
254
66
|
|
|
255
|
-
|
|
67
|
+
`check` 检查全部 Profile 和案例,不读取秘密或写文件。验证构建使用假值,生成 `generated/out/laptop.conf`。实际使用前为 `EDGE_PASSWORD` 提供生产来源,再运行 `npx proxylang build`;将文件交给客户端的部署流程由你负责。
|
|
256
68
|
|
|
257
|
-
##
|
|
69
|
+
## 心智模型
|
|
258
70
|
|
|
259
|
-
|
|
71
|
+
- **一台设备一份 Profile**:声明精确目标、节点、分组、有序路由、默认出口和引擎设置;构建定义的键决定文件名。
|
|
72
|
+
- **复用靠函数和数组**:共享路由、案例以所需出口为参数。嵌套数组按书写顺序展开,不需要另一套模板语言。
|
|
73
|
+
- **引用写对象**:`to`、`members`、`via`、`fallback` 和案例的 `expect` 引用构造器返回的资源。被引用资源必须列入同一 Profile 的 `proxies` 或 `groups`;内置出口使用 `outbound.direct`、`outbound.reject`。
|
|
74
|
+
- **首条命中**:按作者顺序求值,第一条命中决定出口;全部不命中才使用 `fallback`。
|
|
75
|
+
- **引擎差异写明处**:`group.auto()` 在 Surge 选择 Smart,在 sing-box 和 Mihomo 选择 URLTest;Stash 不支持它。规则集按引擎声明来源,不自动转换内容或替换机制。
|
|
260
76
|
|
|
261
|
-
|
|
262
|
-
import { match, ruleSet, rule, direct, surge } from "proxylang";
|
|
77
|
+
资源的 `id` 用于诊断和案例,`name` 是原生名字,省略时使用 id。所有引用跟随同一个原生名字;改名可能重置客户端保存的选择。同一资源对象可以跨 Profile 复用,形状相同的普通对象不能代替 SDK 资源。
|
|
263
78
|
|
|
264
|
-
|
|
265
|
-
id: "streaming",
|
|
266
|
-
url: "https://example.com/streaming.list",
|
|
267
|
-
});
|
|
268
|
-
const media = ruleSet({
|
|
269
|
-
id: "media",
|
|
270
|
-
when: match.any(streaming, match.domainSuffix("video.example")),
|
|
271
|
-
});
|
|
272
|
-
const mediaRule = rule("media", match.all(match.network("tcp"), media), direct());
|
|
273
|
-
```
|
|
79
|
+
## 四个引擎
|
|
274
80
|
|
|
275
|
-
|
|
81
|
+
| 引擎 | 平台与精确版本 | 输出与边界 |
|
|
82
|
+
| -------- | -------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
83
|
+
| Surge | macOS 6.10.0、iOS 5.21.0 | 完整 `.conf`;`surge.general/hosts/managed`,以及 `surge.tailscale/wireguard` |
|
|
84
|
+
| sing-box | macOS/Linux 1.12.0、1.14.4 | 完整或片段 `.json`;`singBox` 设置,TUN 只在 1.14.4 完整模式可用;`singbox.tailscale` 创建 endpoint |
|
|
85
|
+
| Mihomo | macOS/Linux 1.19.32 | 完整或片段 `.yaml`;完整模式需 `mihomo.listener` |
|
|
86
|
+
| Stash | macOS 4.2.1 | 片段 `.yaml`;宿主提供入口与 DNS,仅支持 select,不支持远程规则集 |
|
|
276
87
|
|
|
277
|
-
|
|
88
|
+
sing-box 完整模式默认开启,须提供 `singBox.listener`;Mihomo 默认输出片段。精确能力与模式矩阵见[目标](docs/feature/targets/README.md)。不受支持的行为报 error,不静默省略。
|
|
278
89
|
|
|
279
|
-
Surge
|
|
90
|
+
Surge 的 `includeSniffed: true` 匹配原域名或嗅探域名;sing-box 1.14.4 的 `domainMatching: "prefer-sniffed"` 对全部域名条件优先使用嗅探域名。两者语义不同。
|
|
280
91
|
|
|
281
|
-
|
|
92
|
+
## 跨引擎规则集
|
|
282
93
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
用 `proxy.<协议>()` 定义节点。每种协议有自己的必填字段:SS 使用加密方法和密码,VMess/VLESS 使用 UUID;TypeScript 和运行时检查都会拒绝混用字段。
|
|
94
|
+
后续 TypeScript 片段沿用上手示例中的导入和节点。把声明放在 `defineProfile` 之前,再将路由、案例和新增出口列入相应字段。
|
|
286
95
|
|
|
287
96
|
```ts
|
|
288
|
-
import {
|
|
289
|
-
|
|
290
|
-
const
|
|
291
|
-
id: "
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
server: "192.0.2.20",
|
|
301
|
-
port: 443,
|
|
302
|
-
uuid: secret("VMESS_UUID"),
|
|
303
|
-
tls: { serverName: "tls.example.com" },
|
|
304
|
-
transport: { type: "ws", path: "/proxy", host: "ws.example.com" },
|
|
305
|
-
});
|
|
306
|
-
|
|
307
|
-
const vless = proxy.vless({
|
|
308
|
-
id: "vless",
|
|
309
|
-
server: "192.0.2.30",
|
|
310
|
-
port: 443,
|
|
311
|
-
uuid: secret("VLESS_UUID"),
|
|
312
|
-
tls: { serverName: "vless.example.com" },
|
|
97
|
+
import { ruleSet } from "proxylang";
|
|
98
|
+
|
|
99
|
+
const localServices = ruleSet({
|
|
100
|
+
id: "local-services",
|
|
101
|
+
surge: { format: "rule-set", url: "https://rules.example.com/local.list" },
|
|
102
|
+
singBox: { format: "binary", url: "https://rules.example.com/local.srs", intervalSeconds: 86400 },
|
|
103
|
+
mihomo: {
|
|
104
|
+
format: "text",
|
|
105
|
+
behavior: "domain",
|
|
106
|
+
url: "https://rules.example.com/local.txt",
|
|
107
|
+
intervalSeconds: 86400,
|
|
108
|
+
},
|
|
313
109
|
});
|
|
110
|
+
const routes = defineRoutes({ when: localServices, to: outbound.direct });
|
|
314
111
|
```
|
|
315
112
|
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
TLS 的 `serverName` 是证书身份,WebSocket 的 `host` 是 HTTP Host,二者明确分开。省略 `tls` 表示不启用 TLS,省略 `transport` 表示普通 TCP。VMess 当前固定 AEAD/AES-128-GCM;VMess 与 VLESS 暂只允许 TCP 规则,不能作为覆盖 UDP 的 fallback。SS 支持三种 AEAD 方法及 TCP/UDP。
|
|
113
|
+
`ruleSet` 是条件,路由单独给出出口。目标没有对应分支就报错。sing-box 与 Mihomo 对同来源资源去重,使用路由中先引用的规则集 id;相同 URL 的来源参数冲突会报错。更新、缓存和下载失败由客户端处理,构建不下载规则集,也不保证不同上游内容相同。Surge 内置集使用 `surge.ruleSets.lan` 和 `surge.ruleSets.system`。
|
|
319
114
|
|
|
320
|
-
|
|
115
|
+
## 用 cases 保护路由意图
|
|
321
116
|
|
|
322
|
-
|
|
117
|
+
Profile 的 `cases` 给出请求和期望出口;`check` 与 `build` 每次执行,失败阻止发布。共享案例用函数绑定每台设备的出口。
|
|
323
118
|
|
|
324
|
-
|
|
119
|
+
远程规则内容不能静态推断,案例通过对象声明假设:
|
|
325
120
|
|
|
326
121
|
```ts
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
username: secret("PROXY_USER"),
|
|
336
|
-
password: secret("PROXY_PASSWORD"),
|
|
337
|
-
}),
|
|
338
|
-
)
|
|
339
|
-
.via("relay")
|
|
340
|
-
.build();
|
|
341
|
-
|
|
342
|
-
const compiled = compileProxyNodes({
|
|
343
|
-
target: { engine: "surge", platform: "macos", version: "6.10.0" },
|
|
344
|
-
nodes: [exit],
|
|
345
|
-
names: { exit: "🇭🇰 我的出口" },
|
|
346
|
-
externals: [{ id: "relay", nativeName: "已有代理组", kind: "group" }],
|
|
347
|
-
});
|
|
348
|
-
if (!compiled.ok) throw new Error("节点编译失败");
|
|
349
|
-
const artifact = renderProxyNodes(compiled, (token) => process.env[token.name]);
|
|
350
|
-
// artifact.entries 提供每个节点的 id 和原生行,可按 provider 分文件。
|
|
122
|
+
const cases = [
|
|
123
|
+
{
|
|
124
|
+
name: "Local rule set uses direct",
|
|
125
|
+
request: { domain: "service.example" },
|
|
126
|
+
assume: { hit: [localServices] },
|
|
127
|
+
expect: outbound.direct,
|
|
128
|
+
},
|
|
129
|
+
];
|
|
351
130
|
```
|
|
352
131
|
|
|
353
|
-
|
|
132
|
+
缺少规则集假设或解析信息时,求值可能为 `indeterminate`,案例失败并提示缺少什么。`ip` 是入口已知地址,`resolvedIp` 是解析后用于规则的地址。求值不执行 DNS、不模拟分组当前选择、拨号或远端健康。完整语义见[路由案例](docs/feature/routing-cases/README.md)。
|
|
354
133
|
|
|
355
|
-
|
|
134
|
+
## 秘密与链式代理
|
|
356
135
|
|
|
357
|
-
|
|
136
|
+
`defineSecrets` 的每项选择 `env` 或 `value` 来源,返回不带值的句柄。构建只接受同一集合的引用;秘密仅在检查和案例通过后读取,批次中每项只读一次。`--validation` 只读取显式 `validation`,缺少就失败,不回退生产来源。UUID 和 WireGuard 密钥的假值也须符合对应格式。
|
|
358
137
|
|
|
359
|
-
|
|
138
|
+
先创建中转,再把它传给节点工厂:
|
|
360
139
|
|
|
361
140
|
```ts
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
listener: { port: 17890, resolver: "1.1.1.1" },
|
|
379
|
-
routing: [rule("home", match.ipCidr("192.168.50.0/24"), route("home-net"))],
|
|
380
|
-
fallback: direct(),
|
|
381
|
-
};
|
|
141
|
+
const relay = defineProxyGroup({
|
|
142
|
+
id: "relay",
|
|
143
|
+
name: "Relay",
|
|
144
|
+
algorithm: group.select(),
|
|
145
|
+
members: [edge],
|
|
146
|
+
});
|
|
147
|
+
const createExit = (via: typeof relay) =>
|
|
148
|
+
proxy.http({
|
|
149
|
+
id: "exit",
|
|
150
|
+
name: "Exit",
|
|
151
|
+
server: "192.0.2.20",
|
|
152
|
+
port: 8080,
|
|
153
|
+
password: secrets.EDGE_PASSWORD,
|
|
154
|
+
via,
|
|
155
|
+
});
|
|
156
|
+
const exit = createExit(relay);
|
|
382
157
|
```
|
|
383
158
|
|
|
384
|
-
|
|
159
|
+
路径是客户端 → Relay → Exit → 目的地。节点和中转都要登记;成员与 `via` 构成的图不能有环。也可用 `proxy.builder(base).name("Exit").via(relay).build()` 派生节点。Surge 编码 `underlying-proxy`,sing-box 编码 `detour`;Surge 不允许端口跳跃与 `via` 同用。协议和 UDP 能力依目标检查,配置通过不证明远端可达。
|
|
385
160
|
|
|
386
|
-
|
|
161
|
+
## CLI
|
|
387
162
|
|
|
388
|
-
|
|
163
|
+
CLI 从调用目录固定加载 `proxylang.config.ts`,配置是可信可执行代码。
|
|
389
164
|
|
|
390
165
|
```sh
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
166
|
+
npx proxylang targets
|
|
167
|
+
npx proxylang check --json
|
|
168
|
+
npx proxylang explain --profile laptop --request '{"domain":"chat.example"}'
|
|
169
|
+
npx proxylang build --validation
|
|
170
|
+
npx proxylang build
|
|
395
171
|
```
|
|
396
172
|
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
| 目标 | 精确版本 | 平台 | 策略组 |
|
|
400
|
-
| -------- | ------------------ | ------------ | ------------------------------- |
|
|
401
|
-
| Surge | 6.10.0 | macOS | select、Smart |
|
|
402
|
-
| sing-box | 1.11.0、1.12.0 | Linux、macOS | select、URLTest |
|
|
403
|
-
| Mihomo | 1.19.32 | Linux、macOS | select、URLTest |
|
|
404
|
-
| Stash | 4.2.1(build 487) | macOS | select;仅 fragment,原生未验证 |
|
|
405
|
-
|
|
406
|
-
支持域名、IP、端口、网络类型与逻辑条件,节点协议包括 Trojan、HTTP、AnyTLS、Shadowsocks、VMess、VLESS,具体能力按目标版本检查。未知版本和影响路由行为的不支持项报错;可省略的展示信息报 warning,可用 `warningsAsErrors` 提升为错误。
|
|
407
|
-
|
|
408
|
-
需要额外匹配 TLS SNI 和 HTTP Host 时,使用 `match.domainSuffix("example.com", { includeSniffed: true })`。原域名仍参与匹配;Surge 编译为 `extended-matching`。其他引擎的嗅探通常优先使用识别域名,不能等价为多来源 OR,因此拒绝此选项的 `true`。它不改变连接目的地址。详见[匹配语义](docs/feature/target-compilation/domain-sources.md)。
|
|
409
|
-
|
|
410
|
-
Stash 当前拒绝来源 IP/端口、端口范围和组级 URLTest。完整配置的节点地址暂限 IP,独立 Surge 节点片段支持域名。HTTP、VMess、VLESS 及经过它们的代理链按 TCP-only 检查;AnyTLS 目前仅在 Surge 放行 UDP。远程节点订阅、MAC/进程匹配和 TUN 尚未实现。完整限制见 [SDK 细则](docs/implementation-v0.md)和 [Mihomo / Stash 转换边界](docs/engineering/clash-backends/README.md)。
|
|
411
|
-
|
|
412
|
-
配置文件是可信的 TypeScript 代码;构建产物包含密码,旧构建也会保留。凭据与产物管理见[构建契约](docs/feature/artifact-publication/use-case/publish-generation.md)。
|
|
413
|
-
|
|
414
|
-
## 更多示例与文档
|
|
173
|
+
`explain` 也可用 `--request-file request.json`。CLI 的规则集假设写在请求 JSON 的 `ruleSets` 中,以规则集 id 为键、布尔值为命中结果。所有命令支持 `--json`,参数与结果见[CLI 契约](docs/feature/build/cli.md)。
|
|
415
174
|
|
|
416
|
-
|
|
417
|
-
- [四客户端共用规则](examples/clients/proxylang.config.ts):同一个 policy,不同平台策略。
|
|
418
|
-
- [家庭与开发环境规则](examples/policies.ts):规则模块、守卫与出口绑定。
|
|
419
|
-
- [多设备配置与请求案例](examples/home/proxylang.config.ts):节点筛选、代理链和路由回归案例。
|
|
420
|
-
- [设计与使用文档](docs/README.md):规则组合、平台机制、CLI 与构建边界。
|
|
421
|
-
- [开发工作流](docs/engineering/development/README.md):Concord 管理 Feature 与 Use Case,`vp` 负责构建和测试。
|
|
422
|
-
|
|
423
|
-
### 按域名指定 DNS
|
|
424
|
-
|
|
425
|
-
同一服务器的多个域名可以一起声明,Profile 的 `dns` 支持数组片段组合:
|
|
426
|
-
|
|
427
|
-
```ts
|
|
428
|
-
import { dnsRule } from "proxylang";
|
|
429
|
-
|
|
430
|
-
const domestic = dnsRule(["*.example.cn", "example.cn"], "https://223.5.5.5/dns-query");
|
|
431
|
-
const tailnet = dnsRule("*.ts.net", "100.100.100.100");
|
|
432
|
-
// defineProfile({ ..., dns: [domestic, tailnet] })
|
|
433
|
-
```
|
|
175
|
+
构建整批渲染成功后才切换 `out` 相对符号链接,正常失败保留上一批。退出 0 为成功,1 为未发布,2 表示已发布但清理失败或需要人工恢复;以返回的 code 为准。旧 generation 保留,维护者负责秘密产物的留存与恢复。详见[构建与发布](docs/feature/build/README.md)。
|
|
434
176
|
|
|
435
|
-
|
|
177
|
+
生成结果、原生语法检查和真实网络验收是不同证据;本工具不启动客户端或接管系统网络。
|