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/README.md +3 -1
- package/dist/index.d.mts +453 -59
- package/dist/index.mjs +833 -47
- package/docs/generators.md +31 -0
- package/docs/internal/README.md +2 -2
- package/docs/proxy-profiles.md +63 -0
- package/docs/rules.md +5 -3
- package/docs/settings.md +2 -0
- package/index.ts +3 -1
- package/package.json +7 -3
- package/schema/README.md +1 -1
- package/src/compile.ts +2 -2
- package/src/factories.ts +73 -68
- package/src/generate.ts +116 -0
- package/src/internal/certificate.ts +72 -0
- package/src/internal/crypto.ts +34 -0
- package/src/internal/ech.ts +32 -0
- package/src/internal/reality.ts +21 -0
- package/src/ip.ts +63 -0
- package/src/proxy-profile.ts +847 -0
- package/src/rules.ts +6 -14
- package/src/settings.ts +11 -1
- package/src/user.ts +82 -5
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { isIP } from "node:net";
|
|
2
1
|
import Ajv2020 from "ajv/dist/2020.js";
|
|
2
|
+
import { base64, base64url, hex } from "@peculiar/utils/encoding";
|
|
3
|
+
import { concatToUint8Array, toArrayBuffer } from "@peculiar/utils/bytes";
|
|
4
|
+
import "reflect-metadata";
|
|
5
|
+
import { pem } from "@peculiar/utils/pem";
|
|
6
|
+
import { BasicConstraintsExtension, ExtendedKeyUsage, ExtendedKeyUsageExtension, KeyUsageFlags, KeyUsagesExtension, SubjectAlternativeNameExtension, X509CertificateGenerator } from "@peculiar/x509";
|
|
3
7
|
// schema/sing-box.json
|
|
4
8
|
var sing_box_default = {
|
|
5
9
|
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
@@ -9210,6 +9214,42 @@ function ruleActionSchemas(scope) {
|
|
|
9210
9214
|
return [action, projectObject(properties, [...required])];
|
|
9211
9215
|
}));
|
|
9212
9216
|
}
|
|
9217
|
+
// src/ip.ts
|
|
9218
|
+
const ipv4Octet = /^(?:0|[1-9][0-9]{0,2})$/;
|
|
9219
|
+
const ipv6Group = /^[0-9a-f]{1,4}$/i;
|
|
9220
|
+
const ipv6Zone = /^[0-9a-z.:-]+$/i;
|
|
9221
|
+
const cidrPrefix = /^[0-9]+$/;
|
|
9222
|
+
function isIPv4(input) {
|
|
9223
|
+
const octets = input.split(".", 5);
|
|
9224
|
+
return octets.length === 4 && octets.every((octet) => ipv4Octet.test(octet) && Number(octet) <= 255);
|
|
9225
|
+
}
|
|
9226
|
+
function isIPv6(input) {
|
|
9227
|
+
const [address = "", zone, extraZone] = input.split("%", 3);
|
|
9228
|
+
if (!address.includes(":") || extraZone !== void 0 || zone !== void 0 && !ipv6Zone.test(zone)) return false;
|
|
9229
|
+
const [head = "", tail, extra] = address.split("::", 3);
|
|
9230
|
+
if (extra !== void 0) return false;
|
|
9231
|
+
const groups = head ? head.split(":", 9) : [];
|
|
9232
|
+
if (tail) groups.push(...tail.split(":", 9));
|
|
9233
|
+
let groupCount = groups.length;
|
|
9234
|
+
const last = groups.at(-1);
|
|
9235
|
+
if (last?.includes(".")) {
|
|
9236
|
+
if (tail === "" || !isIPv4(last)) return false;
|
|
9237
|
+
groups.pop();
|
|
9238
|
+
groupCount++;
|
|
9239
|
+
}
|
|
9240
|
+
return (tail === void 0 ? groupCount === 8 : groupCount < 8) && groups.every((group) => ipv6Group.test(group));
|
|
9241
|
+
}
|
|
9242
|
+
function isIP(input) {
|
|
9243
|
+
if (isIPv4(input)) return 4;
|
|
9244
|
+
if (isIPv6(input)) return 6;
|
|
9245
|
+
return 0;
|
|
9246
|
+
}
|
|
9247
|
+
function isCIDR(input) {
|
|
9248
|
+
const [address = "", prefix, extra] = input.split("/", 3);
|
|
9249
|
+
if (prefix === void 0 || extra !== void 0 || address.includes("%") || !cidrPrefix.test(prefix)) return false;
|
|
9250
|
+
const version = isIP(address);
|
|
9251
|
+
return version !== 0 && Number(prefix) <= (version === 4 ? 32 : 128);
|
|
9252
|
+
}
|
|
9213
9253
|
// src/resource.ts
|
|
9214
9254
|
const preferredRouteEndpoints = [
|
|
9215
9255
|
"tailscale",
|
|
@@ -9352,8 +9392,12 @@ function listenFields(address, input, path, schema) {
|
|
|
9352
9392
|
if (port !== void 0) options.listen_port = port;
|
|
9353
9393
|
return options;
|
|
9354
9394
|
}
|
|
9355
|
-
function
|
|
9395
|
+
function checkTlsAlpn(kind, options, path) {
|
|
9396
|
+
if (kind === "naive" && options.alpn !== void 0) fail(`${path}.alpn`, "ALPN cannot be configured for Naive");
|
|
9397
|
+
}
|
|
9398
|
+
function tlsFields(input, path, schema, mode, kind) {
|
|
9356
9399
|
const profile = record(input, path);
|
|
9400
|
+
checkTlsAlpn(kind, profile, path);
|
|
9357
9401
|
const enabled = Object.hasOwn(schemaShape(schema).properties ?? {}, "enabled");
|
|
9358
9402
|
if (!Object.hasOwn(profile, "side")) return { tls: enabled && profile.enabled === void 0 ? {
|
|
9359
9403
|
...profile,
|
|
@@ -9484,16 +9528,12 @@ const actions = {
|
|
|
9484
9528
|
route: actionsFor("route"),
|
|
9485
9529
|
dns: actionsFor("dns")
|
|
9486
9530
|
};
|
|
9487
|
-
function validSubnet(input) {
|
|
9488
|
-
const [address = "", prefix, ...extra] = input.split("/");
|
|
9489
|
-
const version = isIP(address);
|
|
9490
|
-
return version !== 0 && !address.includes("%") && extra.length === 0 && (prefix === void 0 || /^\d+$/.test(prefix) && Number(prefix) <= (version === 4 ? 32 : 128));
|
|
9491
|
-
}
|
|
9492
9531
|
function checkAction(fields, path, scope) {
|
|
9493
9532
|
if (fields.method === "drop" && fields.no_drop === true) fail(`${path}.no_drop`, "no_drop with drop");
|
|
9494
9533
|
if (fields.client_subnet !== void 0) {
|
|
9495
9534
|
if (fields.remove_client_subnet === true) fail(`${path}.client_subnet`, "client_subnet with remove_client_subnet");
|
|
9496
|
-
|
|
9535
|
+
const subnet = fields.client_subnet;
|
|
9536
|
+
if (!(subnet.includes("/") ? isCIDR(subnet) : !subnet.includes("%") && isIP(subnet) !== 0)) fail(`${path}.client_subnet`, "invalid subnet");
|
|
9497
9537
|
}
|
|
9498
9538
|
if (scope === "dns" && fields.action === "route-options" && fields.rewrite_ttl === void 0 && fields.client_subnet === void 0 && fields.disable_cache !== true && fields.disable_optimistic_cache !== true && fields.remove_client_subnet !== true && durationNanoseconds(fields.timeout, `${path}.timeout`) <= 0) fail(path, "route-options requires an effective DNS option");
|
|
9499
9539
|
if (fields.race === true && fields.speculative === true) fail(`${path}.race`, "race with speculative");
|
|
@@ -9755,17 +9795,52 @@ function compile(input) {
|
|
|
9755
9795
|
return validateConfig(output);
|
|
9756
9796
|
}
|
|
9757
9797
|
// src/user.ts
|
|
9758
|
-
const
|
|
9798
|
+
const passwordFields = ["password"];
|
|
9799
|
+
const credentialFields = {
|
|
9800
|
+
anytls: passwordFields,
|
|
9801
|
+
http: passwordFields,
|
|
9802
|
+
hysteria: ["auth", "auth_str"],
|
|
9803
|
+
hysteria2: passwordFields,
|
|
9804
|
+
mixed: passwordFields,
|
|
9805
|
+
naive: passwordFields,
|
|
9806
|
+
shadowsocks: passwordFields,
|
|
9807
|
+
shadowtls: passwordFields,
|
|
9808
|
+
snell: ["userkey"],
|
|
9809
|
+
socks: passwordFields,
|
|
9810
|
+
trojan: passwordFields,
|
|
9811
|
+
tuic: ["password", "uuid"],
|
|
9812
|
+
vless: ["uuid", "flow"],
|
|
9813
|
+
vmess: ["uuid", "alter_id"]
|
|
9814
|
+
};
|
|
9815
|
+
const users = /* @__PURE__ */ new WeakMap();
|
|
9759
9816
|
function user(input) {
|
|
9760
|
-
const { name } = fieldsFrom(input, "user", ["name"], ["name"]);
|
|
9817
|
+
const { name, credentials } = fieldsFrom(input, "user", ["name", "credentials"], ["name"]);
|
|
9761
9818
|
if (typeof name !== "string") fail("user.name", "expected a string");
|
|
9819
|
+
const entries = fieldsFrom(credentials === void 0 ? {} : credentials, "user.credentials", Object.keys(credentialFields));
|
|
9820
|
+
for (const key of Object.keys(credentialFields)) {
|
|
9821
|
+
if (!Object.hasOwn(entries, key)) continue;
|
|
9822
|
+
const path = `user.credentials.${key}`;
|
|
9823
|
+
const allowed = credentialFields[key];
|
|
9824
|
+
const required = key === "hysteria" ? [] : allowed.filter((field) => field !== "flow" && field !== "alter_id");
|
|
9825
|
+
const entry = fieldsFrom(entries[key], path, allowed, required);
|
|
9826
|
+
if (key === "hysteria" && Object.keys(entry).length !== 1) fail(path, "expected either auth or auth_str");
|
|
9827
|
+
for (const [field, value] of Object.entries(entry)) if (field === "alter_id") {
|
|
9828
|
+
if (typeof value !== "number" || !Number.isSafeInteger(value) || value < 0) fail(`${path}.${field}`, "expected a non-negative integer");
|
|
9829
|
+
} else if (field === "auth" && Array.isArray(value)) {
|
|
9830
|
+
if (Array.from(value).some((byte) => !Number.isInteger(byte) || byte < 0 || byte > 255)) fail(`${path}.${field}`, "expected an array of bytes");
|
|
9831
|
+
} else if (typeof value !== "string") fail(`${path}.${field}`, "expected a string");
|
|
9832
|
+
entries[key] = entry;
|
|
9833
|
+
}
|
|
9762
9834
|
const value = preserveIdentity({ name });
|
|
9763
|
-
users.
|
|
9835
|
+
users.set(value, snapshot(entries, "user.credentials"));
|
|
9764
9836
|
return value;
|
|
9765
9837
|
}
|
|
9766
9838
|
function isUser(value) {
|
|
9767
9839
|
return value !== null && typeof value === "object" && users.has(value);
|
|
9768
9840
|
}
|
|
9841
|
+
function requireUserCredentials(value, kind, path) {
|
|
9842
|
+
return users.get(value)?.[kind] ?? fail(path, `missing ${kind} credentials`);
|
|
9843
|
+
}
|
|
9769
9844
|
// src/factories.ts
|
|
9770
9845
|
const targetRoles = {
|
|
9771
9846
|
inbounds: ["inbound", "listen-detour"],
|
|
@@ -9783,52 +9858,67 @@ const endpointRoles = {
|
|
|
9783
9858
|
openconnect: "openconnect-endpoint",
|
|
9784
9859
|
"openvpn-client": "openvpn-client-endpoint"
|
|
9785
9860
|
};
|
|
9786
|
-
const
|
|
9861
|
+
const namedAuth = {
|
|
9862
|
+
name: "name",
|
|
9863
|
+
allowRaw: true
|
|
9864
|
+
};
|
|
9865
|
+
const usernameAuth = {
|
|
9787
9866
|
name: "username",
|
|
9788
|
-
|
|
9789
|
-
required: ["password"]
|
|
9867
|
+
allowRaw: true
|
|
9790
9868
|
};
|
|
9791
9869
|
const authOptions = {
|
|
9792
|
-
|
|
9793
|
-
|
|
9794
|
-
|
|
9795
|
-
|
|
9796
|
-
|
|
9797
|
-
|
|
9798
|
-
|
|
9799
|
-
|
|
9800
|
-
|
|
9870
|
+
anytls: namedAuth,
|
|
9871
|
+
http: usernameAuth,
|
|
9872
|
+
hysteria: namedAuth,
|
|
9873
|
+
hysteria2: namedAuth,
|
|
9874
|
+
mixed: usernameAuth,
|
|
9875
|
+
naive: usernameAuth,
|
|
9876
|
+
shadowsocks: namedAuth,
|
|
9877
|
+
shadowtls: namedAuth,
|
|
9878
|
+
snell: namedAuth,
|
|
9879
|
+
socks: {
|
|
9880
|
+
name: "username",
|
|
9881
|
+
allowRaw: false
|
|
9801
9882
|
},
|
|
9883
|
+
trojan: namedAuth,
|
|
9884
|
+
tuic: namedAuth,
|
|
9802
9885
|
vless: {
|
|
9803
9886
|
name: "name",
|
|
9804
|
-
|
|
9805
|
-
|
|
9806
|
-
|
|
9887
|
+
allowRaw: false
|
|
9888
|
+
},
|
|
9889
|
+
vmess: namedAuth
|
|
9807
9890
|
};
|
|
9808
|
-
function prepareUsers(fields,
|
|
9891
|
+
function prepareUsers(fields, kind, path) {
|
|
9809
9892
|
if (fields.users === void 0) return [];
|
|
9810
9893
|
const users = fields.users;
|
|
9811
9894
|
if (!Array.isArray(users)) fail(`${path}.users`, "expected an array");
|
|
9895
|
+
const auth = authOptions[kind];
|
|
9812
9896
|
const members = [];
|
|
9813
9897
|
fields.users = Array.from(users, (value, index) => {
|
|
9814
9898
|
const entryPath = `${path}.users[${index}]`;
|
|
9815
|
-
|
|
9816
|
-
|
|
9817
|
-
|
|
9818
|
-
|
|
9819
|
-
|
|
9899
|
+
if (isUser(value)) {
|
|
9900
|
+
members.push({
|
|
9901
|
+
user: value,
|
|
9902
|
+
name: value.name
|
|
9903
|
+
});
|
|
9904
|
+
if (kind === "vmess") {
|
|
9905
|
+
const { alter_id, ...credentials } = requireUserCredentials(value, kind, entryPath);
|
|
9906
|
+
return {
|
|
9907
|
+
name: value.name,
|
|
9908
|
+
...credentials,
|
|
9909
|
+
alterId: alter_id
|
|
9910
|
+
};
|
|
9911
|
+
}
|
|
9912
|
+
return {
|
|
9913
|
+
[auth.name]: value.name,
|
|
9914
|
+
...requireUserCredentials(value, kind, entryPath)
|
|
9915
|
+
};
|
|
9820
9916
|
}
|
|
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
|
-
};
|
|
9917
|
+
if (!auth.allowRaw) fail(entryPath, "expected a User from user()");
|
|
9918
|
+
const raw = record(value, entryPath);
|
|
9919
|
+
if (Object.hasOwn(raw, "user")) fail(`${entryPath}.user`, "pass the User directly and configure credentials in user()");
|
|
9920
|
+
members.push({ name: raw[auth.name] });
|
|
9921
|
+
return raw;
|
|
9832
9922
|
});
|
|
9833
9923
|
return members;
|
|
9834
9924
|
}
|
|
@@ -9912,7 +10002,7 @@ function getRoles(target, kind) {
|
|
|
9912
10002
|
function makeFactory(target, kind, branches) {
|
|
9913
10003
|
const path = `${target}.${kind}`;
|
|
9914
10004
|
const validators = /* @__PURE__ */ new Map();
|
|
9915
|
-
const auth = target === "inbounds" && Object.hasOwn(authOptions, kind) ?
|
|
10005
|
+
const auth = target === "inbounds" && Object.hasOwn(authOptions, kind) ? kind : void 0;
|
|
9916
10006
|
return (input = {}) => {
|
|
9917
10007
|
const raw = record(input, path);
|
|
9918
10008
|
const branch = branches.length === 1 ? branches[0] : branches.find((item) => matchesDiscriminators(item.options, raw));
|
|
@@ -9922,7 +10012,8 @@ function makeFactory(target, kind, branches) {
|
|
|
9922
10012
|
}
|
|
9923
10013
|
const options = fieldsFrom(raw, path, Object.keys(branch.options.properties), branch.options.required);
|
|
9924
10014
|
validateTag(options.tag, `${path}.tag`);
|
|
9925
|
-
const
|
|
10015
|
+
const users = auth ? prepareUsers(options, auth, path) : [];
|
|
10016
|
+
const members = kind === "shadowtls" && options.version !== 3 ? [] : users;
|
|
9926
10017
|
const roles = getRoles(target, kind);
|
|
9927
10018
|
prepareOptions(target, kind, options, path, roles);
|
|
9928
10019
|
const base = snapshot(target === "http_clients" ? options : {
|
|
@@ -9959,7 +10050,7 @@ function makeFactory(target, kind, branches) {
|
|
|
9959
10050
|
let mode;
|
|
9960
10051
|
if (name === "InboundTLSOptions") mode = kind === "vless" ? "server-reality" : "server";
|
|
9961
10052
|
if (name === "OutboundTLSOptions") mode = kind === "vless" ? "client-reality" : "client";
|
|
9962
|
-
methods.tls = (input) => apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode));
|
|
10053
|
+
methods.tls = (input) => apply("tls", () => tlsFields(input, `${path}.tls`, tls, mode, kind));
|
|
9963
10054
|
}
|
|
9964
10055
|
if (auth) methods.authUser = (user) => {
|
|
9965
10056
|
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 +10085,699 @@ const certificateProvider = Object.freeze(factoriesFor("certificate_providers"))
|
|
|
9994
10085
|
const networkNamespace = Object.freeze(factoriesFor("network_namespaces"));
|
|
9995
10086
|
const httpClient = factoriesFor("http_clients").httpClient;
|
|
9996
10087
|
const ruleSet = Object.freeze(factoriesFor("route.rule_set"));
|
|
9997
|
-
|
|
10088
|
+
// src/internal/crypto.ts
|
|
10089
|
+
const x25519Pkcs8Header = hex.decode("302e020100300506032b656e04220420");
|
|
10090
|
+
function randomBytes(length) {
|
|
10091
|
+
if (!Number.isSafeInteger(length) || length < 0) throw new RangeError("Random byte length must be a non-negative integer");
|
|
10092
|
+
const bytes = new Uint8Array(length);
|
|
10093
|
+
for (let offset = 0; offset < bytes.length; offset += 65536) globalThis.crypto.getRandomValues(bytes.subarray(offset, offset + 65536));
|
|
10094
|
+
return bytes;
|
|
10095
|
+
}
|
|
10096
|
+
async function deriveX25519PublicKey(privateKey) {
|
|
10097
|
+
if (privateKey.length !== 32) throw new RangeError("X25519 private key must be 32 bytes");
|
|
10098
|
+
const { subtle } = globalThis.crypto;
|
|
10099
|
+
const pkcs8 = toArrayBuffer(concatToUint8Array([x25519Pkcs8Header, privateKey]));
|
|
10100
|
+
const key = await subtle.importKey("pkcs8", pkcs8, "X25519", false, ["deriveBits"]);
|
|
10101
|
+
const basePoint = /* @__PURE__ */ new Uint8Array(32);
|
|
10102
|
+
basePoint[0] = 9;
|
|
10103
|
+
const publicKey = await subtle.importKey("raw", basePoint, "X25519", false, []);
|
|
10104
|
+
return new Uint8Array(await subtle.deriveBits({
|
|
10105
|
+
name: "X25519",
|
|
10106
|
+
public: publicKey
|
|
10107
|
+
}, key, 256));
|
|
10108
|
+
}
|
|
10109
|
+
// src/internal/reality.ts
|
|
10110
|
+
function decodeKey(input, path) {
|
|
10111
|
+
if (typeof input !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(input)) fail(path, "expected an unpadded 32-byte Base64url key");
|
|
10112
|
+
const bytes = base64url.decode(input);
|
|
10113
|
+
if (base64url.encode(bytes) !== input) fail(path, "expected an unpadded 32-byte Base64url key");
|
|
10114
|
+
return bytes;
|
|
10115
|
+
}
|
|
10116
|
+
async function validateRealityKeyPair({ privateKey, publicKey }, path) {
|
|
10117
|
+
const privateBytes = decodeKey(privateKey, `${path}.privateKey`);
|
|
10118
|
+
decodeKey(publicKey, `${path}.publicKey`);
|
|
10119
|
+
if (base64url.encode(await deriveX25519PublicKey(privateBytes)) !== publicKey) fail(`${path}.publicKey`, "does not match private key");
|
|
10120
|
+
}
|
|
10121
|
+
// src/proxy-profile.ts
|
|
10122
|
+
const sharedOption = {
|
|
10123
|
+
hysteria: "obfs",
|
|
10124
|
+
hysteria2: "obfs",
|
|
10125
|
+
shadowsocks: "method"
|
|
10126
|
+
};
|
|
10127
|
+
const ownedServer = [
|
|
10128
|
+
"tag",
|
|
10129
|
+
"users",
|
|
10130
|
+
"password",
|
|
10131
|
+
"version",
|
|
10132
|
+
"transport",
|
|
10133
|
+
"managed",
|
|
10134
|
+
"destinations",
|
|
10135
|
+
"psk",
|
|
10136
|
+
"mode",
|
|
10137
|
+
"obfs_mode",
|
|
10138
|
+
"realm",
|
|
10139
|
+
"handshake"
|
|
10140
|
+
];
|
|
10141
|
+
const ownedClient = [
|
|
10142
|
+
"tag",
|
|
10143
|
+
"server",
|
|
10144
|
+
"server_port",
|
|
10145
|
+
"server_ports",
|
|
10146
|
+
"username",
|
|
10147
|
+
"password",
|
|
10148
|
+
"uuid",
|
|
10149
|
+
"flow",
|
|
10150
|
+
"alter_id",
|
|
10151
|
+
"auth",
|
|
10152
|
+
"auth_str",
|
|
10153
|
+
"userkey",
|
|
10154
|
+
"psk",
|
|
10155
|
+
"version",
|
|
10156
|
+
"transport",
|
|
10157
|
+
"mode",
|
|
10158
|
+
"obfs_mode",
|
|
10159
|
+
"realm"
|
|
10160
|
+
];
|
|
10161
|
+
const transportFields = {
|
|
10162
|
+
tcp: [],
|
|
10163
|
+
quic: [],
|
|
10164
|
+
http: [
|
|
10165
|
+
"host",
|
|
10166
|
+
"path",
|
|
10167
|
+
"method"
|
|
10168
|
+
],
|
|
10169
|
+
ws: [
|
|
10170
|
+
"path",
|
|
10171
|
+
"max_early_data",
|
|
10172
|
+
"early_data_header_name"
|
|
10173
|
+
],
|
|
10174
|
+
grpc: ["service_name"],
|
|
10175
|
+
httpupgrade: ["host", "path"]
|
|
10176
|
+
};
|
|
10177
|
+
const localTransportFields = {
|
|
10178
|
+
tcp: [],
|
|
10179
|
+
quic: [],
|
|
10180
|
+
http: [
|
|
10181
|
+
"headers",
|
|
10182
|
+
"idle_timeout",
|
|
10183
|
+
"ping_timeout"
|
|
10184
|
+
],
|
|
10185
|
+
ws: ["headers"],
|
|
10186
|
+
grpc: ["idle_timeout", "ping_timeout"],
|
|
10187
|
+
httpupgrade: ["headers"]
|
|
10188
|
+
};
|
|
10189
|
+
const grpcClientFields = ["permit_without_stream"];
|
|
10190
|
+
const tlsRequired = /* @__PURE__ */ new Set([
|
|
10191
|
+
"anytls",
|
|
10192
|
+
"hysteria",
|
|
10193
|
+
"hysteria2",
|
|
10194
|
+
"naive",
|
|
10195
|
+
"tuic"
|
|
10196
|
+
]);
|
|
10197
|
+
const versioned = [
|
|
10198
|
+
"socks",
|
|
10199
|
+
"shadowtls",
|
|
10200
|
+
"snell"
|
|
10201
|
+
];
|
|
10202
|
+
const transportProtocols = [
|
|
10203
|
+
"vmess",
|
|
10204
|
+
"vless",
|
|
10205
|
+
"trojan"
|
|
10206
|
+
];
|
|
10207
|
+
const namedClients = [
|
|
10208
|
+
"socks",
|
|
10209
|
+
"http",
|
|
10210
|
+
"naive"
|
|
10211
|
+
];
|
|
10212
|
+
const optionalUsers = [
|
|
10213
|
+
"http",
|
|
10214
|
+
"socks",
|
|
10215
|
+
"snell",
|
|
10216
|
+
"shadowsocks",
|
|
10217
|
+
"shadowtls"
|
|
10218
|
+
];
|
|
10219
|
+
const specialFields = {
|
|
10220
|
+
hysteria2: ["realm"],
|
|
10221
|
+
shadowsocks: [
|
|
10222
|
+
"password",
|
|
10223
|
+
"serverKey",
|
|
10224
|
+
"managed"
|
|
10225
|
+
],
|
|
10226
|
+
shadowtls: [
|
|
10227
|
+
"password",
|
|
10228
|
+
"serverName",
|
|
10229
|
+
"handshake"
|
|
10230
|
+
],
|
|
10231
|
+
snell: [
|
|
10232
|
+
"psk",
|
|
10233
|
+
"obfs_mode",
|
|
10234
|
+
"mode"
|
|
10235
|
+
]
|
|
10236
|
+
};
|
|
10237
|
+
function native(factory, options) {
|
|
10238
|
+
return factory(options);
|
|
10239
|
+
}
|
|
10240
|
+
function connection(value, path) {
|
|
10241
|
+
const { host, port } = fieldsFrom(value, path, ["host", "port"], ["host", "port"]);
|
|
10242
|
+
if (typeof host !== "string" || host === "") fail(`${path}.host`, "expected a host");
|
|
10243
|
+
if (typeof port !== "number" || !Number.isInteger(port) || port < 1 || port > 65535) fail(`${path}.port`, "expected a port from 1 to 65535");
|
|
10244
|
+
return {
|
|
10245
|
+
server: host,
|
|
10246
|
+
server_port: port
|
|
10247
|
+
};
|
|
10248
|
+
}
|
|
10249
|
+
function profileConnection(kind, value, path) {
|
|
10250
|
+
if (kind !== "hysteria" && kind !== "hysteria2") return connection(value, path);
|
|
10251
|
+
const fields = record(value, path);
|
|
10252
|
+
if (!Object.hasOwn(fields, "ports")) return connection(fields, path);
|
|
10253
|
+
const { host, ports } = fieldsFrom(fields, path, ["host", "ports"], ["host", "ports"]);
|
|
10254
|
+
return {
|
|
10255
|
+
server: requiredString(host, `${path}.host`),
|
|
10256
|
+
server_ports: ports
|
|
10257
|
+
};
|
|
10258
|
+
}
|
|
10259
|
+
function usersFor(input, path) {
|
|
10260
|
+
if (!Array.isArray(input) || input.length === 0) fail(path, "expected a non-empty array of Users");
|
|
10261
|
+
const names = /* @__PURE__ */ new Set();
|
|
10262
|
+
return Array.from(input, (value, index) => {
|
|
10263
|
+
const entry = `${path}[${index}]`;
|
|
10264
|
+
if (!isUser(value) || !value.name) fail(entry, "expected a User with a non-empty name");
|
|
10265
|
+
if (names.has(value.name)) fail(entry, "duplicate user name");
|
|
10266
|
+
names.add(value.name);
|
|
10267
|
+
return value;
|
|
10268
|
+
});
|
|
10269
|
+
}
|
|
10270
|
+
function tlsFor(kind, input, needsTls, path) {
|
|
10271
|
+
const { mode } = record(input ?? { mode: "none" }, path);
|
|
10272
|
+
if (mode === "tls") {
|
|
10273
|
+
if (kind === "shadowsocks" || kind === "snell" || kind === "socks" || kind === "shadowtls") fail(path, "TLS is unavailable for this profile");
|
|
10274
|
+
const fields = fieldsFrom(input, path, [
|
|
10275
|
+
"mode",
|
|
10276
|
+
"serverName",
|
|
10277
|
+
"alpn"
|
|
10278
|
+
]);
|
|
10279
|
+
checkTlsAlpn(kind, fields, path);
|
|
10280
|
+
if (fields.serverName !== void 0 && (typeof fields.serverName !== "string" || !fields.serverName)) fail(`${path}.serverName`, "expected a server name");
|
|
10281
|
+
return {
|
|
10282
|
+
mode,
|
|
10283
|
+
fields
|
|
10284
|
+
};
|
|
10285
|
+
}
|
|
10286
|
+
if (needsTls) fail(path, "requires regular TLS");
|
|
10287
|
+
if (mode === "none") {
|
|
10288
|
+
if (input !== void 0) fieldsFrom(input, path, ["mode"]);
|
|
10289
|
+
return { mode };
|
|
10290
|
+
}
|
|
10291
|
+
if (mode !== "reality" || kind !== "vless") fail(`${path}.mode`, "unsupported TLS mode");
|
|
10292
|
+
const fields = fieldsFrom(input, path, [
|
|
10293
|
+
"mode",
|
|
10294
|
+
"serverName",
|
|
10295
|
+
"handshake",
|
|
10296
|
+
"keyPair",
|
|
10297
|
+
"shortId",
|
|
10298
|
+
"shortIds"
|
|
10299
|
+
], [
|
|
10300
|
+
"serverName",
|
|
10301
|
+
"handshake",
|
|
10302
|
+
"keyPair"
|
|
10303
|
+
]);
|
|
10304
|
+
if (typeof fields.serverName !== "string" || !fields.serverName) fail(`${path}.serverName`, "expected a server name");
|
|
10305
|
+
if (fields.shortId === void 0 === (fields.shortIds === void 0)) fail(path, "specify either shortId or shortIds");
|
|
10306
|
+
const ids = fields.shortIds ?? [fields.shortId];
|
|
10307
|
+
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");
|
|
10308
|
+
const { host, port, ...dial } = record(fields.handshake, `${path}.handshake`);
|
|
10309
|
+
if (Object.hasOwn(dial, "server") || Object.hasOwn(dial, "server_port")) fail(`${path}.handshake`, "use host and port for the handshake address");
|
|
10310
|
+
return {
|
|
10311
|
+
mode,
|
|
10312
|
+
serverName: fields.serverName,
|
|
10313
|
+
keys: fieldsFrom(fields.keyPair, `${path}.keyPair`, ["privateKey", "publicKey"], ["privateKey", "publicKey"]),
|
|
10314
|
+
ids,
|
|
10315
|
+
handshake: {
|
|
10316
|
+
...dial,
|
|
10317
|
+
...connection({
|
|
10318
|
+
host,
|
|
10319
|
+
port
|
|
10320
|
+
}, `${path}.handshake`)
|
|
10321
|
+
}
|
|
10322
|
+
};
|
|
10323
|
+
}
|
|
10324
|
+
function checkedOptions(value, owned, path) {
|
|
10325
|
+
const fields = record(value ?? {}, path);
|
|
10326
|
+
for (const key of Object.keys(fields)) if (owned.includes(key)) fail(keyPath(path, key), "field is controlled by the profile");
|
|
10327
|
+
return fields;
|
|
10328
|
+
}
|
|
10329
|
+
function protocolOptions(kind, config, hasUsers, sharedKey, path) {
|
|
10330
|
+
const serverFields = {};
|
|
10331
|
+
const clientFields = {};
|
|
10332
|
+
if (sharedKey && config[sharedKey] !== void 0) {
|
|
10333
|
+
serverFields[sharedKey] = config[sharedKey];
|
|
10334
|
+
clientFields[sharedKey] = config[sharedKey];
|
|
10335
|
+
}
|
|
10336
|
+
switch (kind) {
|
|
10337
|
+
case "socks": {
|
|
10338
|
+
const version = config.version ?? "5";
|
|
10339
|
+
if (version !== "4" && version !== "4a" && version !== "5") fail(`${path}.version`, "unsupported SOCKS version");
|
|
10340
|
+
clientFields.version = version;
|
|
10341
|
+
break;
|
|
10342
|
+
}
|
|
10343
|
+
case "snell": {
|
|
10344
|
+
const version = config.version ?? 6;
|
|
10345
|
+
if (version !== 5 && version !== 6) fail(`${path}.version`, "unsupported Snell version");
|
|
10346
|
+
const forbidden = version === 5 ? "mode" : "obfs_mode";
|
|
10347
|
+
if (config[forbidden] !== void 0) fail(`${path}.${forbidden}`, "unavailable for this Snell version");
|
|
10348
|
+
serverFields.version = version;
|
|
10349
|
+
clientFields.version = version === 5 ? 4 : 6;
|
|
10350
|
+
const flag = version === 5 ? "obfs_mode" : "mode";
|
|
10351
|
+
if (config[flag] !== void 0) serverFields[flag] = clientFields[flag] = config[flag];
|
|
10352
|
+
serverFields.psk = clientFields.psk = requiredString(config.psk, `${path}.psk`);
|
|
10353
|
+
break;
|
|
10354
|
+
}
|
|
10355
|
+
case "shadowtls": {
|
|
10356
|
+
const version = config.version ?? 3;
|
|
10357
|
+
if (version !== 1 && version !== 2 && version !== 3) fail(`${path}.version`, "unsupported ShadowTLS version");
|
|
10358
|
+
if (version === 3 ? !hasUsers : hasUsers) fail(`${path}.users`, "users are required for ShadowTLS v3 and unavailable in other versions");
|
|
10359
|
+
if (version === 2) serverFields.password = clientFields.password = requiredString(config.password, `${path}.password`);
|
|
10360
|
+
else if (config.password !== void 0) fail(`${path}.password`, "shared password is available only in ShadowTLS v2");
|
|
10361
|
+
const handshake = record(config.handshake, `${path}.handshake`);
|
|
10362
|
+
connection({
|
|
10363
|
+
host: handshake.server,
|
|
10364
|
+
port: handshake.server_port
|
|
10365
|
+
}, `${path}.handshake`);
|
|
10366
|
+
serverFields.handshake = handshake;
|
|
10367
|
+
requiredString(config.serverName, `${path}.serverName`);
|
|
10368
|
+
serverFields.version = clientFields.version = version;
|
|
10369
|
+
break;
|
|
10370
|
+
}
|
|
10371
|
+
case "shadowsocks": {
|
|
10372
|
+
if (config.managed !== void 0 && typeof config.managed !== "boolean") fail(`${path}.managed`, "expected a boolean");
|
|
10373
|
+
if (config.managed && hasUsers) fail(`${path}.users`, "managed Shadowsocks uses SSM accounts");
|
|
10374
|
+
const method = requiredString(config.method, `${path}.method`);
|
|
10375
|
+
if (config.managed || hasUsers) {
|
|
10376
|
+
if (method === "none" || method === "2022-blake3-chacha20-poly1305") fail(`${path}.method`, "unavailable with multiple Shadowsocks users");
|
|
10377
|
+
if (config.password !== void 0) fail(`${path}.password`, "use User credentials for multiple users");
|
|
10378
|
+
if (method.startsWith("2022-")) serverFields.password = requiredString(config.serverKey, `${path}.serverKey`);
|
|
10379
|
+
else if (config.serverKey !== void 0) fail(`${path}.serverKey`, "requires a Shadowsocks 2022 AES method");
|
|
10380
|
+
if (config.managed) serverFields.managed = true;
|
|
10381
|
+
} else {
|
|
10382
|
+
if (config.serverKey !== void 0) fail(`${path}.serverKey`, "requires multiple users");
|
|
10383
|
+
if (method !== "none") serverFields.password = clientFields.password = requiredString(config.password, `${path}.password`);
|
|
10384
|
+
else if (config.password !== void 0) fail(`${path}.password`, "unavailable with method none");
|
|
10385
|
+
}
|
|
10386
|
+
break;
|
|
10387
|
+
}
|
|
10388
|
+
}
|
|
10389
|
+
return {
|
|
10390
|
+
serverFields,
|
|
10391
|
+
clientFields
|
|
10392
|
+
};
|
|
10393
|
+
}
|
|
10394
|
+
function makeProfile(kind, input) {
|
|
10395
|
+
const path = `proxyProfile.${kind}`;
|
|
10396
|
+
const sharedKey = Object.hasOwn(sharedOption, kind) ? sharedOption[kind] : void 0;
|
|
10397
|
+
const config = snapshot(fieldsFrom(input, path, [
|
|
10398
|
+
"connect",
|
|
10399
|
+
"users",
|
|
10400
|
+
...kind === "shadowtls" || kind === "shadowsocks" || kind === "snell" || kind === "socks" ? [] : ["tls"],
|
|
10401
|
+
...transportProtocols.includes(kind) ? ["transport"] : [],
|
|
10402
|
+
...versioned.includes(kind) ? ["version"] : [],
|
|
10403
|
+
...sharedKey ? [sharedKey] : [],
|
|
10404
|
+
...specialFields[kind] ?? []
|
|
10405
|
+
]), path);
|
|
10406
|
+
const realm = config.realm === void 0 ? void 0 : fieldsFrom(config.realm, `${path}.realm`, [
|
|
10407
|
+
"server_url",
|
|
10408
|
+
"realm_id",
|
|
10409
|
+
"stun_servers",
|
|
10410
|
+
"token"
|
|
10411
|
+
], [
|
|
10412
|
+
"server_url",
|
|
10413
|
+
"realm_id",
|
|
10414
|
+
"stun_servers"
|
|
10415
|
+
]);
|
|
10416
|
+
if (realm && config.connect !== void 0) fail(`${path}.connect`, "conflicts with Realm");
|
|
10417
|
+
const connect = realm ? {} : profileConnection(kind, config.connect, `${path}.connect`);
|
|
10418
|
+
const users = config.users === void 0 ? [] : usersFor(config.users, `${path}.users`);
|
|
10419
|
+
if (!optionalUsers.includes(kind) && !users.length) fail(`${path}.users`, "expected at least one User");
|
|
10420
|
+
let transport = {};
|
|
10421
|
+
let transportType = "tcp";
|
|
10422
|
+
if (config.transport !== void 0) {
|
|
10423
|
+
const { type } = record(config.transport, `${path}.transport`);
|
|
10424
|
+
if (typeof type !== "string" || !Object.hasOwn(transportFields, type)) fail(`${path}.transport.type`, "unsupported transport");
|
|
10425
|
+
transportType = type;
|
|
10426
|
+
transport = fieldsFrom(config.transport, `${path}.transport`, ["type", ...transportFields[transportType]]);
|
|
10427
|
+
}
|
|
10428
|
+
const tls = tlsFor(kind, config.tls, tlsRequired.has(kind) || transportType === "quic", `${path}.tls`);
|
|
10429
|
+
if (realm && (tls.mode !== "tls" || !tls.fields.serverName)) fail(`${path}.tls.serverName`, "required for Realm connections");
|
|
10430
|
+
const { serverFields, clientFields } = protocolOptions(kind, config, users.length > 0, sharedKey, path);
|
|
10431
|
+
function credentials(user, location) {
|
|
10432
|
+
if (kind === "socks" && clientFields.version !== "5") {
|
|
10433
|
+
if (requireUserCredentials(user, "socks", location).password !== "") fail(location, "SOCKS4 and SOCKS4a use username-only authentication");
|
|
10434
|
+
return {};
|
|
10435
|
+
}
|
|
10436
|
+
return { ...requireUserCredentials(user, kind, location) };
|
|
10437
|
+
}
|
|
10438
|
+
users.forEach((user, index) => credentials(user, `${path}.users[${index}]`));
|
|
10439
|
+
if (users.length) serverFields.users = users;
|
|
10440
|
+
function sideOptions(input, client) {
|
|
10441
|
+
const location = `${path}.${client ? "client" : "server"}`;
|
|
10442
|
+
const local = fieldsFrom(input, location, [
|
|
10443
|
+
"tag",
|
|
10444
|
+
"options",
|
|
10445
|
+
"tls",
|
|
10446
|
+
"transport",
|
|
10447
|
+
"realm",
|
|
10448
|
+
...client ? ["dial", "shortId"] : ["listen"]
|
|
10449
|
+
], ["tag"]);
|
|
10450
|
+
const extra = checkedOptions(local.options, [...client ? ownedClient : ownedServer, ...sharedKey ? [sharedKey] : []], `${location}.options`);
|
|
10451
|
+
let transportOptions = {};
|
|
10452
|
+
if (local.transport !== void 0) {
|
|
10453
|
+
if (transportType === "tcp" || transportType === "quic") fail(`${location}.transport`, "this transport has no side-specific fields");
|
|
10454
|
+
transportOptions = fieldsFrom(local.transport, `${location}.transport`, [...localTransportFields[transportType], ...client && transportType === "grpc" ? grpcClientFields : []]);
|
|
10455
|
+
}
|
|
10456
|
+
let realmOptions = {};
|
|
10457
|
+
if (local.realm !== void 0) {
|
|
10458
|
+
if (!realm) fail(`${location}.realm`, "requires a Realm profile");
|
|
10459
|
+
realmOptions = fieldsFrom(local.realm, `${location}.realm`, [
|
|
10460
|
+
"token",
|
|
10461
|
+
"ip_version",
|
|
10462
|
+
"port_mapping",
|
|
10463
|
+
"http_client",
|
|
10464
|
+
...client ? [] : ["stun_domain_resolver"]
|
|
10465
|
+
]);
|
|
10466
|
+
}
|
|
10467
|
+
return {
|
|
10468
|
+
location,
|
|
10469
|
+
local,
|
|
10470
|
+
fields: {
|
|
10471
|
+
...extra,
|
|
10472
|
+
tag: local.tag,
|
|
10473
|
+
...transportType === "tcp" ? {} : { transport: {
|
|
10474
|
+
...transport,
|
|
10475
|
+
...transportOptions
|
|
10476
|
+
} },
|
|
10477
|
+
...realm ? { realm: {
|
|
10478
|
+
...realm,
|
|
10479
|
+
...realmOptions
|
|
10480
|
+
} } : {}
|
|
10481
|
+
}
|
|
10482
|
+
};
|
|
10483
|
+
}
|
|
10484
|
+
function applyTls(builder, local, location, client) {
|
|
10485
|
+
if (local.shortId !== void 0 && tls.mode !== "reality") fail(`${location}.shortId`, "requires REALITY");
|
|
10486
|
+
if (tls.mode === "reality") {
|
|
10487
|
+
if (local.tls !== void 0) fail(`${location}.tls`, "REALITY settings belong to the profile");
|
|
10488
|
+
if (!client) return builder.tls({
|
|
10489
|
+
side: "server",
|
|
10490
|
+
server_name: tls.serverName,
|
|
10491
|
+
reality: {
|
|
10492
|
+
private_key: tls.keys.privateKey,
|
|
10493
|
+
short_id: tls.ids,
|
|
10494
|
+
handshake: tls.handshake
|
|
10495
|
+
}
|
|
10496
|
+
});
|
|
10497
|
+
const selected = local.shortId ?? (tls.ids.length === 1 ? tls.ids[0] : void 0);
|
|
10498
|
+
if (typeof selected !== "string" || !tls.ids.includes(selected)) fail(`${location}.shortId`, "select a short ID from the profile");
|
|
10499
|
+
return builder.tls({
|
|
10500
|
+
side: "client",
|
|
10501
|
+
server_name: tls.serverName,
|
|
10502
|
+
reality: {
|
|
10503
|
+
public_key: tls.keys.publicKey,
|
|
10504
|
+
short_id: selected
|
|
10505
|
+
}
|
|
10506
|
+
});
|
|
10507
|
+
}
|
|
10508
|
+
if (tls.mode === "tls" || kind === "shadowtls" && client) {
|
|
10509
|
+
const settings = checkedOptions(local.tls, [
|
|
10510
|
+
"enabled",
|
|
10511
|
+
"reality",
|
|
10512
|
+
"side",
|
|
10513
|
+
...client ? ["server_name"] : [],
|
|
10514
|
+
...tls.mode === "tls" && tls.fields.alpn !== void 0 ? ["alpn"] : []
|
|
10515
|
+
], `${location}.tls`);
|
|
10516
|
+
checkTlsAlpn(kind, settings, `${location}.tls`);
|
|
10517
|
+
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");
|
|
10518
|
+
return builder.tls({
|
|
10519
|
+
...settings,
|
|
10520
|
+
enabled: true,
|
|
10521
|
+
...tls.mode === "tls" && tls.fields.alpn !== void 0 ? { alpn: tls.fields.alpn } : {},
|
|
10522
|
+
...client ? { server_name: tls.mode === "tls" ? tls.fields.serverName ?? connect.server : config.serverName } : {}
|
|
10523
|
+
});
|
|
10524
|
+
}
|
|
10525
|
+
if (local.tls !== void 0) fail(`${location}.tls`, "TLS must be enabled in the profile");
|
|
10526
|
+
return builder;
|
|
10527
|
+
}
|
|
10528
|
+
const result = Object.freeze({
|
|
10529
|
+
server(input) {
|
|
10530
|
+
const { local, fields, location } = sideOptions(input, false);
|
|
10531
|
+
let builder = native(inbound[kind], {
|
|
10532
|
+
...fields,
|
|
10533
|
+
...serverFields
|
|
10534
|
+
});
|
|
10535
|
+
if (local.listen !== void 0) {
|
|
10536
|
+
const { address, ...listen } = record(local.listen, `${location}.listen`);
|
|
10537
|
+
builder = builder.listen(address, listen);
|
|
10538
|
+
}
|
|
10539
|
+
return applyTls(builder, local, location, false);
|
|
10540
|
+
},
|
|
10541
|
+
client(userOrOptions, options) {
|
|
10542
|
+
const authenticated = users.length > 0 || config.managed === true;
|
|
10543
|
+
if (authenticated && (!isUser(userOrOptions) || !userOrOptions.name || config.managed !== true && !users.includes(userOrOptions))) fail(`${path}.client.user`, "expected a User for this profile");
|
|
10544
|
+
if (!authenticated && options !== void 0) fail(`${path}.client.user`, "profile has no user selection");
|
|
10545
|
+
const { local, fields, location } = sideOptions(authenticated ? options : userOrOptions, true);
|
|
10546
|
+
const selected = isUser(userOrOptions) ? userOrOptions : void 0;
|
|
10547
|
+
const auth = selected ? {
|
|
10548
|
+
...credentials(selected, `${location}.user`),
|
|
10549
|
+
...namedClients.includes(kind) ? { username: selected.name } : {}
|
|
10550
|
+
} : {};
|
|
10551
|
+
if (kind === "shadowsocks" && typeof auth.password === "string" && typeof serverFields.password === "string") auth.password = `${serverFields.password}:${auth.password}`;
|
|
10552
|
+
let builder = native(outbound[kind], {
|
|
10553
|
+
...fields,
|
|
10554
|
+
...clientFields,
|
|
10555
|
+
...connect,
|
|
10556
|
+
...auth
|
|
10557
|
+
});
|
|
10558
|
+
if (local.dial !== void 0) builder = builder.dial(local.dial);
|
|
10559
|
+
return applyTls(builder, local, location, true);
|
|
10560
|
+
}
|
|
10561
|
+
});
|
|
10562
|
+
if (tls.mode === "reality") return validateRealityKeyPair(tls.keys, `${path}.tls.keyPair`).then(() => result);
|
|
10563
|
+
return result;
|
|
10564
|
+
}
|
|
10565
|
+
function requiredString(input, path) {
|
|
10566
|
+
if (typeof input !== "string" || !input) fail(path, "expected a non-empty string");
|
|
10567
|
+
return input;
|
|
10568
|
+
}
|
|
10569
|
+
function http(input) {
|
|
10570
|
+
return makeProfile("http", input);
|
|
10571
|
+
}
|
|
10572
|
+
function socks(input) {
|
|
10573
|
+
return makeProfile("socks", input);
|
|
10574
|
+
}
|
|
10575
|
+
function snell(input) {
|
|
10576
|
+
return makeProfile("snell", input);
|
|
10577
|
+
}
|
|
10578
|
+
function shadowtls(input) {
|
|
10579
|
+
return makeProfile("shadowtls", input);
|
|
10580
|
+
}
|
|
10581
|
+
function shadowsocks(input) {
|
|
10582
|
+
return makeProfile("shadowsocks", input);
|
|
10583
|
+
}
|
|
10584
|
+
function transportProfile(kind) {
|
|
10585
|
+
function create(input) {
|
|
10586
|
+
return makeProfile(kind, input);
|
|
10587
|
+
}
|
|
10588
|
+
return create;
|
|
10589
|
+
}
|
|
10590
|
+
async function vlessReality(input) {
|
|
10591
|
+
return makeProfile("vless", input);
|
|
10592
|
+
}
|
|
10593
|
+
function vless(input) {
|
|
10594
|
+
if (input?.tls?.mode === "reality") return vlessReality(input);
|
|
10595
|
+
return makeProfile("vless", input);
|
|
10596
|
+
}
|
|
10597
|
+
const proxyProfile = Object.freeze({
|
|
10598
|
+
anytls: (input) => makeProfile("anytls", input),
|
|
10599
|
+
http,
|
|
10600
|
+
hysteria: (input) => makeProfile("hysteria", input),
|
|
10601
|
+
hysteria2: (input) => makeProfile("hysteria2", input),
|
|
10602
|
+
naive: (input) => makeProfile("naive", input),
|
|
10603
|
+
shadowsocks,
|
|
10604
|
+
shadowtls,
|
|
10605
|
+
snell,
|
|
10606
|
+
socks,
|
|
10607
|
+
trojan: transportProfile("trojan"),
|
|
10608
|
+
tuic: (input) => makeProfile("tuic", input),
|
|
10609
|
+
vless,
|
|
10610
|
+
vmess: transportProfile("vmess")
|
|
10611
|
+
});
|
|
10612
|
+
// src/internal/certificate.ts
|
|
10613
|
+
async function serverCertificate(serverName, months) {
|
|
10614
|
+
if (!Number.isSafeInteger(months) || months < 1) throw new RangeError("TLS validity must be a positive integer number of months");
|
|
10615
|
+
const now = /* @__PURE__ */ new Date();
|
|
10616
|
+
const expires = new Date(now);
|
|
10617
|
+
expires.setUTCMonth(expires.getUTCMonth() + months);
|
|
10618
|
+
if (!Number.isFinite(expires.getTime()) || expires.getUTCFullYear() > 9999) throw new RangeError("TLS expiry is outside the supported date range");
|
|
10619
|
+
if (/[\s\\/@?#%]/.test(serverName)) throw new TypeError("TLS server name must be a DNS name or IP address");
|
|
10620
|
+
const version = isIP(serverName);
|
|
10621
|
+
const dnsName = version || serverName.includes(":") ? void 0 : URL.parse(`http://${serverName}/`)?.hostname;
|
|
10622
|
+
if (!version && (!dnsName || !/^(?:\*\.)?[a-z\d_-]+(?:\.[a-z\d_-]+)*\.?$/i.test(dnsName))) throw new TypeError("TLS server name must be a DNS name or IP address");
|
|
10623
|
+
const crypto = globalThis.crypto;
|
|
10624
|
+
const algorithm = {
|
|
10625
|
+
name: "RSASSA-PKCS1-v1_5",
|
|
10626
|
+
modulusLength: 2048,
|
|
10627
|
+
publicExponent: new Uint8Array([
|
|
10628
|
+
1,
|
|
10629
|
+
0,
|
|
10630
|
+
1
|
|
10631
|
+
]),
|
|
10632
|
+
hash: "SHA-256"
|
|
10633
|
+
};
|
|
10634
|
+
const keys = await crypto.subtle.generateKey(algorithm, true, ["sign", "verify"]);
|
|
10635
|
+
const name = dnsName || serverName;
|
|
10636
|
+
const san = version === 6 ? new URL(`http://[${serverName}]`).hostname.slice(1, -1) : name;
|
|
10637
|
+
const certificate = await X509CertificateGenerator.createSelfSigned({
|
|
10638
|
+
name: [{ CN: [name] }],
|
|
10639
|
+
notBefore: /* @__PURE__ */ new Date(now.getTime() - 36e5),
|
|
10640
|
+
notAfter: expires,
|
|
10641
|
+
signingAlgorithm: algorithm,
|
|
10642
|
+
keys,
|
|
10643
|
+
extensions: [
|
|
10644
|
+
new KeyUsagesExtension(KeyUsageFlags.digitalSignature | KeyUsageFlags.keyEncipherment, true),
|
|
10645
|
+
new ExtendedKeyUsageExtension([ExtendedKeyUsage.serverAuth]),
|
|
10646
|
+
new BasicConstraintsExtension(false),
|
|
10647
|
+
new SubjectAlternativeNameExtension([{
|
|
10648
|
+
type: version ? "ip" : "dns",
|
|
10649
|
+
value: san
|
|
10650
|
+
}])
|
|
10651
|
+
]
|
|
10652
|
+
}, crypto);
|
|
10653
|
+
return {
|
|
10654
|
+
privateKey: pem.encode("PRIVATE KEY", await crypto.subtle.exportKey("pkcs8", keys.privateKey)),
|
|
10655
|
+
certificate: pem.encode("CERTIFICATE", certificate.rawData)
|
|
10656
|
+
};
|
|
10657
|
+
}
|
|
10658
|
+
// src/internal/ech.ts
|
|
10659
|
+
const cipherSuites = hex.decode("000100010001000200010003");
|
|
10660
|
+
function uint16(value) {
|
|
10661
|
+
const bytes = /* @__PURE__ */ new Uint8Array(2);
|
|
10662
|
+
new DataView(bytes.buffer).setUint16(0, value);
|
|
10663
|
+
return bytes;
|
|
10664
|
+
}
|
|
10665
|
+
function vector16(bytes) {
|
|
10666
|
+
return concatToUint8Array([uint16(bytes.length), bytes]);
|
|
10667
|
+
}
|
|
10668
|
+
function encodeEchKeyPair(name, privateKey, publicKey) {
|
|
10669
|
+
const contents = concatToUint8Array([
|
|
10670
|
+
Uint8Array.of(0),
|
|
10671
|
+
uint16(32),
|
|
10672
|
+
vector16(publicKey),
|
|
10673
|
+
vector16(cipherSuites),
|
|
10674
|
+
Uint8Array.of(0, name.length),
|
|
10675
|
+
name,
|
|
10676
|
+
uint16(0)
|
|
10677
|
+
]);
|
|
10678
|
+
const config = vector16(concatToUint8Array([uint16(65037), vector16(contents)]));
|
|
10679
|
+
return {
|
|
10680
|
+
config: pem.encode("ECH CONFIGS", config),
|
|
10681
|
+
key: pem.encode("ECH KEYS", concatToUint8Array([vector16(privateKey), config]))
|
|
10682
|
+
};
|
|
10683
|
+
}
|
|
10684
|
+
// src/generate.ts
|
|
10685
|
+
function uuid() {
|
|
10686
|
+
return globalThis.crypto.randomUUID();
|
|
10687
|
+
}
|
|
10688
|
+
function random(length) {
|
|
10689
|
+
return randomBytes(length);
|
|
10690
|
+
}
|
|
10691
|
+
function randomHex(length) {
|
|
10692
|
+
return hex.encode(random(length));
|
|
10693
|
+
}
|
|
10694
|
+
function randomBase64(length) {
|
|
10695
|
+
return base64.encode(random(length));
|
|
10696
|
+
}
|
|
10697
|
+
function randomBase64Url(length) {
|
|
10698
|
+
return base64url.encode(random(length));
|
|
10699
|
+
}
|
|
10700
|
+
function x25519PublicKey(privateKey) {
|
|
10701
|
+
return deriveX25519PublicKey(privateKey);
|
|
10702
|
+
}
|
|
10703
|
+
async function x25519KeyPair() {
|
|
10704
|
+
const privateKey = randomBytes(32);
|
|
10705
|
+
privateKey[0] = privateKey[0] & 248;
|
|
10706
|
+
privateKey[31] = privateKey[31] & 127 | 64;
|
|
10707
|
+
return {
|
|
10708
|
+
privateKey,
|
|
10709
|
+
publicKey: await x25519PublicKey(privateKey)
|
|
10710
|
+
};
|
|
10711
|
+
}
|
|
10712
|
+
async function reality() {
|
|
10713
|
+
const { privateKey, publicKey } = await x25519KeyPair();
|
|
10714
|
+
return {
|
|
10715
|
+
privateKey: base64url.encode(privateKey),
|
|
10716
|
+
publicKey: base64url.encode(publicKey)
|
|
10717
|
+
};
|
|
10718
|
+
}
|
|
10719
|
+
async function wireguard() {
|
|
10720
|
+
const { privateKey, publicKey } = await x25519KeyPair();
|
|
10721
|
+
return {
|
|
10722
|
+
privateKey: base64.encode(privateKey),
|
|
10723
|
+
publicKey: base64.encode(publicKey)
|
|
10724
|
+
};
|
|
10725
|
+
}
|
|
10726
|
+
async function vapidKeyPair() {
|
|
10727
|
+
const { subtle } = globalThis.crypto;
|
|
10728
|
+
const key = await subtle.generateKey({
|
|
10729
|
+
name: "ECDSA",
|
|
10730
|
+
namedCurve: "P-256"
|
|
10731
|
+
}, true, ["sign", "verify"]);
|
|
10732
|
+
const { d: privateKey } = await subtle.exportKey("jwk", key.privateKey);
|
|
10733
|
+
const publicKey = await subtle.exportKey("raw", key.publicKey);
|
|
10734
|
+
return {
|
|
10735
|
+
privateKey,
|
|
10736
|
+
publicKey: base64url.encode(publicKey)
|
|
10737
|
+
};
|
|
10738
|
+
}
|
|
10739
|
+
async function tailcatKeyPair() {
|
|
10740
|
+
const { privateKey, publicKey } = await x25519KeyPair();
|
|
10741
|
+
const { subtle } = globalThis.crypto;
|
|
10742
|
+
const hmacKey = await subtle.importKey("raw", privateKey, {
|
|
10743
|
+
name: "HMAC",
|
|
10744
|
+
hash: "SHA-256"
|
|
10745
|
+
}, false, ["sign"]);
|
|
10746
|
+
const discoLabel = new TextEncoder().encode("github.com/tailscale/tailcat disco key v1");
|
|
10747
|
+
const discoPrivateKey = new Uint8Array(await subtle.sign("HMAC", hmacKey, discoLabel));
|
|
10748
|
+
const view = new DataView(publicKey.buffer, publicKey.byteOffset, publicKey.byteLength);
|
|
10749
|
+
const groups = Array.from({ length: 5 }, (_, index) => view.getUint16(index * 2).toString(16));
|
|
10750
|
+
const address = new URL(`http://[fd7a:115c:a1e0:${groups.join(":")}]`).hostname.slice(1, -1);
|
|
10751
|
+
return {
|
|
10752
|
+
privateKey: base64.encode(privateKey),
|
|
10753
|
+
publicKey: base64.encode(publicKey),
|
|
10754
|
+
discoKey: base64.encode(await x25519PublicKey(discoPrivateKey)),
|
|
10755
|
+
preSharedKey: randomBase64(32),
|
|
10756
|
+
address
|
|
10757
|
+
};
|
|
10758
|
+
}
|
|
10759
|
+
async function echKeyPair(publicName) {
|
|
10760
|
+
const name = new TextEncoder().encode(publicName);
|
|
10761
|
+
if (name.length < 1 || name.length > 255) throw new RangeError("ECH public name must be between 1 and 255 bytes");
|
|
10762
|
+
const { privateKey, publicKey } = await x25519KeyPair();
|
|
10763
|
+
return encodeEchKeyPair(name, privateKey, publicKey);
|
|
10764
|
+
}
|
|
10765
|
+
function tlsKeyPair(serverName, months = 1) {
|
|
10766
|
+
return serverCertificate(serverName, months);
|
|
10767
|
+
}
|
|
10768
|
+
const generate = {
|
|
10769
|
+
uuid,
|
|
10770
|
+
random,
|
|
10771
|
+
randomHex,
|
|
10772
|
+
randomBase64,
|
|
10773
|
+
randomBase64Url,
|
|
10774
|
+
x25519KeyPair,
|
|
10775
|
+
x25519PublicKey,
|
|
10776
|
+
reality,
|
|
10777
|
+
wireguard,
|
|
10778
|
+
vapidKeyPair,
|
|
10779
|
+
tailcatKeyPair,
|
|
10780
|
+
echKeyPair,
|
|
10781
|
+
tlsKeyPair
|
|
10782
|
+
};
|
|
10783
|
+
export { Rule, certificateProvider, compile, dns, dnsServer, endpoint, generate, httpClient, inbound, networkNamespace, outbound, proxyProfile, route, ruleSet, service, user, validateConfig };
|