sing-box-kit 0.1.2 → 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 +1 -0
- package/dist/index.d.mts +387 -59
- package/dist/index.mjs +590 -40
- package/docs/proxy-profiles.md +63 -0
- package/docs/rules.md +5 -3
- package/docs/settings.md +2 -0
- package/index.ts +2 -1
- package/package.json +1 -1
- package/src/factories.ts +73 -68
- package/src/proxy-profile.ts +825 -0
- package/src/settings.ts +11 -1
- package/src/user.ts +82 -5
package/src/settings.ts
CHANGED
|
@@ -185,13 +185,23 @@ export function listenFields(
|
|
|
185
185
|
|
|
186
186
|
export type TlsMode = "server" | "client" | "server-reality" | "client-reality";
|
|
187
187
|
|
|
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
|
+
|
|
188
196
|
export function tlsFields(
|
|
189
197
|
input: unknown,
|
|
190
198
|
path: string,
|
|
191
199
|
schema: SchemaNode,
|
|
192
|
-
mode
|
|
200
|
+
mode: TlsMode | undefined,
|
|
201
|
+
kind: string,
|
|
193
202
|
): Fields {
|
|
194
203
|
const profile = record(input, path);
|
|
204
|
+
checkTlsAlpn(kind, profile, path);
|
|
195
205
|
const enabled = Object.hasOwn(schemaShape(schema).properties ?? {}, "enabled");
|
|
196
206
|
if (!Object.hasOwn(profile, "side")) {
|
|
197
207
|
return {
|
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
|
+
}
|