sing-box-kit 0.1.0 → 0.1.2
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 +27 -9
- package/dist/index.d.mts +2573 -596
- package/dist/index.mjs +612 -651
- package/docs/configuration.md +21 -43
- package/docs/dns-rules.md +43 -0
- package/docs/internal/README.md +31 -0
- package/docs/resources.md +47 -0
- package/docs/rules.md +38 -45
- package/docs/settings.md +36 -0
- package/package.json +1 -1
- package/schema/README.md +3 -3
- package/scripts/generate-schema.ts +153 -0
- package/src/compile.ts +35 -59
- package/src/factories.ts +315 -532
- package/src/generated/sbkit-inputs.ts +2671 -0
- package/src/input-schema.ts +299 -0
- package/src/resource.ts +30 -39
- package/src/rules.ts +82 -207
- package/src/schema.ts +15 -23
- package/src/settings.ts +155 -156
- package/src/type-utils.ts +15 -0
- package/vite.config.ts +2 -2
package/src/compile.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { isIP } from "node:net";
|
|
2
|
+
import type { SBKitInputs } from "./generated/sbkit-inputs";
|
|
3
|
+
import { rootSettings, sectionSettings } from "./input-schema";
|
|
2
4
|
import type { SingBoxConfig } from "./generated/sing-box";
|
|
3
|
-
import { copyJson,
|
|
5
|
+
import { copyJson, fieldsFrom, resourceData } from "./resource";
|
|
4
6
|
import type {
|
|
5
7
|
DnsServer,
|
|
6
|
-
Egress,
|
|
7
|
-
Fields,
|
|
8
|
-
ReadonlyJson,
|
|
9
8
|
Resolve,
|
|
10
9
|
Resource,
|
|
11
10
|
ResourceData,
|
|
@@ -15,12 +14,10 @@ import type {
|
|
|
15
14
|
import { checkDnsRules, expandRule } from "./rules";
|
|
16
15
|
import type { RuleValue } from "./rules";
|
|
17
16
|
import { fail, validateConfig } from "./schema";
|
|
18
|
-
import {
|
|
19
|
-
import type {
|
|
17
|
+
import { expandInput, isEmptyDirect } from "./settings";
|
|
18
|
+
import type { Fields, ReadonlyJson } from "./type-utils";
|
|
20
19
|
|
|
21
|
-
export interface CompileInput {
|
|
22
|
-
readonly log?: ReadonlyJson<NonNullable<SingBoxConfig["log"]>>;
|
|
23
|
-
readonly experimental?: ReadonlyJson<NonNullable<SingBoxConfig["experimental"]>>;
|
|
20
|
+
export interface CompileInput extends Partial<ReadonlyJson<SBKitInputs["root"]>> {
|
|
24
21
|
readonly inbounds?: readonly Resource<"inbounds">[];
|
|
25
22
|
readonly outbounds?: readonly Resource<"outbounds">[];
|
|
26
23
|
readonly endpoints?: readonly Resource<"endpoints">[];
|
|
@@ -31,15 +28,11 @@ export interface CompileInput {
|
|
|
31
28
|
readonly route?: {
|
|
32
29
|
readonly rules?: readonly RuleValue<"route">[];
|
|
33
30
|
readonly rule_set?: readonly Resource<"route.rule_set">[];
|
|
34
|
-
|
|
35
|
-
readonly default_domain_resolver?: DomainResolver;
|
|
36
|
-
readonly default_http_client?: TaggedHttpClient;
|
|
37
|
-
};
|
|
31
|
+
} & ReadonlyJson<SBKitInputs["sections"]["route"]>;
|
|
38
32
|
readonly dns?: {
|
|
39
33
|
readonly rules?: readonly RuleValue<"dns">[];
|
|
40
34
|
readonly servers?: readonly DnsServer[];
|
|
41
|
-
|
|
42
|
-
};
|
|
35
|
+
} & ReadonlyJson<SBKitInputs["sections"]["dns"]>;
|
|
43
36
|
}
|
|
44
37
|
|
|
45
38
|
type TopLevelTarget = Exclude<Target, "dns.servers" | "route.rule_set">;
|
|
@@ -52,14 +45,8 @@ const topLevelLists = [
|
|
|
52
45
|
"certificate_providers",
|
|
53
46
|
"network_namespaces",
|
|
54
47
|
] as const satisfies readonly TopLevelTarget[];
|
|
55
|
-
const routeKeys = [
|
|
56
|
-
|
|
57
|
-
"rule_set",
|
|
58
|
-
"final",
|
|
59
|
-
"default_domain_resolver",
|
|
60
|
-
"default_http_client",
|
|
61
|
-
] as const;
|
|
62
|
-
const dnsKeys = ["rules", "servers", "final"] as const;
|
|
48
|
+
const routeKeys = ["rules", "rule_set", ...Object.keys(sectionSettings.route.properties!)];
|
|
49
|
+
const dnsKeys = ["rules", "servers", ...Object.keys(sectionSettings.dns.properties!)];
|
|
63
50
|
|
|
64
51
|
interface Registered {
|
|
65
52
|
readonly value: Resource;
|
|
@@ -108,8 +95,7 @@ function checkCycles(resources: Iterable<Registered>): void {
|
|
|
108
95
|
*/
|
|
109
96
|
export function compile(input: CompileInput): SingBoxConfig {
|
|
110
97
|
const fields = fieldsFrom(input, "config", [
|
|
111
|
-
|
|
112
|
-
"experimental",
|
|
98
|
+
...Object.keys(rootSettings),
|
|
113
99
|
...topLevelLists,
|
|
114
100
|
"route",
|
|
115
101
|
"dns",
|
|
@@ -220,53 +206,43 @@ export function compile(input: CompileInput): SingBoxConfig {
|
|
|
220
206
|
) {
|
|
221
207
|
fail(`${path}.domain_resolver`, "hostname requires a per-server resolver or detour");
|
|
222
208
|
}
|
|
223
|
-
|
|
209
|
+
const expand = data.expand ?? copyJson;
|
|
210
|
+
return expand(data.fields, path, resolveFrom(entry)) as Fields;
|
|
224
211
|
});
|
|
225
212
|
}
|
|
226
213
|
|
|
227
214
|
const resolve = resolveFrom();
|
|
228
215
|
const output: Fields = {};
|
|
229
|
-
for (const key
|
|
230
|
-
if (fields[key] !== undefined) output[key] =
|
|
216
|
+
for (const [key, schema] of Object.entries(rootSettings)) {
|
|
217
|
+
if (fields[key] !== undefined) output[key] = expandInput(schema, fields[key], key, resolve);
|
|
231
218
|
}
|
|
232
219
|
for (const target of topLevelLists) {
|
|
233
220
|
const values = emitList(target);
|
|
234
221
|
if (values !== undefined) output[target] = values;
|
|
235
222
|
}
|
|
236
223
|
if (route) {
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
expandRule(value, "route", `${path}[${index}]`, resolve),
|
|
247
|
-
),
|
|
248
|
-
},
|
|
249
|
-
"route",
|
|
250
|
-
resolve,
|
|
251
|
-
);
|
|
224
|
+
const { rules, rule_set, ...settings } = route;
|
|
225
|
+
const result = expandInput(sectionSettings.route, settings, "route", resolve) as Fields;
|
|
226
|
+
if (rule_set !== undefined) result.rule_set = emitList("route.rule_set");
|
|
227
|
+
if (rules !== undefined) {
|
|
228
|
+
result.rules = array(rules, "route.rules").map((value, index) =>
|
|
229
|
+
expandRule(value, "route", `route.rules[${index}]`, resolve),
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
output.route = result;
|
|
252
233
|
}
|
|
253
234
|
if (dns) {
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
},
|
|
266
|
-
},
|
|
267
|
-
"dns",
|
|
268
|
-
resolve,
|
|
269
|
-
);
|
|
235
|
+
const { rules, servers, ...settings } = dns;
|
|
236
|
+
const result = expandInput(sectionSettings.dns, settings, "dns", resolve) as Fields;
|
|
237
|
+
if (servers !== undefined) result.servers = emitList("dns.servers");
|
|
238
|
+
if (rules !== undefined) {
|
|
239
|
+
const compiled = array(rules, "dns.rules").map((value, index) =>
|
|
240
|
+
expandRule(value, "dns", `dns.rules[${index}]`, resolve),
|
|
241
|
+
);
|
|
242
|
+
checkDnsRules(compiled, "dns.rules");
|
|
243
|
+
result.rules = compiled;
|
|
244
|
+
}
|
|
245
|
+
output.dns = result;
|
|
270
246
|
}
|
|
271
247
|
checkCycles(registrations.values());
|
|
272
248
|
return validateConfig(output);
|