-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update VPNServer entity & add internal endpoint
- Loading branch information
Showing
14 changed files
with
749 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import express, { | ||
ErrorRequestHandler, | ||
NextFunction, | ||
Request, | ||
Response, | ||
} from "express"; | ||
import internal from "./internal"; | ||
import logger from "../utils/logger"; | ||
import { inspect } from "util"; | ||
|
||
export function getApi() { | ||
const app = express(); | ||
|
||
app.use(internal); | ||
|
||
app.use((( | ||
err: Error, | ||
_req: Request, | ||
res: Response, | ||
_next: () => NextFunction | ||
) => { | ||
logger.error(`[api] Unexpected error ${err.message} ${inspect(err)}`); | ||
res.status(500).send("Unknown error"); | ||
}) as ErrorRequestHandler); | ||
|
||
return app; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Request, Response } from "express"; | ||
import { VPNServerService } from "../../services/VPNServerService"; | ||
|
||
export async function getServers(_req: Request, res: Response): Promise<void> { | ||
const vpnServerService = VPNServerService.getService(); | ||
const vpnServers = await vpnServerService.getAll(); | ||
res.status(200).send(vpnServers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Router } from "express"; | ||
import jwt, { JwtPayload } from "jsonwebtoken"; | ||
import config from "../../config"; | ||
import logger from "../../utils/logger"; | ||
import { getServers } from "./getServers"; | ||
|
||
const router = Router(); | ||
|
||
router.use((req, res, next) => { | ||
try { | ||
const jwtData = jwt.verify( | ||
req.headers.authorization as string, | ||
config.api.internalJwtSecret | ||
) as JwtPayload; | ||
logger.info(`[Internal] ${req.method} ${req.path} (${jwtData.app})`); | ||
next(); | ||
} catch (e) { | ||
res.status(401).send("Unauthorized"); | ||
return; | ||
} | ||
}); | ||
|
||
router.get("/internal/servers", getServers); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import logger from "../utils/logger"; | ||
import { getApi } from "../api"; | ||
import config from "../config"; | ||
|
||
export async function initAPI(): Promise<void> { | ||
await new Promise<void>((resolve) => | ||
getApi().listen(config.api.port, resolve) | ||
); | ||
logger.info( | ||
`[api][initAPI] API initialized, listening on port ${config.api.port}` | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import pkg from "../package.json"; | ||
import "dotenv/config"; | ||
import pkg from "../package.json"; | ||
import { initBot } from "./entryPoints/bot"; | ||
import { initCron } from "./entryPoints/cron"; | ||
import { initSQS } from "./entryPoints/sqs"; | ||
import AppDataSource from "./orm/dataSource"; | ||
import logger from "./utils/logger"; | ||
import { initAPI } from "./entryPoints/api"; | ||
|
||
async function main() { | ||
logger.info(`[main] Starting ovpnrental(v${pkg.version})`); | ||
await AppDataSource.initialize(); | ||
await initCron(); | ||
await initBot(); | ||
await initSQS(); | ||
await initAPI(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Repository } from "typeorm"; | ||
import AppDataSource from "../dataSource"; | ||
import { VPNServer } from "../entities/VPNServer"; | ||
|
||
export class VPNServerDao { | ||
private vpnServerRepository: Repository<VPNServer>; | ||
|
||
public constructor() { | ||
this.vpnServerRepository = AppDataSource.getRepository(VPNServer); | ||
} | ||
|
||
public async getServers(): Promise<VPNServer[]> { | ||
return await this.vpnServerRepository.find(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class UpdateVPNServer1706360105938 implements MigrationInterface { | ||
name = "UpdateVPNServer1706360105938"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "vpn_server"`); | ||
await queryRunner.query( | ||
`CREATE TABLE "vpn_server" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "host" varchar NOT NULL, "countryIsoCode" varchar NOT NULL, "agentQueueName" varchar NOT NULL, "tags" json NOT NULL)` | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "vpn_server"`); | ||
await queryRunner.query( | ||
`CREATE TABLE "vpn_server" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "ipAddress" varchar NOT NULL, "name" integer NOT NULL, "region" varchar NOT NULL, "agentVersionInstalled" varchar NOT NULL, "agentInstalledAt" varchar NOT NULL)` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Dao } from "../../orm/dao"; | ||
import { VPNServerDao } from "../../orm/dao/VPNServerDao"; | ||
import { VPNServer } from "../../orm/entities/VPNServer"; | ||
|
||
export class VPNServerService { | ||
private vpnServerDao: VPNServerDao; | ||
constructor(vpnServerDao: VPNServerDao) { | ||
this.vpnServerDao = vpnServerDao; | ||
} | ||
|
||
public static getService(): VPNServerService { | ||
const vpnServerDao = Dao.getDao<VPNServerDao>(VPNServer); | ||
return new VPNServerService(vpnServerDao); | ||
} | ||
|
||
public async getAll(): Promise<Array<VPNServer>> { | ||
return this.vpnServerDao.getServers(); | ||
} | ||
} |
Oops, something went wrong.