Skip to content

Commit

Permalink
#21 DB service object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Sep 21, 2023
1 parent f0d22bc commit 40fc7ec
Show file tree
Hide file tree
Showing 17 changed files with 198 additions and 91 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ In the classic sense, FlyingFish is a nginx manager. But the way the nginx serve
## Quick setup
[Read the Documentation quick setup](https://flying-fish.gitbook.io/flyingfish/index/installation#quick-setup)

## In progress

[Version 1.0.24](https://github.com/stefanwerfling/flyingfish/milestone/1)

## Docker Hub


Expand Down
11 changes: 2 additions & 9 deletions backend/src/Routes/Main/Dashboard/Info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper, IpBlacklistServiceDB} from 'flyingfish_core';
import {IpBlacklistServiceDB, IpLocationServiceDB} from 'flyingfish_core';
import {DashboardInfoIpBlock, DashboardInfoResponse, StatusCodes} from 'flyingfish_schemas';
import {IpLocation as IpLocationDB} from '../../../inc/Db/MariaDb/Entity/IpLocation.js';
import {HimHIP} from '../../../inc/HimHIP/HimHIP.js';
import {HowIsMyPublicIpService} from '../../../inc/Service/HowIsMyPublicIpService.js';
import {IpService} from '../../../inc/Service/IpService.js';
Expand All @@ -14,8 +13,6 @@ export class Info {
* getInfo
*/
public static async getInfo(): Promise<DashboardInfoResponse> {
const ipLocationRepository = DBHelper.getRepository(IpLocationDB);

// ip blocks ---------------------------------------------------------------------------------------------------

const ipblocks: DashboardInfoIpBlock[] = [];
Expand All @@ -27,11 +24,7 @@ export class Info {

if (entries) {
for await (const entry of entries) {
const tlocation = await ipLocationRepository.findOne({
where: {
id: entry.ip_location_id
}
});
const tlocation = await IpLocationServiceDB.getInstance().findOne(entry.ip_location_id);

if (tlocation) {
ipblocks.push({
Expand Down
15 changes: 6 additions & 9 deletions backend/src/Routes/Main/IpAccess/Blacklist/Import/List.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {DBHelper, IpBlacklistCategoryServiceDB, IpBlacklistServiceDB} from 'flyingfish_core';
import {
IpBlacklistCategoryServiceDB,
IpBlacklistMaintainerServiceDB,
IpBlacklistServiceDB
} from 'flyingfish_core';
import {IpAccessBlackListImport, IpAccessBlackListImportsResponse, StatusCodes} from 'flyingfish_schemas';
import {UtilsLocation} from '../../UtilsLocation.js';
import {IpBlacklistMaintainer as IpBlacklistMaintainerDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklistMaintainer.js';

/**
* List
Expand All @@ -14,8 +17,6 @@ export class List {
public static async getBlackListImports(): Promise<IpAccessBlackListImportsResponse> {
const limit = 20;

const ipBlacklistMaintainerRepository = DBHelper.getRepository(IpBlacklistMaintainerDB);

const entries = await IpBlacklistServiceDB.getInstance().findAllImported(limit, 'DESC');

const list: IpAccessBlackListImport[] = [];
Expand All @@ -34,11 +35,7 @@ export class List {
}
}

const maints = await ipBlacklistMaintainerRepository.find({
where: {
ip_id: entry.id
}
});
const maints = await IpBlacklistMaintainerServiceDB.getInstance().findAllByIp(entry.id);

if (maints) {
for (const maint of maints) {
Expand Down
6 changes: 2 additions & 4 deletions backend/src/Routes/Main/IpAccess/Maintainer/List.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {IpListMaintainerServiceDB} from 'flyingfish_core';
import {IpAccessMaintainer, IpAccessMaintainerResponse, StatusCodes} from 'flyingfish_schemas';
import {IpListMaintainer as IpListMaintainerDB} from '../../../../inc/Db/MariaDb/Entity/IpListMaintainer.js';

/**
* List
Expand All @@ -11,8 +10,7 @@ export class List {
* getMaintainerList
*/
public static async getMaintainerList(): Promise<IpAccessMaintainerResponse> {
const ipListMaintainerRepository = DBHelper.getRepository(IpListMaintainerDB);
const maintainers = await ipListMaintainerRepository.find();
const maintainers = await IpListMaintainerServiceDB.getInstance().findAll();

const list: IpAccessMaintainer[] = [];

Expand Down
12 changes: 2 additions & 10 deletions backend/src/Routes/Main/IpAccess/UtilsLocation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {IpLocationServiceDB} from 'flyingfish_core';
import {IpAccessLocation} from 'flyingfish_schemas';
import {IpLocation as IpLocationDB} from '../../../inc/Db/MariaDb/Entity/IpLocation.js';
import {In} from 'typeorm';

/**
* UtilsLocation
Expand All @@ -14,15 +12,9 @@ export class UtilsLocation {
* @protected
*/
public static async getLocations(locationIds: number[]): Promise<IpAccessLocation[]> {
const ipLocationRepository = DBHelper.getRepository(IpLocationDB);

const locations: IpAccessLocation[] = [];

const tlocations = await ipLocationRepository.find({
where: {
id: In(locationIds)
}
});
const tlocations = await IpLocationServiceDB.getInstance().findAllByIds(locationIds);

if (tlocations) {
for (const tlocation of tlocations) {
Expand Down
1 change: 1 addition & 0 deletions backend/src/inc/Provider/Firehol/FireholDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export class FireholDownloader {

/**
* download url
* @check https://github.com/lucadp76/blocklist-ipsets
* @protected
*/
protected _url = 'https://github.com/firehol/blocklist-ipsets/archive/refs/heads/master.zip';
Expand Down
31 changes: 11 additions & 20 deletions backend/src/inc/Service/BlacklistService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import {
DateHelper,
DBHelper, IpBlacklistCategoryDB,
IpBlacklistCategoryServiceDB,
IpBlacklistDB,
IpBlacklistServiceDB,
IpBlacklistDB, IpBlacklistMaintainerDB, IpBlacklistMaintainerServiceDB,
IpBlacklistServiceDB, IpListMaintainerDB, IpListMaintainerServiceDB,
Logger
} from 'flyingfish_core';
import {Job, scheduleJob} from 'node-schedule';
import {IpBlacklistMaintainer as IpBlacklistMaintainerDB} from '../Db/MariaDb/Entity/IpBlacklistMaintainer.js';
import {IpListMaintainer as IpListMaintainerDB} from '../Db/MariaDb/Entity/IpListMaintainer.js';
import {Firehol} from '../Provider/Firehol/Firehol.js';
import {Settings as GlobalSettings} from '../Settings/Settings.js';

Expand Down Expand Up @@ -76,9 +74,6 @@ export class BlacklistService {
const fh = new Firehol();
await fh.loadList();

const ipListMaintainerRepository = DBHelper.getRepository(IpListMaintainerDB);
const ipBlacklistMaintainerRepository = DBHelper.getRepository(IpBlacklistMaintainerDB);

const ipSetParsers = fh.getIpSets().values();

for await (const ipSetParser of ipSetParsers) {
Expand All @@ -90,11 +85,8 @@ export class BlacklistService {
// add maintainer infos ------------------------------------------------------------------------------------

if (meta.maintainer) {
ipListMaintainer = await ipListMaintainerRepository.findOne({
where: {
maintainer_name: ipSetParser.getMeta().maintainer
}
});

ipListMaintainer = await IpListMaintainerServiceDB.getInstance().findByMaintainerName(meta.maintainer);

if (!ipListMaintainer) {
const nIpListMaintainer = new IpListMaintainerDB();
Expand All @@ -103,7 +95,7 @@ export class BlacklistService {
nIpListMaintainer.maintainer_url = meta.maintainer_url ? meta.maintainer_url : '';
nIpListMaintainer.list_source_url = meta.list_source_url ? meta.list_source_url : '';

ipListMaintainer = await DBHelper.getDataSource().manager.save(nIpListMaintainer);
ipListMaintainer = await IpListMaintainerServiceDB.getInstance().save(nIpListMaintainer);
}
}

Expand Down Expand Up @@ -141,19 +133,18 @@ export class BlacklistService {
// link maintainer ---------------------------------------------------------------------------------

if (ipListMaintainer) {
const blackMaintainer = await ipBlacklistMaintainerRepository.findOne({
where: {
ip_id: ipBlacklistEntry.id,
ip_maintainer_id: ipListMaintainer.id
}
});
const blackMaintainer =
await IpBlacklistMaintainerServiceDB.getInstance().findByIp(
ipBlacklistEntry.id,
ipListMaintainer.id
);

if (!blackMaintainer) {
const nBlackMaintainer = new IpBlacklistMaintainerDB();
nBlackMaintainer.ip_id = ipBlacklistEntry.id;
nBlackMaintainer.ip_maintainer_id = ipListMaintainer.id;

await DBHelper.getDataSource().manager.save(nBlackMaintainer);
await IpBlacklistMaintainerServiceDB.getInstance().save(nBlackMaintainer);
}
}

Expand Down
12 changes: 3 additions & 9 deletions backend/src/inc/Service/IpLocationService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper, IpBlacklistServiceDB, Logger} from 'flyingfish_core';
import {DBHelper, IpBlacklistServiceDB, IpLocationDB, IpLocationServiceDB, Logger} from 'flyingfish_core';
import {Job, scheduleJob} from 'node-schedule';
import {IpLocation as IpLocationDB} from '../Db/MariaDb/Entity/IpLocation.js';
import {IpWhitelist as IpWhitelistDB} from '../Db/MariaDb/Entity/IpWhitelist.js';
import {IpLocateIo} from '../Provider/IpLocate/IpLocateIo.js';
import {Settings as GlobalSettings} from '../Settings/Settings.js';
Expand Down Expand Up @@ -39,12 +38,7 @@ export class IpLocationService {
* @protected
*/
protected async _getIpLocation(ip: string): Promise<number | null> {
const ipLocationRepository = DBHelper.getRepository(IpLocationDB);
const aIpLocation = await ipLocationRepository.findOne({
where: {
ip: ip
}
});
const aIpLocation = await IpLocationServiceDB.getInstance().findByIp(ip);

if (aIpLocation) {
return aIpLocation.id;
Expand All @@ -69,7 +63,7 @@ export class IpLocationService {
newIpLocation.org = location.org || '';
newIpLocation.asn = location.asn || '';

newIpLocation = await DBHelper.getDataSource().manager.save(newIpLocation);
newIpLocation = await IpLocationServiceDB.getInstance().save(newIpLocation);

return newIpLocation.id;
}
Expand Down
6 changes: 0 additions & 6 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import {
} from 'flyingfish_core';
import {EntitySchema, MixedList} from 'typeorm';
import {InfluxDbHelper} from './inc/Db/InfluxDb/InfluxDbHelper.js';
import {IpBlacklistMaintainer as IpBlacklistMaintainerDB} from './inc/Db/MariaDb/Entity/IpBlacklistMaintainer.js';
import {IpListMaintainer as IpListMaintainerDB} from './inc/Db/MariaDb/Entity/IpListMaintainer.js';
import {IpLocation as IpLocationDB} from './inc/Db/MariaDb/Entity/IpLocation.js';
import {IpWhitelist as IpWhitelistDB} from './inc/Db/MariaDb/Entity/IpWhitelist.js';
import {NginxHttpVariable as NginxHttpVariableDB} from './inc/Db/MariaDb/Entity/NginxHttpVariable.js';
import {Dns2Server} from './inc/Dns/Dns2Server.js';
Expand Down Expand Up @@ -116,9 +113,6 @@ import exitHook from 'async-exit-hook';
entities.push(NginxHttpDB);
entities.push(NginxHttpVariableDB);
entities.push(NginxLocationDB);
entities.push(IpListMaintainerDB);
entities.push(IpLocationDB);
entities.push(IpBlacklistMaintainerDB);
entities.push(IpWhitelistDB);

await DBHelper.init({
Expand Down
6 changes: 6 additions & 0 deletions core/src/inc/Db/MariaDb/DBEntitiesLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {DynDnsServerUser} from './Entity/DynDnsServerUser.js';
import {GatewayIdentifier} from './Entity/GatewayIdentifier.js';
import {IpBlacklist} from './Entity/IpBlacklist.js';
import {IpBlacklistCategory} from './Entity/IpBlacklistCategory.js';
import {IpBlacklistMaintainer} from './Entity/IpBlacklistMaintainer.js';
import {IpListMaintainer} from './Entity/IpListMaintainer.js';
import {IpLocation} from './Entity/IpLocation.js';
import {NatPort} from './Entity/NatPort.js';
import {NginxStream} from './Entity/NginxStream.js';
import {NginxUpstream} from './Entity/NginxUpstream.js';
Expand Down Expand Up @@ -43,6 +46,9 @@ export class DBEntitiesLoader {
GatewayIdentifier,
IpBlacklist,
IpBlacklistCategory,
IpBlacklistMaintainer,
IpListMaintainer,
IpLocation,
NatPort,
NginxStream,
NginxUpstream,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {BaseEntity, Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {Column, Entity, Index} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

/**
* ip blacklist maintainer
*/
@Entity({name: 'ip_blacklist_maintainer'})
export class IpBlacklistMaintainer extends BaseEntity {

/**
* id
*/
@PrimaryGeneratedColumn()
public id!: number;
export class IpBlacklistMaintainer extends DBBaseEntityId {

/**
* ip id
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {BaseEntity, Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {Column, Entity, Index} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

/**
* ip list maintainer
*/
@Entity({name: 'ip_list_maintainer'})
export class IpListMaintainer extends BaseEntity {

/**
* id
*/
@PrimaryGeneratedColumn()
public id!: number;
export class IpListMaintainer extends DBBaseEntityId {

/**
* maintainer name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import {BaseEntity, Column, Entity, Index, PrimaryGeneratedColumn} from 'typeorm';
import {Column, Entity, Index} from 'typeorm';
import {DBBaseEntityId} from '../DBBaseEntityId.js';

/**
* IpLocation
*/
@Entity({name: 'ip_location'})
export class IpLocation extends BaseEntity {

/**
* id
*/
@PrimaryGeneratedColumn()
public id!: number;
export class IpLocation extends DBBaseEntityId {

/**
* ip
Expand Down
54 changes: 54 additions & 0 deletions core/src/inc/Db/MariaDb/IpBlacklistMaintainerService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {DBService} from './DBService.js';
import {IpBlacklistMaintainer} from './Entity/IpBlacklistMaintainer.js';

/**
* Ip blacklist maintainer service object.
*/
export class IpBlacklistMaintainerService extends DBService<IpBlacklistMaintainer> {

/**
* register name
*/
public static REGISTER_NAME = 'ip_blacklist_maintainer';

/**
* Return an intance from ip blacklist maintainer service.
* @returns {IpBlacklistMaintainerService}
*/
public static getInstance(): IpBlacklistMaintainerService {
return DBService.getSingleInstance(
IpBlacklistMaintainerService,
IpBlacklistMaintainer,
IpBlacklistMaintainerService.REGISTER_NAME
);
}

/**
* Find a blacklist maintainer by ip id and ip maintainer id.
* @param {number} ipId
* @param {number} ipMaintainerId
* @returns {IpBlacklistMaintainer|null}
*/
public async findByIp(ipId: number, ipMaintainerId: number): Promise<IpBlacklistMaintainer|null> {
return this._repository.findOne({
where: {
ip_id: ipId,
ip_maintainer_id: ipMaintainerId
}
});
}

/**
* Find all blacklist maintainers by ip id.
* @param {number} ipId
* @returns {IpBlacklistMaintainer[]}
*/
public async findAllByIp(ipId: number): Promise<IpBlacklistMaintainer[]> {
return this._repository.find({
where: {
ip_id: ipId
}
});
}

}
Loading

0 comments on commit 40fc7ec

Please sign in to comment.