sing-box-kit 0.1.1 → 0.1.3
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 +28 -9
- package/dist/index.d.mts +2915 -610
- package/dist/index.mjs +1179 -668
- package/docs/configuration.md +21 -43
- package/docs/dns-rules.md +43 -0
- package/docs/internal/README.md +31 -0
- package/docs/proxy-profiles.md +63 -0
- package/docs/resources.md +47 -0
- package/docs/rules.md +43 -48
- package/docs/settings.md +38 -0
- package/index.ts +2 -1
- 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 +345 -557
- package/src/generated/sbkit-inputs.ts +2671 -0
- package/src/input-schema.ts +299 -0
- package/src/proxy-profile.ts +825 -0
- package/src/resource.ts +30 -39
- package/src/rules.ts +82 -207
- package/src/schema.ts +15 -23
- package/src/settings.ts +165 -156
- package/src/type-utils.ts +15 -0
- package/src/user.ts +82 -5
- 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,143 @@ 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
|
|
188
|
+
export type ProtocolTlsOptions<K, T> = T &
|
|
189
|
+
(K extends "naive" ? { readonly alpn?: never } : unknown);
|
|
190
|
+
|
|
191
|
+
export function checkTlsAlpn(kind: string, options: Fields, path: string): void {
|
|
192
|
+
if (kind === "naive" && options.alpn !== undefined)
|
|
193
|
+
fail(`${path}.alpn`, "ALPN cannot be configured for Naive");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function tlsFields(
|
|
197
|
+
input: unknown,
|
|
198
|
+
path: string,
|
|
199
|
+
schema: SchemaNode,
|
|
200
|
+
mode: TlsMode | undefined,
|
|
201
|
+
kind: string,
|
|
202
|
+
): Fields {
|
|
179
203
|
const profile = record(input, path);
|
|
204
|
+
checkTlsAlpn(kind, profile, path);
|
|
205
|
+
const enabled = Object.hasOwn(schemaShape(schema).properties ?? {}, "enabled");
|
|
206
|
+
if (!Object.hasOwn(profile, "side")) {
|
|
207
|
+
return {
|
|
208
|
+
tls: enabled && profile.enabled === undefined ? { ...profile, enabled: true } : profile,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
if (!mode) fail(`${path}.side`, "unsupported TLS profile");
|
|
180
212
|
const server = mode === "server" || mode === "server-reality";
|
|
181
213
|
const isReality = profile.reality !== undefined;
|
|
182
214
|
if (isReality && !mode.endsWith("-reality")) fail(`${path}.reality`, "unsupported TLS profile");
|
|
@@ -190,58 +222,35 @@ export function tlsFields(input: unknown, path: string, mode: TlsMode): Fields {
|
|
|
190
222
|
? tlsProfileValidators.client.reality
|
|
191
223
|
: tlsProfileValidators.client.regular;
|
|
192
224
|
|
|
193
|
-
checkSchema(validate,
|
|
225
|
+
checkSchema(validate, expandInput(schema, profile, path, previewReference), path);
|
|
194
226
|
const { side: _side, ...fields } = profile;
|
|
195
227
|
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 };
|
|
228
|
+
fields.reality = { ...record(profile.reality, `${path}.reality`), enabled: true };
|
|
203
229
|
if (!server) fields.utls = { enabled: true };
|
|
204
230
|
}
|
|
205
231
|
return { tls: { ...fields, enabled: true } };
|
|
206
232
|
}
|
|
207
233
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
if (
|
|
213
|
-
|
|
234
|
+
export function checkSsmServers(value: unknown, path: string): void {
|
|
235
|
+
const servers = Object.keys(record(value, path));
|
|
236
|
+
if (servers.length === 0) fail(path, "expected at least one SSM server");
|
|
237
|
+
for (const pattern of servers) {
|
|
238
|
+
if (!pattern.startsWith("/")) {
|
|
239
|
+
fail(`${path}[${JSON.stringify(pattern)}]`, "expected a route pattern starting with /");
|
|
214
240
|
}
|
|
215
|
-
return resolve(value, "http-client", path);
|
|
216
241
|
}
|
|
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
|
-
};
|
|
242
|
+
}
|
|
223
243
|
|
|
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
|
-
};
|
|
244
|
+
const directDialProperties = definition("DialerOptions").properties!;
|
|
240
245
|
|
|
241
246
|
export function isEmptyDirect(fields: Fields): boolean {
|
|
242
|
-
return (
|
|
243
|
-
|
|
244
|
-
(
|
|
245
|
-
|
|
246
|
-
|
|
247
|
+
return Object.entries(fields).every(([key, value]) => {
|
|
248
|
+
if (value === undefined || !Object.hasOwn(directDialProperties, key)) return true;
|
|
249
|
+
if (definitionName(directDialProperties[key]!) === "Duration") {
|
|
250
|
+
return durationNanoseconds(value, key) === 0;
|
|
251
|
+
}
|
|
252
|
+
if (key === "udp_fragment") return false; // This setting distinguishes false from omission.
|
|
253
|
+
if (key === "routing_mark") return Number(value) === 0;
|
|
254
|
+
return value === "" || value === 0 || value === false;
|
|
255
|
+
});
|
|
247
256
|
}
|
|
@@ -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/src/user.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { fieldsFrom, preserveIdentity } from "./resource";
|
|
1
|
+
import { fieldsFrom, preserveIdentity, snapshot } from "./resource";
|
|
2
2
|
import { fail } from "./schema";
|
|
3
|
+
import type { StrictUnion } from "./type-utils";
|
|
3
4
|
|
|
4
5
|
declare const userBrand: unique symbol;
|
|
5
6
|
export interface User {
|
|
@@ -7,16 +8,92 @@ export interface User {
|
|
|
7
8
|
readonly [userBrand]: true;
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
type Password = { readonly password: string };
|
|
11
12
|
|
|
12
|
-
export
|
|
13
|
-
|
|
13
|
+
export interface UserCredentials {
|
|
14
|
+
readonly anytls?: Password;
|
|
15
|
+
readonly http?: Password;
|
|
16
|
+
readonly hysteria?: StrictUnion<
|
|
17
|
+
{ readonly auth: string | readonly number[] } | { readonly auth_str: string }
|
|
18
|
+
>;
|
|
19
|
+
readonly hysteria2?: Password;
|
|
20
|
+
readonly mixed?: Password;
|
|
21
|
+
readonly naive?: Password;
|
|
22
|
+
readonly shadowsocks?: Password;
|
|
23
|
+
readonly shadowtls?: Password;
|
|
24
|
+
readonly snell?: { readonly userkey: string };
|
|
25
|
+
readonly socks?: Password;
|
|
26
|
+
readonly trojan?: Password;
|
|
27
|
+
readonly tuic?: Password & { readonly uuid: string };
|
|
28
|
+
readonly vless?: { readonly uuid: string; readonly flow?: string };
|
|
29
|
+
readonly vmess?: { readonly uuid: string; readonly alter_id?: number };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const passwordFields = ["password"] as const;
|
|
33
|
+
const credentialFields = {
|
|
34
|
+
anytls: passwordFields,
|
|
35
|
+
http: passwordFields,
|
|
36
|
+
hysteria: ["auth", "auth_str"],
|
|
37
|
+
hysteria2: passwordFields,
|
|
38
|
+
mixed: passwordFields,
|
|
39
|
+
naive: passwordFields,
|
|
40
|
+
shadowsocks: passwordFields,
|
|
41
|
+
shadowtls: passwordFields,
|
|
42
|
+
snell: ["userkey"],
|
|
43
|
+
socks: passwordFields,
|
|
44
|
+
trojan: passwordFields,
|
|
45
|
+
tuic: ["password", "uuid"],
|
|
46
|
+
vless: ["uuid", "flow"],
|
|
47
|
+
vmess: ["uuid", "alter_id"],
|
|
48
|
+
} as const satisfies {
|
|
49
|
+
[K in keyof UserCredentials]-?: readonly (keyof NonNullable<UserCredentials[K]> & string)[];
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const users = new WeakMap<object, UserCredentials>();
|
|
53
|
+
|
|
54
|
+
export function user(input: { name: string; credentials?: UserCredentials }): User {
|
|
55
|
+
const { name, credentials } = fieldsFrom(input, "user", ["name", "credentials"], ["name"]);
|
|
14
56
|
if (typeof name !== "string") fail("user.name", "expected a string");
|
|
57
|
+
const entries = fieldsFrom(
|
|
58
|
+
credentials === undefined ? {} : credentials,
|
|
59
|
+
"user.credentials",
|
|
60
|
+
Object.keys(credentialFields),
|
|
61
|
+
);
|
|
62
|
+
for (const key of Object.keys(credentialFields) as (keyof UserCredentials)[]) {
|
|
63
|
+
if (!Object.hasOwn(entries, key)) continue;
|
|
64
|
+
const path = `user.credentials.${key}`;
|
|
65
|
+
const allowed = credentialFields[key];
|
|
66
|
+
const required =
|
|
67
|
+
key === "hysteria" ? [] : allowed.filter((field) => field !== "flow" && field !== "alter_id");
|
|
68
|
+
const entry = fieldsFrom(entries[key], path, allowed, required);
|
|
69
|
+
if (key === "hysteria" && Object.keys(entry).length !== 1)
|
|
70
|
+
fail(path, "expected either auth or auth_str");
|
|
71
|
+
for (const [field, value] of Object.entries(entry)) {
|
|
72
|
+
if (field === "alter_id") {
|
|
73
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)
|
|
74
|
+
fail(`${path}.${field}`, "expected a non-negative integer");
|
|
75
|
+
} else if (field === "auth" && Array.isArray(value)) {
|
|
76
|
+
if (Array.from(value).some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255))
|
|
77
|
+
fail(`${path}.${field}`, "expected an array of bytes");
|
|
78
|
+
} else if (typeof value !== "string") {
|
|
79
|
+
fail(`${path}.${field}`, "expected a string");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
entries[key] = entry;
|
|
83
|
+
}
|
|
15
84
|
const value = preserveIdentity({ name });
|
|
16
|
-
users.
|
|
85
|
+
users.set(value, snapshot(entries, "user.credentials"));
|
|
17
86
|
return value as User;
|
|
18
87
|
}
|
|
19
88
|
|
|
20
89
|
export function isUser(value: unknown): value is User {
|
|
21
90
|
return value !== null && typeof value === "object" && users.has(value);
|
|
22
91
|
}
|
|
92
|
+
|
|
93
|
+
export function requireUserCredentials<K extends keyof UserCredentials>(
|
|
94
|
+
value: User,
|
|
95
|
+
kind: K,
|
|
96
|
+
path: string,
|
|
97
|
+
): NonNullable<UserCredentials[K]> {
|
|
98
|
+
return users.get(value)?.[kind] ?? fail(path, `missing ${kind} credentials`);
|
|
99
|
+
}
|
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
|
});
|