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
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { concatToUint8Array } from "@peculiar/utils/bytes";
|
|
2
|
+
import { hex } from "@peculiar/utils/encoding";
|
|
3
|
+
import { pem } from "@peculiar/utils/pem";
|
|
4
|
+
|
|
5
|
+
const cipherSuites = hex.decode("000100010001000200010003"); // HKDF-SHA256 and three AEADs.
|
|
6
|
+
|
|
7
|
+
function uint16(value: number): Uint8Array {
|
|
8
|
+
const bytes = new Uint8Array(2);
|
|
9
|
+
new DataView(bytes.buffer).setUint16(0, value);
|
|
10
|
+
return bytes;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function vector16(bytes: Uint8Array): Uint8Array {
|
|
14
|
+
return concatToUint8Array([uint16(bytes.length), bytes]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function encodeEchKeyPair(name: Uint8Array, privateKey: Uint8Array, publicKey: Uint8Array) {
|
|
18
|
+
const contents = concatToUint8Array([
|
|
19
|
+
Uint8Array.of(0), // Configuration ID.
|
|
20
|
+
uint16(0x0020), // DHKEM(X25519, HKDF-SHA256).
|
|
21
|
+
vector16(publicKey),
|
|
22
|
+
vector16(cipherSuites),
|
|
23
|
+
Uint8Array.of(0, name.length), // Maximum name length and public name length.
|
|
24
|
+
name,
|
|
25
|
+
uint16(0), // No extensions.
|
|
26
|
+
]);
|
|
27
|
+
const config = vector16(concatToUint8Array([uint16(0xfe0d), vector16(contents)]));
|
|
28
|
+
return {
|
|
29
|
+
config: pem.encode("ECH CONFIGS", config),
|
|
30
|
+
key: pem.encode("ECH KEYS", concatToUint8Array([vector16(privateKey), config])),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { base64url } from "@peculiar/utils/encoding";
|
|
2
|
+
import { fail } from "../schema";
|
|
3
|
+
import { deriveX25519PublicKey } from "./crypto";
|
|
4
|
+
|
|
5
|
+
function decodeKey(input: unknown, path: string): Uint8Array {
|
|
6
|
+
if (typeof input !== "string" || !/^[A-Za-z0-9_-]{43}$/.test(input))
|
|
7
|
+
fail(path, "expected an unpadded 32-byte Base64url key");
|
|
8
|
+
const bytes = base64url.decode(input);
|
|
9
|
+
if (base64url.encode(bytes) !== input) fail(path, "expected an unpadded 32-byte Base64url key");
|
|
10
|
+
return bytes;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function validateRealityKeyPair(
|
|
14
|
+
{ privateKey, publicKey }: { privateKey?: unknown; publicKey?: unknown },
|
|
15
|
+
path: string,
|
|
16
|
+
): Promise<void> {
|
|
17
|
+
const privateBytes = decodeKey(privateKey, `${path}.privateKey`);
|
|
18
|
+
decodeKey(publicKey, `${path}.publicKey`);
|
|
19
|
+
if (base64url.encode(await deriveX25519PublicKey(privateBytes)) !== publicKey)
|
|
20
|
+
fail(`${path}.publicKey`, "does not match private key");
|
|
21
|
+
}
|
package/src/ip.ts
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const ipv4Octet = /^(?:0|[1-9][0-9]{0,2})$/;
|
|
2
|
+
const ipv6Group = /^[0-9a-f]{1,4}$/i;
|
|
3
|
+
const ipv6Zone = /^[0-9a-z.:-]+$/i;
|
|
4
|
+
const cidrPrefix = /^[0-9]+$/;
|
|
5
|
+
|
|
6
|
+
export function isIPv4(input: string): boolean {
|
|
7
|
+
const octets = input.split(".", 5);
|
|
8
|
+
return (
|
|
9
|
+
octets.length === 4 && octets.every((octet) => ipv4Octet.test(octet) && Number(octet) <= 255)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function isIPv6(input: string): boolean {
|
|
14
|
+
const [address = "", zone, extraZone] = input.split("%", 3);
|
|
15
|
+
if (
|
|
16
|
+
!address.includes(":") ||
|
|
17
|
+
extraZone !== undefined ||
|
|
18
|
+
(zone !== undefined && !ipv6Zone.test(zone))
|
|
19
|
+
) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const [head = "", tail, extra] = address.split("::", 3);
|
|
24
|
+
if (extra !== undefined) return false;
|
|
25
|
+
|
|
26
|
+
const groups = head ? head.split(":", 9) : [];
|
|
27
|
+
if (tail) groups.push(...tail.split(":", 9));
|
|
28
|
+
|
|
29
|
+
let groupCount = groups.length;
|
|
30
|
+
const last = groups.at(-1);
|
|
31
|
+
if (last?.includes(".")) {
|
|
32
|
+
if (tail === "" || !isIPv4(last)) return false;
|
|
33
|
+
groups.pop();
|
|
34
|
+
// An embedded IPv4 address occupies two 16-bit groups.
|
|
35
|
+
groupCount++;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
(tail === undefined ? groupCount === 8 : groupCount < 8) &&
|
|
40
|
+
groups.every((group) => ipv6Group.test(group))
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function isIP(input: string): 0 | 4 | 6 {
|
|
45
|
+
if (isIPv4(input)) return 4;
|
|
46
|
+
if (isIPv6(input)) return 6;
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function isCIDR(input: string): boolean {
|
|
51
|
+
const [address = "", prefix, extra] = input.split("/", 3);
|
|
52
|
+
if (
|
|
53
|
+
prefix === undefined ||
|
|
54
|
+
extra !== undefined ||
|
|
55
|
+
address.includes("%") ||
|
|
56
|
+
!cidrPrefix.test(prefix)
|
|
57
|
+
) {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const version = isIP(address);
|
|
62
|
+
return version !== 0 && Number(prefix) <= (version === 4 ? 32 : 128);
|
|
63
|
+
}
|