sing-box-kit 0.1.1 → 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/settings.ts
CHANGED
|
@@ -1,62 +1,30 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
DomainResolver as
|
|
3
|
-
HTTPClient,
|
|
2
|
+
DomainResolver as SBKitDomainResolver,
|
|
4
3
|
HTTPClientReference,
|
|
5
|
-
InboundRealityHandshakeOptions,
|
|
6
4
|
InboundTLSOptions,
|
|
7
5
|
OutboundTLSOptions,
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
Egress,
|
|
14
|
-
Expand,
|
|
15
|
-
Fields,
|
|
16
|
-
ListenDetour,
|
|
17
|
-
Namespace,
|
|
18
|
-
ReadonlyJson,
|
|
19
|
-
Resolve,
|
|
20
|
-
Resource,
|
|
21
|
-
} from "./resource";
|
|
6
|
+
SBKitInputs,
|
|
7
|
+
} from "./generated/sbkit-inputs";
|
|
8
|
+
import { inputProperty, schemaShape, tagReference } from "./input-schema";
|
|
9
|
+
import { copyJson, fieldsFrom, record, resourceData } from "./resource";
|
|
10
|
+
import type { CertificateProvider, HttpClientValue, Resolve } from "./resource";
|
|
22
11
|
import {
|
|
23
12
|
checkSchema,
|
|
24
|
-
compileSchema,
|
|
25
13
|
definition,
|
|
14
|
+
definitionName,
|
|
26
15
|
durationNanoseconds,
|
|
27
16
|
fail,
|
|
17
|
+
keyPath,
|
|
28
18
|
tlsProfileValidators,
|
|
29
19
|
} from "./schema";
|
|
20
|
+
import type { SchemaNode } from "./schema";
|
|
21
|
+
import type { Fields, ReadonlyJson } from "./type-utils";
|
|
30
22
|
|
|
31
|
-
export type DomainResolver =
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
export interface DirectDial {
|
|
38
|
-
/**
|
|
39
|
-
* Go Duration string. An explicit detour disallows a non-zero timeout on
|
|
40
|
-
* the resource using it. Configure the timeout on the detour target.
|
|
41
|
-
* On a direct detour target, an effective zero is equivalent to omission.
|
|
42
|
-
*/
|
|
43
|
-
readonly connect_timeout?: HTTPClient["connect_timeout"];
|
|
44
|
-
readonly domain_resolver?: DomainResolver;
|
|
45
|
-
readonly netns?: Namespace | string;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export interface Dial extends DirectDial {
|
|
49
|
-
readonly detour?: Egress;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
export interface Listener {
|
|
53
|
-
readonly port: number;
|
|
54
|
-
readonly netns?: Namespace | string;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export interface InboundListener extends Listener {
|
|
58
|
-
readonly detour?: ListenDetour;
|
|
59
|
-
}
|
|
23
|
+
export type DomainResolver = ReadonlyJson<SBKitDomainResolver>;
|
|
24
|
+
export type DirectDial = ReadonlyJson<SBKitInputs["factories"]["outbounds"]["direct"]["dial"]>;
|
|
25
|
+
export type Dial = ReadonlyJson<SBKitInputs["factories"]["outbounds"]["vless"]["dial"]>;
|
|
26
|
+
export type InboundListener = ReadonlyJson<SBKitInputs["factories"]["inbounds"]["mixed"]["listen"]>;
|
|
27
|
+
export type Listener = Omit<InboundListener, "detour">;
|
|
60
28
|
|
|
61
29
|
export type ServerTls = { side: "server"; reality?: never } & (
|
|
62
30
|
| (Required<Pick<InboundTLSOptions, "certificate_path" | "key_path">> & {
|
|
@@ -76,7 +44,7 @@ export type RealityServerTls = {
|
|
|
76
44
|
reality: {
|
|
77
45
|
private_key: string;
|
|
78
46
|
short_id: string | readonly string[];
|
|
79
|
-
handshake:
|
|
47
|
+
handshake: { server: string; server_port: number } & Dial;
|
|
80
48
|
};
|
|
81
49
|
};
|
|
82
50
|
export type RealityClientTls = {
|
|
@@ -86,36 +54,16 @@ export type RealityClientTls = {
|
|
|
86
54
|
reality: { public_key: string; short_id: string };
|
|
87
55
|
};
|
|
88
56
|
|
|
89
|
-
export type
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
Dial & { readonly tag?: never };
|
|
94
|
-
export type HttpClientInput = HttpClientValue | InlineHttpClient;
|
|
57
|
+
export type InlineHttpClient = ReadonlyJson<Exclude<HTTPClientReference, HttpClientValue>> & {
|
|
58
|
+
readonly tag?: never;
|
|
59
|
+
};
|
|
60
|
+
export type HttpClientInput = ReadonlyJson<HTTPClientReference>;
|
|
95
61
|
|
|
96
62
|
export const previewReference: Resolve = (value, role, path) => {
|
|
97
63
|
if (!resourceData(value)) fail(path, `expected a typed ${role} reference`);
|
|
98
64
|
return "reference";
|
|
99
65
|
};
|
|
100
66
|
|
|
101
|
-
export const expandResolver: Expand = (value, path, resolve) => {
|
|
102
|
-
if (resourceData(value) || typeof value !== "object" || value === null) {
|
|
103
|
-
return resolve(value, "dns-server", path);
|
|
104
|
-
}
|
|
105
|
-
const options = record(value, path);
|
|
106
|
-
if (options.server === undefined) fail(`${path}.server`, "required field");
|
|
107
|
-
return expandFields(options, { server: ref("dns-server") }, path, resolve);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
const expandNamespace: Expand = (value, path, resolve) =>
|
|
111
|
-
typeof value === "string" ? value : resolve(value, "netns", path);
|
|
112
|
-
export const listenReferences = { netns: expandNamespace, detour: ref("listen-detour") };
|
|
113
|
-
export const dialReferences = {
|
|
114
|
-
netns: expandNamespace,
|
|
115
|
-
detour: ref("dial-detour"),
|
|
116
|
-
domain_resolver: expandResolver,
|
|
117
|
-
};
|
|
118
|
-
|
|
119
67
|
export function checkDial(fields: Fields, path: string): void {
|
|
120
68
|
const timeout = durationNanoseconds(fields.connect_timeout, `${path}.connect_timeout`);
|
|
121
69
|
if (fields.detour === undefined) return;
|
|
@@ -124,59 +72,133 @@ export function checkDial(fields: Fields, path: string): void {
|
|
|
124
72
|
fail(`${path}.netns`, "namespace with detour");
|
|
125
73
|
}
|
|
126
74
|
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
75
|
+
const discriminators = ["type", "version", "provider"] as const;
|
|
76
|
+
|
|
77
|
+
export function matchesDiscriminators(schema: SchemaNode, fields: Fields): boolean {
|
|
78
|
+
return discriminators.every((key) => {
|
|
79
|
+
const property = schema.properties?.[key];
|
|
80
|
+
if (property?.const === undefined && !property?.enum) return true;
|
|
81
|
+
const value = fields[key];
|
|
82
|
+
if (value === undefined) return !schema.required?.includes(key);
|
|
83
|
+
return property.const === value || property.enum?.includes(value) === true;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function matchesShape(input: SchemaNode, value: unknown): boolean {
|
|
88
|
+
const schema = schemaShape(input);
|
|
89
|
+
const alternatives = schema.oneOf ?? schema.anyOf;
|
|
90
|
+
if (alternatives) return alternatives.some((option) => matchesShape(option, value));
|
|
91
|
+
if (schema.const !== undefined) return value === schema.const;
|
|
92
|
+
if (schema.enum) return schema.enum.includes(value);
|
|
93
|
+
if (resourceData(value)) return schema["x-tag-reference"] !== undefined;
|
|
94
|
+
if (schema.type === "array") return Array.isArray(value);
|
|
95
|
+
if (schema.type === "object") {
|
|
96
|
+
return (
|
|
97
|
+
value !== null &&
|
|
98
|
+
typeof value === "object" &&
|
|
99
|
+
!Array.isArray(value) &&
|
|
100
|
+
matchesDiscriminators(schema, value as Fields)
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
if (schema.type === "integer") return typeof value === "number";
|
|
104
|
+
return schema.type === undefined || typeof value === schema.type;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Expand annotated tags anywhere in a schema-shaped input. Ajv checks the resulting JSON. */
|
|
108
|
+
export function expandInput(
|
|
109
|
+
input: SchemaNode,
|
|
110
|
+
value: unknown,
|
|
111
|
+
path: string,
|
|
112
|
+
resolve: Resolve,
|
|
113
|
+
): unknown {
|
|
114
|
+
const name = definitionName(input);
|
|
115
|
+
if (name === "HTTPClientReference") {
|
|
116
|
+
const client = resourceData(value);
|
|
117
|
+
if (client?.target === "http_clients" && client.fields.tag === undefined) {
|
|
118
|
+
return expandInput(definition("HTTPClient"), client.fields, path, resolve);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const schema = schemaShape(input);
|
|
123
|
+
const annotation = schema["x-tag-reference"];
|
|
124
|
+
if (annotation !== undefined) {
|
|
125
|
+
const reference = tagReference(annotation);
|
|
126
|
+
if ("allowString" in reference && typeof value === "string") return value;
|
|
127
|
+
return resolve(value, reference.role, path);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const alternatives = schema.oneOf ?? schema.anyOf;
|
|
131
|
+
if (alternatives) {
|
|
132
|
+
const option = alternatives.find((branch) => matchesShape(branch, value));
|
|
133
|
+
return option ? expandInput(option, value, path, resolve) : copyJson(value, path);
|
|
134
|
+
}
|
|
135
|
+
if (schema.items && Array.isArray(value)) {
|
|
136
|
+
const items = schema.items;
|
|
137
|
+
return Array.from(value, (item, index) =>
|
|
138
|
+
expandInput(items, item, `${path}[${index}]`, resolve),
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
if (
|
|
142
|
+
schema.type === "object" &&
|
|
143
|
+
value !== null &&
|
|
144
|
+
typeof value === "object" &&
|
|
145
|
+
!Array.isArray(value)
|
|
146
|
+
) {
|
|
147
|
+
const fields = record(value, path);
|
|
148
|
+
const detour = schema.properties?.detour?.["x-tag-reference"];
|
|
149
|
+
if (detour === "dial_detour" || detour === "outbound") checkDial(fields, path);
|
|
150
|
+
const additional =
|
|
151
|
+
typeof schema.additionalProperties === "object" ? schema.additionalProperties : {};
|
|
152
|
+
return Object.fromEntries(
|
|
153
|
+
Object.entries(fields)
|
|
154
|
+
.filter(([, item]) => item !== undefined)
|
|
155
|
+
.map(([key, item]) => {
|
|
156
|
+
const property =
|
|
157
|
+
schema.properties && Object.hasOwn(schema.properties, key)
|
|
158
|
+
? schema.properties[key]!
|
|
159
|
+
: additional;
|
|
160
|
+
return [
|
|
161
|
+
key,
|
|
162
|
+
expandInput(inputProperty(key, property), item, keyPath(path, key), resolve),
|
|
163
|
+
];
|
|
164
|
+
}),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
return copyJson(value, path);
|
|
136
168
|
}
|
|
137
169
|
|
|
138
170
|
export function listenFields(
|
|
139
|
-
address:
|
|
171
|
+
address: unknown,
|
|
140
172
|
input: unknown,
|
|
141
173
|
path: string,
|
|
142
|
-
|
|
174
|
+
schema: SchemaNode,
|
|
143
175
|
): Fields {
|
|
144
|
-
const { port, ...options } = fieldsFrom(
|
|
145
|
-
|
|
146
|
-
path
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
return
|
|
176
|
+
const { port, ...options } = fieldsFrom(input, path, Object.keys(schema.properties!));
|
|
177
|
+
if (address !== undefined) {
|
|
178
|
+
if (!Object.hasOwn(schema.properties!, "port")) fail(`${path}.listen`, "unexpected field");
|
|
179
|
+
if (typeof address !== "string") fail(`${path}.listen`, "expected a string");
|
|
180
|
+
options.listen = address;
|
|
181
|
+
}
|
|
182
|
+
if (port !== undefined) options.listen_port = port;
|
|
183
|
+
return options;
|
|
152
184
|
}
|
|
153
185
|
|
|
154
|
-
const expandReality: Expand = (value, path, resolve) =>
|
|
155
|
-
expandFields(
|
|
156
|
-
record(value, path),
|
|
157
|
-
{
|
|
158
|
-
handshake: (handshake, path, resolve) =>
|
|
159
|
-
expandFields(record(handshake, path), dialReferences, path, resolve),
|
|
160
|
-
},
|
|
161
|
-
path,
|
|
162
|
-
resolve,
|
|
163
|
-
);
|
|
164
|
-
|
|
165
|
-
export const expandTls: Expand = (value, path, resolve) =>
|
|
166
|
-
expandFields(
|
|
167
|
-
record(value, path),
|
|
168
|
-
{
|
|
169
|
-
certificate_provider: ref("certificate-provider"),
|
|
170
|
-
reality: expandReality,
|
|
171
|
-
},
|
|
172
|
-
path,
|
|
173
|
-
resolve,
|
|
174
|
-
);
|
|
175
|
-
|
|
176
186
|
export type TlsMode = "server" | "client" | "server-reality" | "client-reality";
|
|
177
187
|
|
|
178
|
-
export function tlsFields(
|
|
188
|
+
export function tlsFields(
|
|
189
|
+
input: unknown,
|
|
190
|
+
path: string,
|
|
191
|
+
schema: SchemaNode,
|
|
192
|
+
mode?: TlsMode,
|
|
193
|
+
): Fields {
|
|
179
194
|
const profile = record(input, path);
|
|
195
|
+
const enabled = Object.hasOwn(schemaShape(schema).properties ?? {}, "enabled");
|
|
196
|
+
if (!Object.hasOwn(profile, "side")) {
|
|
197
|
+
return {
|
|
198
|
+
tls: enabled && profile.enabled === undefined ? { ...profile, enabled: true } : profile,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (!mode) fail(`${path}.side`, "unsupported TLS profile");
|
|
180
202
|
const server = mode === "server" || mode === "server-reality";
|
|
181
203
|
const isReality = profile.reality !== undefined;
|
|
182
204
|
if (isReality && !mode.endsWith("-reality")) fail(`${path}.reality`, "unsupported TLS profile");
|
|
@@ -190,58 +212,35 @@ export function tlsFields(input: unknown, path: string, mode: TlsMode): Fields {
|
|
|
190
212
|
? tlsProfileValidators.client.reality
|
|
191
213
|
: tlsProfileValidators.client.regular;
|
|
192
214
|
|
|
193
|
-
checkSchema(validate,
|
|
215
|
+
checkSchema(validate, expandInput(schema, profile, path, previewReference), path);
|
|
194
216
|
const { side: _side, ...fields } = profile;
|
|
195
217
|
if (isReality) {
|
|
196
|
-
|
|
197
|
-
if (server)
|
|
198
|
-
checkDial(
|
|
199
|
-
record(reality.handshake, `${path}.reality.handshake`),
|
|
200
|
-
`${path}.reality.handshake`,
|
|
201
|
-
);
|
|
202
|
-
fields.reality = { ...reality, enabled: true };
|
|
218
|
+
fields.reality = { ...record(profile.reality, `${path}.reality`), enabled: true };
|
|
203
219
|
if (!server) fields.utls = { enabled: true };
|
|
204
220
|
}
|
|
205
221
|
return { tls: { ...fields, enabled: true } };
|
|
206
222
|
}
|
|
207
223
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (
|
|
213
|
-
|
|
224
|
+
export function checkSsmServers(value: unknown, path: string): void {
|
|
225
|
+
const servers = Object.keys(record(value, path));
|
|
226
|
+
if (servers.length === 0) fail(path, "expected at least one SSM server");
|
|
227
|
+
for (const pattern of servers) {
|
|
228
|
+
if (!pattern.startsWith("/")) {
|
|
229
|
+
fail(`${path}[${JSON.stringify(pattern)}]`, "expected a route pattern starting with /");
|
|
214
230
|
}
|
|
215
|
-
return resolve(value, "http-client", path);
|
|
216
231
|
}
|
|
217
|
-
|
|
218
|
-
if (Object.hasOwn(fields, "tag")) fail(`${path}.tag`, "unexpected field");
|
|
219
|
-
checkDial(fields, path);
|
|
220
|
-
const output = expandFields(fields, dialReferences, path, resolve);
|
|
221
|
-
return checkSchema(httpClientObject, output, path);
|
|
222
|
-
};
|
|
232
|
+
}
|
|
223
233
|
|
|
224
|
-
|
|
225
|
-
typeof value === "string" || typeof value === "boolean"
|
|
226
|
-
? value
|
|
227
|
-
: expandFields(record(value, path), { http_client: expandHttpClient }, path, resolve);
|
|
228
|
-
|
|
229
|
-
export const expandSsmServers: Expand = (value, path, resolve) => {
|
|
230
|
-
const servers = Object.entries(record(value, path));
|
|
231
|
-
if (servers.length === 0) fail(path, "expected at least one SSM server");
|
|
232
|
-
return Object.fromEntries(
|
|
233
|
-
servers.map(([pattern, inbound]) => {
|
|
234
|
-
const fieldPath = `${path}[${JSON.stringify(pattern)}]`;
|
|
235
|
-
if (!pattern.startsWith("/")) fail(fieldPath, "expected a route pattern starting with /");
|
|
236
|
-
return [pattern, resolve(inbound, "ssm", fieldPath)];
|
|
237
|
-
}),
|
|
238
|
-
);
|
|
239
|
-
};
|
|
234
|
+
const directDialProperties = definition("DialerOptions").properties!;
|
|
240
235
|
|
|
241
236
|
export function isEmptyDirect(fields: Fields): boolean {
|
|
242
|
-
return (
|
|
243
|
-
|
|
244
|
-
(
|
|
245
|
-
|
|
246
|
-
|
|
237
|
+
return Object.entries(fields).every(([key, value]) => {
|
|
238
|
+
if (value === undefined || !Object.hasOwn(directDialProperties, key)) return true;
|
|
239
|
+
if (definitionName(directDialProperties[key]!) === "Duration") {
|
|
240
|
+
return durationNanoseconds(value, key) === 0;
|
|
241
|
+
}
|
|
242
|
+
if (key === "udp_fragment") return false; // This setting distinguishes false from omission.
|
|
243
|
+
if (key === "routing_mark") return Number(value) === 0;
|
|
244
|
+
return value === "" || value === 0 || value === false;
|
|
245
|
+
});
|
|
247
246
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type Fields = Record<string, unknown>;
|
|
2
|
+
|
|
3
|
+
export type ReadonlyJson<T> = T extends object
|
|
4
|
+
? { readonly [K in keyof T]: ReadonlyJson<T[K]> }
|
|
5
|
+
: T;
|
|
6
|
+
|
|
7
|
+
type UnionKeys<T> = T extends unknown ? keyof T : never;
|
|
8
|
+
|
|
9
|
+
export type StrictUnion<T, All = T> = T extends unknown
|
|
10
|
+
? T & { readonly [K in Exclude<UnionKeys<All>, keyof T>]?: never }
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
export type CamelCase<S extends string> = S extends `${infer Head}-${infer Tail}`
|
|
14
|
+
? `${Head}${Capitalize<CamelCase<Tail>>}`
|
|
15
|
+
: S;
|
package/vite.config.ts
CHANGED
|
@@ -7,10 +7,10 @@ export default defineConfig({
|
|
|
7
7
|
dts: true,
|
|
8
8
|
},
|
|
9
9
|
fmt: {
|
|
10
|
-
ignorePatterns: ["dist/**", "schema/sing-box.json", "src/generated
|
|
10
|
+
ignorePatterns: ["dist/**", "schema/sing-box.json", "src/generated/**"],
|
|
11
11
|
},
|
|
12
12
|
lint: {
|
|
13
|
-
ignorePatterns: ["dist/**", "src/generated
|
|
13
|
+
ignorePatterns: ["dist/**", "src/generated/**"],
|
|
14
14
|
options: { typeAware: true, typeCheck: true },
|
|
15
15
|
},
|
|
16
16
|
});
|