Skip to content

Commit 1dce6eb

Browse files
committed
feat: add database seeding to docker entrypoint and package scripts
- Updated docker-entrypoint.sh to include database seeding step - Added migrate:seed script to package.json - Configured Prisma seed script in package.json - Removed hardcoded Xray config file - Enhanced XRay config service to require config from database - Updated XRay config validator to support additional protocols
1 parent b1a4ed4 commit 1dce6eb

File tree

6 files changed

+108
-73
lines changed

6 files changed

+108
-73
lines changed

configs/xray/config/xray_config.json

Lines changed: 0 additions & 59 deletions
This file was deleted.

docker-entrypoint.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ echo "Starting entrypoint script..."
55
echo "Migrating database..."
66
npm run migrate:deploy
77

8+
echo "Seeding database..."
9+
npm run migrate:seed
10+
811
echo "Starting pm2..."
912
pm2-runtime start ecosystem.config.js --env production
1013

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"bugs": {
1515
"url": "https://github.com/remnawave/backend/issues"
1616
},
17+
"prisma": {
18+
"seed": "ts-node prisma/seed/config.seed.ts"
19+
},
1720
"scripts": {
1821
"build": "nest build",
1922
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
@@ -30,6 +33,7 @@
3033
"migrate:dev": "prisma migrate dev",
3134
"migrate:generate": "prisma generate",
3235
"migrate:deploy": "PRISMA_HIDE_UPDATE_MESSAGE=true prisma migrate deploy",
36+
"migrate:seed": "prisma db seed",
3337
"docs:serve": "compodoc -p tsconfig.json -s",
3438
"generate:openapi": "nest start --config nest-cli.gen-doc.json"
3539
},

prisma/seed/config.seed.ts

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { PrismaClient } from '@prisma/client';
2+
3+
export const XTLSDefaultConfig = {
4+
log: {
5+
loglevel: 'info',
6+
},
7+
inbounds: [
8+
{
9+
tag: 'Shadowsocks',
10+
port: 1234,
11+
protocol: 'shadowsocks',
12+
settings: {
13+
clients: [],
14+
network: 'tcp,udp',
15+
},
16+
sniffing: {
17+
enabled: true,
18+
destOverride: ['http', 'tls', 'quic'],
19+
},
20+
},
21+
],
22+
outbounds: [
23+
{
24+
protocol: 'freedom',
25+
tag: 'DIRECT',
26+
},
27+
{
28+
protocol: 'blackhole',
29+
tag: 'BLOCK',
30+
},
31+
],
32+
routing: {
33+
rules: [
34+
{
35+
ip: ['geoip:private'],
36+
outboundTag: 'BLOCK',
37+
type: 'field',
38+
},
39+
{
40+
domain: ['geosite:private'],
41+
outboundTag: 'BLOCK',
42+
type: 'field',
43+
},
44+
{
45+
protocol: ['bittorrent'],
46+
outboundTag: 'BLOCK',
47+
type: 'field',
48+
},
49+
],
50+
},
51+
};
52+
53+
const prisma = new PrismaClient({
54+
datasources: {
55+
db: {
56+
url: process.env.DATABASE_URL,
57+
},
58+
},
59+
});
60+
61+
async function seedConfigVariables() {
62+
const existingConfig = await prisma.xrayConfig.findFirst();
63+
64+
if (existingConfig) {
65+
console.log('Default XTLS config already seeded!');
66+
return;
67+
}
68+
69+
const config = await prisma.xrayConfig.create({
70+
data: {
71+
config: XTLSDefaultConfig,
72+
},
73+
});
74+
75+
if (!config) {
76+
throw new Error('Failed to create default config!');
77+
}
78+
79+
console.log('Default XTLS config seeded!');
80+
}
81+
82+
seedConfigVariables()
83+
.then(async () => {
84+
await prisma.$disconnect();
85+
})
86+
.catch(async (e) => {
87+
console.error(e);
88+
await prisma.$disconnect();
89+
process.exit(1);
90+
});

src/common/helpers/xray-config/xray-config.validator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ export class XRayConfig {
7676
);
7777
}
7878

79+
if (!['shadowsocks', 'trojan', 'vless'].includes(inbound.protocol)) {
80+
throw new Error(
81+
`Invalid protocol in inbound "${inbound.tag}". Allowed values are: shadowsocks, trojan, vless`,
82+
);
83+
}
84+
7985
// console.log(`Inbound ${inbound.tag} network: ${network || 'not set'}`);
8086
}
8187

src/modules/xray-config/xray-config.service.ts

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import fs from 'node:fs/promises';
2-
import path from 'node:path';
3-
41
import { ERRORS } from '@contract/constants';
52

63
import { CommandBus, EventBus, QueryBus } from '@nestjs/cqrs';
74
import { Injectable, Logger } from '@nestjs/common';
85

96
import { ICommandResponse } from '@common/types/command-response.type';
107
import { IXrayConfig } from '@common/helpers/xray-config/interfaces';
11-
import { isDevelopment } from '@common/utils/startup-app';
128
import { XRayConfig } from '@common/helpers/xray-config';
139

1410
import { UpdateInboundCommand } from '@modules/inbounds/commands/update-inbound';
@@ -27,18 +23,13 @@ import { XrayConfigEntity } from './entities/xray-config.entity';
2723
@Injectable()
2824
export class XrayConfigService {
2925
private readonly logger = new Logger(XrayConfigService.name);
30-
private readonly configPath: string;
3126

3227
constructor(
3328
private readonly xrayConfigRepository: XrayConfigRepository,
3429
private readonly commandBus: CommandBus,
3530
private readonly queryBus: QueryBus,
3631
private readonly eventBus: EventBus,
37-
) {
38-
this.configPath = isDevelopment()
39-
? path.join(__dirname, '../../../../configs/xray/config/xray_config.json')
40-
: path.join('/var/lib/remnawave/configs/xray/config/xray_config.json');
41-
}
32+
) {}
4233

4334
public async updateConfig(config: object): Promise<ICommandResponse<XrayConfigEntity>> {
4435
try {
@@ -130,8 +121,8 @@ export class XrayConfigService {
130121
try {
131122
let config: object | string;
132123
const dbConfig = await this.xrayConfigRepository.findFirst();
133-
if (!dbConfig?.config) {
134-
config = await fs.readFile(this.configPath, 'utf-8');
124+
if (!dbConfig || !dbConfig.config) {
125+
throw new Error('No XTLS config found in DB!');
135126
} else {
136127
config = dbConfig.config;
137128
}
@@ -164,8 +155,8 @@ export class XrayConfigService {
164155
try {
165156
let config: object | string;
166157
const dbConfig = await this.xrayConfigRepository.findFirst();
167-
if (!dbConfig?.config) {
168-
config = await fs.readFile(this.configPath, 'utf-8');
158+
if (!dbConfig || !dbConfig.config) {
159+
throw new Error('No XTLS config found in DB!');
169160
} else {
170161
config = dbConfig.config;
171162
}

0 commit comments

Comments
 (0)