From 2fc2ab4259d77d1c2091a4a53c7fc55ef3edb581 Mon Sep 17 00:00:00 2001 From: Shahrad Elahi Date: Tue, 19 Dec 2023 14:54:45 +0330 Subject: [PATCH] fix: prevent the redis client from init in build env --- web/package.json | 2 +- web/src/lib/auth.ts | 5 ++++- web/src/lib/constants.ts | 2 ++ web/src/lib/redis.ts | 28 +++++++++++++++++++++++----- web/src/lib/wireguard/index.ts | 18 +++++++++++++++--- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/web/package.json b/web/package.json index 43fe0c6..3789518 100644 --- a/web/package.json +++ b/web/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "vite dev", - "build": "vite build", + "build": "NODE_ENV=build vite build", "preview": "vite preview", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch", diff --git a/web/src/lib/auth.ts b/web/src/lib/auth.ts index f1f5682..49c84e7 100644 --- a/web/src/lib/auth.ts +++ b/web/src/lib/auth.ts @@ -1,7 +1,7 @@ import jwt from 'jsonwebtoken'; import { randomUUID } from 'node:crypto'; -import { client } from '$lib/redis'; import { sha256 } from '$lib/hash'; +import { getClient } from '$lib/redis'; import 'dotenv/config'; export const AUTH_SECRET = process.env.AUTH_SECRET || sha256(randomUUID()); @@ -17,6 +17,7 @@ export async function generateToken(): Promise { }, AUTH_SECRET, ); + const client = getClient(); await client.setex(token, oneHour, '1'); return token; } @@ -25,6 +26,7 @@ export async function verifyToken(token: string): Promise { try { const decode = jwt.verify(token, AUTH_SECRET); if (!decode) return false; + const client = getClient(); const exists = await client.exists(token); return exists === 1; } catch (e) { @@ -33,5 +35,6 @@ export async function verifyToken(token: string): Promise { } export async function revokeToken(token: string): Promise { + const client = getClient(); await client.del(token); } diff --git a/web/src/lib/constants.ts b/web/src/lib/constants.ts index 315ffe8..107d16b 100644 --- a/web/src/lib/constants.ts +++ b/web/src/lib/constants.ts @@ -1,3 +1,5 @@ export const WG_PATH = '/etc/wireguard'; export const IPV4_REGEX = new RegExp(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/); + +export const WG_SEVER_PATH = `WG::SERVERS`; diff --git a/web/src/lib/redis.ts b/web/src/lib/redis.ts index 62d4abc..78155fc 100644 --- a/web/src/lib/redis.ts +++ b/web/src/lib/redis.ts @@ -1,7 +1,25 @@ -import IORedis from 'ioredis'; +import { Redis } from 'ioredis'; -export const client = new IORedis({ - port: 6479, -}); +export type RedisClient = Redis; -export const WG_SEVER_PATH = `WG::SERVERS`; +let client: RedisClient | undefined; + +export function getClient(): RedisClient { + if (!client) { + throw new Error('Redis client not initialized'); + } + + return client; +} + +export function setClient(redis: RedisClient): void { + client = redis; +} + +if (process.env.NODE_ENV !== 'build') { + setClient( + new Redis({ + port: 6479, + }), + ); +} diff --git a/web/src/lib/wireguard/index.ts b/web/src/lib/wireguard/index.ts index 57bc03e..b4942cf 100644 --- a/web/src/lib/wireguard/index.ts +++ b/web/src/lib/wireguard/index.ts @@ -4,13 +4,13 @@ import deepmerge from 'deepmerge'; import type { Peer, WgKey, WgServer } from '$lib/typings'; import Network from '$lib/network'; import Shell from '$lib/shell'; -import { WG_PATH } from '$lib/constants'; -import { client, WG_SEVER_PATH } from '$lib/redis'; +import { WG_PATH, WG_SEVER_PATH } from '$lib/constants'; import { dynaJoin, isJson } from '$lib/utils'; import { getPeerConf } from '$lib/wireguard/utils'; import logger from '$lib/logger'; import { sha256 } from '$lib/hash'; import { fsAccess } from '$lib/fs-extra'; +import { getClient } from '$lib/redis'; export class WGServer { readonly id: string; @@ -90,6 +90,7 @@ export class WGServer { return true; } + const client = getClient(); const element = await client.lindex(WG_SEVER_PATH, index); if (!element) { logger.warn('remove: element not found'); @@ -110,6 +111,7 @@ export class WGServer { return true; } + const client = getClient(); const res = await client.lset( WG_SEVER_PATH, index, @@ -184,6 +186,7 @@ class WGPeers { return true; } + const client = getClient(); await client.lset( WG_SEVER_PATH, index, @@ -210,6 +213,8 @@ class WGPeers { logger.warn('findServerIndex: index not found'); return true; } + + const client = getClient(); await client.lset( WG_SEVER_PATH, index, @@ -254,6 +259,7 @@ class WGPeers { return deepmerge(p, update); }); + const client = getClient(); await client.lset(WG_SEVER_PATH, index, JSON.stringify({ ...server, peers: updatedPeers })); await this.storePeers(publicKey, updatedPeers); @@ -428,10 +434,13 @@ async function syncServers(): Promise { const confs = files.filter((f) => reg.test(f)); // read all confs const servers = await Promise.all(confs.map((f) => readWgConf(parseInt(f.match(reg)![1])))); + + const client = getClient(); // remove old servers await client.del(WG_SEVER_PATH); // save all servers to redis await client.lpush(WG_SEVER_PATH, ...servers.map((s) => JSON.stringify(s))); + return true; } @@ -503,6 +512,7 @@ export async function generateWgServer(config: GenerateWgServerParams): Promise< // save server config if (false !== config.insertDb) { + const client = getClient(); await client.lpush(WG_SEVER_PATH, JSON.stringify(server)); } @@ -582,7 +592,9 @@ export function maxConfId(): number { } export async function getServers(): Promise { - return (await client.lrange(WG_SEVER_PATH, 0, -1)).map((s) => JSON.parse(s)); + const client = getClient(); + const rawServers = await client.lrange(WG_SEVER_PATH, 0, -1); + return rawServers.map((s) => JSON.parse(s)); } export async function findServerIndex(id: string): Promise {