mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-01 00:06:38 +00:00
improve site regenerate cred ui
This commit is contained in:
61
src/lib/wireguard.ts
Normal file
61
src/lib/wireguard.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
export function generateWireGuardConfig(
|
||||
privateKey: string,
|
||||
publicKey: string,
|
||||
subnet: string,
|
||||
address: string,
|
||||
endpoint: string,
|
||||
listenPort: string | number
|
||||
): string {
|
||||
const addressWithoutCidr = address.split("/")[0];
|
||||
const port = typeof listenPort === "number" ? listenPort : listenPort;
|
||||
|
||||
return `[Interface]
|
||||
Address = ${subnet}
|
||||
ListenPort = 51820
|
||||
PrivateKey = ${privateKey}
|
||||
|
||||
[Peer]
|
||||
PublicKey = ${publicKey}
|
||||
AllowedIPs = ${addressWithoutCidr}/32
|
||||
Endpoint = ${endpoint}:${port}
|
||||
PersistentKeepalive = 5`;
|
||||
}
|
||||
|
||||
export function generateObfuscatedWireGuardConfig(options?: {
|
||||
subnet?: string | null;
|
||||
address?: string | null;
|
||||
endpoint?: string | null;
|
||||
listenPort?: number | string | null;
|
||||
publicKey?: string | null;
|
||||
}): string {
|
||||
const obfuscate = (value: string | null | undefined, length: number = 20): string => {
|
||||
return value || "•".repeat(length);
|
||||
};
|
||||
|
||||
const obfuscateKey = (value: string | null | undefined): string => {
|
||||
return value || "•".repeat(44); // Base64 key length
|
||||
};
|
||||
|
||||
const subnet = options?.subnet || obfuscate(null, 20);
|
||||
const subnetWithCidr = subnet.includes("•")
|
||||
? `${subnet}/32`
|
||||
: (subnet.includes("/") ? subnet : `${subnet}/32`);
|
||||
const address = options?.address ? options.address.split("/")[0] : obfuscate(null, 20);
|
||||
const endpoint = obfuscate(options?.endpoint, 20);
|
||||
const listenPort = options?.listenPort
|
||||
? (typeof options.listenPort === "number" ? options.listenPort : options.listenPort)
|
||||
: 51820;
|
||||
const publicKey = obfuscateKey(options?.publicKey);
|
||||
|
||||
return `[Interface]
|
||||
Address = ${subnetWithCidr}
|
||||
ListenPort = 51820
|
||||
PrivateKey = ${obfuscateKey(null)}
|
||||
|
||||
[Peer]
|
||||
PublicKey = ${publicKey}
|
||||
AllowedIPs = ${address}/32
|
||||
Endpoint = ${endpoint}:${listenPort}
|
||||
PersistentKeepalive = 5`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user