feat: proxy services

This commit is contained in:
2024-08-28 15:26:30 +03:00
parent 74d099a130
commit ca91420a57
9 changed files with 105 additions and 8172 deletions

View File

@@ -1,9 +1,8 @@
FROM node:lts-alpine FROM oven/bun:latest
WORKDIR /app WORKDIR /app
RUN corepack enable && corepack prepare pnpm@latest --activate COPY package.json bun.lockb ./
COPY package.json pnpm-*.yaml ./ RUN bun install --frozen-lockfile
RUN pnpm install --frozen-lockfile
COPY . . COPY . .
RUN pnpm build RUN bun run build
EXPOSE 3000 EXPOSE 3000
CMD [ "pnpm", "start:prod" ] CMD [ "bun", "run", "start:prod" ]

View File

@@ -0,0 +1,17 @@
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm";
@Entity()
export class ProxyInfo {
constructor(props?: Partial<ProxyInfo>) {
Object.assign(this, props);
}
@PrimaryGeneratedColumn("uuid")
public uuid!: string;
@Column()
public enabled: boolean;
@Column()
public text: string;
}

View File

@@ -36,4 +36,7 @@ export class ProxyUser {
@OneToMany(() => Payment, (payment) => payment.user) @OneToMany(() => Payment, (payment) => payment.user)
public payments: Payment[]; public payments: Payment[];
@Column()
public tg_id!: string;
} }

View File

@@ -4,13 +4,24 @@ import { Admin } from "./database/admin.entity";
import { Image } from "./database/image.entity"; import { Image } from "./database/image.entity";
import { Payment } from "./database/payment.entity"; import { Payment } from "./database/payment.entity";
import { Post } from "./database/post.entity"; import { Post } from "./database/post.entity";
import { ProxyInfo } from "./database/proxy_info";
import { ProxyUser } from "./database/proxy_user.entity"; import { ProxyUser } from "./database/proxy_user.entity";
import { BotSettings } from "./database/settings.entity"; import { BotSettings } from "./database/settings.entity";
import { User } from "./database/user.entity"; import { User } from "./database/user.entity";
import { WebUser } from "./database/web_user.entity"; import { WebUser } from "./database/web_user.entity";
@Module({ @Module({
imports: [ imports: [
TypeOrmModule.forFeature([User, Admin, Post, Image, Payment, ProxyUser, BotSettings, WebUser]), TypeOrmModule.forFeature([
User,
Admin,
Post,
Image,
Payment,
ProxyUser,
BotSettings,
WebUser,
ProxyInfo,
]),
], ],
exports: [TypeOrmModule], exports: [TypeOrmModule],
}) })

8164
backend/pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
import { Body, Controller, Get, Param, Post } from "@nestjs/common"; import { Body, Controller, Get, Param, Post } from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger"; import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { IOperation, IProxyUser } from "./proxy.dto"; import { IOperation, IProxyInfoEnabled, IProxyInfoText, IProxyUser } from "./proxy.dto";
import { ProxyService } from "./proxy.service"; import { ProxyService } from "./proxy.service";
@ApiTags("Proxy") @ApiTags("Proxy")
@@ -43,4 +43,22 @@ export class ProxyController {
async getAllOperations() { async getAllOperations() {
return this.proxyService.getAllOperations(); return this.proxyService.getAllOperations();
} }
@ApiOperation({ description: "get current info" })
@Get("info")
async getProxyInfo() {
return this.proxyService.getProxyInfo();
}
@ApiOperation({ description: "set proxy info text" })
@Post("set-info-text")
async setProxyInfoText(@Body() data: IProxyInfoText) {
return this.proxyService.setProxyInfoText(data.text);
}
@ApiOperation({ description: "set proxy info status" })
@Post("set-info-status")
async setProxyInfoStatus(@Body() data: IProxyInfoEnabled) {
return this.proxyService.setProxyInfoStatus(data.enabled);
}
} }

View File

@@ -9,9 +9,21 @@ export class IProxyUser {
readonly link!: string; readonly link!: string;
@ApiProperty({ description: "telegram user id to connect to user entity", example: "187564" }) @ApiProperty({ description: "telegram user id to connect to user entity", example: "187564" })
readonly user_id?: string; readonly user_id?: string;
@ApiProperty({ description: "telegram user id", example: "111111" })
readonly tg_id: string;
} }
export class IOperation { export class IOperation {
@ApiProperty({ description: "user name of user, that made new operation", example: "username" }) @ApiProperty({ description: "user name of user, that made new operation", example: "username" })
readonly userName!: string; readonly userName!: string;
} }
export class IProxyInfoText {
@ApiProperty({ description: "proxy info text", example: "Working normal" })
readonly text!: string;
}
export class IProxyInfoEnabled {
@ApiProperty({ description: "proxy info enabled", example: true })
readonly enabled!: boolean;
}

View File

@@ -1,6 +1,7 @@
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common"; import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm"; import { InjectRepository } from "@nestjs/typeorm";
import { Payment } from "libs/database/payment.entity"; import { Payment } from "libs/database/payment.entity";
import { ProxyInfo } from "libs/database/proxy_info";
import { ProxyUser } from "libs/database/proxy_user.entity"; import { ProxyUser } from "libs/database/proxy_user.entity";
import { User } from "libs/database/user.entity"; import { User } from "libs/database/user.entity";
import { Repository } from "typeorm"; import { Repository } from "typeorm";
@@ -13,6 +14,7 @@ export class ProxyService {
@InjectRepository(ProxyUser) private proxyUserRepository: Repository<ProxyUser>, @InjectRepository(ProxyUser) private proxyUserRepository: Repository<ProxyUser>,
@InjectRepository(Payment) private paymentRepository: Repository<Payment>, @InjectRepository(Payment) private paymentRepository: Repository<Payment>,
@InjectRepository(User) private userRepository: Repository<User>, @InjectRepository(User) private userRepository: Repository<User>,
@InjectRepository(ProxyInfo) private proxyInfoRepository: Repository<ProxyInfo>,
) {} ) {}
async newUser(data: IProxyUser) { async newUser(data: IProxyUser) {
@@ -33,6 +35,7 @@ export class ProxyService {
proxyUser.connectDate = new Date(); proxyUser.connectDate = new Date();
proxyUser.userName = data.userName; proxyUser.userName = data.userName;
proxyUser.link = data.link; proxyUser.link = data.link;
proxyUser.tg_id = data.tg_id;
return await this.proxyUserRepository.save(proxyUser); return await this.proxyUserRepository.save(proxyUser);
} catch (error) { } catch (error) {
if (error instanceof HttpException) { if (error instanceof HttpException) {
@@ -109,4 +112,38 @@ export class ProxyService {
throw new HttpException("Bad data", HttpStatus.BAD_REQUEST); throw new HttpException("Bad data", HttpStatus.BAD_REQUEST);
} }
} }
async getProxyInfo() {
try {
return (await this.proxyInfoRepository.find({ select: { enabled: true, text: true } }))[0];
} catch (error) {
this.logger.debug(`[proxy.getProxyInfo] error: ${error}`);
throw new HttpException("Bad data", HttpStatus.BAD_REQUEST);
}
}
async setProxyInfoText(text: string) {
this.logger.log(`[proxy.setProxyInfoText] Data: ${text}`);
try {
const info = (await this.proxyInfoRepository.find())[0];
info.text = text;
return await this.proxyInfoRepository.save(info);
} catch (error) {
this.logger.debug(`[proxy.setProxyInfoText] error: ${error}`);
throw new HttpException("Bad data", HttpStatus.BAD_REQUEST);
}
}
async setProxyInfoStatus(status: boolean) {
this.logger.log(`[proxy.setProxyInfoStatus] Data: ${status}`);
try {
const info = (await this.proxyInfoRepository.find())[0];
info.enabled = status;
return this.proxyInfoRepository.save(info);
} catch (error) {
this.logger.debug(`[proxy.setProxyInfoStatus] error: ${error}`);
throw new HttpException("Bad data", HttpStatus.BAD_REQUEST);
}
}
} }

BIN
bun.lockb

Binary file not shown.