From 43e5d9a5c89e276e2526e762731faeaaddbf60ce Mon Sep 17 00:00:00 2001 From: Shahrad Elahi Date: Sun, 8 Oct 2023 13:21:11 +0330 Subject: [PATCH] fix --- src/lib/wireguard.ts | 18 ++++++++---------- src/middleware.ts | 5 ++--- src/pages/api/wireguard/healthcheck.ts | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/lib/wireguard.ts b/src/lib/wireguard.ts index 9b171ad..6221440 100644 --- a/src/lib/wireguard.ts +++ b/src/lib/wireguard.ts @@ -109,7 +109,7 @@ export class WGServer { peer.persistentKeepalive && `PersistentKeepalive = ${peer.persistentKeepalive}` ])) await fs.writeFile(confPath, lines.join('\n')) - await WGServer.update(id, { confHash: await getConfigHash(id) }); + await WGServer.update(id, { confHash: await getConfigHash(server.confId) }); const index = await findServerIndex(id) if (typeof index !== 'number') { @@ -157,7 +157,7 @@ export class WGServer { conf const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n') await fs.writeFile(confPath, `${serverConfStr}\n${peersStr}`) - await WGServer.update(server.id, { confHash: await getConfigHash(server.id) }); + await WGServer.update(server.id, { confHash: await getConfigHash(server.confId) }); await WGServer.stop(server.id) await WGServer.start(server.id) @@ -217,7 +217,7 @@ export class WGServer { const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n') await fs.writeFile(confPath, `${serverConfStr}\n${peersStr}`) - await WGServer.update(sd.id, { confHash: await getConfigHash(sd.id) }); + await WGServer.update(sd.id, { confHash: await getConfigHash(sd.confId) }); } static async getFreePeerIp(id: string): Promise { @@ -468,7 +468,7 @@ export async function generateWgServer(config: { await fs.writeFile(CONFIG_PATH, await genServerConf(server), { mode: 0o600 }) // updating hash of the config - await WGServer.update(uuid, { confHash: await getConfigHash(uuid) }); + await WGServer.update(uuid, { confHash: await getConfigHash(confId) }); // to ensure interface does not exists await Shell.exec(`wg-quick down wg${confId}`, true) @@ -480,13 +480,11 @@ export async function generateWgServer(config: { return uuid } -export async function getConfigHash(id: string): Promise { - const server = await findServer(id) - if (!server) { - console.error('getConfigHash: server not found') +export async function getConfigHash(confId: number): Promise { + if (!await wgConfExists(confId)) { return undefined } - const confPath = path.join(WG_PATH, `wg${server.confId}.conf`) + const confPath = path.join(WG_PATH, `wg${confId}.conf`) const conf = await fs.readFile(confPath, 'utf-8') return CryptoJS.enc.Hex.stringify(SHA256(conf)); } @@ -494,7 +492,7 @@ export async function getConfigHash(id: string): Promise { export async function writeConfigFile(wg: WgServer): Promise { const CONFIG_PATH = path.join(WG_PATH, `wg${wg.confId}.conf`) await fs.writeFile(CONFIG_PATH, await genServerConf(wg), { mode: 0o600 }) - await WGServer.update(wg.id, { confHash: await getConfigHash(wg.id) }); + await WGServer.update(wg.id, { confHash: await getConfigHash(wg.confId) }); } export async function maxConfId(): Promise { diff --git a/src/middleware.ts b/src/middleware.ts index 6fdd33a..213c2e7 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -3,8 +3,7 @@ import { withAuth } from "next-auth/middleware"; // More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware export default withAuth({ callbacks: { - authorized({ req, token }) { - // `/me` only requires the user to be logged in + authorized({ token }) { return !!token }, }, @@ -13,6 +12,6 @@ export default withAuth({ // See "Matching Paths" below to learn more export const config = { matcher: [ - '/((?!api/auth|login|logo.png|fonts|_next/static|_next/image|favicon.ico).*)' + '/((?!api/auth|api/wireguard/healthcheck|api/ping|login|logo.png|fonts|_next/static|_next/image|favicon.ico).*)', ], } \ No newline at end of file diff --git a/src/pages/api/wireguard/healthcheck.ts b/src/pages/api/wireguard/healthcheck.ts index 5bf03ce..970bf54 100644 --- a/src/pages/api/wireguard/healthcheck.ts +++ b/src/pages/api/wireguard/healthcheck.ts @@ -9,7 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse) for (const s of servers) { - const HASH = await getConfigHash(s.id); + const HASH = await getConfigHash(s.confId); if (s.confId && s.confHash === HASH) { // Skip, due to no changes on the config continue;