proxylang 0.1.4 → 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/dist/cli.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { R as requestSchema, a as explain, i as compile, l as targetCatalog, p as StaticRuleSetUnknownError, t as buildToDirectory } from "./host-BPBSZ-jw.js";
2
+ import { R as requestSchema, a as explain, i as compile, l as targetCatalog, p as StaticRuleSetUnknownError, t as buildToDirectory } from "./host-Bea3np6i.js";
3
3
  import { open } from "node:fs/promises";
4
4
  import { resolve } from "node:path";
5
5
  import { pathToFileURL } from "node:url";
@@ -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])?$/);
@@ -503,6 +822,7 @@ const group = {
503
822
  })
504
823
  };
505
824
  const surge$1 = {
825
+ match: surgeMatch,
506
826
  smart: () => ({ type: "surge-smart" }),
507
827
  ruleSet: (options) => ({
508
828
  format: "rule-set",
@@ -1999,4 +2319,4 @@ async function buildToDirectory(compilation, output, resolveSecret = (token) =>
1999
2319
  };
2000
2320
  }
2001
2321
  //#endregion
2002
- 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 };
package/dist/index.d.ts CHANGED
@@ -298,6 +298,202 @@ export type Explanation = {
298
298
  }[];
299
299
  };
300
300
  //#endregion
301
+ //#region src/surge-profile.d.ts
302
+ declare const policy$1: z.ZodUnion<readonly [z.ZodObject<{
303
+ ref: z.ZodString;
304
+ }, z.core.$strict>, z.ZodObject<{
305
+ builtin: z.ZodEnum<{
306
+ DIRECT: "DIRECT";
307
+ REJECT: "REJECT";
308
+ "REJECT-DROP": "REJECT-DROP";
309
+ "REJECT-NO-DROP": "REJECT-NO-DROP";
310
+ "REJECT-TINYGIF": "REJECT-TINYGIF";
311
+ }>;
312
+ }, z.core.$strict>]>;
313
+ type SurgePolicy = z.infer<typeof policy$1>;
314
+ declare const generalSchema: z.ZodObject<{
315
+ loglevel: z.ZodOptional<z.ZodEnum<{
316
+ info: "info";
317
+ notify: "notify";
318
+ verbose: "verbose";
319
+ warning: "warning";
320
+ }>>;
321
+ "dns-server": z.ZodOptional<z.ZodArray<z.ZodString>>;
322
+ "encrypted-dns-server": z.ZodOptional<z.ZodArray<z.ZodString>>;
323
+ "show-error-page-for-reject": z.ZodOptional<z.ZodBoolean>;
324
+ "exclude-simple-hostnames": z.ZodOptional<z.ZodBoolean>;
325
+ "internet-test-url": z.ZodOptional<z.ZodString>;
326
+ "skip-proxy": z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodString, z.ZodString]>>>;
327
+ ipv6: z.ZodOptional<z.ZodBoolean>;
328
+ "ipv6-vif": z.ZodOptional<z.ZodEnum<{
329
+ always: "always";
330
+ auto: "auto";
331
+ disabled: "disabled";
332
+ }>>;
333
+ "bypass-system": z.ZodOptional<z.ZodBoolean>;
334
+ replica: z.ZodOptional<z.ZodBoolean>;
335
+ "tls-provider": z.ZodOptional<z.ZodEnum<{
336
+ default: "default";
337
+ openssl: "openssl";
338
+ }>>;
339
+ "bypass-tun": z.ZodOptional<z.ZodArray<z.ZodString>>;
340
+ "always-real-ip": z.ZodOptional<z.ZodArray<z.ZodString>>;
341
+ "http-api": z.ZodOptional<z.ZodObject<{
342
+ password: z.ZodObject<{
343
+ type: z.ZodLiteral<"env">;
344
+ name: z.ZodString;
345
+ }, z.core.$strict>;
346
+ listen: z.ZodString;
347
+ }, z.core.$strict>>;
348
+ "http-api-web-dashboard": z.ZodOptional<z.ZodBoolean>;
349
+ "external-controller-access": z.ZodOptional<z.ZodObject<{
350
+ password: z.ZodObject<{
351
+ type: z.ZodLiteral<"env">;
352
+ name: z.ZodString;
353
+ }, z.core.$strict>;
354
+ listen: z.ZodString;
355
+ }, z.core.$strict>>;
356
+ "hijack-dns": z.ZodOptional<z.ZodArray<z.ZodString>>;
357
+ "http-listen": z.ZodOptional<z.ZodString>;
358
+ "socks5-listen": z.ZodOptional<z.ZodString>;
359
+ "proxy-restricted-to-lan": z.ZodOptional<z.ZodBoolean>;
360
+ }, z.core.$strict>;
361
+ type SurgeGeneral = z.infer<typeof generalSchema>;
362
+ declare const hostSchema: z.ZodObject<{
363
+ pattern: z.ZodString;
364
+ address: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>;
365
+ server: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>;
366
+ }, z.core.$strict>;
367
+ type SurgeHost = z.infer<typeof hostSchema>;
368
+ declare const tunnelSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
369
+ type: z.ZodLiteral<"tailscale">;
370
+ id: z.ZodString;
371
+ sectionName: z.ZodString;
372
+ authKey: z.ZodObject<{
373
+ type: z.ZodLiteral<"env">;
374
+ name: z.ZodString;
375
+ }, z.core.$strict>;
376
+ hostname: z.ZodString;
377
+ dnsServer: z.ZodOptional<z.ZodArray<z.ZodString>>;
378
+ autoAddMagicDnsRule: z.ZodOptional<z.ZodBoolean>;
379
+ idleKeepalive: z.ZodOptional<z.ZodNumber>;
380
+ }, z.core.$strict>, z.ZodObject<{
381
+ type: z.ZodLiteral<"wireguard">;
382
+ id: z.ZodString;
383
+ sectionName: z.ZodString;
384
+ privateKey: z.ZodObject<{
385
+ type: z.ZodLiteral<"env">;
386
+ name: z.ZodString;
387
+ }, z.core.$strict>;
388
+ selfIp: z.ZodString;
389
+ dnsServer: z.ZodOptional<z.ZodArray<z.ZodString>>;
390
+ mtu: z.ZodOptional<z.ZodNumber>;
391
+ peers: z.ZodArray<z.ZodObject<{
392
+ publicKey: z.ZodString;
393
+ endpoint: z.ZodString;
394
+ allowedIps: z.ZodArray<z.ZodString>;
395
+ presharedKey: z.ZodOptional<z.ZodObject<{
396
+ type: z.ZodLiteral<"env">;
397
+ name: z.ZodString;
398
+ }, z.core.$strict>>;
399
+ persistentKeepalive: z.ZodOptional<z.ZodNumber>;
400
+ }, z.core.$strict>>;
401
+ }, z.core.$strict>], "type">;
402
+ type SurgeTunnel = z.infer<typeof tunnelSchema>;
403
+ declare const groupSchema: z.ZodObject<{
404
+ id: z.ZodString;
405
+ type: z.ZodEnum<{
406
+ select: "select";
407
+ smart: "smart";
408
+ }>;
409
+ members: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
410
+ ref: z.ZodString;
411
+ }, z.core.$strict>, z.ZodObject<{
412
+ builtin: z.ZodEnum<{
413
+ DIRECT: "DIRECT";
414
+ REJECT: "REJECT";
415
+ "REJECT-DROP": "REJECT-DROP";
416
+ "REJECT-NO-DROP": "REJECT-NO-DROP";
417
+ "REJECT-TINYGIF": "REJECT-TINYGIF";
418
+ }>;
419
+ }, z.core.$strict>]>>;
420
+ url: z.ZodOptional<z.ZodString>;
421
+ intervalSeconds: z.ZodOptional<z.ZodNumber>;
422
+ timeoutSeconds: z.ZodOptional<z.ZodNumber>;
423
+ }, z.core.$strict>;
424
+ type SurgeGroup = z.infer<typeof groupSchema>;
425
+ type ConditionOptions = {
426
+ readonly noResolve?: boolean;
427
+ readonly extendedMatching?: boolean;
428
+ };
429
+ type SurgeCondition = ConditionOptions & ({
430
+ readonly type: "domain" | "domainSuffix" | "ipCidr" | "ipCidr6" | "sourceIp" | "processName" | "macAddress";
431
+ readonly value: string;
432
+ } | {
433
+ readonly type: "port" | "sourcePort";
434
+ readonly from: number;
435
+ readonly to?: number;
436
+ } | {
437
+ readonly type: "network";
438
+ readonly value: "tcp" | "udp";
439
+ } | {
440
+ readonly type: "all" | "any";
441
+ readonly conditions: readonly SurgeCondition[];
442
+ } | {
443
+ readonly type: "not";
444
+ readonly condition: SurgeCondition;
445
+ } | {
446
+ readonly type: "ruleSet";
447
+ readonly format: "rule-set" | "domain-set";
448
+ readonly url?: string;
449
+ readonly builtin?: "LAN" | "SYSTEM";
450
+ });
451
+ type SurgeRule = {
452
+ readonly when: SurgeCondition;
453
+ readonly then: SurgePolicy;
454
+ };
455
+ declare const managedSchema: z.ZodObject<{
456
+ url: z.ZodString;
457
+ query: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
458
+ type: z.ZodLiteral<"env">;
459
+ name: z.ZodString;
460
+ }, z.core.$strict>]>>>;
461
+ intervalSeconds: z.ZodNumber;
462
+ strict: z.ZodBoolean;
463
+ }, z.core.$strict>;
464
+ type SurgeProfileInput = {
465
+ readonly target: {
466
+ readonly engine: "surge";
467
+ readonly platform: "macos" | "ios";
468
+ readonly version: string;
469
+ };
470
+ readonly managed?: z.infer<typeof managedSchema>;
471
+ readonly general?: SurgeGeneral;
472
+ readonly hosts?: readonly SurgeHost[];
473
+ readonly nodes: readonly ProxyNode[];
474
+ readonly tunnels?: readonly SurgeTunnel[];
475
+ readonly groups: readonly SurgeGroup[];
476
+ readonly rules: readonly SurgeRule[];
477
+ readonly fallback: SurgePolicy;
478
+ readonly names?: Readonly<Record<string, string>>;
479
+ };
480
+ type SurgeProfileCompilation = {
481
+ readonly ok: boolean;
482
+ readonly artifactKind: "surge-profile";
483
+ readonly diagnostics: readonly Diagnostic[];
484
+ readonly bindings: readonly {
485
+ readonly id: string;
486
+ readonly nativeName: string;
487
+ }[];
488
+ };
489
+ type SurgeProfileArtifact = {
490
+ readonly artifactKind: "surge-profile";
491
+ readonly content: string;
492
+ };
493
+ export declare function compileSurgeProfile(input: unknown): SurgeProfileCompilation;
494
+ export declare function renderSurgeProfile(compilation: SurgeProfileCompilation, resolveSecret: (token: Secret) => string | undefined): SurgeProfileArtifact;
495
+ export declare function renderSurgeProfiles(compilations: readonly SurgeProfileCompilation[], resolveSecret: (token: Secret) => string | undefined): readonly SurgeProfileArtifact[];
496
+ //#endregion
301
497
  //#region src/proxy-builder.d.ts
302
498
  type ProxyBuilder<N extends ProxyNode> = {
303
499
  readonly via: (id: string) => ProxyBuilder<N>;
@@ -372,6 +568,63 @@ export declare const group: {
372
568
  }>;
373
569
  };
374
570
  export declare const surge: {
571
+ match: {
572
+ domain: (value: string, options?: {
573
+ readonly noResolve?: boolean;
574
+ readonly extendedMatching?: boolean;
575
+ }) => SurgeCondition;
576
+ domainSuffix: (value: string, options?: {
577
+ readonly noResolve?: boolean;
578
+ readonly extendedMatching?: boolean;
579
+ }) => SurgeCondition;
580
+ ipCidr: (value: string, options?: {
581
+ readonly noResolve?: boolean;
582
+ readonly extendedMatching?: boolean;
583
+ }) => SurgeCondition;
584
+ ipCidr6: (value: string, options?: {
585
+ readonly noResolve?: boolean;
586
+ readonly extendedMatching?: boolean;
587
+ }) => SurgeCondition;
588
+ sourceIp: (value: string, options?: {
589
+ readonly noResolve?: boolean;
590
+ readonly extendedMatching?: boolean;
591
+ }) => SurgeCondition;
592
+ processName: (value: string, options?: {
593
+ readonly noResolve?: boolean;
594
+ readonly extendedMatching?: boolean;
595
+ }) => SurgeCondition;
596
+ macAddress: (value: string, options?: {
597
+ readonly noResolve?: boolean;
598
+ readonly extendedMatching?: boolean;
599
+ }) => SurgeCondition;
600
+ network: (value: "tcp" | "udp", options?: {
601
+ readonly noResolve?: boolean;
602
+ readonly extendedMatching?: boolean;
603
+ }) => SurgeCondition;
604
+ port: (from: number, to?: number, options?: {
605
+ readonly noResolve?: boolean;
606
+ readonly extendedMatching?: boolean;
607
+ }) => SurgeCondition;
608
+ sourcePort: (from: number, to?: number, options?: {
609
+ readonly noResolve?: boolean;
610
+ readonly extendedMatching?: boolean;
611
+ }) => SurgeCondition;
612
+ all: (conditions: readonly SurgeCondition[], options?: {
613
+ readonly noResolve?: boolean;
614
+ readonly extendedMatching?: boolean;
615
+ }) => SurgeCondition;
616
+ any: (conditions: readonly SurgeCondition[], options?: {
617
+ readonly noResolve?: boolean;
618
+ readonly extendedMatching?: boolean;
619
+ }) => SurgeCondition;
620
+ not: (value: SurgeCondition, options?: {
621
+ readonly noResolve?: boolean;
622
+ readonly extendedMatching?: boolean;
623
+ }) => SurgeCondition;
624
+ ruleSet: (options: Omit<Extract<SurgeCondition, {
625
+ type: "ruleSet";
626
+ }>, "type">) => SurgeCondition;
627
+ };
375
628
  smart: () => Extract<GroupAlgorithm, {
376
629
  type: "surge-smart";
377
630
  }>;
@@ -591,202 +844,6 @@ type ProxyNodeArtifact = {
591
844
  export declare function compileProxyNodes(input: unknown): ProxyNodeCompilation;
592
845
  export declare function renderProxyNodes(compilation: ProxyNodeCompilation, resolveSecret: (token: Secret) => string | undefined): ProxyNodeArtifact;
593
846
  //#endregion
594
- //#region src/surge-profile.d.ts
595
- declare const policy$1: z.ZodUnion<readonly [z.ZodObject<{
596
- ref: z.ZodString;
597
- }, z.core.$strict>, z.ZodObject<{
598
- builtin: z.ZodEnum<{
599
- DIRECT: "DIRECT";
600
- REJECT: "REJECT";
601
- "REJECT-DROP": "REJECT-DROP";
602
- "REJECT-NO-DROP": "REJECT-NO-DROP";
603
- "REJECT-TINYGIF": "REJECT-TINYGIF";
604
- }>;
605
- }, z.core.$strict>]>;
606
- type SurgePolicy = z.infer<typeof policy$1>;
607
- declare const generalSchema: z.ZodObject<{
608
- loglevel: z.ZodOptional<z.ZodEnum<{
609
- info: "info";
610
- notify: "notify";
611
- verbose: "verbose";
612
- warning: "warning";
613
- }>>;
614
- "dns-server": z.ZodOptional<z.ZodArray<z.ZodString>>;
615
- "encrypted-dns-server": z.ZodOptional<z.ZodArray<z.ZodString>>;
616
- "show-error-page-for-reject": z.ZodOptional<z.ZodBoolean>;
617
- "exclude-simple-hostnames": z.ZodOptional<z.ZodBoolean>;
618
- "internet-test-url": z.ZodOptional<z.ZodString>;
619
- "skip-proxy": z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodString, z.ZodString]>>>;
620
- ipv6: z.ZodOptional<z.ZodBoolean>;
621
- "ipv6-vif": z.ZodOptional<z.ZodEnum<{
622
- always: "always";
623
- auto: "auto";
624
- disabled: "disabled";
625
- }>>;
626
- "bypass-system": z.ZodOptional<z.ZodBoolean>;
627
- replica: z.ZodOptional<z.ZodBoolean>;
628
- "tls-provider": z.ZodOptional<z.ZodEnum<{
629
- default: "default";
630
- openssl: "openssl";
631
- }>>;
632
- "bypass-tun": z.ZodOptional<z.ZodArray<z.ZodString>>;
633
- "always-real-ip": z.ZodOptional<z.ZodArray<z.ZodString>>;
634
- "http-api": z.ZodOptional<z.ZodObject<{
635
- password: z.ZodObject<{
636
- type: z.ZodLiteral<"env">;
637
- name: z.ZodString;
638
- }, z.core.$strict>;
639
- listen: z.ZodString;
640
- }, z.core.$strict>>;
641
- "http-api-web-dashboard": z.ZodOptional<z.ZodBoolean>;
642
- "external-controller-access": z.ZodOptional<z.ZodObject<{
643
- password: z.ZodObject<{
644
- type: z.ZodLiteral<"env">;
645
- name: z.ZodString;
646
- }, z.core.$strict>;
647
- listen: z.ZodString;
648
- }, z.core.$strict>>;
649
- "hijack-dns": z.ZodOptional<z.ZodArray<z.ZodString>>;
650
- "http-listen": z.ZodOptional<z.ZodString>;
651
- "socks5-listen": z.ZodOptional<z.ZodString>;
652
- "proxy-restricted-to-lan": z.ZodOptional<z.ZodBoolean>;
653
- }, z.core.$strict>;
654
- type SurgeGeneral = z.infer<typeof generalSchema>;
655
- declare const hostSchema: z.ZodObject<{
656
- pattern: z.ZodString;
657
- address: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>;
658
- server: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodString]>>;
659
- }, z.core.$strict>;
660
- type SurgeHost = z.infer<typeof hostSchema>;
661
- declare const tunnelSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
662
- type: z.ZodLiteral<"tailscale">;
663
- id: z.ZodString;
664
- sectionName: z.ZodString;
665
- authKey: z.ZodObject<{
666
- type: z.ZodLiteral<"env">;
667
- name: z.ZodString;
668
- }, z.core.$strict>;
669
- hostname: z.ZodString;
670
- dnsServer: z.ZodOptional<z.ZodArray<z.ZodString>>;
671
- autoAddMagicDnsRule: z.ZodOptional<z.ZodBoolean>;
672
- idleKeepalive: z.ZodOptional<z.ZodNumber>;
673
- }, z.core.$strict>, z.ZodObject<{
674
- type: z.ZodLiteral<"wireguard">;
675
- id: z.ZodString;
676
- sectionName: z.ZodString;
677
- privateKey: z.ZodObject<{
678
- type: z.ZodLiteral<"env">;
679
- name: z.ZodString;
680
- }, z.core.$strict>;
681
- selfIp: z.ZodString;
682
- dnsServer: z.ZodOptional<z.ZodArray<z.ZodString>>;
683
- mtu: z.ZodOptional<z.ZodNumber>;
684
- peers: z.ZodArray<z.ZodObject<{
685
- publicKey: z.ZodString;
686
- endpoint: z.ZodString;
687
- allowedIps: z.ZodArray<z.ZodString>;
688
- presharedKey: z.ZodOptional<z.ZodObject<{
689
- type: z.ZodLiteral<"env">;
690
- name: z.ZodString;
691
- }, z.core.$strict>>;
692
- persistentKeepalive: z.ZodOptional<z.ZodNumber>;
693
- }, z.core.$strict>>;
694
- }, z.core.$strict>], "type">;
695
- type SurgeTunnel = z.infer<typeof tunnelSchema>;
696
- declare const groupSchema: z.ZodObject<{
697
- id: z.ZodString;
698
- type: z.ZodEnum<{
699
- select: "select";
700
- smart: "smart";
701
- }>;
702
- members: z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
703
- ref: z.ZodString;
704
- }, z.core.$strict>, z.ZodObject<{
705
- builtin: z.ZodEnum<{
706
- DIRECT: "DIRECT";
707
- REJECT: "REJECT";
708
- "REJECT-DROP": "REJECT-DROP";
709
- "REJECT-NO-DROP": "REJECT-NO-DROP";
710
- "REJECT-TINYGIF": "REJECT-TINYGIF";
711
- }>;
712
- }, z.core.$strict>]>>;
713
- url: z.ZodOptional<z.ZodString>;
714
- intervalSeconds: z.ZodOptional<z.ZodNumber>;
715
- timeoutSeconds: z.ZodOptional<z.ZodNumber>;
716
- }, z.core.$strict>;
717
- type SurgeGroup = z.infer<typeof groupSchema>;
718
- type ConditionOptions = {
719
- readonly noResolve?: boolean;
720
- readonly extendedMatching?: boolean;
721
- };
722
- type SurgeCondition = ConditionOptions & ({
723
- readonly type: "domain" | "domainSuffix" | "ipCidr" | "ipCidr6" | "sourceIp" | "processName" | "macAddress";
724
- readonly value: string;
725
- } | {
726
- readonly type: "port" | "sourcePort";
727
- readonly from: number;
728
- readonly to?: number;
729
- } | {
730
- readonly type: "network";
731
- readonly value: "tcp" | "udp";
732
- } | {
733
- readonly type: "all" | "any";
734
- readonly conditions: readonly SurgeCondition[];
735
- } | {
736
- readonly type: "not";
737
- readonly condition: SurgeCondition;
738
- } | {
739
- readonly type: "ruleSet";
740
- readonly format: "rule-set" | "domain-set";
741
- readonly url?: string;
742
- readonly builtin?: "LAN" | "SYSTEM";
743
- });
744
- type SurgeRule = {
745
- readonly when: SurgeCondition;
746
- readonly then: SurgePolicy;
747
- };
748
- declare const managedSchema: z.ZodObject<{
749
- url: z.ZodString;
750
- query: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
751
- type: z.ZodLiteral<"env">;
752
- name: z.ZodString;
753
- }, z.core.$strict>]>>>;
754
- intervalSeconds: z.ZodNumber;
755
- strict: z.ZodBoolean;
756
- }, z.core.$strict>;
757
- type SurgeProfileInput = {
758
- readonly target: {
759
- readonly engine: "surge";
760
- readonly platform: "macos" | "ios";
761
- readonly version: string;
762
- };
763
- readonly managed?: z.infer<typeof managedSchema>;
764
- readonly general?: SurgeGeneral;
765
- readonly hosts?: readonly SurgeHost[];
766
- readonly nodes: readonly ProxyNode[];
767
- readonly tunnels?: readonly SurgeTunnel[];
768
- readonly groups: readonly SurgeGroup[];
769
- readonly rules: readonly SurgeRule[];
770
- readonly fallback: SurgePolicy;
771
- readonly names?: Readonly<Record<string, string>>;
772
- };
773
- type SurgeProfileCompilation = {
774
- readonly ok: boolean;
775
- readonly artifactKind: "surge-profile";
776
- readonly diagnostics: readonly Diagnostic[];
777
- readonly bindings: readonly {
778
- readonly id: string;
779
- readonly nativeName: string;
780
- }[];
781
- };
782
- type SurgeProfileArtifact = {
783
- readonly artifactKind: "surge-profile";
784
- readonly content: string;
785
- };
786
- export declare function compileSurgeProfile(input: unknown): SurgeProfileCompilation;
787
- export declare function renderSurgeProfile(compilation: SurgeProfileCompilation, resolveSecret: (token: Secret) => string | undefined): SurgeProfileArtifact;
788
- export declare function renderSurgeProfiles(compilations: readonly SurgeProfileCompilation[], resolveSecret: (token: Secret) => string | undefined): readonly SurgeProfileArtifact[];
789
- //#endregion
790
847
  //#region src/surge-compose.d.ts
791
848
  /** Bind shared names only for included resources; explicit input mappings remain compiler-checked. */
792
849
  export declare function surgeProfile(input: SurgeProfileInput, names?: Readonly<Record<string, string>>): SurgeProfileInput;
@@ -797,4 +854,63 @@ export declare function surgeMembers(nodes: readonly ProxyNode[], query?: Parame
797
854
  /** Complete-profile batches use the out symlink protocol; the Program host remains unchanged. */
798
855
  export declare function buildSurgeProfilesToDirectory(profiles: Readonly<Record<string, SurgeProfileInput>>, output: string, resolveSecret?: (token: Secret) => string | undefined): Promise<BuildResult>;
799
856
  //#endregion
800
- export type { Artifact, BuildResult, CaseResult, Compilation, ProxyBuilder, ProxyNodeArtifact, ProxyNodeCompilation, ProxyNodeInput, StrategyGroupCompilation, StrategyGroupInput, SurgeCondition, SurgeGeneral, SurgeGroup, SurgeHost, SurgePolicy, SurgeProfileArtifact, SurgeProfileCompilation, SurgeProfileInput, SurgeRule, SurgeTunnel, TargetPlan };
857
+ //#region src/surge-definitions.d.ts
858
+ type Builtin = Extract<SurgePolicy, {
859
+ builtin: string;
860
+ }>;
861
+ type Destination = ProxyNode | ProxyGroupDefinition | SurgeTunnel | Builtin;
862
+ type ProxyGroupDefinition = {
863
+ readonly type: "proxy-group";
864
+ readonly id: string;
865
+ readonly name: string;
866
+ readonly algorithm: Extract<GroupAlgorithm, {
867
+ type: "select" | "surge-smart";
868
+ }>;
869
+ readonly members: readonly Destination[];
870
+ readonly url?: string;
871
+ readonly intervalSeconds?: number;
872
+ readonly timeoutSeconds?: number;
873
+ };
874
+ type RouteDefinition = {
875
+ readonly type: "route";
876
+ readonly id: string;
877
+ readonly when: SurgeCondition;
878
+ readonly to: Destination;
879
+ };
880
+ type SurgeProfileDefinition = Omit<SurgeProfileInput, "groups" | "rules" | "fallback"> & {
881
+ readonly groups: readonly ProxyGroupDefinition[];
882
+ readonly routes: readonly RouteDefinition[];
883
+ readonly fallback: Destination;
884
+ };
885
+ export declare class ProfileDefinitionError extends Error {
886
+ readonly code: string;
887
+ constructor(code: string);
888
+ }
889
+ export declare const outbound: {
890
+ direct: Readonly<{
891
+ readonly builtin: "DIRECT";
892
+ }>;
893
+ reject: Readonly<{
894
+ readonly builtin: "REJECT";
895
+ }>;
896
+ rejectDrop: Readonly<{
897
+ readonly builtin: "REJECT-DROP";
898
+ }>;
899
+ rejectNoDrop: Readonly<{
900
+ readonly builtin: "REJECT-NO-DROP";
901
+ }>;
902
+ rejectTinyGif: Readonly<{
903
+ readonly builtin: "REJECT-TINYGIF";
904
+ }>;
905
+ };
906
+ export declare function defineProxyGroup(input: Omit<ProxyGroupDefinition, "type">): ProxyGroupDefinition;
907
+ export declare function defineRoute(input: Omit<RouteDefinition, "type">): RouteDefinition;
908
+ export declare function selectProxies(nodes: readonly ProxyNode[], query?: {
909
+ ids?: readonly string[];
910
+ regions?: readonly string[];
911
+ providers?: readonly string[];
912
+ tags?: readonly string[];
913
+ }): ProxyNode[];
914
+ export declare function defineSurgeProfile(input: SurgeProfileDefinition): SurgeProfileInput;
915
+ //#endregion
916
+ export type { Artifact, BuildResult, CaseResult, Compilation, Destination, ProxyBuilder, ProxyGroupDefinition, ProxyNodeArtifact, ProxyNodeCompilation, ProxyNodeInput, RouteDefinition, StrategyGroupCompilation, StrategyGroupInput, SurgeCondition, SurgeGeneral, SurgeGroup, SurgeHost, SurgePolicy, SurgeProfileArtifact, SurgeProfileCompilation, SurgeProfileDefinition, SurgeProfileInput, SurgeRule, SurgeTunnel, TargetPlan };
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { A as slot, C as route, D as selectNodes, E as secret, F as deepFreeze, I as idSchema, L as nodeSchema, M as surge, N as use, O as sequence, P as assertData, S as reject, T as ruleSet, _ as group, a as explain, b as policy, c as nativeNameSchema, d as surgeProxy, f as validateProxyOptions, g as direct, h as defineConfig, i as compile, j as strategy, k as singbox, l as targetCatalog, m as block, n as render, o as compileStrategyGroups, p as StaticRuleSetUnknownError, r as secretReader, s as isReservedNativeName, t as buildToDirectory, u as RenderError, v as match, w as rule, x as proxy, y as mihomo } from "./host-BPBSZ-jw.js";
1
+ import { A as slot, B as defineProxyGroup, C as route, D as selectNodes, E as secret, F as deepFreeze, H as defineSurgeProfile, I as idSchema, L as nodeSchema, M as surge, N as use, O as sequence, P as assertData, S as reject, T as ruleSet, U as outbound, V as defineRoute, W as selectProxies, _ as group, a as explain, b as policy, c as nativeNameSchema, d as surgeProxy, f as validateProxyOptions, g as direct, h as defineConfig, i as compile, j as strategy, k as singbox, l as targetCatalog, m as block, n as render, o as compileStrategyGroups, p as StaticRuleSetUnknownError, r as secretReader, s as isReservedNativeName, t as buildToDirectory, u as RenderError, v as match, w as rule, x as proxy, y as mihomo, z as ProfileDefinitionError } from "./host-Bea3np6i.js";
2
2
  import { z } from "zod";
3
3
  import { isIP } from "node:net";
4
4
  import ipaddr from "ipaddr.js";
@@ -744,4 +744,4 @@ async function buildSurgeProfilesToDirectory(profiles, output, resolveSecret = (
744
744
  }
745
745
  }
746
746
  //#endregion
747
- export { RenderError, StaticRuleSetUnknownError, block, buildSurgeProfilesToDirectory, buildToDirectory, compile, compileProxyNodes, compileStrategyGroups, compileSurgeProfile, defineConfig, direct, explain, group, match, mihomo, policy, proxy, reject, render, renderProxyNodes, renderSurgeProfile, renderSurgeProfiles, route, rule, ruleSet, secret, selectNodes, sequence, singbox, slot, strategy, surge, surgeMembers, surgeProfile, targetCatalog, use };
747
+ export { ProfileDefinitionError, RenderError, StaticRuleSetUnknownError, block, buildSurgeProfilesToDirectory, buildToDirectory, compile, compileProxyNodes, compileStrategyGroups, compileSurgeProfile, defineConfig, defineProxyGroup, defineRoute, defineSurgeProfile, direct, explain, group, match, mihomo, outbound, policy, proxy, reject, render, renderProxyNodes, renderSurgeProfile, renderSurgeProfiles, route, rule, ruleSet, secret, selectNodes, selectProxies, sequence, singbox, slot, strategy, surge, surgeMembers, surgeProfile, targetCatalog, use };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "proxylang",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Composable routing policies in TypeScript with explicit platform targets",
5
5
  "homepage": "https://github.com/CorrectRoadH/proxylang#readme",
6
6
  "bugs": {