proxylang 0.1.3 → 0.1.5
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 +31 -2
- package/dist/cli.js +48 -13
- package/dist/{host-CB08cTs0.js → host-Bea3np6i.js} +438 -22
- package/dist/index.d.ts +360 -203
- package/dist/index.js +165 -2
- package/package.json +1 -1
|
@@ -5,6 +5,325 @@ import { stringify } from "yaml";
|
|
|
5
5
|
import { lstat, mkdir, rename, rm, writeFile } from "node:fs/promises";
|
|
6
6
|
import { join, resolve } from "node:path";
|
|
7
7
|
import { randomUUID } from "node:crypto";
|
|
8
|
+
//#region src/surge-definitions.ts
|
|
9
|
+
var ProfileDefinitionError = class extends Error {
|
|
10
|
+
code;
|
|
11
|
+
constructor(code) {
|
|
12
|
+
super(code);
|
|
13
|
+
this.code = code;
|
|
14
|
+
this.name = "ProfileDefinitionError";
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
const fail = (code) => {
|
|
18
|
+
throw new ProfileDefinitionError(code);
|
|
19
|
+
};
|
|
20
|
+
/** Inspect descriptors before any caller spreads or reads input fields. Shared DAGs are legal. */
|
|
21
|
+
function inspect(root) {
|
|
22
|
+
let edges = 0;
|
|
23
|
+
let objects = 0;
|
|
24
|
+
const visiting = /* @__PURE__ */ new Set();
|
|
25
|
+
const heights = /* @__PURE__ */ new Map();
|
|
26
|
+
function visit(value, depth) {
|
|
27
|
+
if (depth > 64) fail("DEFINITION_DEPTH_EXCEEDED");
|
|
28
|
+
if (value === null || typeof value !== "object") {
|
|
29
|
+
if (![
|
|
30
|
+
"undefined",
|
|
31
|
+
"string",
|
|
32
|
+
"number",
|
|
33
|
+
"boolean"
|
|
34
|
+
].includes(typeof value) && value !== null) fail("DEFINITION_NOT_DATA");
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
if (visiting.has(value)) fail("DEFINITION_CYCLE");
|
|
38
|
+
const cached = heights.get(value);
|
|
39
|
+
if (cached !== void 0) {
|
|
40
|
+
if (depth + cached > 64) fail("DEFINITION_DEPTH_EXCEEDED");
|
|
41
|
+
return cached;
|
|
42
|
+
}
|
|
43
|
+
if (++objects > 2e4) fail("DEFINITION_OBJECT_BUDGET_EXCEEDED");
|
|
44
|
+
const proto = Object.getPrototypeOf(value);
|
|
45
|
+
if (proto !== Object.prototype && proto !== null && !(Array.isArray(value) && proto === Array.prototype)) fail("DEFINITION_NOT_DATA");
|
|
46
|
+
if (Array.isArray(value) && value.length > 1e5) fail("DEFINITION_EDGE_BUDGET_EXCEEDED");
|
|
47
|
+
visiting.add(value);
|
|
48
|
+
let height = 0;
|
|
49
|
+
for (const key of Reflect.ownKeys(value)) {
|
|
50
|
+
if (++edges > 1e5) fail("DEFINITION_EDGE_BUDGET_EXCEEDED");
|
|
51
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
52
|
+
if (typeof key !== "string" || !("value" in descriptor) || !descriptor.enumerable && !(Array.isArray(value) && key === "length")) fail("DEFINITION_NOT_DATA");
|
|
53
|
+
height = Math.max(height, 1 + visit(descriptor.value, depth + 1));
|
|
54
|
+
}
|
|
55
|
+
visiting.delete(value);
|
|
56
|
+
heights.set(value, height);
|
|
57
|
+
return height;
|
|
58
|
+
}
|
|
59
|
+
visit(root, 0);
|
|
60
|
+
}
|
|
61
|
+
/** Count output occurrences, not identities, before allocating each copied value. */
|
|
62
|
+
function snapshot(input) {
|
|
63
|
+
let remaining = 1e5;
|
|
64
|
+
function copy(value, depth) {
|
|
65
|
+
if (--remaining < 0) fail("DEFINITION_OUTPUT_BUDGET_EXCEEDED");
|
|
66
|
+
if (depth > 64) fail("DEFINITION_DEPTH_EXCEEDED");
|
|
67
|
+
if (value === null || typeof value !== "object") return value;
|
|
68
|
+
if (Array.isArray(value)) {
|
|
69
|
+
const output = [];
|
|
70
|
+
for (const item of value) output.push(copy(item, depth + 1));
|
|
71
|
+
return output;
|
|
72
|
+
}
|
|
73
|
+
const output = {};
|
|
74
|
+
for (const [key, item] of Object.entries(value)) Object.defineProperty(output, key, {
|
|
75
|
+
value: copy(item, depth + 1),
|
|
76
|
+
enumerable: true,
|
|
77
|
+
writable: true,
|
|
78
|
+
configurable: true
|
|
79
|
+
});
|
|
80
|
+
return output;
|
|
81
|
+
}
|
|
82
|
+
return copy(input, 0);
|
|
83
|
+
}
|
|
84
|
+
function exact(value, keys) {
|
|
85
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) fail("DEFINITION_NOT_DATA");
|
|
86
|
+
if (Object.keys(value).some((key) => !keys.includes(key))) fail("DEFINITION_UNKNOWN_FIELD");
|
|
87
|
+
}
|
|
88
|
+
const outbound = {
|
|
89
|
+
direct: Object.freeze({ builtin: "DIRECT" }),
|
|
90
|
+
reject: Object.freeze({ builtin: "REJECT" }),
|
|
91
|
+
rejectDrop: Object.freeze({ builtin: "REJECT-DROP" }),
|
|
92
|
+
rejectNoDrop: Object.freeze({ builtin: "REJECT-NO-DROP" }),
|
|
93
|
+
rejectTinyGif: Object.freeze({ builtin: "REJECT-TINYGIF" })
|
|
94
|
+
};
|
|
95
|
+
function defineProxyGroup(input) {
|
|
96
|
+
inspect(input);
|
|
97
|
+
exact(input, [
|
|
98
|
+
"id",
|
|
99
|
+
"name",
|
|
100
|
+
"algorithm",
|
|
101
|
+
"members",
|
|
102
|
+
"url",
|
|
103
|
+
"intervalSeconds",
|
|
104
|
+
"timeoutSeconds"
|
|
105
|
+
]);
|
|
106
|
+
if (!input.algorithm || !["select", "surge-smart"].includes(input.algorithm.type)) fail("UNSUPPORTED_GROUP_ALGORITHM");
|
|
107
|
+
exact(input.algorithm, ["type"]);
|
|
108
|
+
if (!Array.isArray(input.members)) fail("INVALID_GROUP_DEFINITION");
|
|
109
|
+
return {
|
|
110
|
+
...input,
|
|
111
|
+
type: "proxy-group",
|
|
112
|
+
algorithm: { ...input.algorithm },
|
|
113
|
+
members: [...input.members]
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
function defineRoute(input) {
|
|
117
|
+
inspect(input);
|
|
118
|
+
exact(input, [
|
|
119
|
+
"id",
|
|
120
|
+
"when",
|
|
121
|
+
"to"
|
|
122
|
+
]);
|
|
123
|
+
return {
|
|
124
|
+
...input,
|
|
125
|
+
type: "route"
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
function selectProxies(nodes, query = {}) {
|
|
129
|
+
inspect({
|
|
130
|
+
nodes,
|
|
131
|
+
query
|
|
132
|
+
});
|
|
133
|
+
exact(query, [
|
|
134
|
+
"ids",
|
|
135
|
+
"regions",
|
|
136
|
+
"providers",
|
|
137
|
+
"tags"
|
|
138
|
+
]);
|
|
139
|
+
if (!Array.isArray(nodes) || nodes.some((node) => !node || typeof node !== "object")) fail("DEFINITION_NOT_DATA");
|
|
140
|
+
for (const values of Object.values(query)) if (values !== void 0 && !Array.isArray(values)) fail("DEFINITION_NOT_DATA");
|
|
141
|
+
return nodes.filter((node) => (!query.ids || query.ids.includes(node.id)) && (!query.regions || node.region !== void 0 && query.regions.includes(node.region)) && (!query.providers || node.provider !== void 0 && query.providers.includes(node.provider)) && (!query.tags || query.tags.every((tag) => node.tags?.includes(tag))));
|
|
142
|
+
}
|
|
143
|
+
function defineSurgeProfile(input) {
|
|
144
|
+
inspect(input);
|
|
145
|
+
exact(input, [
|
|
146
|
+
"target",
|
|
147
|
+
"managed",
|
|
148
|
+
"general",
|
|
149
|
+
"hosts",
|
|
150
|
+
"nodes",
|
|
151
|
+
"tunnels",
|
|
152
|
+
"names",
|
|
153
|
+
"groups",
|
|
154
|
+
"routes",
|
|
155
|
+
"fallback"
|
|
156
|
+
]);
|
|
157
|
+
if (!Array.isArray(input.nodes) || !Array.isArray(input.groups) || !Array.isArray(input.routes) || input.tunnels !== void 0 && !Array.isArray(input.tunnels)) fail("DEFINITION_NOT_DATA");
|
|
158
|
+
const registry = /* @__PURE__ */ new Map();
|
|
159
|
+
const names = Object.assign(Object.create(null), input.names);
|
|
160
|
+
for (const resource of [
|
|
161
|
+
...input.nodes,
|
|
162
|
+
...input.tunnels ?? [],
|
|
163
|
+
...input.groups
|
|
164
|
+
]) {
|
|
165
|
+
if (!resource || typeof resource !== "object" || typeof resource.id !== "string") fail("INVALID_RESOURCE_DEFINITION");
|
|
166
|
+
if (registry.has(resource.id)) fail("DUPLICATE_RESOURCE_ID");
|
|
167
|
+
registry.set(resource.id, resource);
|
|
168
|
+
const name = resource.type === "proxy-group" ? resource.name : "label" in resource ? resource.label ?? resource.id : resource.id;
|
|
169
|
+
if (resource.type === "proxy-group" && names[resource.id] !== void 0 && names[resource.id] !== name) fail("GROUP_NAME_CONFLICT");
|
|
170
|
+
Object.defineProperty(names, resource.id, {
|
|
171
|
+
value: names[resource.id] ?? name,
|
|
172
|
+
enumerable: true,
|
|
173
|
+
configurable: true,
|
|
174
|
+
writable: true
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
function lower(destination) {
|
|
178
|
+
if (!destination || typeof destination !== "object") fail("INVALID_DESTINATION");
|
|
179
|
+
if ("builtin" in destination) {
|
|
180
|
+
exact(destination, ["builtin"]);
|
|
181
|
+
if (!Object.values(outbound).some((value) => value.builtin === destination.builtin)) fail("INVALID_DESTINATION");
|
|
182
|
+
return { builtin: destination.builtin };
|
|
183
|
+
}
|
|
184
|
+
if (registry.get(destination.id) !== destination) fail("UNREGISTERED_DESTINATION_INSTANCE");
|
|
185
|
+
return { ref: destination.id };
|
|
186
|
+
}
|
|
187
|
+
const groups = input.groups.map((definition) => {
|
|
188
|
+
exact(definition, [
|
|
189
|
+
"type",
|
|
190
|
+
"id",
|
|
191
|
+
"name",
|
|
192
|
+
"algorithm",
|
|
193
|
+
"members",
|
|
194
|
+
"url",
|
|
195
|
+
"intervalSeconds",
|
|
196
|
+
"timeoutSeconds"
|
|
197
|
+
]);
|
|
198
|
+
if (definition.type !== "proxy-group") fail("INVALID_GROUP_DEFINITION");
|
|
199
|
+
if (!definition.algorithm || !["select", "surge-smart"].includes(definition.algorithm.type)) fail("UNSUPPORTED_GROUP_ALGORITHM");
|
|
200
|
+
exact(definition.algorithm, ["type"]);
|
|
201
|
+
if (!Array.isArray(definition.members)) fail("INVALID_GROUP_DEFINITION");
|
|
202
|
+
const { id, url, intervalSeconds, timeoutSeconds } = definition;
|
|
203
|
+
return {
|
|
204
|
+
id,
|
|
205
|
+
type: definition.algorithm.type === "select" ? "select" : "smart",
|
|
206
|
+
members: definition.members.map(lower),
|
|
207
|
+
...url === void 0 ? {} : { url },
|
|
208
|
+
...intervalSeconds === void 0 ? {} : { intervalSeconds },
|
|
209
|
+
...timeoutSeconds === void 0 ? {} : { timeoutSeconds }
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
const ids = /* @__PURE__ */ new Set();
|
|
213
|
+
const rules = input.routes.map((definition) => {
|
|
214
|
+
exact(definition, [
|
|
215
|
+
"type",
|
|
216
|
+
"id",
|
|
217
|
+
"when",
|
|
218
|
+
"to"
|
|
219
|
+
]);
|
|
220
|
+
if (definition.type !== "route") fail("INVALID_ROUTE_DEFINITION");
|
|
221
|
+
if (ids.has(definition.id)) fail("DUPLICATE_ROUTE_ID");
|
|
222
|
+
ids.add(definition.id);
|
|
223
|
+
return {
|
|
224
|
+
when: definition.when,
|
|
225
|
+
then: lower(definition.to)
|
|
226
|
+
};
|
|
227
|
+
});
|
|
228
|
+
const { routes: _routes, groups: _groups, fallback, ...rest } = input;
|
|
229
|
+
return snapshot({
|
|
230
|
+
...rest,
|
|
231
|
+
names,
|
|
232
|
+
groups,
|
|
233
|
+
rules,
|
|
234
|
+
fallback: lower(fallback)
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
function condition(value) {
|
|
238
|
+
inspect(value);
|
|
239
|
+
return value;
|
|
240
|
+
}
|
|
241
|
+
function atom$1(type) {
|
|
242
|
+
return (value, options = {}) => {
|
|
243
|
+
inspect(options);
|
|
244
|
+
return condition({
|
|
245
|
+
...options,
|
|
246
|
+
type,
|
|
247
|
+
value
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
const surgeMatch = {
|
|
252
|
+
domain: atom$1("domain"),
|
|
253
|
+
domainSuffix: atom$1("domainSuffix"),
|
|
254
|
+
ipCidr: atom$1("ipCidr"),
|
|
255
|
+
ipCidr6: atom$1("ipCidr6"),
|
|
256
|
+
sourceIp: atom$1("sourceIp"),
|
|
257
|
+
processName: atom$1("processName"),
|
|
258
|
+
macAddress: atom$1("macAddress"),
|
|
259
|
+
network: (value, options = {}) => {
|
|
260
|
+
inspect(options);
|
|
261
|
+
return condition({
|
|
262
|
+
...options,
|
|
263
|
+
type: "network",
|
|
264
|
+
value
|
|
265
|
+
});
|
|
266
|
+
},
|
|
267
|
+
port: (from, to, options = {}) => {
|
|
268
|
+
inspect(options);
|
|
269
|
+
return condition({
|
|
270
|
+
...options,
|
|
271
|
+
type: "port",
|
|
272
|
+
from,
|
|
273
|
+
...to === void 0 ? {} : { to }
|
|
274
|
+
});
|
|
275
|
+
},
|
|
276
|
+
sourcePort: (from, to, options = {}) => {
|
|
277
|
+
inspect(options);
|
|
278
|
+
return condition({
|
|
279
|
+
...options,
|
|
280
|
+
type: "sourcePort",
|
|
281
|
+
from,
|
|
282
|
+
...to === void 0 ? {} : { to }
|
|
283
|
+
});
|
|
284
|
+
},
|
|
285
|
+
all: (conditions, options = {}) => {
|
|
286
|
+
inspect({
|
|
287
|
+
conditions,
|
|
288
|
+
options
|
|
289
|
+
});
|
|
290
|
+
return condition({
|
|
291
|
+
...options,
|
|
292
|
+
type: "all",
|
|
293
|
+
conditions: [...conditions]
|
|
294
|
+
});
|
|
295
|
+
},
|
|
296
|
+
any: (conditions, options = {}) => {
|
|
297
|
+
inspect({
|
|
298
|
+
conditions,
|
|
299
|
+
options
|
|
300
|
+
});
|
|
301
|
+
return condition({
|
|
302
|
+
...options,
|
|
303
|
+
type: "any",
|
|
304
|
+
conditions: [...conditions]
|
|
305
|
+
});
|
|
306
|
+
},
|
|
307
|
+
not: (value, options = {}) => {
|
|
308
|
+
inspect({
|
|
309
|
+
value,
|
|
310
|
+
options
|
|
311
|
+
});
|
|
312
|
+
return condition({
|
|
313
|
+
...options,
|
|
314
|
+
type: "not",
|
|
315
|
+
condition: value
|
|
316
|
+
});
|
|
317
|
+
},
|
|
318
|
+
ruleSet: (options) => {
|
|
319
|
+
inspect(options);
|
|
320
|
+
return condition({
|
|
321
|
+
...options,
|
|
322
|
+
type: "ruleSet"
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
//#endregion
|
|
8
327
|
//#region src/schema.ts
|
|
9
328
|
const idSchema = z.string().regex(/^[a-z][a-z0-9_-]{0,63}$/).refine((x) => !x.startsWith("pl-"));
|
|
10
329
|
const domain = z.string().max(253).regex(/^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/);
|
|
@@ -243,23 +562,33 @@ const url = z.string().url().refine((value) => {
|
|
|
243
562
|
return false;
|
|
244
563
|
}
|
|
245
564
|
});
|
|
246
|
-
const
|
|
565
|
+
const urltestAlgorithmSchema = z.strictObject({
|
|
247
566
|
type: z.literal("urltest"),
|
|
248
567
|
url,
|
|
249
568
|
intervalSeconds: z.number().int().min(1).max(86400).default(180),
|
|
250
569
|
toleranceMs: z.number().int().min(0).max(6e4).default(50)
|
|
251
|
-
})
|
|
252
|
-
const
|
|
570
|
+
});
|
|
571
|
+
const algorithmSchema = z.union([z.strictObject({ type: z.enum(["select", "surge-smart"]) }), urltestAlgorithmSchema]);
|
|
572
|
+
const portableAlgorithmSchema = z.union([z.strictObject({ type: z.enum(["select", "auto"]) }), urltestAlgorithmSchema]);
|
|
573
|
+
const implementationsSchema = z.strictObject({
|
|
574
|
+
surge: algorithmSchema.optional(),
|
|
575
|
+
"sing-box": algorithmSchema.optional(),
|
|
576
|
+
mihomo: algorithmSchema.optional(),
|
|
577
|
+
stash: algorithmSchema.optional()
|
|
578
|
+
});
|
|
579
|
+
const strategyBase = {
|
|
253
580
|
type: z.literal("strategy"),
|
|
254
581
|
id: idSchema,
|
|
255
|
-
members
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
582
|
+
members
|
|
583
|
+
};
|
|
584
|
+
const strategySchema = z.union([z.strictObject({
|
|
585
|
+
...strategyBase,
|
|
586
|
+
algorithm: portableAlgorithmSchema,
|
|
587
|
+
implementations: implementationsSchema.optional()
|
|
588
|
+
}), z.strictObject({
|
|
589
|
+
...strategyBase,
|
|
590
|
+
implementations: implementationsSchema.refine((v) => !!v.surge || !!v["sing-box"] || !!v.mihomo || !!v.stash)
|
|
591
|
+
})]);
|
|
263
592
|
const groupSchema = z.union([
|
|
264
593
|
strategySchema,
|
|
265
594
|
z.strictObject({
|
|
@@ -479,12 +808,13 @@ const defineConfig = (config) => config;
|
|
|
479
808
|
function selectNodes(nodes, query = {}) {
|
|
480
809
|
return nodes.filter((node) => (!query.regions || node.region !== void 0 && query.regions.includes(node.region)) && (!query.providers || node.provider !== void 0 && query.providers.includes(node.provider)) && (!query.tags || query.tags.every((tag) => node.tags?.includes(tag)))).map((node) => node.id);
|
|
481
810
|
}
|
|
482
|
-
/** A shared strategy
|
|
811
|
+
/** A shared strategy declares an algorithm or explicitly selects native implementations. */
|
|
483
812
|
const strategy = (definition) => ({
|
|
484
813
|
type: "strategy",
|
|
485
814
|
...definition
|
|
486
815
|
});
|
|
487
816
|
const group = {
|
|
817
|
+
auto: () => ({ type: "auto" }),
|
|
488
818
|
select: () => ({ type: "select" }),
|
|
489
819
|
urltest: (options) => ({
|
|
490
820
|
type: "urltest",
|
|
@@ -492,6 +822,7 @@ const group = {
|
|
|
492
822
|
})
|
|
493
823
|
};
|
|
494
824
|
const surge$1 = {
|
|
825
|
+
match: surgeMatch,
|
|
495
826
|
smart: () => ({ type: "surge-smart" }),
|
|
496
827
|
ruleSet: (options) => ({
|
|
497
828
|
format: "rule-set",
|
|
@@ -1116,6 +1447,7 @@ const targetCatalog = deepFreeze([
|
|
|
1116
1447
|
{
|
|
1117
1448
|
engine: "sing-box",
|
|
1118
1449
|
version: "1.11.0",
|
|
1450
|
+
autoGroup: "urltest",
|
|
1119
1451
|
platforms: ["linux", "macos"],
|
|
1120
1452
|
protocols: [
|
|
1121
1453
|
"trojan",
|
|
@@ -1132,6 +1464,7 @@ const targetCatalog = deepFreeze([
|
|
|
1132
1464
|
{
|
|
1133
1465
|
engine: "sing-box",
|
|
1134
1466
|
version: "1.12.0",
|
|
1467
|
+
autoGroup: "urltest",
|
|
1135
1468
|
platforms: ["linux", "macos"],
|
|
1136
1469
|
protocols: [
|
|
1137
1470
|
"trojan",
|
|
@@ -1149,6 +1482,7 @@ const targetCatalog = deepFreeze([
|
|
|
1149
1482
|
{
|
|
1150
1483
|
engine: "surge",
|
|
1151
1484
|
version: "6.10.0",
|
|
1485
|
+
autoGroup: "surge-smart",
|
|
1152
1486
|
platforms: ["macos"],
|
|
1153
1487
|
protocols: [
|
|
1154
1488
|
"trojan",
|
|
@@ -1165,6 +1499,7 @@ const targetCatalog = deepFreeze([
|
|
|
1165
1499
|
{
|
|
1166
1500
|
engine: "mihomo",
|
|
1167
1501
|
version: "1.19.32",
|
|
1502
|
+
autoGroup: "urltest",
|
|
1168
1503
|
platforms: ["linux", "macos"],
|
|
1169
1504
|
protocols: [
|
|
1170
1505
|
"trojan",
|
|
@@ -1182,6 +1517,7 @@ const targetCatalog = deepFreeze([
|
|
|
1182
1517
|
{
|
|
1183
1518
|
engine: "stash",
|
|
1184
1519
|
version: "4.2.1",
|
|
1520
|
+
autoGroup: null,
|
|
1185
1521
|
platforms: ["macos"],
|
|
1186
1522
|
protocols: [
|
|
1187
1523
|
"trojan",
|
|
@@ -1219,16 +1555,44 @@ function isReservedNativeName(name, engine) {
|
|
|
1219
1555
|
}
|
|
1220
1556
|
//#endregion
|
|
1221
1557
|
//#region src/strategies.ts
|
|
1222
|
-
function
|
|
1558
|
+
function satisfiesAlgorithm(requested, actual) {
|
|
1559
|
+
if (requested.type === "auto") return actual.type === "urltest" || actual.type === "surge-smart";
|
|
1560
|
+
if (requested.type === "select") return actual.type === "select";
|
|
1561
|
+
return actual.type === "urltest" && actual.url === requested.url && (actual.intervalSeconds ?? 180) === (requested.intervalSeconds ?? 180) && (actual.toleranceMs ?? 50) === (requested.toleranceMs ?? 50);
|
|
1562
|
+
}
|
|
1563
|
+
function lowerStrategies(groups, target, diagnostics, path) {
|
|
1223
1564
|
const selected = [];
|
|
1224
1565
|
const selections = [];
|
|
1566
|
+
const { engine } = target;
|
|
1567
|
+
const descriptor = targetCatalog.find((d) => d.engine === engine && d.version === target.version && d.platforms.includes(target.platform));
|
|
1225
1568
|
for (const group of groups) {
|
|
1226
1569
|
if (group.type !== "strategy") {
|
|
1227
1570
|
selected.push(group);
|
|
1228
1571
|
continue;
|
|
1229
1572
|
}
|
|
1230
|
-
const
|
|
1231
|
-
|
|
1573
|
+
const requested = group.algorithm;
|
|
1574
|
+
if (requested && Object.values(group.implementations ?? {}).some((implementation) => implementation && !satisfiesAlgorithm(requested, implementation))) {
|
|
1575
|
+
diagnostics.push({
|
|
1576
|
+
severity: "error",
|
|
1577
|
+
code: "ALGORITHM_CONTRACT_MISMATCH",
|
|
1578
|
+
path: `${path}.groups.${group.id}.implementations`,
|
|
1579
|
+
message: "平台实现不满足声明的通用算法或固定参数"
|
|
1580
|
+
});
|
|
1581
|
+
continue;
|
|
1582
|
+
}
|
|
1583
|
+
const override = group.implementations?.[engine];
|
|
1584
|
+
let algorithm = override;
|
|
1585
|
+
if (!algorithm && requested) {
|
|
1586
|
+
if (requested.type !== "auto") algorithm = requested;
|
|
1587
|
+
else if (descriptor?.autoGroup === "surge-smart") algorithm = { type: "surge-smart" };
|
|
1588
|
+
else if (descriptor?.autoGroup === "urltest") algorithm = {
|
|
1589
|
+
type: "urltest",
|
|
1590
|
+
url: "https://www.gstatic.com/generate_204",
|
|
1591
|
+
intervalSeconds: 180,
|
|
1592
|
+
toleranceMs: 50
|
|
1593
|
+
};
|
|
1594
|
+
}
|
|
1595
|
+
const code = !algorithm ? requested ? "GROUP_UNSUPPORTED" : "STRATEGY_IMPLEMENTATION_MISSING" : algorithm.type === "surge-smart" && engine !== "surge" ? "ALGORITHM_ENGINE_MISMATCH" : void 0;
|
|
1232
1596
|
if (code) {
|
|
1233
1597
|
diagnostics.push({
|
|
1234
1598
|
severity: "error",
|
|
@@ -1247,7 +1611,11 @@ function lowerStrategies(groups, engine, diagnostics, path) {
|
|
|
1247
1611
|
id: group.id,
|
|
1248
1612
|
engine,
|
|
1249
1613
|
algorithm,
|
|
1250
|
-
members: group.members
|
|
1614
|
+
members: group.members,
|
|
1615
|
+
...requested ? {
|
|
1616
|
+
requestedAlgorithm: requested,
|
|
1617
|
+
implementationSource: override ? "override" : "default"
|
|
1618
|
+
} : {}
|
|
1251
1619
|
});
|
|
1252
1620
|
}
|
|
1253
1621
|
return {
|
|
@@ -1344,7 +1712,7 @@ function compileStrategyGroups(input) {
|
|
|
1344
1712
|
if ([...nativeNames.values()].some((name) => !nativeNameSchema.safeParse(name).success)) error("INVALID_NATIVE_NAME", "names", "最终名称不符合原生名称约束");
|
|
1345
1713
|
if ([...nativeNames.values()].some((value) => isReservedNativeName(value, target.engine))) error("INVALID_NATIVE_NAME", "names", "最终名称与目标内建策略冲突");
|
|
1346
1714
|
if (new Set(nativeNames.values()).size !== nativeNames.size) error("DUPLICATE_NATIVE_NAME", "names", "最终节点和策略名称必须唯一");
|
|
1347
|
-
const { groups, selections } = lowerStrategies(strategies, target
|
|
1715
|
+
const { groups, selections } = lowerStrategies(strategies, target, diagnostics, "$");
|
|
1348
1716
|
const all = new Set(ids);
|
|
1349
1717
|
for (const g of groups) {
|
|
1350
1718
|
if (!descriptor.groups.includes(g.type)) error("GROUP_UNSUPPORTED", `strategies.${g.id}`, "目标后端不能保持此算法的参数语义");
|
|
@@ -1476,7 +1844,7 @@ function compile(input) {
|
|
|
1476
1844
|
continue;
|
|
1477
1845
|
}
|
|
1478
1846
|
const originalProgram = config.programs[target.program];
|
|
1479
|
-
const lowered = lowerStrategies(originalProgram?.groups ?? [], target
|
|
1847
|
+
const lowered = lowerStrategies(originalProgram?.groups ?? [], target, diagnostics, path);
|
|
1480
1848
|
const program = originalProgram ? {
|
|
1481
1849
|
...originalProgram,
|
|
1482
1850
|
groups: lowered.groups
|
|
@@ -1785,13 +2153,49 @@ function surge(plan, secret) {
|
|
|
1785
2153
|
}
|
|
1786
2154
|
//#endregion
|
|
1787
2155
|
//#region src/host.ts
|
|
2156
|
+
const buildHints = {
|
|
2157
|
+
INVALID_OUTPUT_DIRECTORY: "提供非空输出目录路径",
|
|
2158
|
+
OUTPUT_LOCKED: "确认没有构建正在运行;检查并处理遗留锁后重试,不要删除仍在使用的锁",
|
|
2159
|
+
SECRET_MISSING: "在构建环境提供配置引用的非空秘密变量",
|
|
2160
|
+
SECRET_RESOLUTION_FAILED: "检查秘密解析器;原始异常不会进入构建报告",
|
|
2161
|
+
PUBLISHED_CLEANUP_FAILED: "活动指针已经切换;检查残留锁和临时文件,不要假设发布失败",
|
|
2162
|
+
FAILED_CLEANUP_REQUIRED: "旧活动指针保留;检查本次残留产物和锁后重试"
|
|
2163
|
+
};
|
|
2164
|
+
function outputFailure(error) {
|
|
2165
|
+
const code = error && typeof error === "object" ? Object.getOwnPropertyDescriptor(error, "code")?.value : void 0;
|
|
2166
|
+
if (code === "EACCES" || code === "EPERM") return {
|
|
2167
|
+
reason: "ACCESS_DENIED",
|
|
2168
|
+
hint: "检查输出目录的写入和遍历权限"
|
|
2169
|
+
};
|
|
2170
|
+
if (code === "ENOSPC" || code === "EDQUOT") return {
|
|
2171
|
+
reason: "NO_SPACE",
|
|
2172
|
+
hint: "释放磁盘空间或调整存储配额后重试"
|
|
2173
|
+
};
|
|
2174
|
+
if (code === "EROFS") return {
|
|
2175
|
+
reason: "READ_ONLY",
|
|
2176
|
+
hint: "选择可写的本地输出目录"
|
|
2177
|
+
};
|
|
2178
|
+
if ([
|
|
2179
|
+
"ENOTDIR",
|
|
2180
|
+
"EISDIR",
|
|
2181
|
+
"EEXIST",
|
|
2182
|
+
"UNSAFE_DIRECTORY"
|
|
2183
|
+
].includes(code)) return {
|
|
2184
|
+
reason: "INVALID_DIRECTORY",
|
|
2185
|
+
hint: "检查输出根、generations 和 active.json 的文件类型;输出目录不能是符号链接"
|
|
2186
|
+
};
|
|
2187
|
+
return {
|
|
2188
|
+
reason: "IO_ERROR",
|
|
2189
|
+
hint: "检查输出目录及文件系统状态,旧活动配置在发布前失败时保持不变"
|
|
2190
|
+
};
|
|
2191
|
+
}
|
|
1788
2192
|
async function directory(path) {
|
|
1789
2193
|
await mkdir(path, {
|
|
1790
2194
|
recursive: true,
|
|
1791
2195
|
mode: 448
|
|
1792
2196
|
});
|
|
1793
2197
|
const stat = await lstat(path);
|
|
1794
|
-
if (!stat.isDirectory() || stat.isSymbolicLink()) throw new Error("UNSAFE_DIRECTORY");
|
|
2198
|
+
if (!stat.isDirectory() || stat.isSymbolicLink()) throw Object.assign(/* @__PURE__ */ new Error("UNSAFE_DIRECTORY"), { code: "UNSAFE_DIRECTORY" });
|
|
1795
2199
|
}
|
|
1796
2200
|
/** All targets publish together by one active.json rename. Config files never go to stdout. */
|
|
1797
2201
|
async function buildToDirectory(compilation, output, resolveSecret = (token) => process.env[token.name]) {
|
|
@@ -1800,6 +2204,12 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1800
2204
|
exitCode: 1,
|
|
1801
2205
|
code: "COMPILATION_FAILED"
|
|
1802
2206
|
};
|
|
2207
|
+
if (typeof output !== "string" || !output.trim()) return {
|
|
2208
|
+
published: false,
|
|
2209
|
+
exitCode: 1,
|
|
2210
|
+
code: "INVALID_OUTPUT_DIRECTORY",
|
|
2211
|
+
hint: buildHints.INVALID_OUTPUT_DIRECTORY
|
|
2212
|
+
};
|
|
1803
2213
|
const root = resolve(output);
|
|
1804
2214
|
if (compilation.plans.some((plan) => plan.program.endpoints?.some((endpoint) => pathsOverlap(root, endpoint.stateDirectory)))) return {
|
|
1805
2215
|
published: false,
|
|
@@ -1816,6 +2226,7 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1816
2226
|
let published = false;
|
|
1817
2227
|
let code = "BUILD_FAILED";
|
|
1818
2228
|
let cleanupFailed = false;
|
|
2229
|
+
let detail = {};
|
|
1819
2230
|
try {
|
|
1820
2231
|
await directory(root);
|
|
1821
2232
|
try {
|
|
@@ -1825,7 +2236,8 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1825
2236
|
if (error.code === "EEXIST") return {
|
|
1826
2237
|
published: false,
|
|
1827
2238
|
exitCode: 1,
|
|
1828
|
-
code: "OUTPUT_LOCKED"
|
|
2239
|
+
code: "OUTPUT_LOCKED",
|
|
2240
|
+
hint: buildHints.OUTPUT_LOCKED
|
|
1829
2241
|
};
|
|
1830
2242
|
throw error;
|
|
1831
2243
|
}
|
|
@@ -1873,6 +2285,7 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1873
2285
|
code = "PUBLISHED";
|
|
1874
2286
|
} catch (error) {
|
|
1875
2287
|
code = error instanceof RenderError ? error.code : "OUTPUT_FAILED";
|
|
2288
|
+
detail = error instanceof RenderError ? { hint: buildHints[code] } : outputFailure(error);
|
|
1876
2289
|
} finally {
|
|
1877
2290
|
if (locked) {
|
|
1878
2291
|
try {
|
|
@@ -1892,10 +2305,13 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1892
2305
|
}
|
|
1893
2306
|
}
|
|
1894
2307
|
}
|
|
2308
|
+
const resultCode = cleanupFailed ? published ? "PUBLISHED_CLEANUP_FAILED" : "FAILED_CLEANUP_REQUIRED" : code;
|
|
1895
2309
|
return {
|
|
1896
2310
|
published,
|
|
1897
2311
|
exitCode: published ? cleanupFailed ? 2 : 0 : 1,
|
|
1898
|
-
code:
|
|
2312
|
+
code: resultCode,
|
|
2313
|
+
...detail,
|
|
2314
|
+
...buildHints[resultCode] ? { hint: buildHints[resultCode] } : {},
|
|
1899
2315
|
...published ? {
|
|
1900
2316
|
active: join(root, "active.json"),
|
|
1901
2317
|
generation
|
|
@@ -1903,4 +2319,4 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
|
|
|
1903
2319
|
};
|
|
1904
2320
|
}
|
|
1905
2321
|
//#endregion
|
|
1906
|
-
export { slot as A, route as C, selectNodes as D, secret as E, deepFreeze as F, idSchema as I, nodeSchema as L, surge$1 as M, use as N, sequence as O, assertData as P, requestSchema as R, reject as S, ruleSet as T, group as _, explain as a, policy$1 as b, nativeNameSchema as c, surgeProxy as d, validateProxyOptions as f, direct as g, defineConfig as h, compile as i, strategy as j, singbox$1 as k, targetCatalog as l, block as m, render as n, compileStrategyGroups as o, StaticRuleSetUnknownError as p, secretReader as r, isReservedNativeName as s, buildToDirectory as t, RenderError as u, match as v, rule as w, proxy as x, mihomo as y };
|
|
2322
|
+
export { slot as A, defineProxyGroup as B, route as C, selectNodes as D, secret as E, deepFreeze as F, defineSurgeProfile as H, idSchema as I, nodeSchema as L, surge$1 as M, use as N, sequence as O, assertData as P, requestSchema as R, reject as S, ruleSet as T, outbound as U, defineRoute as V, selectProxies as W, group as _, explain as a, policy$1 as b, nativeNameSchema as c, surgeProxy as d, validateProxyOptions as f, direct as g, defineConfig as h, compile as i, strategy as j, singbox$1 as k, targetCatalog as l, block as m, render as n, compileStrategyGroups as o, StaticRuleSetUnknownError as p, secretReader as r, isReservedNativeName as s, buildToDirectory as t, RenderError as u, match as v, rule as w, proxy as x, mihomo as y, ProfileDefinitionError as z };
|