sing-box-kit 0.1.2 → 0.1.4

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/src/rules.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { isIP } from "node:net";
2
1
  import type { SBKitInputs } from "./generated/sbkit-inputs";
3
2
  import { ruleActionSchemas, ruleMatchSchema } from "./input-schema";
3
+ import { isCIDR, isIP } from "./ip";
4
4
  import { fieldsFrom, preserveIdentity, record, resourceData, snapshot } from "./resource";
5
5
  import type { Resolve } from "./resource";
6
6
  import { checkSchema, compileSchema, durationNanoseconds, fail, keyPath } from "./schema";
@@ -162,25 +162,17 @@ function actionsFor(scope: Scope) {
162
162
 
163
163
  const actions = { route: actionsFor("route"), dns: actionsFor("dns") };
164
164
 
165
- function validSubnet(input: string): boolean {
166
- const [address = "", prefix, ...extra] = input.split("/");
167
- const version = isIP(address);
168
- return (
169
- version !== 0 &&
170
- !address.includes("%") &&
171
- extra.length === 0 &&
172
- (prefix === undefined || (/^\d+$/.test(prefix) && Number(prefix) <= (version === 4 ? 32 : 128)))
173
- );
174
- }
175
-
176
165
  function checkAction(fields: Fields, path: string, scope: Scope): void {
177
166
  if (fields.method === "drop" && fields.no_drop === true)
178
167
  fail(`${path}.no_drop`, "no_drop with drop");
179
168
  if (fields.client_subnet !== undefined) {
180
169
  if (fields.remove_client_subnet === true)
181
170
  fail(`${path}.client_subnet`, "client_subnet with remove_client_subnet");
182
- if (!validSubnet(fields.client_subnet as string))
183
- fail(`${path}.client_subnet`, "invalid subnet");
171
+ const subnet = fields.client_subnet as string;
172
+ const valid = subnet.includes("/")
173
+ ? isCIDR(subnet)
174
+ : !subnet.includes("%") && isIP(subnet) !== 0;
175
+ if (!valid) fail(`${path}.client_subnet`, "invalid subnet");
184
176
  }
185
177
  if (
186
178
  scope === "dns" &&
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?: TlsMode,
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
- const users = new WeakSet<object>();
11
+ type Password = { readonly password: string };
11
12
 
12
- export function user(input: { name: string }): User {
13
- const { name } = fieldsFrom(input, "user", ["name"], ["name"]);
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.add(value);
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
+ }