Matheus Sampaio Queiroga
e6cab72032
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { NextRequest } from "next/server";
|
|
import { wgServer } from "@db/server";
|
|
import { wgPeer } from "@db/peers";
|
|
import { key, wgQuick } from "wireguard-tools.js";
|
|
import { isIPv6 } from "net";
|
|
|
|
const {
|
|
WG_HOSTNAME = "localhost",
|
|
} = process.env;
|
|
|
|
export async function GET(req: NextRequest, {params: { id }}) {
|
|
const peer = await wgPeer.findOne({ where: { id: parseInt(id) } });
|
|
if (!peer) return new Response(JSON.stringify({ message: "Peer not exists" }, null, 2), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
const interfaceInfo = await wgServer.findOne({ where: { id: (await peer).interfaceOwner } });
|
|
if (!interfaceInfo) return new Response(JSON.stringify({ message: "Interface not exists" }, null, 2), { status: 400, headers: { "Content-Type": "application/json" } });
|
|
|
|
const type = String(req.nextUrl.searchParams.get("type")||"quick").toLowerCase();
|
|
const wgConfig: wgQuick.QuickConfig = {
|
|
privateKey: peer.privateKey,
|
|
DNS: [ "8.8.8.8", "1.1.1.1", "8.8.4.4", "1.0.0.1" ],
|
|
Address: [
|
|
peer.IPv4,
|
|
peer.IPv6
|
|
],
|
|
peers: {
|
|
[key.publicKey(interfaceInfo.privateKey)]: {
|
|
presharedKey: peer.presharedKey,
|
|
endpoint: `${isIPv6(WG_HOSTNAME) ? String.prototype.concat("[", WG_HOSTNAME,"]") : WG_HOSTNAME}:${interfaceInfo.portListen}`,
|
|
allowedIPs: ["0.0.0.0/0", "::/0"]
|
|
}
|
|
}
|
|
};
|
|
|
|
if (type === "json") return Response.json(wgConfig);
|
|
|
|
return new Response(wgQuick.stringify(wgConfig), {
|
|
status: 200,
|
|
headers: {
|
|
"Cotent-Type": "text/plain",
|
|
"Content-Disposition": `inline; filename="peer_${peer.interfaceOwner}_${peer.id}.conf"`
|
|
}
|
|
});
|
|
} |