proxylang 0.6.0 → 0.7.0
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 +14 -0
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +36 -24
- package/dist/index.js +57 -3
- package/dist/{profile-build-DX7t23HZ.js → profile-build-EO6TsFJ-.js} +114 -79
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -417,3 +417,17 @@ Stash 当前拒绝来源 IP/端口、端口范围和组级 URLTest。完整配
|
|
|
417
417
|
- [多设备配置与请求案例](examples/home/proxylang.config.ts):节点筛选、代理链和路由回归案例。
|
|
418
418
|
- [设计与使用文档](docs/README.md):规则组合、平台机制、CLI 与构建边界。
|
|
419
419
|
- [开发工作流](docs/engineering/development/README.md):Concord 管理 Feature 与 Use Case,`vp` 负责构建和测试。
|
|
420
|
+
|
|
421
|
+
### 按域名指定 DNS
|
|
422
|
+
|
|
423
|
+
同一服务器的多个域名可以一起声明,Profile 的 `dns` 支持数组片段组合:
|
|
424
|
+
|
|
425
|
+
```ts
|
|
426
|
+
import { dnsRule } from "proxylang";
|
|
427
|
+
|
|
428
|
+
const domestic = dnsRule(["*.example.cn", "example.cn"], "https://223.5.5.5/dns-query");
|
|
429
|
+
const tailnet = dnsRule("*.ts.net", "100.100.100.100");
|
|
430
|
+
// defineProfile({ ..., dns: [domestic, tailnet] })
|
|
431
|
+
```
|
|
432
|
+
|
|
433
|
+
`*.example.cn` 仅匹配子域,根域需单独列出。函数保留输入顺序,沿用既有 DNS 冲突检查;`DnsRule` 类型仍可用于类型标注。
|
package/dist/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { H as StaticRuleSetUnknownError, N as compile, P as explain, R as targetCatalog, i as buildToDirectory, n as defineBuild, q as requestSchema, r as runBuild, t as checkBuild } from "./profile-build-EO6TsFJ-.js";
|
|
3
3
|
import { lstat, open, stat } from "node:fs/promises";
|
|
4
4
|
import { resolve } from "node:path";
|
|
5
5
|
import { pathToFileURL } from "node:url";
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
//#region src/types.d.ts
|
|
3
|
+
export type DomainSource = "destination" | "tlsSni" | "httpHost";
|
|
3
4
|
export type Network = "tcp" | "udp";
|
|
4
5
|
export type NamedRuleSet = {
|
|
5
6
|
readonly type: "ruleSet";
|
|
@@ -15,7 +16,7 @@ export type NativeRuleSet = {
|
|
|
15
16
|
readonly format: "rule-set" | "domain-set";
|
|
16
17
|
readonly intervalSeconds?: number;
|
|
17
18
|
readonly noResolve?: boolean;
|
|
18
|
-
readonly
|
|
19
|
+
readonly sources?: readonly DomainSource[];
|
|
19
20
|
} | {
|
|
20
21
|
readonly engine: "sing-box";
|
|
21
22
|
readonly format: "source" | "binary";
|
|
@@ -28,7 +29,7 @@ export type NativeRuleSet = {
|
|
|
28
29
|
});
|
|
29
30
|
export type Match = NamedRuleSet | NativeRuleSet | {
|
|
30
31
|
readonly type: "domain" | "domainSuffix";
|
|
31
|
-
readonly
|
|
32
|
+
readonly sources?: readonly DomainSource[];
|
|
32
33
|
readonly value: string;
|
|
33
34
|
} | {
|
|
34
35
|
readonly type: "ipCidr";
|
|
@@ -443,7 +444,7 @@ declare const groupSchema: z.ZodObject<{
|
|
|
443
444
|
type SurgeGroup = z.infer<typeof groupSchema>;
|
|
444
445
|
type ConditionOptions = {
|
|
445
446
|
readonly noResolve?: boolean;
|
|
446
|
-
readonly
|
|
447
|
+
readonly sources?: readonly DomainSource[];
|
|
447
448
|
};
|
|
448
449
|
type SurgeCondition = ConditionOptions & ({
|
|
449
450
|
readonly type: "domain" | "domainSuffix" | "ipCidr" | "ipCidr6" | "sourceIp" | "processName" | "macAddress";
|
|
@@ -663,12 +664,12 @@ type ProfileCondition = Exclude<Match | SurgeCondition, {
|
|
|
663
664
|
readonly type: "all" | "any";
|
|
664
665
|
readonly conditions: readonly ProfileCondition[];
|
|
665
666
|
readonly noResolve?: boolean;
|
|
666
|
-
readonly
|
|
667
|
+
readonly sources?: readonly DomainSource[];
|
|
667
668
|
} | {
|
|
668
669
|
readonly type: "not";
|
|
669
670
|
readonly condition: ProfileCondition;
|
|
670
671
|
readonly noResolve?: boolean;
|
|
671
|
-
readonly
|
|
672
|
+
readonly sources?: readonly DomainSource[];
|
|
672
673
|
};
|
|
673
674
|
type ProfileDestination = string | ProxyNode | ProxyGroupDefinition | SurgeTunnel | TailscaleEndpoint | {
|
|
674
675
|
readonly builtin: "DIRECT" | "REJECT" | "REJECT-DROP" | "REJECT-NO-DROP" | "REJECT-TINYGIF";
|
|
@@ -737,9 +738,11 @@ declare function proxyBuilder<N extends ProxyNode>(node: N): ProxyBuilder<Extrac
|
|
|
737
738
|
}>>;
|
|
738
739
|
//#endregion
|
|
739
740
|
//#region src/dsl.d.ts
|
|
741
|
+
export declare function dnsRule(pattern: string, server: string): DnsRule;
|
|
742
|
+
export declare function dnsRule(patterns: readonly string[], server: string): DnsRule[];
|
|
740
743
|
export declare const ruleSet: (definition: Omit<NamedRuleSet, "type">) => NamedRuleSet;
|
|
741
744
|
type DomainOptions = {
|
|
742
|
-
readonly
|
|
745
|
+
readonly sources?: readonly DomainSource[];
|
|
743
746
|
};
|
|
744
747
|
type IpOptions = {
|
|
745
748
|
readonly noResolve?: boolean;
|
|
@@ -751,12 +754,12 @@ export declare const match: {
|
|
|
751
754
|
};
|
|
752
755
|
ruleSet: (set: NamedRuleSet) => NamedRuleSet;
|
|
753
756
|
domain: (value: string, opts?: DomainOptions) => {
|
|
754
|
-
|
|
757
|
+
sources?: readonly DomainSource[];
|
|
755
758
|
type: "domain";
|
|
756
759
|
value: string;
|
|
757
760
|
};
|
|
758
761
|
domainSuffix: (value: string, opts?: DomainOptions) => {
|
|
759
|
-
|
|
762
|
+
sources?: readonly DomainSource[];
|
|
760
763
|
type: "domainSuffix";
|
|
761
764
|
value: string;
|
|
762
765
|
};
|
|
@@ -856,55 +859,55 @@ export declare const surge: {
|
|
|
856
859
|
match: {
|
|
857
860
|
domain: (value: string, options?: {
|
|
858
861
|
readonly noResolve?: boolean;
|
|
859
|
-
readonly
|
|
862
|
+
readonly sources?: readonly DomainSource[];
|
|
860
863
|
}) => SurgeCondition;
|
|
861
864
|
domainSuffix: (value: string, options?: {
|
|
862
865
|
readonly noResolve?: boolean;
|
|
863
|
-
readonly
|
|
866
|
+
readonly sources?: readonly DomainSource[];
|
|
864
867
|
}) => SurgeCondition;
|
|
865
868
|
ipCidr: (value: string, options?: {
|
|
866
869
|
readonly noResolve?: boolean;
|
|
867
|
-
readonly
|
|
870
|
+
readonly sources?: readonly DomainSource[];
|
|
868
871
|
}) => SurgeCondition;
|
|
869
872
|
ipCidr6: (value: string, options?: {
|
|
870
873
|
readonly noResolve?: boolean;
|
|
871
|
-
readonly
|
|
874
|
+
readonly sources?: readonly DomainSource[];
|
|
872
875
|
}) => SurgeCondition;
|
|
873
876
|
sourceIp: (value: string, options?: {
|
|
874
877
|
readonly noResolve?: boolean;
|
|
875
|
-
readonly
|
|
878
|
+
readonly sources?: readonly DomainSource[];
|
|
876
879
|
}) => SurgeCondition;
|
|
877
880
|
processName: (value: string, options?: {
|
|
878
881
|
readonly noResolve?: boolean;
|
|
879
|
-
readonly
|
|
882
|
+
readonly sources?: readonly DomainSource[];
|
|
880
883
|
}) => SurgeCondition;
|
|
881
884
|
macAddress: (value: string, options?: {
|
|
882
885
|
readonly noResolve?: boolean;
|
|
883
|
-
readonly
|
|
886
|
+
readonly sources?: readonly DomainSource[];
|
|
884
887
|
}) => SurgeCondition;
|
|
885
888
|
network: (value: "tcp" | "udp", options?: {
|
|
886
889
|
readonly noResolve?: boolean;
|
|
887
|
-
readonly
|
|
890
|
+
readonly sources?: readonly DomainSource[];
|
|
888
891
|
}) => SurgeCondition;
|
|
889
892
|
port: (from: number, to?: number, options?: {
|
|
890
893
|
readonly noResolve?: boolean;
|
|
891
|
-
readonly
|
|
894
|
+
readonly sources?: readonly DomainSource[];
|
|
892
895
|
}) => SurgeCondition;
|
|
893
896
|
sourcePort: (from: number, to?: number, options?: {
|
|
894
897
|
readonly noResolve?: boolean;
|
|
895
|
-
readonly
|
|
898
|
+
readonly sources?: readonly DomainSource[];
|
|
896
899
|
}) => SurgeCondition;
|
|
897
900
|
all: (conditions: readonly SurgeCondition[], options?: {
|
|
898
901
|
readonly noResolve?: boolean;
|
|
899
|
-
readonly
|
|
902
|
+
readonly sources?: readonly DomainSource[];
|
|
900
903
|
}) => SurgeCondition;
|
|
901
904
|
any: (conditions: readonly SurgeCondition[], options?: {
|
|
902
905
|
readonly noResolve?: boolean;
|
|
903
|
-
readonly
|
|
906
|
+
readonly sources?: readonly DomainSource[];
|
|
904
907
|
}) => SurgeCondition;
|
|
905
908
|
not: (value: SurgeCondition, options?: {
|
|
906
909
|
readonly noResolve?: boolean;
|
|
907
|
-
readonly
|
|
910
|
+
readonly sources?: readonly DomainSource[];
|
|
908
911
|
}) => SurgeCondition;
|
|
909
912
|
ruleSet: (options: Omit<Extract<SurgeCondition, {
|
|
910
913
|
type: "ruleSet";
|
|
@@ -928,7 +931,7 @@ export declare const surge: {
|
|
|
928
931
|
readonly format: "rule-set" | "domain-set";
|
|
929
932
|
readonly intervalSeconds?: number;
|
|
930
933
|
readonly noResolve?: boolean;
|
|
931
|
-
readonly
|
|
934
|
+
readonly sources?: readonly DomainSource[];
|
|
932
935
|
};
|
|
933
936
|
};
|
|
934
937
|
};
|
|
@@ -1143,7 +1146,7 @@ type BuildDefinition = {
|
|
|
1143
1146
|
readonly outDir?: string;
|
|
1144
1147
|
readonly profiles: Readonly<Record<string, Profile | ProfileDefinition>>;
|
|
1145
1148
|
readonly preflight?: () => readonly string[];
|
|
1146
|
-
readonly
|
|
1149
|
+
readonly secrets?: (token: Secret, context: BuildContext) => string | undefined;
|
|
1147
1150
|
};
|
|
1148
1151
|
type ProfileBuildResult = BuildResult & {
|
|
1149
1152
|
readonly warnings: Readonly<Record<string, number>>;
|
|
@@ -1161,4 +1164,13 @@ export declare function runBuild(input: BuildDefinition, output: string, options
|
|
|
1161
1164
|
/** Stable identity independent of engine and mutable metadata. */
|
|
1162
1165
|
export declare function proxyId(provider: string, label: string): string;
|
|
1163
1166
|
//#endregion
|
|
1164
|
-
|
|
1167
|
+
//#region src/secret-inputs.d.ts
|
|
1168
|
+
type SecretsInput = {
|
|
1169
|
+
readonly values?: Readonly<Record<string, string>>;
|
|
1170
|
+
readonly env?: readonly string[];
|
|
1171
|
+
readonly validation?: Readonly<Record<string, string>>;
|
|
1172
|
+
};
|
|
1173
|
+
/** Capture declarations now; read only referenced, allowed environment names at render time. */
|
|
1174
|
+
export declare function secrets(input: SecretsInput, environment?: Readonly<Record<string, string | undefined>>): (token: Secret, context?: BuildContext) => string | undefined;
|
|
1175
|
+
//#endregion
|
|
1176
|
+
export type { Artifact, BuildContext, BuildDefinition, BuildResult, CaseResult, Compilation, DeepReadonly, Fragments, Profile, ProfileBuildResult, ProfileCheckResult, ProfileCompilation, ProfileCondition, ProfileDefinition, ProfileDestination, ProfileProxy, ProxyBuilder, ProxyGroupDefinition, ProxyNodeArtifact, ProxyNodeCompilation, ProxyNodeInput, RouteDefinition, SecretsInput, StrategyGroupCompilation, StrategyGroupInput, SurgeCondition, SurgeConfig, SurgeGeneral, SurgeGroup, SurgeHost, SurgePolicy, SurgeProfileArtifact, SurgeProfileCompilation, SurgeProfileInput, SurgeRule, SurgeTunnel, TargetPlan };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { A as
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
1
|
+
import { $ as inspectDefinition, A as renderSurgeProfile, B as surgeProxy, C as slot, D as compileProfile, E as use, F as compileStrategyGroups, G as idSchema, H as StaticRuleSetUnknownError, I as isReservedNativeName, J as ProfileDefinitionError, K as nodeSchema, L as nativeNameSchema, M as secretReader, N as compile, O as defineProfile, P as explain, Q as exactDefinition, R as targetCatalog, S as singbox, T as surge, U as assertData, V as validateProxyOptions, W as deepFreeze, X as defineRoute, Y as defineProxyGroup, Z as defineRoutes, _ as rule, a as render, b as selectNodes, c as direct, d as match, et as outbound, f as mihomo, g as route, h as reject, i as buildToDirectory, j as renderSurgeProfiles, k as compileSurgeProfile, l as dnsRule, m as proxy, n as defineBuild, o as block, p as policy, r as runBuild, s as defineConfig, t as checkBuild, tt as selectProxies, u as group, v as ruleSet, w as strategy, x as sequence, y as secret, z as RenderError } from "./profile-build-EO6TsFJ-.js";
|
|
3
2
|
import { z } from "zod";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
4
|
//#region src/proxy-fragments.ts
|
|
5
5
|
const inputSchema = z.strictObject({
|
|
6
6
|
target: z.strictObject({
|
|
@@ -137,4 +137,58 @@ function proxyId(provider, label) {
|
|
|
137
137
|
return `node-${createHash("sha256").update(JSON.stringify([provider, label])).digest("hex").slice(0, 16)}`;
|
|
138
138
|
}
|
|
139
139
|
//#endregion
|
|
140
|
-
|
|
140
|
+
//#region src/secret-inputs.ts
|
|
141
|
+
/** Capture declarations now; read only referenced, allowed environment names at render time. */
|
|
142
|
+
function secrets(input, environment = process.env) {
|
|
143
|
+
let values;
|
|
144
|
+
let validation;
|
|
145
|
+
let env;
|
|
146
|
+
try {
|
|
147
|
+
inspectDefinition(input);
|
|
148
|
+
exactDefinition(input, [
|
|
149
|
+
"values",
|
|
150
|
+
"env",
|
|
151
|
+
"validation"
|
|
152
|
+
]);
|
|
153
|
+
if (!environment || typeof environment !== "object") throw new Error();
|
|
154
|
+
let total = 0;
|
|
155
|
+
const name = /^[A-Z][A-Z0-9_]{0,127}$/;
|
|
156
|
+
const entries = (record) => {
|
|
157
|
+
if (record === void 0) return [];
|
|
158
|
+
exactDefinition(record, Object.keys(record));
|
|
159
|
+
const fields = Object.entries(record);
|
|
160
|
+
if (fields.length > 2e3) throw new Error();
|
|
161
|
+
for (const [key, value] of fields) {
|
|
162
|
+
if (!name.test(key) || typeof value !== "string" || !value.length || value.length > 65536) throw new Error();
|
|
163
|
+
total += value.length;
|
|
164
|
+
if (total > 1048576) throw new Error();
|
|
165
|
+
}
|
|
166
|
+
return fields;
|
|
167
|
+
};
|
|
168
|
+
const fixed = entries(input.values);
|
|
169
|
+
const substitutes = entries(input.validation);
|
|
170
|
+
const allowed = input.env === void 0 ? [] : input.env;
|
|
171
|
+
if (!Array.isArray(allowed) || allowed.length > 2e3 || Array.from(allowed).some((key) => typeof key !== "string" || !name.test(key))) throw new Error();
|
|
172
|
+
values = new Map(fixed);
|
|
173
|
+
validation = new Map(substitutes);
|
|
174
|
+
env = new Set(allowed);
|
|
175
|
+
if (env.size !== allowed.length || [...env].some((key) => values.has(key)) || [...validation.keys()].some((key) => !values.has(key) && !env.has(key))) throw new Error();
|
|
176
|
+
} catch {
|
|
177
|
+
throw new ProfileDefinitionError("INVALID_SECRETS_DEFINITION");
|
|
178
|
+
}
|
|
179
|
+
return (token, context = { validation: false }) => {
|
|
180
|
+
if (context.validation && validation.has(token.name)) return validation.get(token.name);
|
|
181
|
+
if (values.has(token.name)) return values.get(token.name);
|
|
182
|
+
if (!env.has(token.name)) return void 0;
|
|
183
|
+
try {
|
|
184
|
+
const descriptor = Object.getOwnPropertyDescriptor(environment, token.name);
|
|
185
|
+
if (!descriptor) return void 0;
|
|
186
|
+
if (!("value" in descriptor) || descriptor.value !== void 0 && typeof descriptor.value !== "string") throw new Error();
|
|
187
|
+
return descriptor.value;
|
|
188
|
+
} catch {
|
|
189
|
+
throw new RenderError("SECRET_RESOLUTION_FAILED");
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
//#endregion
|
|
194
|
+
export { ProfileDefinitionError, RenderError, StaticRuleSetUnknownError, block, buildToDirectory, checkBuild, compile, compileProfile, compileProxyNodes, compileStrategyGroups, compileSurgeProfile, defineBuild, defineConfig, defineProfile, defineProxyGroup, defineRoute, defineRoutes, direct, dnsRule, explain, group, match, mihomo, outbound, policy, proxy, proxyId, reject, render, renderProxyNodes, renderSurgeProfile, renderSurgeProfiles, route, rule, ruleSet, runBuild, secret, secrets, selectNodes, selectProxies, sequence, singbox, slot, strategy, surge, targetCatalog, use };
|
|
@@ -1,10 +1,78 @@
|
|
|
1
|
-
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { isIP } from "node:net";
|
|
3
1
|
import { z } from "zod";
|
|
2
|
+
import { isIP } from "node:net";
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
4
|
import ipaddr from "ipaddr.js";
|
|
5
5
|
import { stringify } from "yaml";
|
|
6
6
|
import { lstat, mkdir, readlink, rename, rm, symlink, writeFile } from "node:fs/promises";
|
|
7
7
|
import { join, resolve } from "node:path";
|
|
8
|
+
//#region src/domain-sources.ts
|
|
9
|
+
const order = [
|
|
10
|
+
"destination",
|
|
11
|
+
"tlsSni",
|
|
12
|
+
"httpHost"
|
|
13
|
+
];
|
|
14
|
+
const domainSourcesSchema = z.array(z.enum(order)).min(1).max(3).refine((sources) => new Set(sources).size === sources.length);
|
|
15
|
+
function supportsDomainSources(sources, engine) {
|
|
16
|
+
return sources === void 0 || sources.length === 1 && sources[0] === "destination" || engine === "surge" && sources.length === 3;
|
|
17
|
+
}
|
|
18
|
+
function extendedDomainSources(sources) {
|
|
19
|
+
return sources?.length === 3;
|
|
20
|
+
}
|
|
21
|
+
/** Identity only: preserve author snapshots and all non-source options. */
|
|
22
|
+
function domainIdentityFields(value) {
|
|
23
|
+
return Object.entries(value).map(([key, field]) => {
|
|
24
|
+
if (key !== "sources" || field === void 0) return [key, field];
|
|
25
|
+
const sources = field;
|
|
26
|
+
return [key, sources.length === 1 && sources[0] === "destination" ? void 0 : order.filter((source) => sources.includes(source))];
|
|
27
|
+
}).filter(([, field]) => field !== void 0).sort(([a], [b]) => a.localeCompare(b));
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/dns.ts
|
|
31
|
+
const domain$2 = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
|
|
32
|
+
function dohUrl(value) {
|
|
33
|
+
const url = new URL(value);
|
|
34
|
+
if (!/^https:\/\/[^/]+\//.test(value)) url.pathname = "/dns-query";
|
|
35
|
+
return url;
|
|
36
|
+
}
|
|
37
|
+
const server = z.string().max(2048).refine((value) => {
|
|
38
|
+
if (isIP(value) && !value.includes("%")) return true;
|
|
39
|
+
if (!value.startsWith("https://") || /[\s\p{C},()\\"'?#]/u.test(value)) return false;
|
|
40
|
+
try {
|
|
41
|
+
const url = new URL(value);
|
|
42
|
+
const authority = value.slice(8).split("/")[0];
|
|
43
|
+
const path = value.slice(8 + authority.length);
|
|
44
|
+
return !url.username && !url.password && !authority.endsWith(":") && !!url.hostname && (!path || url.pathname === path);
|
|
45
|
+
} catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
const dnsRulesSchema = z.array(z.strictObject({
|
|
50
|
+
pattern: z.string().min(1).max(253).refine((value) => domain$2.test(value.startsWith("*.") ? value.slice(2) : value)),
|
|
51
|
+
server
|
|
52
|
+
})).max(2e3).superRefine((rules, ctx) => {
|
|
53
|
+
if (rules.length > 2e3 || rules.some((rule) => rule.pattern.length > 253 || rule.server.length > 2048)) return;
|
|
54
|
+
for (let i = 0; i < rules.length; i++) for (let j = 0; j < i; j++) {
|
|
55
|
+
const a = rules[i], b = rules[j];
|
|
56
|
+
const x = a.pattern.toLowerCase(), y = b.pattern.toLowerCase();
|
|
57
|
+
if (x === y || overlap(x, y) && a.server !== b.server) {
|
|
58
|
+
ctx.addIssue({
|
|
59
|
+
code: "custom",
|
|
60
|
+
message: "DNS_RULE_CONFLICT"
|
|
61
|
+
});
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
function overlap(a, b) {
|
|
67
|
+
const wildA = a.startsWith("*."), wildB = b.startsWith("*.");
|
|
68
|
+
if (wildA && wildB) return a.slice(1).endsWith(b.slice(1)) || b.slice(1).endsWith(a.slice(1));
|
|
69
|
+
return wildA ? b.endsWith(a.slice(1)) : wildB && a.endsWith(b.slice(1));
|
|
70
|
+
}
|
|
71
|
+
function dnsCondition(rule) {
|
|
72
|
+
const pattern = rule.pattern.toLowerCase();
|
|
73
|
+
return pattern.startsWith("*.") ? { domain_regex: [`^.+\\.${pattern.slice(2).replaceAll(".", "\\.")}$`] } : { domain: [pattern] };
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
8
76
|
//#region src/surge-definitions.ts
|
|
9
77
|
var ProfileDefinitionError = class extends Error {
|
|
10
78
|
code;
|
|
@@ -343,52 +411,6 @@ const surgeMatch = {
|
|
|
343
411
|
}
|
|
344
412
|
};
|
|
345
413
|
//#endregion
|
|
346
|
-
//#region src/dns.ts
|
|
347
|
-
const domain$2 = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/i;
|
|
348
|
-
function dohUrl(value) {
|
|
349
|
-
const url = new URL(value);
|
|
350
|
-
if (!/^https:\/\/[^/]+\//.test(value)) url.pathname = "/dns-query";
|
|
351
|
-
return url;
|
|
352
|
-
}
|
|
353
|
-
const server = z.string().max(2048).refine((value) => {
|
|
354
|
-
if (isIP(value) && !value.includes("%")) return true;
|
|
355
|
-
if (!value.startsWith("https://") || /[\s\p{C},()\\"'?#]/u.test(value)) return false;
|
|
356
|
-
try {
|
|
357
|
-
const url = new URL(value);
|
|
358
|
-
const authority = value.slice(8).split("/")[0];
|
|
359
|
-
const path = value.slice(8 + authority.length);
|
|
360
|
-
return !url.username && !url.password && !authority.endsWith(":") && !!url.hostname && (!path || url.pathname === path);
|
|
361
|
-
} catch {
|
|
362
|
-
return false;
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
const dnsRulesSchema = z.array(z.strictObject({
|
|
366
|
-
pattern: z.string().min(1).max(253).refine((value) => domain$2.test(value.startsWith("*.") ? value.slice(2) : value)),
|
|
367
|
-
server
|
|
368
|
-
})).max(2e3).superRefine((rules, ctx) => {
|
|
369
|
-
if (rules.length > 2e3 || rules.some((rule) => rule.pattern.length > 253 || rule.server.length > 2048)) return;
|
|
370
|
-
for (let i = 0; i < rules.length; i++) for (let j = 0; j < i; j++) {
|
|
371
|
-
const a = rules[i], b = rules[j];
|
|
372
|
-
const x = a.pattern.toLowerCase(), y = b.pattern.toLowerCase();
|
|
373
|
-
if (x === y || overlap(x, y) && a.server !== b.server) {
|
|
374
|
-
ctx.addIssue({
|
|
375
|
-
code: "custom",
|
|
376
|
-
message: "DNS_RULE_CONFLICT"
|
|
377
|
-
});
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
});
|
|
382
|
-
function overlap(a, b) {
|
|
383
|
-
const wildA = a.startsWith("*."), wildB = b.startsWith("*.");
|
|
384
|
-
if (wildA && wildB) return a.slice(1).endsWith(b.slice(1)) || b.slice(1).endsWith(a.slice(1));
|
|
385
|
-
return wildA ? b.endsWith(a.slice(1)) : wildB && a.endsWith(b.slice(1));
|
|
386
|
-
}
|
|
387
|
-
function dnsCondition(rule) {
|
|
388
|
-
const pattern = rule.pattern.toLowerCase();
|
|
389
|
-
return pattern.startsWith("*.") ? { domain_regex: [`^.+\\.${pattern.slice(2).replaceAll(".", "\\.")}$`] } : { domain: [pattern] };
|
|
390
|
-
}
|
|
391
|
-
//#endregion
|
|
392
414
|
//#region src/schema.ts
|
|
393
415
|
const idSchema = z.string().regex(/^[a-z][a-z0-9_-]{0,63}$/).refine((x) => !x.startsWith("pl-"));
|
|
394
416
|
const domain$1 = 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])?$/);
|
|
@@ -430,7 +452,7 @@ const nativeRuleSetSchema = z.discriminatedUnion("engine", [
|
|
|
430
452
|
format: z.enum(["rule-set", "domain-set"]),
|
|
431
453
|
intervalSeconds: updateInterval.optional(),
|
|
432
454
|
noResolve: z.boolean().optional(),
|
|
433
|
-
|
|
455
|
+
sources: domainSourcesSchema.optional()
|
|
434
456
|
}),
|
|
435
457
|
z.strictObject({
|
|
436
458
|
...remoteBase,
|
|
@@ -460,12 +482,12 @@ const matchSchema = z.lazy(() => z.discriminatedUnion("type", [
|
|
|
460
482
|
z.strictObject({
|
|
461
483
|
type: z.literal("domain"),
|
|
462
484
|
value: domain$1,
|
|
463
|
-
|
|
485
|
+
sources: domainSourcesSchema.optional()
|
|
464
486
|
}),
|
|
465
487
|
z.strictObject({
|
|
466
488
|
type: z.literal("domainSuffix"),
|
|
467
489
|
value: domain$1,
|
|
468
|
-
|
|
490
|
+
sources: domainSourcesSchema.optional()
|
|
469
491
|
}),
|
|
470
492
|
z.strictObject({
|
|
471
493
|
type: z.literal("ipCidr"),
|
|
@@ -787,7 +809,7 @@ function deepFreeze(value) {
|
|
|
787
809
|
//#region src/rulesets.ts
|
|
788
810
|
function canonical(value) {
|
|
789
811
|
if (Array.isArray(value)) return `[${value.map(canonical).join(",")}]`;
|
|
790
|
-
if (value && typeof value === "object") return `{${
|
|
812
|
+
if (value && typeof value === "object") return `{${domainIdentityFields(value).map(([k, v]) => `${JSON.stringify(k)}:${canonical(v)}`).join(",")}}`;
|
|
791
813
|
return JSON.stringify(value);
|
|
792
814
|
}
|
|
793
815
|
function validateRuleSets(entries, engine, diagnostics, path) {
|
|
@@ -1008,11 +1030,7 @@ function evaluate(condition, request) {
|
|
|
1008
1030
|
switch (condition.type) {
|
|
1009
1031
|
case "domain":
|
|
1010
1032
|
case "domainSuffix": {
|
|
1011
|
-
const domains = condition.
|
|
1012
|
-
request.domain,
|
|
1013
|
-
request.tlsSni,
|
|
1014
|
-
request.httpHost
|
|
1015
|
-
] : [request.domain];
|
|
1033
|
+
const domains = (condition.sources ?? ["destination"]).map((source) => source === "destination" ? request.domain : request[source]);
|
|
1016
1034
|
present = domains.some((value) => value !== void 0);
|
|
1017
1035
|
matched = domains.some((value) => value !== void 0 && (value === condition.value || condition.type === "domainSuffix" && value.endsWith(`.${condition.value}`)));
|
|
1018
1036
|
break;
|
|
@@ -1811,7 +1829,7 @@ function compile(input) {
|
|
|
1811
1829
|
for (const rule of rules) if (logicalDepth(rule.when) > 10) error("LOGICAL_DEPTH", rule.id, "Surge 逻辑条件最多嵌套 10 层", target.id);
|
|
1812
1830
|
}
|
|
1813
1831
|
const checkOptions = (condition) => {
|
|
1814
|
-
if ("
|
|
1832
|
+
if ("sources" in condition && !supportsDomainSources(condition.sources, target.engine) || condition.type === "ipCidr" && condition.noResolve !== void 0 && target.engine === "sing-box") error("MATCH_OPTION_UNSUPPORTED", path, "目标无法等价实现显式匹配选项", target.id);
|
|
1815
1833
|
if (condition.type === "all" || condition.type === "any") condition.conditions.forEach(checkOptions);
|
|
1816
1834
|
else if (condition.type === "not") checkOptions(condition.condition);
|
|
1817
1835
|
else if (condition.type === "ruleSet") checkOptions(condition.when);
|
|
@@ -2064,7 +2082,7 @@ const groupSchema = z.strictObject({
|
|
|
2064
2082
|
});
|
|
2065
2083
|
const options$1 = {
|
|
2066
2084
|
noResolve: z.boolean().optional(),
|
|
2067
|
-
|
|
2085
|
+
sources: domainSourcesSchema.optional()
|
|
2068
2086
|
};
|
|
2069
2087
|
const atom = (type, value) => z.strictObject({
|
|
2070
2088
|
type: z.literal(type),
|
|
@@ -2239,11 +2257,12 @@ function compileSurgeProfile(input) {
|
|
|
2239
2257
|
"ipCidr6",
|
|
2240
2258
|
"ruleSet"
|
|
2241
2259
|
].includes(c.type)) error("RULE_OPTION_UNSUPPORTED");
|
|
2242
|
-
if (c.
|
|
2260
|
+
if (c.sources !== void 0 && ![
|
|
2243
2261
|
"domain",
|
|
2244
2262
|
"domainSuffix",
|
|
2245
2263
|
"ruleSet"
|
|
2246
2264
|
].includes(c.type)) error("RULE_OPTION_UNSUPPORTED");
|
|
2265
|
+
if (c.sources !== void 0 && !supportsDomainSources(c.sources, "surge")) error("MATCH_OPTION_UNSUPPORTED");
|
|
2247
2266
|
if (p.target.platform === "ios" && ["processName", "macAddress"].includes(c.type)) error("PLATFORM_RULE_UNSUPPORTED");
|
|
2248
2267
|
if (c.type === "all" || c.type === "any") for (const child of c.conditions) checkCondition(child, depth + 1);
|
|
2249
2268
|
if (c.type === "not") checkCondition(c.condition, depth + 1);
|
|
@@ -2282,7 +2301,7 @@ function compileSurgeProfile(input) {
|
|
|
2282
2301
|
function conditionOptions(c) {
|
|
2283
2302
|
return [
|
|
2284
2303
|
...c.noResolve ? ["no-resolve"] : [],
|
|
2285
|
-
...c.
|
|
2304
|
+
...extendedDomainSources(c.sources) ? ["extended-matching"] : [],
|
|
2286
2305
|
...c.type === "ruleSet" && c.intervalSeconds !== void 0 ? [`update-interval=${c.intervalSeconds}`] : []
|
|
2287
2306
|
];
|
|
2288
2307
|
}
|
|
@@ -2521,7 +2540,7 @@ function surgeCondition$1(condition, depth = 0) {
|
|
|
2521
2540
|
url: value.url,
|
|
2522
2541
|
...value.intervalSeconds !== void 0 ? { intervalSeconds: value.intervalSeconds } : {},
|
|
2523
2542
|
...value.noResolve !== void 0 ? { noResolve: value.noResolve } : {},
|
|
2524
|
-
...value.
|
|
2543
|
+
...value.sources !== void 0 ? { sources: value.sources } : {}
|
|
2525
2544
|
};
|
|
2526
2545
|
}
|
|
2527
2546
|
if (condition.type === "all" || condition.type === "any") {
|
|
@@ -2698,6 +2717,21 @@ function proxyBuilder(node) {
|
|
|
2698
2717
|
}
|
|
2699
2718
|
//#endregion
|
|
2700
2719
|
//#region src/dsl.ts
|
|
2720
|
+
function dnsRule(pattern, server) {
|
|
2721
|
+
inspectDefinition({
|
|
2722
|
+
pattern,
|
|
2723
|
+
server
|
|
2724
|
+
});
|
|
2725
|
+
const batch = Array.isArray(pattern);
|
|
2726
|
+
const patterns = batch ? pattern : [pattern];
|
|
2727
|
+
if (patterns.length > 2e3) throw new ProfileDefinitionError("INVALID_DNS_RULE");
|
|
2728
|
+
const checked = dnsRulesSchema.safeParse(patterns.map((value) => ({
|
|
2729
|
+
pattern: value,
|
|
2730
|
+
server
|
|
2731
|
+
})));
|
|
2732
|
+
if (!checked.success) throw new ProfileDefinitionError("INVALID_DNS_RULE");
|
|
2733
|
+
return batch ? checked.data : checked.data[0];
|
|
2734
|
+
}
|
|
2701
2735
|
const ruleSet = (definition) => ({
|
|
2702
2736
|
...definition,
|
|
2703
2737
|
type: "ruleSet"
|
|
@@ -2705,8 +2739,9 @@ const ruleSet = (definition) => ({
|
|
|
2705
2739
|
function options(value, keys) {
|
|
2706
2740
|
inspectDefinition(value);
|
|
2707
2741
|
exactDefinition(value, keys);
|
|
2708
|
-
if (
|
|
2709
|
-
|
|
2742
|
+
if ("sources" in value && value.sources !== void 0 && !domainSourcesSchema.safeParse(value.sources).success) throw new ProfileDefinitionError("INVALID_DOMAIN_SOURCES");
|
|
2743
|
+
if (Object.entries(value).some(([key, v]) => key !== "sources" && v !== void 0 && typeof v !== "boolean")) throw new ProfileDefinitionError("INVALID_MATCH_OPTIONS");
|
|
2744
|
+
return snapshotDefinition(value);
|
|
2710
2745
|
}
|
|
2711
2746
|
const match = {
|
|
2712
2747
|
processName: (value) => {
|
|
@@ -2720,12 +2755,12 @@ const match = {
|
|
|
2720
2755
|
domain: (value, opts = {}) => ({
|
|
2721
2756
|
type: "domain",
|
|
2722
2757
|
value,
|
|
2723
|
-
...options(opts, ["
|
|
2758
|
+
...options(opts, ["sources"])
|
|
2724
2759
|
}),
|
|
2725
2760
|
domainSuffix: (value, opts = {}) => ({
|
|
2726
2761
|
type: "domainSuffix",
|
|
2727
2762
|
value,
|
|
2728
|
-
...options(opts, ["
|
|
2763
|
+
...options(opts, ["sources"])
|
|
2729
2764
|
}),
|
|
2730
2765
|
ipCidr: (value, opts = {}) => ({
|
|
2731
2766
|
type: "ipCidr",
|
|
@@ -2773,7 +2808,7 @@ function nativeConstructor(engine) {
|
|
|
2773
2808
|
"format",
|
|
2774
2809
|
"intervalSeconds",
|
|
2775
2810
|
"noResolve",
|
|
2776
|
-
"
|
|
2811
|
+
"sources",
|
|
2777
2812
|
"behavior",
|
|
2778
2813
|
"to"
|
|
2779
2814
|
]);
|
|
@@ -2784,7 +2819,7 @@ function nativeConstructor(engine) {
|
|
|
2784
2819
|
"url",
|
|
2785
2820
|
"format",
|
|
2786
2821
|
"intervalSeconds",
|
|
2787
|
-
...engine === "surge" ? ["noResolve", "
|
|
2822
|
+
...engine === "surge" ? ["noResolve", "sources"] : engine === "mihomo" ? ["behavior"] : []
|
|
2788
2823
|
]);
|
|
2789
2824
|
const parsed = nativeRuleSetSchema.safeParse({
|
|
2790
2825
|
...engine === "surge" ? { format: "rule-set" } : {},
|
|
@@ -2795,7 +2830,7 @@ function nativeConstructor(engine) {
|
|
|
2795
2830
|
});
|
|
2796
2831
|
if (!parsed.success) throw new ProfileDefinitionError("INVALID_NATIVE_RULESET");
|
|
2797
2832
|
const { id: _id, type: _type, ...fields } = parsed.data;
|
|
2798
|
-
const id = resource.id ?? `ruleset-${createHash("sha256").update(JSON.stringify(
|
|
2833
|
+
const id = resource.id ?? `ruleset-${createHash("sha256").update(JSON.stringify(domainIdentityFields(fields))).digest("hex").slice(0, 16)}`;
|
|
2799
2834
|
const when = snapshotDefinition({
|
|
2800
2835
|
...parsed.data,
|
|
2801
2836
|
id
|
|
@@ -3088,7 +3123,7 @@ function surgeOptions(condition) {
|
|
|
3088
3123
|
return [
|
|
3089
3124
|
...condition.type === "ipCidr" && condition.noResolve !== false ? ["no-resolve"] : [],
|
|
3090
3125
|
...condition.type === "nativeRuleSet" && condition.engine === "surge" && condition.noResolve ? ["no-resolve"] : [],
|
|
3091
|
-
..."
|
|
3126
|
+
..."sources" in condition && extendedDomainSources(condition.sources) ? ["extended-matching"] : []
|
|
3092
3127
|
].map((value) => `,${value}`).join("");
|
|
3093
3128
|
}
|
|
3094
3129
|
function surgeCondition(condition) {
|
|
@@ -3441,7 +3476,7 @@ function checkBuild(input) {
|
|
|
3441
3476
|
}))
|
|
3442
3477
|
};
|
|
3443
3478
|
}
|
|
3444
|
-
async function publish(profiles, output,
|
|
3479
|
+
async function publish(profiles, output, readSecret) {
|
|
3445
3480
|
try {
|
|
3446
3481
|
const checked = compileBatch(profiles, output);
|
|
3447
3482
|
if (!checked.ok) return {
|
|
@@ -3450,7 +3485,7 @@ async function publish(profiles, output, resolver) {
|
|
|
3450
3485
|
code: checked.code
|
|
3451
3486
|
};
|
|
3452
3487
|
const { entries, compiled } = checked;
|
|
3453
|
-
const read = secretReader(
|
|
3488
|
+
const read = secretReader(readSecret);
|
|
3454
3489
|
const files = /* @__PURE__ */ new Map();
|
|
3455
3490
|
for (const [index, profile] of compiled.entries()) {
|
|
3456
3491
|
if (profile.engine === "invalid") throw new Error();
|
|
@@ -3492,17 +3527,17 @@ function defineBuild(input) {
|
|
|
3492
3527
|
record(input, [
|
|
3493
3528
|
"profiles",
|
|
3494
3529
|
"preflight",
|
|
3495
|
-
"
|
|
3530
|
+
"secrets",
|
|
3496
3531
|
"outDir"
|
|
3497
3532
|
]);
|
|
3498
3533
|
if (input.outDir !== void 0 && (typeof input.outDir !== "string" || !input.outDir.trim() || input.outDir.trim() !== input.outDir || input.outDir.includes("\0"))) throw new Error();
|
|
3499
3534
|
record(input.profiles);
|
|
3500
|
-
for (const callback of [input.preflight, input.
|
|
3535
|
+
for (const callback of [input.preflight, input.secrets]) if (callback !== void 0 && typeof callback !== "function") throw new Error();
|
|
3501
3536
|
return Object.freeze({
|
|
3502
3537
|
...input.outDir === void 0 ? {} : { outDir: input.outDir },
|
|
3503
3538
|
profiles: snapshotProfile(input.profiles),
|
|
3504
3539
|
...input.preflight === void 0 ? {} : { preflight: input.preflight },
|
|
3505
|
-
...input.
|
|
3540
|
+
...input.secrets === void 0 ? {} : { secrets: input.secrets }
|
|
3506
3541
|
});
|
|
3507
3542
|
} catch {
|
|
3508
3543
|
throw new ProfileDefinitionError("INVALID_BUILD_DEFINITION");
|
|
@@ -3539,7 +3574,7 @@ async function runBuild(input, output, options = {}) {
|
|
|
3539
3574
|
const failure = (code) => profileBuildFailure(code, validation, profileCount);
|
|
3540
3575
|
if (typeof output !== "string" || !output.trim()) return failure("INVALID_OUTPUT_DIRECTORY");
|
|
3541
3576
|
output = resolve(root, output);
|
|
3542
|
-
if (validation && !definition.
|
|
3577
|
+
if (validation && !definition.secrets) return failure("VALIDATION_SECRETS_REQUIRED");
|
|
3543
3578
|
let codes;
|
|
3544
3579
|
try {
|
|
3545
3580
|
codes = definition.preflight ? definition.preflight() : [];
|
|
@@ -3555,13 +3590,13 @@ async function runBuild(input, output, options = {}) {
|
|
|
3555
3590
|
const warnings = {};
|
|
3556
3591
|
for (const code of codes) warnings[code] = (warnings[code] ?? 0) + 1;
|
|
3557
3592
|
const context = Object.freeze({ validation });
|
|
3558
|
-
const
|
|
3593
|
+
const readSecret = definition.secrets;
|
|
3559
3594
|
return {
|
|
3560
|
-
...await publish(definition.profiles, output,
|
|
3595
|
+
...await publish(definition.profiles, output, readSecret ? (token) => readSecret(token, context) : (token) => process.env[token.name]),
|
|
3561
3596
|
warnings,
|
|
3562
3597
|
validation,
|
|
3563
3598
|
profileCount
|
|
3564
3599
|
};
|
|
3565
3600
|
}
|
|
3566
3601
|
//#endregion
|
|
3567
|
-
export {
|
|
3602
|
+
export { inspectDefinition as $, renderSurgeProfile as A, surgeProxy as B, slot as C, compileProfile as D, use as E, compileStrategyGroups as F, idSchema as G, StaticRuleSetUnknownError as H, isReservedNativeName as I, ProfileDefinitionError as J, nodeSchema as K, nativeNameSchema as L, secretReader as M, compile as N, defineProfile as O, explain as P, exactDefinition as Q, targetCatalog as R, singbox$1 as S, surge$1 as T, assertData as U, validateProxyOptions as V, deepFreeze as W, defineRoute as X, defineProxyGroup as Y, defineRoutes as Z, rule as _, render as a, selectNodes as b, direct as c, match as d, outbound as et, mihomo as f, route as g, reject as h, buildToDirectory as i, renderSurgeProfiles as j, compileSurgeProfile as k, dnsRule as l, proxy as m, defineBuild as n, block as o, policy as p, requestSchema as q, runBuild as r, defineConfig as s, checkBuild as t, selectProxies as tt, group as u, ruleSet as v, strategy as w, sequence as x, secret as y, RenderError as z };
|
package/package.json
CHANGED