This commit is contained in:
Shahrad Elahi 2023-10-08 13:21:11 +03:30
parent f765b71478
commit 43e5d9a5c8
3 changed files with 11 additions and 14 deletions

View File

@ -109,7 +109,7 @@ export class WGServer {
peer.persistentKeepalive && `PersistentKeepalive = ${peer.persistentKeepalive}` peer.persistentKeepalive && `PersistentKeepalive = ${peer.persistentKeepalive}`
])) ]))
await fs.writeFile(confPath, lines.join('\n')) 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) const index = await findServerIndex(id)
if (typeof index !== 'number') { if (typeof index !== 'number') {
@ -157,7 +157,7 @@ export class WGServer {
conf conf
const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n') const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n')
await fs.writeFile(confPath, `${serverConfStr}\n${peersStr}`) 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.stop(server.id)
await WGServer.start(server.id) await WGServer.start(server.id)
@ -217,7 +217,7 @@ export class WGServer {
const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n') const peersStr = peers.filter((_, i) => i !== peerIndex).join('\n')
await fs.writeFile(confPath, `${serverConfStr}\n${peersStr}`) 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<string | undefined> { static async getFreePeerIp(id: string): Promise<string | undefined> {
@ -468,7 +468,7 @@ export async function generateWgServer(config: {
await fs.writeFile(CONFIG_PATH, await genServerConf(server), { mode: 0o600 }) await fs.writeFile(CONFIG_PATH, await genServerConf(server), { mode: 0o600 })
// updating hash of the config // 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 // to ensure interface does not exists
await Shell.exec(`wg-quick down wg${confId}`, true) await Shell.exec(`wg-quick down wg${confId}`, true)
@ -480,13 +480,11 @@ export async function generateWgServer(config: {
return uuid return uuid
} }
export async function getConfigHash(id: string): Promise<string | undefined> { export async function getConfigHash(confId: number): Promise<string | undefined> {
const server = await findServer(id) if (!await wgConfExists(confId)) {
if (!server) {
console.error('getConfigHash: server not found')
return undefined 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') const conf = await fs.readFile(confPath, 'utf-8')
return CryptoJS.enc.Hex.stringify(SHA256(conf)); return CryptoJS.enc.Hex.stringify(SHA256(conf));
} }
@ -494,7 +492,7 @@ export async function getConfigHash(id: string): Promise<string | undefined> {
export async function writeConfigFile(wg: WgServer): Promise<void> { export async function writeConfigFile(wg: WgServer): Promise<void> {
const CONFIG_PATH = path.join(WG_PATH, `wg${wg.confId}.conf`) const CONFIG_PATH = path.join(WG_PATH, `wg${wg.confId}.conf`)
await fs.writeFile(CONFIG_PATH, await genServerConf(wg), { mode: 0o600 }) 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<number> { export async function maxConfId(): Promise<number> {

View File

@ -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 // More on how NextAuth.js middleware works: https://next-auth.js.org/configuration/nextjs#middleware
export default withAuth({ export default withAuth({
callbacks: { callbacks: {
authorized({ req, token }) { authorized({ token }) {
// `/me` only requires the user to be logged in
return !!token return !!token
}, },
}, },
@ -13,6 +12,6 @@ export default withAuth({
// See "Matching Paths" below to learn more // See "Matching Paths" below to learn more
export const config = { export const config = {
matcher: [ 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).*)',
], ],
} }

View File

@ -9,7 +9,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
for (const s of servers) { for (const s of servers) {
const HASH = await getConfigHash(s.id); const HASH = await getConfigHash(s.confId);
if (s.confId && s.confHash === HASH) { if (s.confId && s.confHash === HASH) {
// Skip, due to no changes on the config // Skip, due to no changes on the config
continue; continue;