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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Generate server and client proxy configurations
|
|
2
|
+
|
|
3
|
+
A `proxyProfile` defines the public connection address and the settings both ends need to agree on. Call `.server()` to build the inbound for all configured users and `.client()` with a specific `User` to build that user's outbound. Add each resource to its own `compile()` call.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { compile, proxyProfile, user } from "sing-box-kit";
|
|
7
|
+
|
|
8
|
+
const alice = user({
|
|
9
|
+
name: "alice",
|
|
10
|
+
credentials: { vless: { uuid: "00000000-0000-4000-8000-000000000001" } },
|
|
11
|
+
});
|
|
12
|
+
const bob = user({
|
|
13
|
+
name: "bob",
|
|
14
|
+
credentials: { vless: { uuid: "00000000-0000-4000-8000-000000000002" } },
|
|
15
|
+
});
|
|
16
|
+
const proxy = proxyProfile.vless({
|
|
17
|
+
users: [alice, bob],
|
|
18
|
+
connect: { host: "edge.example", port: 443 },
|
|
19
|
+
transport: { type: "grpc", service_name: "proxy" },
|
|
20
|
+
tls: { mode: "tls" },
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const incoming = proxy
|
|
24
|
+
.server({
|
|
25
|
+
tag: "incoming",
|
|
26
|
+
tls: { certificate_path: "cert.pem", key_path: "key.pem" },
|
|
27
|
+
})
|
|
28
|
+
.listen("0.0.0.0", { port: 8443 });
|
|
29
|
+
const outgoing = proxy.client(alice, { tag: "outgoing" });
|
|
30
|
+
|
|
31
|
+
const serverConfig = compile({ inbounds: [incoming] });
|
|
32
|
+
const aliceConfig = compile({ outbounds: [outgoing] });
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Reuse the same `User` object when selecting a client or calling `.authUser()` on the returned inbound. Each protocol has its own credential entry on `User`. The profile uses that entry wherever the same user and protocol appear. Serialize the generated client config when distributing it. The profile inputs can contain every user's credentials and the server's private key.
|
|
36
|
+
|
|
37
|
+
Supply a `tag` to each `.server()` and `.client()` call. Set listen fields in `.server({ tag, listen: { address, port } })` or chain `.listen(address, { port })`. On the client, pass dial fields in the call or chain `.dial()`.
|
|
38
|
+
|
|
39
|
+
## Transport and TLS
|
|
40
|
+
|
|
41
|
+
VMess, VLESS, and Trojan default to TCP. Select `http`, `ws`, `grpc`, `httpupgrade`, or `quic` with `transport.type`. Set shared parameters such as the gRPC service name in the profile. Set side-specific `transport.headers` for HTTP, WebSocket, and HTTPUpgrade, or transport timeouts for HTTP and gRPC. Use `options` on `.server()` and `.client()` for other protocol-specific fields, such as VLESS packet encoding on the client.
|
|
42
|
+
|
|
43
|
+
For regular TLS, the client uses the public host as the verification name by default. Set `tls.serverName` when it differs. Set `tls.alpn` in the profile to send the same list to both ends. V2Ray QUIC requires regular TLS.
|
|
44
|
+
|
|
45
|
+
For VLESS REALITY, use `tls: { mode: "reality", serverName, handshake: { host, port }, keyPair: { privateKey, publicKey }, shortId }`. To accept multiple short IDs, use `shortIds` and pass `shortId` in the client options.
|
|
46
|
+
|
|
47
|
+
## Protocol versions and shared keys
|
|
48
|
+
|
|
49
|
+
| Protocol | Profile setting |
|
|
50
|
+
| --------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
51
|
+
| SOCKS | `version: "4"`, `"4a"`, or `"5"` (default). For SOCKS4 and SOCKS4a username-only authentication, set `credentials.socks.password` to an empty string. |
|
|
52
|
+
| ShadowTLS | `version: 1` for no authentication, `2` for a shared `password`, or `3` (default) for `users`. Set `handshake` and `serverName` in the profile. |
|
|
53
|
+
| Snell | `version: 5` builds a v5 inbound and a compatible v4 outbound. `version: 6` (default) uses v6 on both ends. Set the shared `psk` in the profile. |
|
|
54
|
+
|
|
55
|
+
Call `.client({ tag })` when the profile uses no authentication or a shared key with no per-user credential.
|
|
56
|
+
|
|
57
|
+
With Snell `version: 5` and `obfs_mode: "http"`, set the v4 outbound's obfuscation host in the client call's `options.obfs_host`.
|
|
58
|
+
|
|
59
|
+
## Shadowsocks SSM and Hysteria2 Realm
|
|
60
|
+
|
|
61
|
+
For Shadowsocks 2022 AES with multiple users, put the server key in `serverKey` and each user key in `credentials.shadowsocks.password`. For SSM-managed users, set `managed: true`, register the returned inbound with `service.ssmApi()`, and pass a `User` with the issued key to `.client()`. User registration takes place separately through SSM.
|
|
62
|
+
|
|
63
|
+
For Hysteria or Hysteria2 port hopping, use `connect: { host, ports: ["443:444", "8443:8443"] }` and configure the corresponding server-side port forwarding. For Hysteria2 Realm, provide `realm: { server_url, realm_id, stun_servers, token }` in place of `connect` and specify the Hysteria2 server's certificate name in `tls.serverName`. Both sides use the shared Realm settings. Set `realm.token` on an individual generation call when it uses a different token.
|
package/docs/rules.md
CHANGED
|
@@ -44,18 +44,20 @@ Route options include `override_address`, `override_port`, and the other fields
|
|
|
44
44
|
|
|
45
45
|
## Match authenticated users
|
|
46
46
|
|
|
47
|
-
A `User` represents a name shared across inbounds.
|
|
47
|
+
A `User` represents a name shared across inbounds. Set protocol credentials in `user({ name, credentials })` and pass that object in `users` to an inbound factory or [proxy profile](proxy-profiles.md). For example, use `credentials.socks.password` for SOCKS and `credentials.vless.uuid` with optional `flow` for VLESS.
|
|
48
|
+
|
|
49
|
+
The inbound factories for SOCKS, Mixed, Shadowsocks, VLESS, HTTP, VMess, Trojan, Hysteria, Hysteria2, TUIC, ShadowTLS v3, Naive, AnyTLS, and Snell support `.authUser()` for configured users.
|
|
48
50
|
|
|
49
51
|
Call `inboundValue.authUser(user)` on the final inbound to match that inbound and authenticated name together. The exact `User` object must appear once, and its name must be non-empty and unique on that inbound. To match a name added through the SSM API, use `Rule.match({ inbound: managedInbound, auth_user: "alice" })`. A plain `auth_user` condition can also match a name across inbounds. An empty string can match traffic with no authenticated name.
|
|
50
52
|
|
|
51
53
|
```ts
|
|
52
54
|
import { compile, inbound, outbound, route, Rule, user } from "sing-box-kit";
|
|
53
55
|
|
|
54
|
-
const alice = user({ name: "alice" });
|
|
56
|
+
const alice = user({ name: "alice", credentials: { socks: { password: "example-password" } } });
|
|
55
57
|
const proxy = inbound
|
|
56
58
|
.socks({
|
|
57
59
|
tag: "proxy",
|
|
58
|
-
users: [
|
|
60
|
+
users: [alice],
|
|
59
61
|
})
|
|
60
62
|
.listen("127.0.0.1", { port: 1080 });
|
|
61
63
|
const egress = outbound.direct({ tag: "egress" });
|
package/docs/settings.md
CHANGED
|
@@ -29,6 +29,8 @@ Use `.tls(options)` for the kind's TLS fields, such as `outbound.http({ tag: "pr
|
|
|
29
29
|
|
|
30
30
|
Standard inbound and outbound TLS builders also accept side-tagged profiles. Use `side: "server"` with a `certificate_path` and `key_path` pair or a registered `certificate_provider`. Use `side: "client"` with optional `server_name` and `certificate_path` settings.
|
|
31
31
|
|
|
32
|
+
Naive builders and proxy profiles negotiate ALPN automatically. Omit `alpn` from their TLS settings.
|
|
33
|
+
|
|
32
34
|
### VLESS with REALITY
|
|
33
35
|
|
|
34
36
|
On the inbound, set `server_name` and pass `reality: { private_key, short_id, handshake }`. The handshake has its own `server`, `server_port`, and optional Dial Fields. On the outbound, set the VLESS `server` and `server_port` for the connection, set the TLS `server_name` for verification, and pass `reality: { public_key, short_id }`. SBKit enables uTLS for that client profile.
|
package/index.ts
CHANGED
|
@@ -12,6 +12,7 @@ export {
|
|
|
12
12
|
service,
|
|
13
13
|
} from "./src/factories";
|
|
14
14
|
export type { HeadlessRule, SingBoxConfig } from "./src/generated/sing-box";
|
|
15
|
+
export { proxyProfile } from "./src/proxy-profile";
|
|
15
16
|
export { dns, route, Rule } from "./src/rules";
|
|
16
17
|
export type {
|
|
17
18
|
Condition,
|
|
@@ -37,4 +38,4 @@ export type {
|
|
|
37
38
|
ServerTls,
|
|
38
39
|
} from "./src/settings";
|
|
39
40
|
export { user } from "./src/user";
|
|
40
|
-
export type { User } from "./src/user";
|
|
41
|
+
export type { User, UserCredentials } from "./src/user";
|
package/package.json
CHANGED
package/src/factories.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SBKitInputs } from "./generated/sbkit-inputs";
|
|
2
|
-
import type { ShadowsocksUser
|
|
2
|
+
import type { ShadowsocksUser } from "./generated/sing-box";
|
|
3
3
|
import { factoryCatalog, factoryName } from "./input-schema";
|
|
4
4
|
import type { FactoryBranch } from "./input-schema";
|
|
5
5
|
import {
|
|
@@ -23,9 +23,16 @@ import {
|
|
|
23
23
|
previewReference,
|
|
24
24
|
tlsFields,
|
|
25
25
|
} from "./settings";
|
|
26
|
-
import type {
|
|
26
|
+
import type {
|
|
27
|
+
ClientTls,
|
|
28
|
+
ProtocolTlsOptions,
|
|
29
|
+
RealityClientTls,
|
|
30
|
+
RealityServerTls,
|
|
31
|
+
ServerTls,
|
|
32
|
+
TlsMode,
|
|
33
|
+
} from "./settings";
|
|
27
34
|
import type { CamelCase, Fields, ReadonlyJson, StrictUnion } from "./type-utils";
|
|
28
|
-
import { isUser } from "./user";
|
|
35
|
+
import { isUser, requireUserCredentials } from "./user";
|
|
29
36
|
import type { User } from "./user";
|
|
30
37
|
|
|
31
38
|
interface ListenMethod<T> {
|
|
@@ -60,39 +67,35 @@ const endpointRoles = {
|
|
|
60
67
|
"openvpn-client": "openvpn-client-endpoint",
|
|
61
68
|
} as const satisfies Record<string, Role>;
|
|
62
69
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
readonly password: string;
|
|
66
|
-
} & { readonly [K in Name]?: never };
|
|
67
|
-
type MixedEntry = PasswordEntry<"username"> | (Readonly<RawUser> & { user?: never });
|
|
68
|
-
type ShadowsocksEntry = PasswordEntry<"name"> | (Readonly<ShadowsocksUser> & { user?: never });
|
|
69
|
-
type VlessEntry = Required<Pick<VLESSUser, "uuid">> &
|
|
70
|
-
Pick<VLESSUser, "flow"> & { user: User; name?: never };
|
|
71
|
-
|
|
72
|
-
interface AuthOptions {
|
|
73
|
-
readonly name: "name" | "username";
|
|
74
|
-
readonly credentials: readonly string[];
|
|
75
|
-
readonly required: readonly string[];
|
|
76
|
-
readonly allowRaw?: boolean;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const passwordAuth = {
|
|
80
|
-
name: "username",
|
|
81
|
-
credentials: ["password"],
|
|
82
|
-
required: ["password"],
|
|
83
|
-
} as const;
|
|
70
|
+
const namedAuth = { name: "name", allowRaw: true } as const;
|
|
71
|
+
const usernameAuth = { name: "username", allowRaw: true } as const;
|
|
84
72
|
const authOptions = {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
73
|
+
anytls: namedAuth,
|
|
74
|
+
http: usernameAuth,
|
|
75
|
+
hysteria: namedAuth,
|
|
76
|
+
hysteria2: namedAuth,
|
|
77
|
+
mixed: usernameAuth,
|
|
78
|
+
naive: usernameAuth,
|
|
79
|
+
shadowsocks: namedAuth,
|
|
80
|
+
shadowtls: namedAuth,
|
|
81
|
+
snell: namedAuth,
|
|
82
|
+
socks: { name: "username", allowRaw: false },
|
|
83
|
+
trojan: namedAuth,
|
|
84
|
+
tuic: namedAuth,
|
|
85
|
+
vless: { name: "name", allowRaw: false },
|
|
86
|
+
vmess: namedAuth,
|
|
87
|
+
} as const;
|
|
88
|
+
type AuthKind = keyof typeof authOptions;
|
|
89
|
+
type ShadowsocksEntry = User | (Readonly<ShadowsocksUser> & { user?: never });
|
|
90
90
|
|
|
91
91
|
type Catalog = SBKitInputs["factories"];
|
|
92
92
|
type Kind<T extends Target> = keyof Catalog[T] & string;
|
|
93
93
|
type Groups<T extends Target, K extends Kind<T>> = ReadonlyJson<Catalog[T][K]>;
|
|
94
|
+
type StrictTransport<O> = O extends { readonly transport?: infer V }
|
|
95
|
+
? O & { readonly transport?: StrictUnion<NonNullable<V>> }
|
|
96
|
+
: O;
|
|
94
97
|
type Options<T extends Target, K extends Kind<T>> =
|
|
95
|
-
Groups<T, K> extends { options: infer O } ? StrictUnion<O
|
|
98
|
+
Groups<T, K> extends { options: infer O } ? StrictUnion<StrictTransport<O>> : never;
|
|
96
99
|
type Tag = string | readonly string[] | undefined;
|
|
97
100
|
type ExtraRoles<T extends Target, K> = T extends "endpoints"
|
|
98
101
|
? | (K extends keyof typeof endpointRoles ? (typeof endpointRoles)[K] : never)
|
|
@@ -123,7 +126,9 @@ type Built<
|
|
|
123
126
|
> = Resource<T, K, V, (typeof targetRoles)[T][number] | ExtraRoles<T, K>> &
|
|
124
127
|
(G extends { listen: infer L } ? ListenMethod<L> : unknown) &
|
|
125
128
|
(G extends { dial: infer D } ? DialMethod<D> : unknown) &
|
|
126
|
-
(G extends { tls: infer F }
|
|
129
|
+
(G extends { tls: infer F }
|
|
130
|
+
? TlsMethod<ProtocolTlsOptions<K, (F & { side?: never }) | Profile<T, K, F>>>
|
|
131
|
+
: unknown) &
|
|
127
132
|
(T extends "inbounds" ? (K extends keyof typeof authOptions ? AuthMethod : unknown) : unknown);
|
|
128
133
|
|
|
129
134
|
type Factory<T extends Target, K extends Kind<T>> = T extends "services" | "http_clients"
|
|
@@ -141,30 +146,28 @@ interface Membership {
|
|
|
141
146
|
readonly user?: User;
|
|
142
147
|
readonly name: unknown;
|
|
143
148
|
}
|
|
144
|
-
function prepareUsers(fields: Fields,
|
|
149
|
+
function prepareUsers(fields: Fields, kind: AuthKind, path: string): readonly Membership[] {
|
|
145
150
|
if (fields.users === undefined) return [];
|
|
146
151
|
const users = fields.users;
|
|
147
152
|
if (!Array.isArray(users)) fail(`${path}.users`, "expected an array");
|
|
153
|
+
const auth = authOptions[kind];
|
|
148
154
|
const members: Membership[] = [];
|
|
149
155
|
fields.users = Array.from(users, (value, index) => {
|
|
150
156
|
const entryPath = `${path}.users[${index}]`;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
157
|
+
if (isUser(value)) {
|
|
158
|
+
members.push({ user: value, name: value.name });
|
|
159
|
+
if (kind === "vmess") {
|
|
160
|
+
const { alter_id, ...credentials } = requireUserCredentials(value, kind, entryPath);
|
|
161
|
+
return { name: value.name, ...credentials, alterId: alter_id };
|
|
162
|
+
}
|
|
163
|
+
return { [auth.name]: value.name, ...requireUserCredentials(value, kind, entryPath) };
|
|
156
164
|
}
|
|
157
|
-
if (
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
["user", ...auth.required],
|
|
164
|
-
);
|
|
165
|
-
if (!isUser(user)) fail(`${entryPath}.user`, "expected a User");
|
|
166
|
-
members.push({ user, name: user.name });
|
|
167
|
-
return { [auth.name]: user.name, ...credentials };
|
|
165
|
+
if (!auth.allowRaw) fail(entryPath, "expected a User from user()");
|
|
166
|
+
const raw = record(value, entryPath);
|
|
167
|
+
if (Object.hasOwn(raw, "user"))
|
|
168
|
+
fail(`${entryPath}.user`, "pass the User directly and configure credentials in user()");
|
|
169
|
+
members.push({ name: raw[auth.name] });
|
|
170
|
+
return raw;
|
|
168
171
|
});
|
|
169
172
|
return members;
|
|
170
173
|
}
|
|
@@ -275,9 +278,7 @@ function makeFactory(target: Target, kind: string, branches: FactoryBranch[]) {
|
|
|
275
278
|
const path = `${target}.${kind}`;
|
|
276
279
|
const validators = new Map<FactoryBranch, ReturnType<typeof compileSchema>>();
|
|
277
280
|
const auth =
|
|
278
|
-
target === "inbounds" && Object.hasOwn(authOptions, kind)
|
|
279
|
-
? authOptions[kind as keyof typeof authOptions]
|
|
280
|
-
: undefined;
|
|
281
|
+
target === "inbounds" && Object.hasOwn(authOptions, kind) ? (kind as AuthKind) : undefined;
|
|
281
282
|
return (input: unknown = {}): Resource => {
|
|
282
283
|
const raw = record(input, path);
|
|
283
284
|
const branch =
|
|
@@ -295,7 +296,8 @@ function makeFactory(target: Target, kind: string, branches: FactoryBranch[]) {
|
|
|
295
296
|
branch.options.required,
|
|
296
297
|
);
|
|
297
298
|
validateTag(options.tag, `${path}.tag`);
|
|
298
|
-
const
|
|
299
|
+
const users = auth ? prepareUsers(options, auth, path) : [];
|
|
300
|
+
const members = kind === "shadowtls" && options.version !== 3 ? [] : users;
|
|
299
301
|
const roles = getRoles(target, kind);
|
|
300
302
|
prepareOptions(target, kind, options, path, roles);
|
|
301
303
|
const base = snapshot(target === "http_clients" ? options : { type: kind, ...options }, path);
|
|
@@ -350,7 +352,7 @@ function makeFactory(target: Target, kind: string, branches: FactoryBranch[]) {
|
|
|
350
352
|
if (name === "InboundTLSOptions") mode = kind === "vless" ? "server-reality" : "server";
|
|
351
353
|
if (name === "OutboundTLSOptions") mode = kind === "vless" ? "client-reality" : "client";
|
|
352
354
|
methods.tls = (input: unknown) =>
|
|
353
|
-
apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode));
|
|
355
|
+
apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode, kind));
|
|
354
356
|
}
|
|
355
357
|
if (auth) {
|
|
356
358
|
methods.authUser = (user: User) => {
|
|
@@ -417,21 +419,24 @@ type ShadowsocksOptions = ShadowsocksBase &
|
|
|
417
419
|
| { method: MultiMethod; managed?: false; users?: readonly ShadowsocksEntry[] }
|
|
418
420
|
);
|
|
419
421
|
|
|
420
|
-
type
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
422
|
+
type InboundUsers<O, K extends AuthKind> = O extends { users?: readonly (infer Entry)[] }
|
|
423
|
+
? Omit<O, "users"> &
|
|
424
|
+
(O extends { users: unknown }
|
|
425
|
+
? { readonly users: readonly User[] }
|
|
426
|
+
: {
|
|
427
|
+
readonly users?: readonly (
|
|
428
|
+
| User
|
|
429
|
+
| ((typeof authOptions)[K]["allowRaw"] extends true
|
|
430
|
+
? Entry & { user?: never }
|
|
431
|
+
: never)
|
|
432
|
+
)[];
|
|
433
|
+
})
|
|
434
|
+
: O;
|
|
435
|
+
type InboundFactories = Omit<Factories<"inbounds">, AuthKind> & {
|
|
436
|
+
[K in Exclude<AuthKind, "shadowsocks">]: (
|
|
437
|
+
options: StrictUnion<InboundUsers<Options<"inbounds", K>, K>>,
|
|
438
|
+
) => Built<"inbounds", K>;
|
|
439
|
+
} & {
|
|
435
440
|
shadowsocks: {
|
|
436
441
|
(
|
|
437
442
|
options: ManagedShadowsocks | StaticShadowsocks,
|