fix: prevent the redis client from init in build env

This commit is contained in:
Shahrad Elahi 2023-12-19 14:54:45 +03:30
parent fac671821a
commit 2fc2ab4259
5 changed files with 45 additions and 10 deletions

View File

@ -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",

View File

@ -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<string> {
},
AUTH_SECRET,
);
const client = getClient();
await client.setex(token, oneHour, '1');
return token;
}
@ -25,6 +26,7 @@ export async function verifyToken(token: string): Promise<boolean> {
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<boolean> {
}
export async function revokeToken(token: string): Promise<void> {
const client = getClient();
await client.del(token);
}

View File

@ -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`;

View File

@ -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,
}),
);
}

View File

@ -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<boolean> {
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<WgServer[]> {
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<number | undefined> {