mirror of
https://github.com/wireadmin/wireadmin
synced 2025-02-26 05:48:44 +00:00
fix: prevent the redis client from init in build env
This commit is contained in:
parent
fac671821a
commit
2fc2ab4259
@ -4,7 +4,7 @@
|
|||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "vite dev",
|
"dev": "vite dev",
|
||||||
"build": "vite build",
|
"build": "NODE_ENV=build vite build",
|
||||||
"preview": "vite preview",
|
"preview": "vite preview",
|
||||||
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
|
||||||
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import jwt from 'jsonwebtoken';
|
import jwt from 'jsonwebtoken';
|
||||||
import { randomUUID } from 'node:crypto';
|
import { randomUUID } from 'node:crypto';
|
||||||
import { client } from '$lib/redis';
|
|
||||||
import { sha256 } from '$lib/hash';
|
import { sha256 } from '$lib/hash';
|
||||||
|
import { getClient } from '$lib/redis';
|
||||||
import 'dotenv/config';
|
import 'dotenv/config';
|
||||||
|
|
||||||
export const AUTH_SECRET = process.env.AUTH_SECRET || sha256(randomUUID());
|
export const AUTH_SECRET = process.env.AUTH_SECRET || sha256(randomUUID());
|
||||||
@ -17,6 +17,7 @@ export async function generateToken(): Promise<string> {
|
|||||||
},
|
},
|
||||||
AUTH_SECRET,
|
AUTH_SECRET,
|
||||||
);
|
);
|
||||||
|
const client = getClient();
|
||||||
await client.setex(token, oneHour, '1');
|
await client.setex(token, oneHour, '1');
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
@ -25,6 +26,7 @@ export async function verifyToken(token: string): Promise<boolean> {
|
|||||||
try {
|
try {
|
||||||
const decode = jwt.verify(token, AUTH_SECRET);
|
const decode = jwt.verify(token, AUTH_SECRET);
|
||||||
if (!decode) return false;
|
if (!decode) return false;
|
||||||
|
const client = getClient();
|
||||||
const exists = await client.exists(token);
|
const exists = await client.exists(token);
|
||||||
return exists === 1;
|
return exists === 1;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -33,5 +35,6 @@ export async function verifyToken(token: string): Promise<boolean> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function revokeToken(token: string): Promise<void> {
|
export async function revokeToken(token: string): Promise<void> {
|
||||||
|
const client = getClient();
|
||||||
await client.del(token);
|
await client.del(token);
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
export const WG_PATH = '/etc/wireguard';
|
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 IPV4_REGEX = new RegExp(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/);
|
||||||
|
|
||||||
|
export const WG_SEVER_PATH = `WG::SERVERS`;
|
||||||
|
@ -1,7 +1,25 @@
|
|||||||
import IORedis from 'ioredis';
|
import { Redis } from 'ioredis';
|
||||||
|
|
||||||
export const client = new IORedis({
|
export type RedisClient = Redis;
|
||||||
port: 6479,
|
|
||||||
});
|
|
||||||
|
|
||||||
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,
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
@ -4,13 +4,13 @@ import deepmerge from 'deepmerge';
|
|||||||
import type { Peer, WgKey, WgServer } from '$lib/typings';
|
import type { Peer, WgKey, WgServer } from '$lib/typings';
|
||||||
import Network from '$lib/network';
|
import Network from '$lib/network';
|
||||||
import Shell from '$lib/shell';
|
import Shell from '$lib/shell';
|
||||||
import { WG_PATH } from '$lib/constants';
|
import { WG_PATH, WG_SEVER_PATH } from '$lib/constants';
|
||||||
import { client, WG_SEVER_PATH } from '$lib/redis';
|
|
||||||
import { dynaJoin, isJson } from '$lib/utils';
|
import { dynaJoin, isJson } from '$lib/utils';
|
||||||
import { getPeerConf } from '$lib/wireguard/utils';
|
import { getPeerConf } from '$lib/wireguard/utils';
|
||||||
import logger from '$lib/logger';
|
import logger from '$lib/logger';
|
||||||
import { sha256 } from '$lib/hash';
|
import { sha256 } from '$lib/hash';
|
||||||
import { fsAccess } from '$lib/fs-extra';
|
import { fsAccess } from '$lib/fs-extra';
|
||||||
|
import { getClient } from '$lib/redis';
|
||||||
|
|
||||||
export class WGServer {
|
export class WGServer {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
@ -90,6 +90,7 @@ export class WGServer {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
const element = await client.lindex(WG_SEVER_PATH, index);
|
const element = await client.lindex(WG_SEVER_PATH, index);
|
||||||
if (!element) {
|
if (!element) {
|
||||||
logger.warn('remove: element not found');
|
logger.warn('remove: element not found');
|
||||||
@ -110,6 +111,7 @@ export class WGServer {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
const res = await client.lset(
|
const res = await client.lset(
|
||||||
WG_SEVER_PATH,
|
WG_SEVER_PATH,
|
||||||
index,
|
index,
|
||||||
@ -184,6 +186,7 @@ class WGPeers {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
await client.lset(
|
await client.lset(
|
||||||
WG_SEVER_PATH,
|
WG_SEVER_PATH,
|
||||||
index,
|
index,
|
||||||
@ -210,6 +213,8 @@ class WGPeers {
|
|||||||
logger.warn('findServerIndex: index not found');
|
logger.warn('findServerIndex: index not found');
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
await client.lset(
|
await client.lset(
|
||||||
WG_SEVER_PATH,
|
WG_SEVER_PATH,
|
||||||
index,
|
index,
|
||||||
@ -254,6 +259,7 @@ class WGPeers {
|
|||||||
return deepmerge(p, update);
|
return deepmerge(p, update);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
await client.lset(WG_SEVER_PATH, index, JSON.stringify({ ...server, peers: updatedPeers }));
|
await client.lset(WG_SEVER_PATH, index, JSON.stringify({ ...server, peers: updatedPeers }));
|
||||||
await this.storePeers(publicKey, updatedPeers);
|
await this.storePeers(publicKey, updatedPeers);
|
||||||
|
|
||||||
@ -428,10 +434,13 @@ async function syncServers(): Promise<boolean> {
|
|||||||
const confs = files.filter((f) => reg.test(f));
|
const confs = files.filter((f) => reg.test(f));
|
||||||
// read all confs
|
// read all confs
|
||||||
const servers = await Promise.all(confs.map((f) => readWgConf(parseInt(f.match(reg)![1]))));
|
const servers = await Promise.all(confs.map((f) => readWgConf(parseInt(f.match(reg)![1]))));
|
||||||
|
|
||||||
|
const client = getClient();
|
||||||
// remove old servers
|
// remove old servers
|
||||||
await client.del(WG_SEVER_PATH);
|
await client.del(WG_SEVER_PATH);
|
||||||
// save all servers to redis
|
// save all servers to redis
|
||||||
await client.lpush(WG_SEVER_PATH, ...servers.map((s) => JSON.stringify(s)));
|
await client.lpush(WG_SEVER_PATH, ...servers.map((s) => JSON.stringify(s)));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -503,6 +512,7 @@ export async function generateWgServer(config: GenerateWgServerParams): Promise<
|
|||||||
|
|
||||||
// save server config
|
// save server config
|
||||||
if (false !== config.insertDb) {
|
if (false !== config.insertDb) {
|
||||||
|
const client = getClient();
|
||||||
await client.lpush(WG_SEVER_PATH, JSON.stringify(server));
|
await client.lpush(WG_SEVER_PATH, JSON.stringify(server));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -582,7 +592,9 @@ export function maxConfId(): number {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getServers(): Promise<WgServer[]> {
|
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> {
|
export async function findServerIndex(id: string): Promise<number | undefined> {
|
||||||
|
Loading…
Reference in New Issue
Block a user