sing-box-kit 0.0.0-stage → 0.1.0
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/LICENSE +674 -0
- package/README.md +25 -2
- package/dist/index.d.mts +3974 -0
- package/dist/index.mjs +10036 -0
- package/docs/configuration.md +56 -0
- package/docs/rules.md +81 -0
- package/index.ts +40 -0
- package/package.json +46 -4
- package/schema/README.md +19 -0
- package/schema/sing-box.json +17266 -0
- package/schema/version.json +4 -0
- package/scripts/generate-schema.ts +7 -0
- package/src/compile.ts +273 -0
- package/src/factories.ts +681 -0
- package/src/generated/sing-box.ts +3558 -0
- package/src/resource.ts +185 -0
- package/src/rules.ts +469 -0
- package/src/schema.ts +207 -0
- package/src/settings.ts +247 -0
- package/src/user.ts +22 -0
- package/tsconfig.json +31 -0
- package/vite.config.ts +16 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Configuration reference
|
|
2
|
+
|
|
3
|
+
Create resources with the factories exported from the project root. Pass type-specific options to the factory and use the supported `.listen()`, `.dial()`, and `.tls()` methods for shared settings.
|
|
4
|
+
|
|
5
|
+
## Factories and registration
|
|
6
|
+
|
|
7
|
+
| Factories | Register in |
|
|
8
|
+
| ----------------------------------------------------------------------------------------------------- | ----------------------- |
|
|
9
|
+
| `inbound.mixed()`, `inbound.socks()`, `inbound.shadowsocks()`, `inbound.vless()` | `inbounds` |
|
|
10
|
+
| `outbound.direct()`, `outbound.vless()`, `outbound.selector()`, `outbound.urltest()` | `outbounds` |
|
|
11
|
+
| `endpoint.tailscale()`, `endpoint.wireguard()` | `endpoints` |
|
|
12
|
+
| `service.api()`, `service.ssmApi()` | `services` |
|
|
13
|
+
| Tagged `httpClient()` | `http_clients` |
|
|
14
|
+
| `dnsServer.tcp()`, `dnsServer.udp()`, `dnsServer.tls()`, `dnsServer.https()`, `dnsServer.tailscale()` | `dns.servers` |
|
|
15
|
+
| `ruleSet.inline()`, `ruleSet.local()`, `ruleSet.remote()` | `route.rule_set` |
|
|
16
|
+
| `certificateProvider.tailscale()` | `certificate_providers` |
|
|
17
|
+
| `networkNamespace.default()`, `networkNamespace.unshare()` | `network_namespaces` |
|
|
18
|
+
|
|
19
|
+
Builder methods return new values. Register and reference the same final value after applying the settings. If you rebuild a resource with the same tag, rebuild any references to it. Apply each settings method at most once per chain.
|
|
20
|
+
|
|
21
|
+
`compile(input)` preserves list order, checks tag collisions and reference capabilities, and returns a `SingBoxConfig` suitable for `JSON.stringify()`. Outbounds and endpoints share a tag scope. Services without explicit tags use their zero-based list position for collision checks.
|
|
22
|
+
|
|
23
|
+
Use `validateConfig(value)` to check an existing JSON configuration against the pinned schema. This accepts raw tag strings and checks schema constraints. Use `compile()` for the Kit's registration, reference, and dependency checks.
|
|
24
|
+
|
|
25
|
+
## Shared settings
|
|
26
|
+
|
|
27
|
+
- `.listen(address, { port })` emits `listen` and `listen_port`. To forward traffic from an inbound, pass a compatible regular inbound as `detour`. Tailscale and WireGuard endpoints can match inbound rule conditions and serve as egress.
|
|
28
|
+
- `.dial(options)` accepts `connect_timeout`, `domain_resolver`, and `netns`. On dialers that accept `detour`, supply an outbound or endpoint. Put any non-zero timeout or namespace setting on the detour target. An otherwise empty direct outbound cannot be used as an explicit dial detour. WireGuard also disallows a non-zero `listen_port` with a dial detour.
|
|
29
|
+
- In Listen and Dial Fields, a typed `netns` value resolves to a registered namespace tag. A string passes through as supplied. sing-box looks for a registered tag before treating it as an OS name or path.
|
|
30
|
+
- Supply a DNS server to `domain_resolver` or `route.default_domain_resolver` directly or as `{ server: resolver, ...options }`. A TCP, UDP, TLS, or HTTPS DNS server configured with a hostname needs its own resolver or an explicit detour.
|
|
31
|
+
|
|
32
|
+
## TLS profiles
|
|
33
|
+
|
|
34
|
+
On supported builders, `.tls(profile)` enables TLS. 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.
|
|
35
|
+
|
|
36
|
+
VLESS also supports REALITY on both sides. 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 }`. The Kit enables uTLS for that client profile.
|
|
37
|
+
|
|
38
|
+
REALITY keys are unpadded Base64url strings for 32-byte keys. Short IDs are even-length hexadecimal strings of up to 16 characters, including the empty string. The inbound accepts a string or an array of strings. Keep the private key in the server configuration and share the public key and a selected short ID with the client.
|
|
39
|
+
|
|
40
|
+
## HTTP clients and services
|
|
41
|
+
|
|
42
|
+
The API service defaults to loopback `127.0.0.1`. Omitting TLS uses plaintext TCP, and an omitted or empty `secret` allows unauthenticated connections. `dashboard` accepts a boolean, path string, or settings object. Set `enabled: true` to enable the dashboard in object form.
|
|
43
|
+
|
|
44
|
+
The dashboard and remote rule-sets accept either a tagged client registered in `http_clients`, an untagged `httpClient()` builder, or an inline HTTP settings object. The inline object supports the sing-box HTTP client shape without `tag`, with Kit references for `detour`, `domain_resolver`, and typed `netns`. The dashboard uses its own default settings when its client is omitted. Set `route.default_http_client` to a registered client or let sing-box use the first registered client as its default.
|
|
45
|
+
|
|
46
|
+
The SSM API service accepts a non-empty `servers` map from HTTP paths starting with `/` to registered Shadowsocks inbounds. Use static multi-user Shadowsocks with a non-empty `users` list or managed Shadowsocks with `managed: true`. The `none` method is available for single-user Shadowsocks. Managed mode requires an omitted or empty static user list.
|
|
47
|
+
|
|
48
|
+
## Groups and rule-sets
|
|
49
|
+
|
|
50
|
+
Selectors and URL tests take a non-empty `outbounds` list of egress references with unique tags. A selector's optional `default` must be a member of that list.
|
|
51
|
+
|
|
52
|
+
Inline rule-sets take a single tag and at least one `HeadlessRule`. Local and remote rule-sets infer `source` from a `.json` file and `binary` from a `.srs` file. For remote URLs the path determines the format. Supply `format` explicitly for other paths.
|
|
53
|
+
|
|
54
|
+
Local and remote rule-sets support multiple tags. Put `{tag}` in the local `path`, or in the remote `url` and any supplied `initial_path`. Use a single-tag rule-set when matching a rule-set in a route or DNS rule.
|
|
55
|
+
|
|
56
|
+
See [Route and DNS rules](rules.md) for condition and action examples.
|
package/docs/rules.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# Route and DNS rules
|
|
2
|
+
|
|
3
|
+
Use `Rule.match(fields)` to create a condition. Attach an action with `route.rule(condition, action)` or `dns.rule(condition, action)` and add the result to the matching list in `compile()`. The one-argument form, such as `route.rule({ action: "reject" })`, matches all traffic that reaches the rule.
|
|
4
|
+
|
|
5
|
+
## Match conditions
|
|
6
|
+
|
|
7
|
+
Both rule scopes accept `domain`, `domain_suffix`, `port`, `inbound`, `rule_set`, and `auth_user`. Route rules also accept `client` and can use Tailscale or WireGuard endpoints in `preferred_by`. DNS rules also accept `query_type` and can use Tailscale DNS servers in `preferred_by`. The `inbound`, `rule_set`, and `preferred_by` fields accept one typed resource or an array of typed resources.
|
|
8
|
+
|
|
9
|
+
Use `.and()`, `.or()`, and `.not()` to build a condition tree. Each group retains its operand order, so `a.or(b).and(c)` means `(a OR b) AND c`. The action appears at the top level. Combining conditions requires at least one rule scope common to both operands.
|
|
10
|
+
|
|
11
|
+
## Match authenticated users
|
|
12
|
+
|
|
13
|
+
A `User` represents a name shared across inbounds. Supply credentials on each inbound entry. SOCKS, Mixed, and Shadowsocks use `password`. VLESS uses `uuid` with optional `flow`. Mixed and Shadowsocks also accept their sing-box entry formats. In an entry that uses `user`, omit the explicit `username` or `name`.
|
|
14
|
+
|
|
15
|
+
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.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { compile, inbound, outbound, route, Rule, user } from "./index";
|
|
19
|
+
|
|
20
|
+
const alice = user({ name: "alice" });
|
|
21
|
+
const proxy = inbound
|
|
22
|
+
.socks({
|
|
23
|
+
tag: "proxy",
|
|
24
|
+
users: [{ user: alice, password: "example-password" }],
|
|
25
|
+
})
|
|
26
|
+
.listen("127.0.0.1", { port: 1080 });
|
|
27
|
+
const egress = outbound.direct({ tag: "egress" });
|
|
28
|
+
const allowed = proxy.authUser(alice).and(Rule.match({ domain_suffix: "example.test" }));
|
|
29
|
+
|
|
30
|
+
compile({
|
|
31
|
+
inbounds: [proxy],
|
|
32
|
+
outbounds: [egress],
|
|
33
|
+
route: {
|
|
34
|
+
rules: [
|
|
35
|
+
route.rule(allowed, { action: "route", outbound: egress }),
|
|
36
|
+
route.rule({ action: "reject" }),
|
|
37
|
+
],
|
|
38
|
+
},
|
|
39
|
+
});
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Actions
|
|
43
|
+
|
|
44
|
+
| Rule scope | Action | Options |
|
|
45
|
+
| ---------- | --------------- | -------------------------------------------------------------------------------------- |
|
|
46
|
+
| Route | `route` | Requires an egress `outbound` |
|
|
47
|
+
| Route | `bypass` | Optional egress `outbound` |
|
|
48
|
+
| Route | `hijack-dns` | None |
|
|
49
|
+
| DNS | `route` | Requires a DNS `server`. Accepts shared DNS options, `race`, and `speculative` |
|
|
50
|
+
| DNS | `evaluate` | Requires a DNS `server`. Accepts a result `tag`, shared DNS options, and `speculative` |
|
|
51
|
+
| DNS | `respond` | Optional `race` |
|
|
52
|
+
| DNS | `route-options` | Requires at least one effective shared DNS option |
|
|
53
|
+
| DNS | `predefined` | Optional `rcode`, `answer`, `ns`, `extra`, and `race` |
|
|
54
|
+
| Both | `reject` | Optional `method` of `default` or `drop`, plus `no_drop`. DNS also accepts `race` |
|
|
55
|
+
|
|
56
|
+
Shared DNS options include `timeout`, `disable_cache`, `disable_optimistic_cache`, `rewrite_ttl`, `client_subnet`, and `remove_client_subnet`. For `route-options`, a positive timeout, a true flag, a valid subnet, or an explicit TTL counts as effective. A TTL of zero counts.
|
|
57
|
+
|
|
58
|
+
## DNS response evaluation
|
|
59
|
+
|
|
60
|
+
Place an `evaluate` rule before rules that depend on its response. Match a named response with `match_response: "tag"`, or an untagged response with `match_response: true`. With response matching active, a leaf can also test `response_rcode`, `response_answer`, `response_ns`, `response_extra`, `ip_cidr`, `ip_is_private`, or `ip_accept_any`.
|
|
61
|
+
|
|
62
|
+
A `respond` rule can return a named response when its own default condition matches that tag. For a logical `respond` rule, use an untagged evaluation. Enable `race` only on a rule with active response matching and omit `speculative` on that rule.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
import { compile, dns, dnsServer, Rule } from "./index";
|
|
66
|
+
|
|
67
|
+
const resolver = dnsServer.udp({ tag: "resolver", server: "192.0.2.53" });
|
|
68
|
+
const evaluate = dns.rule(Rule.match({ domain_suffix: "example.test" }), {
|
|
69
|
+
action: "evaluate",
|
|
70
|
+
server: resolver,
|
|
71
|
+
tag: "example-answer",
|
|
72
|
+
});
|
|
73
|
+
const respond = dns.rule(
|
|
74
|
+
Rule.match({ match_response: "example-answer", response_rcode: "NOERROR" }),
|
|
75
|
+
{ action: "respond" },
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
compile({ dns: { servers: [resolver], rules: [evaluate, respond] } });
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
See the [configuration reference](configuration.md) for resource registration and TLS.
|
package/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export { compile } from "./src/compile";
|
|
2
|
+
export type { CompileInput } from "./src/compile";
|
|
3
|
+
export {
|
|
4
|
+
certificateProvider,
|
|
5
|
+
dnsServer,
|
|
6
|
+
endpoint,
|
|
7
|
+
httpClient,
|
|
8
|
+
inbound,
|
|
9
|
+
networkNamespace,
|
|
10
|
+
outbound,
|
|
11
|
+
ruleSet,
|
|
12
|
+
service,
|
|
13
|
+
} from "./src/factories";
|
|
14
|
+
export type { HeadlessRule, SingBoxConfig } from "./src/generated/sing-box";
|
|
15
|
+
export { dns, route, Rule } from "./src/rules";
|
|
16
|
+
export type {
|
|
17
|
+
Condition,
|
|
18
|
+
DnsRuleAction,
|
|
19
|
+
MatchFields,
|
|
20
|
+
RouteRuleAction,
|
|
21
|
+
RuleAction,
|
|
22
|
+
RuleValue,
|
|
23
|
+
Scope,
|
|
24
|
+
} from "./src/rules";
|
|
25
|
+
export { validateConfig } from "./src/schema";
|
|
26
|
+
export type {
|
|
27
|
+
ClientTls,
|
|
28
|
+
Dial,
|
|
29
|
+
DirectDial,
|
|
30
|
+
DomainResolver,
|
|
31
|
+
HttpClientInput,
|
|
32
|
+
InboundListener,
|
|
33
|
+
InlineHttpClient,
|
|
34
|
+
Listener,
|
|
35
|
+
RealityClientTls,
|
|
36
|
+
RealityServerTls,
|
|
37
|
+
ServerTls,
|
|
38
|
+
} from "./src/settings";
|
|
39
|
+
export { user } from "./src/user";
|
|
40
|
+
export type { User } from "./src/user";
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sing-box-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Build and validate sing-box configurations with Bun and Node.js.",
|
|
5
|
+
"license": "GPL-3.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/k-kozika/sing-box-kit.git"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"index.ts",
|
|
13
|
+
"src",
|
|
14
|
+
"schema",
|
|
15
|
+
"docs",
|
|
16
|
+
"scripts/generate-schema.ts",
|
|
17
|
+
"tsconfig.json",
|
|
18
|
+
"vite.config.ts"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"types": "./dist/index.d.mts",
|
|
22
|
+
"exports": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public",
|
|
28
|
+
"registry": "https://registry.npmjs.org"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "vp pack",
|
|
32
|
+
"prepack": "bun run build",
|
|
33
|
+
"schema:generate": "bun scripts/generate-schema.ts"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"ajv": "^8.20.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/bun": "latest",
|
|
40
|
+
"json-schema-to-typescript": "^16.0.0",
|
|
41
|
+
"typescript": "^7.0.2",
|
|
42
|
+
"vite-plus": "1.8.0"
|
|
43
|
+
},
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=22.1.0"
|
|
46
|
+
},
|
|
47
|
+
"packageManager": "bun@1.4.5"
|
|
48
|
+
}
|
package/schema/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Schema and type generation
|
|
2
|
+
|
|
3
|
+
The [pinned schema](sing-box.json) is the verbatim output of `sing-box schema`. The Kit uses it for both Ajv validation and TypeScript generation.
|
|
4
|
+
|
|
5
|
+
[`version.json`](version.json) records the output of `sing-box version --name` and the `Revision` reported by `sing-box version` for the same binary.
|
|
6
|
+
|
|
7
|
+
The upstream sing-box project is Copyright (C) 2022 by nekohasekai and publishes under GPL-3.0-or-later with additional naming terms. See the [sing-box v1.14.4 license notice](https://github.com/SagerNet/sing-box/blob/v1.14.4/README.md#license) and [release](https://github.com/SagerNet/sing-box/releases/tag/v1.14.4).
|
|
8
|
+
|
|
9
|
+
## Regenerate the types
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
vp install --frozen-lockfile
|
|
13
|
+
bun run schema:generate
|
|
14
|
+
git diff --exit-code -- src/generated/sing-box.ts
|
|
15
|
+
vp check
|
|
16
|
+
bun test
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Type generation reads the local snapshot and works offline after dependency installation. CI checks that the generated file matches the checked-in types. When updating the supported sing-box version, update the schema snapshot, `version.json`, generated types, and affected test expectations together.
|