sing-box-kit 0.0.0-stage → 0.1.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/LICENSE +674 -0
- package/README.md +25 -2
- package/dist/index.d.mts +3974 -0
- package/dist/index.mjs +10036 -0
- package/docs/configuration.md +56 -0
- package/docs/rules.md +81 -0
- package/index.ts +40 -0
- package/package.json +46 -4
- package/schema/README.md +19 -0
- package/schema/sing-box.json +17266 -0
- package/schema/version.json +4 -0
- package/scripts/generate-schema.ts +7 -0
- package/src/compile.ts +273 -0
- package/src/factories.ts +681 -0
- package/src/generated/sing-box.ts +3558 -0
- package/src/resource.ts +185 -0
- package/src/rules.ts +469 -0
- package/src/schema.ts +207 -0
- package/src/settings.ts +247 -0
- package/src/user.ts +22 -0
- package/tsconfig.json +31 -0
- package/vite.config.ts +16 -0
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { compile } from "json-schema-to-typescript";
|
|
2
|
+
|
|
3
|
+
const schema = await Bun.file(new URL("../schema/sing-box.json", import.meta.url)).json();
|
|
4
|
+
const types = await compile({ ...schema, title: "SingBoxConfig" }, "SingBoxConfig", {
|
|
5
|
+
additionalProperties: false,
|
|
6
|
+
});
|
|
7
|
+
await Bun.write(new URL("../src/generated/sing-box.ts", import.meta.url), types);
|
package/src/compile.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { isIP } from "node:net";
|
|
2
|
+
import type { SingBoxConfig } from "./generated/sing-box";
|
|
3
|
+
import { copyJson, expandFields, fieldsFrom, ref, resourceData } from "./resource";
|
|
4
|
+
import type {
|
|
5
|
+
DnsServer,
|
|
6
|
+
Egress,
|
|
7
|
+
Fields,
|
|
8
|
+
ReadonlyJson,
|
|
9
|
+
Resolve,
|
|
10
|
+
Resource,
|
|
11
|
+
ResourceData,
|
|
12
|
+
TaggedHttpClient,
|
|
13
|
+
Target,
|
|
14
|
+
} from "./resource";
|
|
15
|
+
import { checkDnsRules, expandRule } from "./rules";
|
|
16
|
+
import type { RuleValue } from "./rules";
|
|
17
|
+
import { fail, validateConfig } from "./schema";
|
|
18
|
+
import { expandResolver, isEmptyDirect } from "./settings";
|
|
19
|
+
import type { DomainResolver } from "./settings";
|
|
20
|
+
|
|
21
|
+
export interface CompileInput {
|
|
22
|
+
readonly log?: ReadonlyJson<NonNullable<SingBoxConfig["log"]>>;
|
|
23
|
+
readonly experimental?: ReadonlyJson<NonNullable<SingBoxConfig["experimental"]>>;
|
|
24
|
+
readonly inbounds?: readonly Resource<"inbounds">[];
|
|
25
|
+
readonly outbounds?: readonly Resource<"outbounds">[];
|
|
26
|
+
readonly endpoints?: readonly Resource<"endpoints">[];
|
|
27
|
+
readonly services?: readonly Resource<"services">[];
|
|
28
|
+
readonly http_clients?: readonly TaggedHttpClient[];
|
|
29
|
+
readonly certificate_providers?: readonly Resource<"certificate_providers">[];
|
|
30
|
+
readonly network_namespaces?: readonly Resource<"network_namespaces">[];
|
|
31
|
+
readonly route?: {
|
|
32
|
+
readonly rules?: readonly RuleValue<"route">[];
|
|
33
|
+
readonly rule_set?: readonly Resource<"route.rule_set">[];
|
|
34
|
+
readonly final?: Egress;
|
|
35
|
+
readonly default_domain_resolver?: DomainResolver;
|
|
36
|
+
readonly default_http_client?: TaggedHttpClient;
|
|
37
|
+
};
|
|
38
|
+
readonly dns?: {
|
|
39
|
+
readonly rules?: readonly RuleValue<"dns">[];
|
|
40
|
+
readonly servers?: readonly DnsServer[];
|
|
41
|
+
readonly final?: DnsServer;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
type TopLevelTarget = Exclude<Target, "dns.servers" | "route.rule_set">;
|
|
46
|
+
const topLevelLists = [
|
|
47
|
+
"inbounds",
|
|
48
|
+
"outbounds",
|
|
49
|
+
"endpoints",
|
|
50
|
+
"services",
|
|
51
|
+
"http_clients",
|
|
52
|
+
"certificate_providers",
|
|
53
|
+
"network_namespaces",
|
|
54
|
+
] as const satisfies readonly TopLevelTarget[];
|
|
55
|
+
const routeKeys = [
|
|
56
|
+
"rules",
|
|
57
|
+
"rule_set",
|
|
58
|
+
"final",
|
|
59
|
+
"default_domain_resolver",
|
|
60
|
+
"default_http_client",
|
|
61
|
+
] as const;
|
|
62
|
+
const dnsKeys = ["rules", "servers", "final"] as const;
|
|
63
|
+
|
|
64
|
+
interface Registered {
|
|
65
|
+
readonly value: Resource;
|
|
66
|
+
readonly data: ResourceData;
|
|
67
|
+
readonly path: string;
|
|
68
|
+
readonly tags: readonly string[];
|
|
69
|
+
readonly dependencies: Dependency[];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
interface Dependency {
|
|
73
|
+
readonly target: Registered;
|
|
74
|
+
readonly path: string;
|
|
75
|
+
readonly tag: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function array(value: unknown, path: string): unknown[] {
|
|
79
|
+
if (!Array.isArray(value)) fail(path, "expected an array");
|
|
80
|
+
return Array.from(value);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function fieldTags(data: ResourceData): readonly unknown[] {
|
|
84
|
+
const tag = data.fields.tag;
|
|
85
|
+
return Array.isArray(tag) ? tag : tag === undefined ? [] : [tag];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function checkCycles(resources: Iterable<Registered>): void {
|
|
89
|
+
const active = new Set<Registered>();
|
|
90
|
+
const visited = new Set<Registered>();
|
|
91
|
+
for (const root of resources) visit(root);
|
|
92
|
+
|
|
93
|
+
function visit(node: Registered): void {
|
|
94
|
+
if (visited.has(node)) return;
|
|
95
|
+
active.add(node);
|
|
96
|
+
for (const edge of node.dependencies) {
|
|
97
|
+
if (active.has(edge.target)) fail(edge.path, "cyclic dependency", edge.tag);
|
|
98
|
+
visit(edge.target);
|
|
99
|
+
}
|
|
100
|
+
active.delete(node);
|
|
101
|
+
visited.add(node);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Compile using registered final values. If you recreate a resource with the
|
|
107
|
+
* same tag, rebuild its references so they refer to the new value.
|
|
108
|
+
*/
|
|
109
|
+
export function compile(input: CompileInput): SingBoxConfig {
|
|
110
|
+
const fields = fieldsFrom(input, "config", [
|
|
111
|
+
"log",
|
|
112
|
+
"experimental",
|
|
113
|
+
...topLevelLists,
|
|
114
|
+
"route",
|
|
115
|
+
"dns",
|
|
116
|
+
]);
|
|
117
|
+
const route =
|
|
118
|
+
fields.route === undefined ? undefined : fieldsFrom(fields.route, "route", routeKeys);
|
|
119
|
+
const dns = fields.dns === undefined ? undefined : fieldsFrom(fields.dns, "dns", dnsKeys);
|
|
120
|
+
const scopes = new Map<Target, Map<string, Registered>>();
|
|
121
|
+
const registrations = new Map<Resource, Registered>();
|
|
122
|
+
|
|
123
|
+
function tagScope(target: Target): Map<string, Registered> {
|
|
124
|
+
const scope = target === "endpoints" ? "outbounds" : target;
|
|
125
|
+
let tags = scopes.get(scope);
|
|
126
|
+
if (!tags) {
|
|
127
|
+
tags = new Map();
|
|
128
|
+
scopes.set(scope, tags);
|
|
129
|
+
}
|
|
130
|
+
return tags;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function registerList(input: unknown, target: Target): Registered[] | undefined {
|
|
134
|
+
if (input === undefined) return undefined;
|
|
135
|
+
const tags = tagScope(target);
|
|
136
|
+
return array(input, target).map((item, index) => {
|
|
137
|
+
const path = `${target}[${index}]`;
|
|
138
|
+
const data = resourceData(item);
|
|
139
|
+
if (!data || data.target !== target) fail(path, `expected a ${target} resource`);
|
|
140
|
+
const explicit = fieldTags(data);
|
|
141
|
+
if (!explicit.length && target !== "services")
|
|
142
|
+
fail(`${path}.tag`, "tagged resource required");
|
|
143
|
+
const entry: Registered = {
|
|
144
|
+
value: item as Resource,
|
|
145
|
+
data,
|
|
146
|
+
path,
|
|
147
|
+
tags: explicit.length
|
|
148
|
+
? explicit.map((value, i) => {
|
|
149
|
+
const tagPath = Array.isArray(data.fields.tag) ? `${path}.tag[${i}]` : `${path}.tag`;
|
|
150
|
+
if (typeof value !== "string" || !value) fail(tagPath, "expected a non-empty tag");
|
|
151
|
+
return value;
|
|
152
|
+
})
|
|
153
|
+
: [String(index)],
|
|
154
|
+
dependencies: [],
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
entry.tags.forEach((tag, i) => {
|
|
158
|
+
const tagPath = Array.isArray(data.fields.tag) ? `${path}.tag[${i}]` : `${path}.tag`;
|
|
159
|
+
if (tags.has(tag)) fail(tagPath, "duplicate tag", tag);
|
|
160
|
+
tags.set(tag, entry);
|
|
161
|
+
});
|
|
162
|
+
registrations.set(entry.value, entry);
|
|
163
|
+
return entry;
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
const lists = new Map<Target, Registered[] | undefined>(
|
|
168
|
+
topLevelLists.map((target) => [target, registerList(fields[target], target)]),
|
|
169
|
+
);
|
|
170
|
+
lists.set("route.rule_set", registerList(route?.rule_set, "route.rule_set"));
|
|
171
|
+
lists.set("dns.servers", registerList(dns?.servers, "dns.servers"));
|
|
172
|
+
|
|
173
|
+
function resolveFrom(origin?: Registered): Resolve {
|
|
174
|
+
return (value, role, path) => {
|
|
175
|
+
const data = resourceData(value);
|
|
176
|
+
if (!data) fail(path, `expected a typed ${role} reference`);
|
|
177
|
+
const tags = fieldTags(data);
|
|
178
|
+
const tag = tags.length === 1 && typeof tags[0] === "string" && tags[0] ? tags[0] : undefined;
|
|
179
|
+
const capability = role === "dial-detour" ? "egress" : role;
|
|
180
|
+
if (!data.roles.includes(capability))
|
|
181
|
+
fail(path, `target lacks ${capability} capability`, tag);
|
|
182
|
+
if (!tag) fail(path, "reference requires a single explicit tag");
|
|
183
|
+
const target = registrations.get(value as Resource);
|
|
184
|
+
if (!target) fail(path, "referenced final value is unregistered", tag);
|
|
185
|
+
if (role === "inbound" || role === "listen-detour") {
|
|
186
|
+
const inbound = tagScope("inbounds").get(tag);
|
|
187
|
+
const egress = tagScope("outbounds").get(tag);
|
|
188
|
+
const endpoint = egress?.data.target === "endpoints" ? egress : undefined;
|
|
189
|
+
if (role === "inbound" && inbound && endpoint) fail(path, "ambiguous inbound tag", tag);
|
|
190
|
+
if ((inbound ?? endpoint) !== target)
|
|
191
|
+
fail(path, "inbound tag resolves to another resource", tag);
|
|
192
|
+
}
|
|
193
|
+
if (role === "dial-detour" && data.kind === "direct" && isEmptyDirect(data.fields)) {
|
|
194
|
+
fail(path, "empty direct outbound cannot be an explicit detour", tag);
|
|
195
|
+
}
|
|
196
|
+
if (origin) {
|
|
197
|
+
const edge = path.slice(origin.path.length);
|
|
198
|
+
const group = role === "egress" && edge.startsWith(".outbounds[");
|
|
199
|
+
const detour = role === "dial-detour" && edge === ".detour" && origin.data.startupDetour;
|
|
200
|
+
const resolver =
|
|
201
|
+
role === "dns-server" &&
|
|
202
|
+
origin.data.target === "dns.servers" &&
|
|
203
|
+
(edge === ".domain_resolver" || edge === ".domain_resolver.server");
|
|
204
|
+
if (group || detour || resolver) origin.dependencies.push({ target, path, tag });
|
|
205
|
+
}
|
|
206
|
+
return tag;
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function emitList(target: Target): Fields[] | undefined {
|
|
211
|
+
return lists.get(target)?.map((entry) => {
|
|
212
|
+
const { data, path } = entry;
|
|
213
|
+
const { server, detour, domain_resolver } = data.fields;
|
|
214
|
+
if (
|
|
215
|
+
data.remoteDns &&
|
|
216
|
+
typeof server === "string" &&
|
|
217
|
+
!isIP(server) &&
|
|
218
|
+
detour === undefined &&
|
|
219
|
+
domain_resolver === undefined
|
|
220
|
+
) {
|
|
221
|
+
fail(`${path}.domain_resolver`, "hostname requires a per-server resolver or detour");
|
|
222
|
+
}
|
|
223
|
+
return expandFields(data.fields, data.references ?? {}, path, resolveFrom(entry));
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const resolve = resolveFrom();
|
|
228
|
+
const output: Fields = {};
|
|
229
|
+
for (const key of ["log", "experimental"] as const) {
|
|
230
|
+
if (fields[key] !== undefined) output[key] = copyJson(fields[key], key);
|
|
231
|
+
}
|
|
232
|
+
for (const target of topLevelLists) {
|
|
233
|
+
const values = emitList(target);
|
|
234
|
+
if (values !== undefined) output[target] = values;
|
|
235
|
+
}
|
|
236
|
+
if (route) {
|
|
237
|
+
output.route = expandFields(
|
|
238
|
+
route,
|
|
239
|
+
{
|
|
240
|
+
rule_set: () => emitList("route.rule_set"),
|
|
241
|
+
final: ref("egress"),
|
|
242
|
+
default_domain_resolver: expandResolver,
|
|
243
|
+
default_http_client: ref("http-client"),
|
|
244
|
+
rules: (values, path) =>
|
|
245
|
+
array(values, path).map((value, index) =>
|
|
246
|
+
expandRule(value, "route", `${path}[${index}]`, resolve),
|
|
247
|
+
),
|
|
248
|
+
},
|
|
249
|
+
"route",
|
|
250
|
+
resolve,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
if (dns) {
|
|
254
|
+
output.dns = expandFields(
|
|
255
|
+
dns,
|
|
256
|
+
{
|
|
257
|
+
servers: () => emitList("dns.servers"),
|
|
258
|
+
final: ref("dns-server"),
|
|
259
|
+
rules: (values, path) => {
|
|
260
|
+
const rules = array(values, path).map((value, index) =>
|
|
261
|
+
expandRule(value, "dns", `${path}[${index}]`, resolve),
|
|
262
|
+
);
|
|
263
|
+
checkDnsRules(rules, path);
|
|
264
|
+
return rules;
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
"dns",
|
|
268
|
+
resolve,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
checkCycles(registrations.values());
|
|
272
|
+
return validateConfig(output);
|
|
273
|
+
}
|