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/dist/index.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isIP } from "node:net";
|
|
2
2
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
3
|
+
import { createPrivateKey, createPublicKey } from "node:crypto";
|
|
3
4
|
// schema/sing-box.json
|
|
4
5
|
var sing_box_default = {
|
|
5
6
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
@@ -9352,8 +9353,12 @@ function listenFields(address, input, path, schema) {
|
|
|
9352
9353
|
if (port !== void 0) options.listen_port = port;
|
|
9353
9354
|
return options;
|
|
9354
9355
|
}
|
|
9355
|
-
function
|
|
9356
|
+
function checkTlsAlpn(kind, options, path) {
|
|
9357
|
+
if (kind === "naive" && options.alpn !== void 0) fail(`${path}.alpn`, "ALPN cannot be configured for Naive");
|
|
9358
|
+
}
|
|
9359
|
+
function tlsFields(input, path, schema, mode, kind) {
|
|
9356
9360
|
const profile = record(input, path);
|
|
9361
|
+
checkTlsAlpn(kind, profile, path);
|
|
9357
9362
|
const enabled = Object.hasOwn(schemaShape(schema).properties ?? {}, "enabled");
|
|
9358
9363
|
if (!Object.hasOwn(profile, "side")) return { tls: enabled && profile.enabled === void 0 ? {
|
|
9359
9364
|
...profile,
|
|
@@ -9755,17 +9760,52 @@ function compile(input) {
|
|
|
9755
9760
|
return validateConfig(output);
|
|
9756
9761
|
}
|
|
9757
9762
|
// src/user.ts
|
|
9758
|
-
const
|
|
9763
|
+
const passwordFields = ["password"];
|
|
9764
|
+
const credentialFields = {
|
|
9765
|
+
anytls: passwordFields,
|
|
9766
|
+
http: passwordFields,
|
|
9767
|
+
hysteria: ["auth", "auth_str"],
|
|
9768
|
+
hysteria2: passwordFields,
|
|
9769
|
+
mixed: passwordFields,
|
|
9770
|
+
naive: passwordFields,
|
|
9771
|
+
shadowsocks: passwordFields,
|
|
9772
|
+
shadowtls: passwordFields,
|
|
9773
|
+
snell: ["userkey"],
|
|
9774
|
+
socks: passwordFields,
|
|
9775
|
+
trojan: passwordFields,
|
|
9776
|
+
tuic: ["password", "uuid"],
|
|
9777
|
+
vless: ["uuid", "flow"],
|
|
9778
|
+
vmess: ["uuid", "alter_id"]
|
|
9779
|
+
};
|
|
9780
|
+
const users = /* @__PURE__ */ new WeakMap();
|
|
9759
9781
|
function user(input) {
|
|
9760
|
-
const { name } = fieldsFrom(input, "user", ["name"], ["name"]);
|
|
9782
|
+
const { name, credentials } = fieldsFrom(input, "user", ["name", "credentials"], ["name"]);
|
|
9761
9783
|
if (typeof name !== "string") fail("user.name", "expected a string");
|
|
9784
|
+
const entries = fieldsFrom(credentials === void 0 ? {} : credentials, "user.credentials", Object.keys(credentialFields));
|
|
9785
|
+
for (const key of Object.keys(credentialFields)) {
|
|
9786
|
+
if (!Object.hasOwn(entries, key)) continue;
|
|
9787
|
+
const path = `user.credentials.${key}`;
|
|
9788
|
+
const allowed = credentialFields[key];
|
|
9789
|
+
const required = key === "hysteria" ? [] : allowed.filter((field) => field !== "flow" && field !== "alter_id");
|
|
9790
|
+
const entry = fieldsFrom(entries[key], path, allowed, required);
|
|
9791
|
+
if (key === "hysteria" && Object.keys(entry).length !== 1) fail(path, "expected either auth or auth_str");
|
|
9792
|
+
for (const [field, value] of Object.entries(entry)) if (field === "alter_id") {
|
|
9793
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) fail(`${path}.${field}`, "expected a non-negative integer");
|
|
9794
|
+
} else if (field === "auth" && Array.isArray(value)) {
|
|
9795
|
+
if (Array.from(value).some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)) fail(`${path}.${field}`, "expected an array of bytes");
|
|
9796
|
+
} else if (typeof value !== "string") fail(`${path}.${field}`, "expected a string");
|
|
9797
|
+
entries[key] = entry;
|
|
9798
|
+
}
|
|
9762
9799
|
const value = preserveIdentity({ name });
|
|
9763
|
-
users.
|
|
9800
|
+
users.set(value, snapshot(entries, "user.credentials"));
|
|
9764
9801
|
return value;
|
|
9765
9802
|
}
|
|
9766
9803
|
function isUser(value) {
|
|
9767
9804
|
return value !== null && typeof value === "object" && users.has(value);
|
|
9768
9805
|
}
|
|
9806
|
+
function requireUserCredentials(value, kind, path) {
|
|
9807
|
+
return users.get(value)?.[kind] ?? fail(path, `missing ${kind} credentials`);
|
|
9808
|
+
}
|
|
9769
9809
|
// src/factories.ts
|
|
9770
9810
|
const targetRoles = {
|
|
9771
9811
|
inbounds: ["inbound", "listen-detour"],
|
|
@@ -9783,52 +9823,67 @@ const endpointRoles = {
|
|
|
9783
9823
|
openconnect: "openconnect-endpoint",
|
|
9784
9824
|
"openvpn-client": "openvpn-client-endpoint"
|
|
9785
9825
|
};
|
|
9786
|
-
const
|
|
9826
|
+
const namedAuth = {
|
|
9827
|
+
name: "name",
|
|
9828
|
+
allowRaw: true
|
|
9829
|
+
};
|
|
9830
|
+
const usernameAuth = {
|
|
9787
9831
|
name: "username",
|
|
9788
|
-
|
|
9789
|
-
required: ["password"]
|
|
9832
|
+
allowRaw: true
|
|
9790
9833
|
};
|
|
9791
9834
|
const authOptions = {
|
|
9792
|
-
|
|
9793
|
-
|
|
9794
|
-
|
|
9795
|
-
|
|
9796
|
-
|
|
9797
|
-
|
|
9798
|
-
|
|
9799
|
-
|
|
9800
|
-
|
|
9835
|
+
anytls: namedAuth,
|
|
9836
|
+
http: usernameAuth,
|
|
9837
|
+
hysteria: namedAuth,
|
|
9838
|
+
hysteria2: namedAuth,
|
|
9839
|
+
mixed: usernameAuth,
|
|
9840
|
+
naive: usernameAuth,
|
|
9841
|
+
shadowsocks: namedAuth,
|
|
9842
|
+
shadowtls: namedAuth,
|
|
9843
|
+
snell: namedAuth,
|
|
9844
|
+
socks: {
|
|
9845
|
+
name: "username",
|
|
9846
|
+
allowRaw: false
|
|
9801
9847
|
},
|
|
9848
|
+
trojan: namedAuth,
|
|
9849
|
+
tuic: namedAuth,
|
|
9802
9850
|
vless: {
|
|
9803
9851
|
name: "name",
|
|
9804
|
-
|
|
9805
|
-
|
|
9806
|
-
|
|
9852
|
+
allowRaw: false
|
|
9853
|
+
},
|
|
9854
|
+
vmess: namedAuth
|
|
9807
9855
|
};
|
|
9808
|
-
function prepareUsers(fields,
|
|
9856
|
+
function prepareUsers(fields, kind, path) {
|
|
9809
9857
|
if (fields.users === void 0) return [];
|
|
9810
9858
|
const users = fields.users;
|
|
9811
9859
|
if (!Array.isArray(users)) fail(`${path}.users`, "expected an array");
|
|
9860
|
+
const auth = authOptions[kind];
|
|
9812
9861
|
const members = [];
|
|
9813
9862
|
fields.users = Array.from(users, (value, index) => {
|
|
9814
9863
|
const entryPath = `${path}.users[${index}]`;
|
|
9815
|
-
|
|
9816
|
-
|
|
9817
|
-
|
|
9818
|
-
|
|
9819
|
-
|
|
9864
|
+
if (isUser(value)) {
|
|
9865
|
+
members.push({
|
|
9866
|
+
user: value,
|
|
9867
|
+
name: value.name
|
|
9868
|
+
});
|
|
9869
|
+
if (kind === "vmess") {
|
|
9870
|
+
const { alter_id, ...credentials } = requireUserCredentials(value, kind, entryPath);
|
|
9871
|
+
return {
|
|
9872
|
+
name: value.name,
|
|
9873
|
+
...credentials,
|
|
9874
|
+
alterId: alter_id
|
|
9875
|
+
};
|
|
9876
|
+
}
|
|
9877
|
+
return {
|
|
9878
|
+
[auth.name]: value.name,
|
|
9879
|
+
...requireUserCredentials(value, kind, entryPath)
|
|
9880
|
+
};
|
|
9820
9881
|
}
|
|
9821
|
-
if (
|
|
9822
|
-
const
|
|
9823
|
-
if (
|
|
9824
|
-
members.push({
|
|
9825
|
-
|
|
9826
|
-
name: user.name
|
|
9827
|
-
});
|
|
9828
|
-
return {
|
|
9829
|
-
[auth.name]: user.name,
|
|
9830
|
-
...credentials
|
|
9831
|
-
};
|
|
9882
|
+
if (!auth.allowRaw) fail(entryPath, "expected a User from user()");
|
|
9883
|
+
const raw = record(value, entryPath);
|
|
9884
|
+
if (Object.hasOwn(raw, "user")) fail(`${entryPath}.user`, "pass the User directly and configure credentials in user()");
|
|
9885
|
+
members.push({ name: raw[auth.name] });
|
|
9886
|
+
return raw;
|
|
9832
9887
|
});
|
|
9833
9888
|
return members;
|
|
9834
9889
|
}
|
|
@@ -9912,7 +9967,7 @@ function getRoles(target, kind) {
|
|
|
9912
9967
|
function makeFactory(target, kind, branches) {
|
|
9913
9968
|
const path = `${target}.${kind}`;
|
|
9914
9969
|
const validators = /* @__PURE__ */ new Map();
|
|
9915
|
-
const auth = target === "inbounds" && Object.hasOwn(authOptions, kind) ?
|
|
9970
|
+
const auth = target === "inbounds" && Object.hasOwn(authOptions, kind) ? kind : void 0;
|
|
9916
9971
|
return (input = {}) => {
|
|
9917
9972
|
const raw = record(input, path);
|
|
9918
9973
|
const branch = branches.length === 1 ? branches[0] : branches.find((item) => matchesDiscriminators(item.options, raw));
|
|
@@ -9922,7 +9977,8 @@ function makeFactory(target, kind, branches) {
|
|
|
9922
9977
|
}
|
|
9923
9978
|
const options = fieldsFrom(raw, path, Object.keys(branch.options.properties), branch.options.required);
|
|
9924
9979
|
validateTag(options.tag, `${path}.tag`);
|
|
9925
|
-
const
|
|
9980
|
+
const users = auth ? prepareUsers(options, auth, path) : [];
|
|
9981
|
+
const members = kind === "shadowtls" && options.version !== 3 ? [] : users;
|
|
9926
9982
|
const roles = getRoles(target, kind);
|
|
9927
9983
|
prepareOptions(target, kind, options, path, roles);
|
|
9928
9984
|
const base = snapshot(target === "http_clients" ? options : {
|
|
@@ -9959,7 +10015,7 @@ function makeFactory(target, kind, branches) {
|
|
|
9959
10015
|
let mode;
|
|
9960
10016
|
if (name === "InboundTLSOptions") mode = kind === "vless" ? "server-reality" : "server";
|
|
9961
10017
|
if (name === "OutboundTLSOptions") mode = kind === "vless" ? "client-reality" : "client";
|
|
9962
|
-
methods.tls = (input) => apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode));
|
|
10018
|
+
methods.tls = (input) => apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode, kind));
|
|
9963
10019
|
}
|
|
9964
10020
|
if (auth) methods.authUser = (user) => {
|
|
9965
10021
|
if (!isUser(user) || members.filter((entry) => entry.user === user).length !== 1 || !user.name || members.filter((entry) => entry.name === user.name).length !== 1) fail(`${path}.authUser`, "expected one User entry with a unique non-empty name");
|
|
@@ -9994,4 +10050,498 @@ const certificateProvider = Object.freeze(factoriesFor("certificate_providers"))
|
|
|
9994
10050
|
const networkNamespace = Object.freeze(factoriesFor("network_namespaces"));
|
|
9995
10051
|
const httpClient = factoriesFor("http_clients").httpClient;
|
|
9996
10052
|
const ruleSet = Object.freeze(factoriesFor("route.rule_set"));
|
|
9997
|
-
|
|
10053
|
+
// src/proxy-profile.ts
|
|
10054
|
+
const sharedOption = {
|
|
10055
|
+
hysteria: "obfs",
|
|
10056
|
+
hysteria2: "obfs",
|
|
10057
|
+
shadowsocks: "method"
|
|
10058
|
+
};
|
|
10059
|
+
const ownedServer = [
|
|
10060
|
+
"tag",
|
|
10061
|
+
"users",
|
|
10062
|
+
"password",
|
|
10063
|
+
"version",
|
|
10064
|
+
"transport",
|
|
10065
|
+
"managed",
|
|
10066
|
+
"destinations",
|
|
10067
|
+
"psk",
|
|
10068
|
+
"mode",
|
|
10069
|
+
"obfs_mode",
|
|
10070
|
+
"realm",
|
|
10071
|
+
"handshake"
|
|
10072
|
+
];
|
|
10073
|
+
const ownedClient = [
|
|
10074
|
+
"tag",
|
|
10075
|
+
"server",
|
|
10076
|
+
"server_port",
|
|
10077
|
+
"server_ports",
|
|
10078
|
+
"username",
|
|
10079
|
+
"password",
|
|
10080
|
+
"uuid",
|
|
10081
|
+
"flow",
|
|
10082
|
+
"alter_id",
|
|
10083
|
+
"auth",
|
|
10084
|
+
"auth_str",
|
|
10085
|
+
"userkey",
|
|
10086
|
+
"psk",
|
|
10087
|
+
"version",
|
|
10088
|
+
"transport",
|
|
10089
|
+
"mode",
|
|
10090
|
+
"obfs_mode",
|
|
10091
|
+
"realm"
|
|
10092
|
+
];
|
|
10093
|
+
const transportFields = {
|
|
10094
|
+
tcp: [],
|
|
10095
|
+
quic: [],
|
|
10096
|
+
http: [
|
|
10097
|
+
"host",
|
|
10098
|
+
"path",
|
|
10099
|
+
"method"
|
|
10100
|
+
],
|
|
10101
|
+
ws: [
|
|
10102
|
+
"path",
|
|
10103
|
+
"max_early_data",
|
|
10104
|
+
"early_data_header_name"
|
|
10105
|
+
],
|
|
10106
|
+
grpc: ["service_name"],
|
|
10107
|
+
httpupgrade: ["host", "path"]
|
|
10108
|
+
};
|
|
10109
|
+
const localTransportFields = {
|
|
10110
|
+
tcp: [],
|
|
10111
|
+
quic: [],
|
|
10112
|
+
http: [
|
|
10113
|
+
"headers",
|
|
10114
|
+
"idle_timeout",
|
|
10115
|
+
"ping_timeout"
|
|
10116
|
+
],
|
|
10117
|
+
ws: ["headers"],
|
|
10118
|
+
grpc: ["idle_timeout", "ping_timeout"],
|
|
10119
|
+
httpupgrade: ["headers"]
|
|
10120
|
+
};
|
|
10121
|
+
const grpcClientFields = ["permit_without_stream"];
|
|
10122
|
+
const tlsRequired = /* @__PURE__ */ new Set([
|
|
10123
|
+
"anytls",
|
|
10124
|
+
"hysteria",
|
|
10125
|
+
"hysteria2",
|
|
10126
|
+
"naive",
|
|
10127
|
+
"tuic"
|
|
10128
|
+
]);
|
|
10129
|
+
const versioned = [
|
|
10130
|
+
"socks",
|
|
10131
|
+
"shadowtls",
|
|
10132
|
+
"snell"
|
|
10133
|
+
];
|
|
10134
|
+
const transportProtocols = [
|
|
10135
|
+
"vmess",
|
|
10136
|
+
"vless",
|
|
10137
|
+
"trojan"
|
|
10138
|
+
];
|
|
10139
|
+
const namedClients = [
|
|
10140
|
+
"socks",
|
|
10141
|
+
"http",
|
|
10142
|
+
"naive"
|
|
10143
|
+
];
|
|
10144
|
+
const optionalUsers = [
|
|
10145
|
+
"http",
|
|
10146
|
+
"socks",
|
|
10147
|
+
"snell",
|
|
10148
|
+
"shadowsocks",
|
|
10149
|
+
"shadowtls"
|
|
10150
|
+
];
|
|
10151
|
+
const specialFields = {
|
|
10152
|
+
hysteria2: ["realm"],
|
|
10153
|
+
shadowsocks: [
|
|
10154
|
+
"password",
|
|
10155
|
+
"serverKey",
|
|
10156
|
+
"managed"
|
|
10157
|
+
],
|
|
10158
|
+
shadowtls: [
|
|
10159
|
+
"password",
|
|
10160
|
+
"serverName",
|
|
10161
|
+
"handshake"
|
|
10162
|
+
],
|
|
10163
|
+
snell: [
|
|
10164
|
+
"psk",
|
|
10165
|
+
"obfs_mode",
|
|
10166
|
+
"mode"
|
|
10167
|
+
]
|
|
10168
|
+
};
|
|
10169
|
+
function native(factory, options) {
|
|
10170
|
+
return factory(options);
|
|
10171
|
+
}
|
|
10172
|
+
function connection(value, path) {
|
|
10173
|
+
const { host, port } = fieldsFrom(value, path, ["host", "port"], ["host", "port"]);
|
|
10174
|
+
if (typeof host !== "string" || host === "") fail(`${path}.host`, "expected a host");
|
|
10175
|
+
if (typeof port !== "number" || !Number.isInteger(port) || port < 1 || port > 65535) fail(`${path}.port`, "expected a port from 1 to 65535");
|
|
10176
|
+
return {
|
|
10177
|
+
server: host,
|
|
10178
|
+
server_port: port
|
|
10179
|
+
};
|
|
10180
|
+
}
|
|
10181
|
+
function profileConnection(kind, value, path) {
|
|
10182
|
+
if (kind !== "hysteria" && kind !== "hysteria2") return connection(value, path);
|
|
10183
|
+
const fields = record(value, path);
|
|
10184
|
+
if (!Object.hasOwn(fields, "ports")) return connection(fields, path);
|
|
10185
|
+
const { host, ports } = fieldsFrom(fields, path, ["host", "ports"], ["host", "ports"]);
|
|
10186
|
+
return {
|
|
10187
|
+
server: requiredString(host, `${path}.host`),
|
|
10188
|
+
server_ports: ports
|
|
10189
|
+
};
|
|
10190
|
+
}
|
|
10191
|
+
function usersFor(input, path) {
|
|
10192
|
+
if (!Array.isArray(input) || input.length === 0) fail(path, "expected a non-empty array of Users");
|
|
10193
|
+
const names = /* @__PURE__ */ new Set();
|
|
10194
|
+
return Array.from(input, (value, index) => {
|
|
10195
|
+
const entry = `${path}[${index}]`;
|
|
10196
|
+
if (!isUser(value) || !value.name) fail(entry, "expected a User with a non-empty name");
|
|
10197
|
+
if (names.has(value.name)) fail(entry, "duplicate user name");
|
|
10198
|
+
names.add(value.name);
|
|
10199
|
+
return value;
|
|
10200
|
+
});
|
|
10201
|
+
}
|
|
10202
|
+
function realityKeys(input, path) {
|
|
10203
|
+
const keys = fieldsFrom(input, path, ["privateKey", "publicKey"], ["privateKey", "publicKey"]);
|
|
10204
|
+
const { privateKey, publicKey } = keys;
|
|
10205
|
+
for (const [key, value] of Object.entries(keys)) if (typeof value !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(value) || Buffer.from(value, "base64url").toString("base64url") !== value) fail(`${path}.${key}`, "expected an unpadded 32-byte Base64url key");
|
|
10206
|
+
const der = Buffer.concat([Buffer.from("302e020100300506032b656e04220420", "hex"), Buffer.from(privateKey, "base64url")]);
|
|
10207
|
+
if (createPublicKey(createPrivateKey({
|
|
10208
|
+
key: der,
|
|
10209
|
+
format: "der",
|
|
10210
|
+
type: "pkcs8"
|
|
10211
|
+
})).export({ format: "jwk" }).x !== publicKey) fail(`${path}.publicKey`, "does not match private key");
|
|
10212
|
+
return keys;
|
|
10213
|
+
}
|
|
10214
|
+
function tlsFor(kind, input, needsTls, path) {
|
|
10215
|
+
const { mode } = record(input ?? { mode: "none" }, path);
|
|
10216
|
+
if (mode === "tls") {
|
|
10217
|
+
if (kind === "shadowsocks" || kind === "snell" || kind === "socks" || kind === "shadowtls") fail(path, "TLS is unavailable for this profile");
|
|
10218
|
+
const fields = fieldsFrom(input, path, [
|
|
10219
|
+
"mode",
|
|
10220
|
+
"serverName",
|
|
10221
|
+
"alpn"
|
|
10222
|
+
]);
|
|
10223
|
+
checkTlsAlpn(kind, fields, path);
|
|
10224
|
+
if (fields.serverName !== void 0 && (typeof fields.serverName !== "string" || !fields.serverName)) fail(`${path}.serverName`, "expected a server name");
|
|
10225
|
+
return {
|
|
10226
|
+
mode,
|
|
10227
|
+
fields
|
|
10228
|
+
};
|
|
10229
|
+
}
|
|
10230
|
+
if (needsTls) fail(path, "requires regular TLS");
|
|
10231
|
+
if (mode === "none") {
|
|
10232
|
+
if (input !== void 0) fieldsFrom(input, path, ["mode"]);
|
|
10233
|
+
return { mode };
|
|
10234
|
+
}
|
|
10235
|
+
if (mode !== "reality" || kind !== "vless") fail(`${path}.mode`, "unsupported TLS mode");
|
|
10236
|
+
const fields = fieldsFrom(input, path, [
|
|
10237
|
+
"mode",
|
|
10238
|
+
"serverName",
|
|
10239
|
+
"handshake",
|
|
10240
|
+
"keyPair",
|
|
10241
|
+
"shortId",
|
|
10242
|
+
"shortIds"
|
|
10243
|
+
], [
|
|
10244
|
+
"serverName",
|
|
10245
|
+
"handshake",
|
|
10246
|
+
"keyPair"
|
|
10247
|
+
]);
|
|
10248
|
+
if (typeof fields.serverName !== "string" || !fields.serverName) fail(`${path}.serverName`, "expected a server name");
|
|
10249
|
+
if (fields.shortId === void 0 === (fields.shortIds === void 0)) fail(path, "specify either shortId or shortIds");
|
|
10250
|
+
const ids = fields.shortIds ?? [fields.shortId];
|
|
10251
|
+
if (!Array.isArray(ids) || !ids.length || Array.from(ids).some((id) => typeof id !== "string" || !/^(?:[\da-f]{2}){0,8}$/i.test(id)) || new Set(ids.map((id) => id.toLowerCase())).size !== ids.length) fail(`${path}.shortIds`, "expected unique hexadecimal short IDs");
|
|
10252
|
+
const { host, port, ...dial } = record(fields.handshake, `${path}.handshake`);
|
|
10253
|
+
if (Object.hasOwn(dial, "server") || Object.hasOwn(dial, "server_port")) fail(`${path}.handshake`, "use host and port for the handshake address");
|
|
10254
|
+
return {
|
|
10255
|
+
mode,
|
|
10256
|
+
serverName: fields.serverName,
|
|
10257
|
+
keys: realityKeys(fields.keyPair, `${path}.keyPair`),
|
|
10258
|
+
ids,
|
|
10259
|
+
handshake: {
|
|
10260
|
+
...dial,
|
|
10261
|
+
...connection({
|
|
10262
|
+
host,
|
|
10263
|
+
port
|
|
10264
|
+
}, `${path}.handshake`)
|
|
10265
|
+
}
|
|
10266
|
+
};
|
|
10267
|
+
}
|
|
10268
|
+
function checkedOptions(value, owned, path) {
|
|
10269
|
+
const fields = record(value ?? {}, path);
|
|
10270
|
+
for (const key of Object.keys(fields)) if (owned.includes(key)) fail(keyPath(path, key), "field is controlled by the profile");
|
|
10271
|
+
return fields;
|
|
10272
|
+
}
|
|
10273
|
+
function protocolOptions(kind, config, hasUsers, sharedKey, path) {
|
|
10274
|
+
const serverFields = {};
|
|
10275
|
+
const clientFields = {};
|
|
10276
|
+
if (sharedKey && config[sharedKey] !== void 0) {
|
|
10277
|
+
serverFields[sharedKey] = config[sharedKey];
|
|
10278
|
+
clientFields[sharedKey] = config[sharedKey];
|
|
10279
|
+
}
|
|
10280
|
+
switch (kind) {
|
|
10281
|
+
case "socks": {
|
|
10282
|
+
const version = config.version ?? "5";
|
|
10283
|
+
if (version !== "4" && version !== "4a" && version !== "5") fail(`${path}.version`, "unsupported SOCKS version");
|
|
10284
|
+
clientFields.version = version;
|
|
10285
|
+
break;
|
|
10286
|
+
}
|
|
10287
|
+
case "snell": {
|
|
10288
|
+
const version = config.version ?? 6;
|
|
10289
|
+
if (version !== 5 && version !== 6) fail(`${path}.version`, "unsupported Snell version");
|
|
10290
|
+
const forbidden = version === 5 ? "mode" : "obfs_mode";
|
|
10291
|
+
if (config[forbidden] !== void 0) fail(`${path}.${forbidden}`, "unavailable for this Snell version");
|
|
10292
|
+
serverFields.version = version;
|
|
10293
|
+
clientFields.version = version === 5 ? 4 : 6;
|
|
10294
|
+
const flag = version === 5 ? "obfs_mode" : "mode";
|
|
10295
|
+
if (config[flag] !== void 0) serverFields[flag] = clientFields[flag] = config[flag];
|
|
10296
|
+
serverFields.psk = clientFields.psk = requiredString(config.psk, `${path}.psk`);
|
|
10297
|
+
break;
|
|
10298
|
+
}
|
|
10299
|
+
case "shadowtls": {
|
|
10300
|
+
const version = config.version ?? 3;
|
|
10301
|
+
if (version !== 1 && version !== 2 && version !== 3) fail(`${path}.version`, "unsupported ShadowTLS version");
|
|
10302
|
+
if (version === 3 ? !hasUsers : hasUsers) fail(`${path}.users`, "users are required for ShadowTLS v3 and unavailable in other versions");
|
|
10303
|
+
if (version === 2) serverFields.password = clientFields.password = requiredString(config.password, `${path}.password`);
|
|
10304
|
+
else if (config.password !== void 0) fail(`${path}.password`, "shared password is available only in ShadowTLS v2");
|
|
10305
|
+
const handshake = record(config.handshake, `${path}.handshake`);
|
|
10306
|
+
connection({
|
|
10307
|
+
host: handshake.server,
|
|
10308
|
+
port: handshake.server_port
|
|
10309
|
+
}, `${path}.handshake`);
|
|
10310
|
+
serverFields.handshake = handshake;
|
|
10311
|
+
requiredString(config.serverName, `${path}.serverName`);
|
|
10312
|
+
serverFields.version = clientFields.version = version;
|
|
10313
|
+
break;
|
|
10314
|
+
}
|
|
10315
|
+
case "shadowsocks": {
|
|
10316
|
+
if (config.managed !== void 0 && typeof config.managed !== "boolean") fail(`${path}.managed`, "expected a boolean");
|
|
10317
|
+
if (config.managed && hasUsers) fail(`${path}.users`, "managed Shadowsocks uses SSM accounts");
|
|
10318
|
+
const method = requiredString(config.method, `${path}.method`);
|
|
10319
|
+
if (config.managed || hasUsers) {
|
|
10320
|
+
if (method === "none" || method === "2022-blake3-chacha20-poly1305") fail(`${path}.method`, "unavailable with multiple Shadowsocks users");
|
|
10321
|
+
if (config.password !== void 0) fail(`${path}.password`, "use User credentials for multiple users");
|
|
10322
|
+
if (method.startsWith("2022-")) serverFields.password = requiredString(config.serverKey, `${path}.serverKey`);
|
|
10323
|
+
else if (config.serverKey !== void 0) fail(`${path}.serverKey`, "requires a Shadowsocks 2022 AES method");
|
|
10324
|
+
if (config.managed) serverFields.managed = true;
|
|
10325
|
+
} else {
|
|
10326
|
+
if (config.serverKey !== void 0) fail(`${path}.serverKey`, "requires multiple users");
|
|
10327
|
+
if (method !== "none") serverFields.password = clientFields.password = requiredString(config.password, `${path}.password`);
|
|
10328
|
+
else if (config.password !== void 0) fail(`${path}.password`, "unavailable with method none");
|
|
10329
|
+
}
|
|
10330
|
+
break;
|
|
10331
|
+
}
|
|
10332
|
+
}
|
|
10333
|
+
return {
|
|
10334
|
+
serverFields,
|
|
10335
|
+
clientFields
|
|
10336
|
+
};
|
|
10337
|
+
}
|
|
10338
|
+
function makeProfile(kind, input) {
|
|
10339
|
+
const path = `proxyProfile.${kind}`;
|
|
10340
|
+
const sharedKey = Object.hasOwn(sharedOption, kind) ? sharedOption[kind] : void 0;
|
|
10341
|
+
const config = snapshot(fieldsFrom(input, path, [
|
|
10342
|
+
"connect",
|
|
10343
|
+
"users",
|
|
10344
|
+
...kind === "shadowtls" || kind === "shadowsocks" || kind === "snell" || kind === "socks" ? [] : ["tls"],
|
|
10345
|
+
...transportProtocols.includes(kind) ? ["transport"] : [],
|
|
10346
|
+
...versioned.includes(kind) ? ["version"] : [],
|
|
10347
|
+
...sharedKey ? [sharedKey] : [],
|
|
10348
|
+
...specialFields[kind] ?? []
|
|
10349
|
+
]), path);
|
|
10350
|
+
const realm = config.realm === void 0 ? void 0 : fieldsFrom(config.realm, `${path}.realm`, [
|
|
10351
|
+
"server_url",
|
|
10352
|
+
"realm_id",
|
|
10353
|
+
"stun_servers",
|
|
10354
|
+
"token"
|
|
10355
|
+
], [
|
|
10356
|
+
"server_url",
|
|
10357
|
+
"realm_id",
|
|
10358
|
+
"stun_servers"
|
|
10359
|
+
]);
|
|
10360
|
+
if (realm && config.connect !== void 0) fail(`${path}.connect`, "conflicts with Realm");
|
|
10361
|
+
const connect = realm ? {} : profileConnection(kind, config.connect, `${path}.connect`);
|
|
10362
|
+
const users = config.users === void 0 ? [] : usersFor(config.users, `${path}.users`);
|
|
10363
|
+
if (!optionalUsers.includes(kind) && !users.length) fail(`${path}.users`, "expected at least one User");
|
|
10364
|
+
let transport = {};
|
|
10365
|
+
let transportType = "tcp";
|
|
10366
|
+
if (config.transport !== void 0) {
|
|
10367
|
+
const { type } = record(config.transport, `${path}.transport`);
|
|
10368
|
+
if (typeof type !== "string" || !Object.hasOwn(transportFields, type)) fail(`${path}.transport.type`, "unsupported transport");
|
|
10369
|
+
transportType = type;
|
|
10370
|
+
transport = fieldsFrom(config.transport, `${path}.transport`, ["type", ...transportFields[transportType]]);
|
|
10371
|
+
}
|
|
10372
|
+
const tls = tlsFor(kind, config.tls, tlsRequired.has(kind) || transportType === "quic", `${path}.tls`);
|
|
10373
|
+
if (realm && (tls.mode !== "tls" || !tls.fields.serverName)) fail(`${path}.tls.serverName`, "required for Realm connections");
|
|
10374
|
+
const { serverFields, clientFields } = protocolOptions(kind, config, users.length > 0, sharedKey, path);
|
|
10375
|
+
function credentials(user, location) {
|
|
10376
|
+
if (kind === "socks" && clientFields.version !== "5") {
|
|
10377
|
+
if (requireUserCredentials(user, "socks", location).password !== "") fail(location, "SOCKS4 and SOCKS4a use username-only authentication");
|
|
10378
|
+
return {};
|
|
10379
|
+
}
|
|
10380
|
+
return { ...requireUserCredentials(user, kind, location) };
|
|
10381
|
+
}
|
|
10382
|
+
users.forEach((user, index) => credentials(user, `${path}.users[${index}]`));
|
|
10383
|
+
if (users.length) serverFields.users = users;
|
|
10384
|
+
function sideOptions(input, client) {
|
|
10385
|
+
const location = `${path}.${client ? "client" : "server"}`;
|
|
10386
|
+
const local = fieldsFrom(input, location, [
|
|
10387
|
+
"tag",
|
|
10388
|
+
"options",
|
|
10389
|
+
"tls",
|
|
10390
|
+
"transport",
|
|
10391
|
+
"realm",
|
|
10392
|
+
...client ? ["dial", "shortId"] : ["listen"]
|
|
10393
|
+
], ["tag"]);
|
|
10394
|
+
const extra = checkedOptions(local.options, [...client ? ownedClient : ownedServer, ...sharedKey ? [sharedKey] : []], `${location}.options`);
|
|
10395
|
+
let transportOptions = {};
|
|
10396
|
+
if (local.transport !== void 0) {
|
|
10397
|
+
if (transportType === "tcp" || transportType === "quic") fail(`${location}.transport`, "this transport has no side-specific fields");
|
|
10398
|
+
transportOptions = fieldsFrom(local.transport, `${location}.transport`, [...localTransportFields[transportType], ...client && transportType === "grpc" ? grpcClientFields : []]);
|
|
10399
|
+
}
|
|
10400
|
+
let realmOptions = {};
|
|
10401
|
+
if (local.realm !== void 0) {
|
|
10402
|
+
if (!realm) fail(`${location}.realm`, "requires a Realm profile");
|
|
10403
|
+
realmOptions = fieldsFrom(local.realm, `${location}.realm`, [
|
|
10404
|
+
"token",
|
|
10405
|
+
"ip_version",
|
|
10406
|
+
"port_mapping",
|
|
10407
|
+
"http_client",
|
|
10408
|
+
...client ? [] : ["stun_domain_resolver"]
|
|
10409
|
+
]);
|
|
10410
|
+
}
|
|
10411
|
+
return {
|
|
10412
|
+
location,
|
|
10413
|
+
local,
|
|
10414
|
+
fields: {
|
|
10415
|
+
...extra,
|
|
10416
|
+
tag: local.tag,
|
|
10417
|
+
...transportType === "tcp" ? {} : { transport: {
|
|
10418
|
+
...transport,
|
|
10419
|
+
...transportOptions
|
|
10420
|
+
} },
|
|
10421
|
+
...realm ? { realm: {
|
|
10422
|
+
...realm,
|
|
10423
|
+
...realmOptions
|
|
10424
|
+
} } : {}
|
|
10425
|
+
}
|
|
10426
|
+
};
|
|
10427
|
+
}
|
|
10428
|
+
function applyTls(builder, local, location, client) {
|
|
10429
|
+
if (local.shortId !== void 0 && tls.mode !== "reality") fail(`${location}.shortId`, "requires REALITY");
|
|
10430
|
+
if (tls.mode === "reality") {
|
|
10431
|
+
if (local.tls !== void 0) fail(`${location}.tls`, "REALITY settings belong to the profile");
|
|
10432
|
+
if (!client) return builder.tls({
|
|
10433
|
+
side: "server",
|
|
10434
|
+
server_name: tls.serverName,
|
|
10435
|
+
reality: {
|
|
10436
|
+
private_key: tls.keys.privateKey,
|
|
10437
|
+
short_id: tls.ids,
|
|
10438
|
+
handshake: tls.handshake
|
|
10439
|
+
}
|
|
10440
|
+
});
|
|
10441
|
+
const selected = local.shortId ?? (tls.ids.length === 1 ? tls.ids[0] : void 0);
|
|
10442
|
+
if (typeof selected !== "string" || !tls.ids.includes(selected)) fail(`${location}.shortId`, "select a short ID from the profile");
|
|
10443
|
+
return builder.tls({
|
|
10444
|
+
side: "client",
|
|
10445
|
+
server_name: tls.serverName,
|
|
10446
|
+
reality: {
|
|
10447
|
+
public_key: tls.keys.publicKey,
|
|
10448
|
+
short_id: selected
|
|
10449
|
+
}
|
|
10450
|
+
});
|
|
10451
|
+
}
|
|
10452
|
+
if (tls.mode === "tls" || kind === "shadowtls" && client) {
|
|
10453
|
+
const settings = checkedOptions(local.tls, [
|
|
10454
|
+
"enabled",
|
|
10455
|
+
"reality",
|
|
10456
|
+
"side",
|
|
10457
|
+
...client ? ["server_name"] : [],
|
|
10458
|
+
...tls.mode === "tls" && tls.fields.alpn !== void 0 ? ["alpn"] : []
|
|
10459
|
+
], `${location}.tls`);
|
|
10460
|
+
checkTlsAlpn(kind, settings, `${location}.tls`);
|
|
10461
|
+
if (!client && !settings.certificate_provider && (!(settings.certificate || settings.certificate_path) || !(settings.key || settings.key_path))) fail(`${location}.tls`, "expected a certificate and key or certificate provider");
|
|
10462
|
+
return builder.tls({
|
|
10463
|
+
...settings,
|
|
10464
|
+
enabled: true,
|
|
10465
|
+
...tls.mode === "tls" && tls.fields.alpn !== void 0 ? { alpn: tls.fields.alpn } : {},
|
|
10466
|
+
...client ? { server_name: tls.mode === "tls" ? tls.fields.serverName ?? connect.server : config.serverName } : {}
|
|
10467
|
+
});
|
|
10468
|
+
}
|
|
10469
|
+
if (local.tls !== void 0) fail(`${location}.tls`, "TLS must be enabled in the profile");
|
|
10470
|
+
return builder;
|
|
10471
|
+
}
|
|
10472
|
+
return Object.freeze({
|
|
10473
|
+
server(input) {
|
|
10474
|
+
const { local, fields, location } = sideOptions(input, false);
|
|
10475
|
+
let builder = native(inbound[kind], {
|
|
10476
|
+
...fields,
|
|
10477
|
+
...serverFields
|
|
10478
|
+
});
|
|
10479
|
+
if (local.listen !== void 0) {
|
|
10480
|
+
const { address, ...listen } = record(local.listen, `${location}.listen`);
|
|
10481
|
+
builder = builder.listen(address, listen);
|
|
10482
|
+
}
|
|
10483
|
+
return applyTls(builder, local, location, false);
|
|
10484
|
+
},
|
|
10485
|
+
client(userOrOptions, options) {
|
|
10486
|
+
const authenticated = users.length > 0 || config.managed === true;
|
|
10487
|
+
if (authenticated && (!isUser(userOrOptions) || !userOrOptions.name || config.managed !== true && !users.includes(userOrOptions))) fail(`${path}.client.user`, "expected a User for this profile");
|
|
10488
|
+
if (!authenticated && options !== void 0) fail(`${path}.client.user`, "profile has no user selection");
|
|
10489
|
+
const { local, fields, location } = sideOptions(authenticated ? options : userOrOptions, true);
|
|
10490
|
+
const selected = isUser(userOrOptions) ? userOrOptions : void 0;
|
|
10491
|
+
const auth = selected ? {
|
|
10492
|
+
...credentials(selected, `${location}.user`),
|
|
10493
|
+
...namedClients.includes(kind) ? { username: selected.name } : {}
|
|
10494
|
+
} : {};
|
|
10495
|
+
if (kind === "shadowsocks" && typeof auth.password === "string" && typeof serverFields.password === "string") auth.password = `${serverFields.password}:${auth.password}`;
|
|
10496
|
+
let builder = native(outbound[kind], {
|
|
10497
|
+
...fields,
|
|
10498
|
+
...clientFields,
|
|
10499
|
+
...connect,
|
|
10500
|
+
...auth
|
|
10501
|
+
});
|
|
10502
|
+
if (local.dial !== void 0) builder = builder.dial(local.dial);
|
|
10503
|
+
return applyTls(builder, local, location, true);
|
|
10504
|
+
}
|
|
10505
|
+
});
|
|
10506
|
+
}
|
|
10507
|
+
function requiredString(input, path) {
|
|
10508
|
+
if (typeof input !== "string" || !input) fail(path, "expected a non-empty string");
|
|
10509
|
+
return input;
|
|
10510
|
+
}
|
|
10511
|
+
function http(input) {
|
|
10512
|
+
return makeProfile("http", input);
|
|
10513
|
+
}
|
|
10514
|
+
function socks(input) {
|
|
10515
|
+
return makeProfile("socks", input);
|
|
10516
|
+
}
|
|
10517
|
+
function snell(input) {
|
|
10518
|
+
return makeProfile("snell", input);
|
|
10519
|
+
}
|
|
10520
|
+
function shadowtls(input) {
|
|
10521
|
+
return makeProfile("shadowtls", input);
|
|
10522
|
+
}
|
|
10523
|
+
function shadowsocks(input) {
|
|
10524
|
+
return makeProfile("shadowsocks", input);
|
|
10525
|
+
}
|
|
10526
|
+
function transportProfile(kind) {
|
|
10527
|
+
function create(input) {
|
|
10528
|
+
return makeProfile(kind, input);
|
|
10529
|
+
}
|
|
10530
|
+
return create;
|
|
10531
|
+
}
|
|
10532
|
+
const proxyProfile = Object.freeze({
|
|
10533
|
+
anytls: (input) => makeProfile("anytls", input),
|
|
10534
|
+
http,
|
|
10535
|
+
hysteria: (input) => makeProfile("hysteria", input),
|
|
10536
|
+
hysteria2: (input) => makeProfile("hysteria2", input),
|
|
10537
|
+
naive: (input) => makeProfile("naive", input),
|
|
10538
|
+
shadowsocks,
|
|
10539
|
+
shadowtls,
|
|
10540
|
+
snell,
|
|
10541
|
+
socks,
|
|
10542
|
+
trojan: transportProfile("trojan"),
|
|
10543
|
+
tuic: (input) => makeProfile("tuic", input),
|
|
10544
|
+
vless: transportProfile("vless"),
|
|
10545
|
+
vmess: transportProfile("vmess")
|
|
10546
|
+
});
|
|
10547
|
+
export { Rule, certificateProvider, compile, dns, dnsServer, endpoint, httpClient, inbound, networkNamespace, outbound, proxyProfile, route, ruleSet, service, user, validateConfig };
|