Skip to content

Commit

Permalink
update db service object
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Werfling committed Sep 14, 2023
1 parent 8890373 commit ece4b08
Show file tree
Hide file tree
Showing 26 changed files with 348 additions and 241 deletions.
23 changes: 3 additions & 20 deletions backend/src/Routes/Main/Dashboard/Info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {DBHelper, IpBlacklistServiceDB} from 'flyingfish_core';
import {DashboardInfoIpBlock, DashboardInfoResponse, StatusCodes} from 'flyingfish_schemas';
import {IpBlacklist as IpBlacklistDB} from '../../../inc/Db/MariaDb/Entity/IpBlacklist.js';
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';
Expand All @@ -15,32 +14,16 @@ export class Info {
* getInfo
*/
public static async getInfo(): Promise<DashboardInfoResponse> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);
const ipLocationRepository = DBHelper.getRepository(IpLocationDB);

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

const ipblocks: DashboardInfoIpBlock[] = [];
let ipblock_count = 0;
const ipblock_count = await IpBlacklistServiceDB.getInstance().countBlocks();

const limit = 100;

const result = await ipBlacklistRepository
.createQueryBuilder('countipblocks')
.select('SUM(countipblocks.count_block)', 'total_count_blocks')
.addSelect('COUNT(*)', 'count')
.getRawOne();

if (result) {
ipblock_count = parseInt(result.total_count_blocks, 10) ?? 0;
}

const entries = await ipBlacklistRepository.find({
take: limit,
order: {
last_block: 'DESC'
}
});
const entries = await IpBlacklistServiceDB.getInstance().findAllSorted(limit, 'DESC');

if (entries) {
for await (const entry of entries) {
Expand Down
16 changes: 3 additions & 13 deletions backend/src/Routes/Main/IpAccess/Blacklist/Import/List.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {DBHelper} from 'flyingfish_core';
import {DBHelper, IpBlacklistServiceDB} from 'flyingfish_core';
import {IpAccessBlackListImport, IpAccessBlackListImportsResponse, StatusCodes} from 'flyingfish_schemas';
import {UtilsLocation} from '../../UtilsLocation.js';
import {IpBlacklist as IpBlacklistDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklist.js';
import {IpBlacklistCategory as IpBlacklistCategoryDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklistCategory.js';
import {IpBlacklistMaintainer as IpBlacklistMaintainerDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklistMaintainer.js';

Expand All @@ -16,19 +15,10 @@ export class List {
public static async getBlackListImports(): Promise<IpAccessBlackListImportsResponse> {
const limit = 20;

const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);
const ipBlacklistCategoryRepository = DBHelper.getRepository(IpBlacklistCategoryDB);
const ipBlacklistMaintainerRepository = DBHelper.getRepository(IpBlacklistMaintainerDB);

const entries = await ipBlacklistRepository.find({
take: limit,
where: {
is_imported: true
},
order: {
last_block: 'DESC'
}
});
const entries = await IpBlacklistServiceDB.getInstance().findAllImported(limit, 'DESC');

const list: IpAccessBlackListImport[] = [];
const locationIds: number[] = [];
Expand Down Expand Up @@ -69,7 +59,7 @@ export class List {
list.push({
id: entry.id,
ip: entry.ip,
disable: entry.disable,
disabled: entry.disabled,
last_update: entry.last_update,
last_block: entry.last_block,
count_block: entry.count_block,
Expand Down
16 changes: 4 additions & 12 deletions backend/src/Routes/Main/IpAccess/Blacklist/Import/Save.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {IpBlacklistServiceDB} from 'flyingfish_core';
import {IpAccessBlackListImportSaveRequest, IpAccessBlackListImportSaveResponse, StatusCodes} from 'flyingfish_schemas';
import {IpBlacklist as IpBlacklistDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklist.js';

/**
* Save
Expand All @@ -12,19 +11,12 @@ export class Save {
* @param request
*/
public static async saveBlackListImport(request: IpAccessBlackListImportSaveRequest): Promise<IpAccessBlackListImportSaveResponse> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

const entrie = await ipBlacklistRepository.findOne({
where: {
id: request.id,
is_imported: true
}
});
const entrie = await IpBlacklistServiceDB.getInstance().findImported(request.id);

if (entrie) {
entrie.disable = request.disable;
entrie.disabled = request.disabled;

await DBHelper.getDataSource().manager.save(entrie);
await IpBlacklistServiceDB.getInstance().save(entrie);

return {
statusCode: StatusCodes.OK
Expand Down
10 changes: 2 additions & 8 deletions backend/src/Routes/Main/IpAccess/Blacklist/Own/Delete.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DBHelper} from 'flyingfish_core';
import {IpBlacklistServiceDB} from 'flyingfish_core';
import {IpAccessBlackDeleteRequest, IpAccessBlackDeleteResponse, StatusCodes} from 'flyingfish_schemas';
import {IpBlacklist as IpBlacklistDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklist.js';

/**
* Delete
Expand All @@ -12,12 +11,7 @@ export class Delete {
* @param data
*/
public static async deleteBlacklist(data: IpAccessBlackDeleteRequest): Promise<IpAccessBlackDeleteResponse> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

const result = await ipBlacklistRepository.delete({
id: data.id,
is_imported: false
});
const result = await IpBlacklistServiceDB.getInstance().removeOwn(data.id);

if (result) {
return {
Expand Down
16 changes: 3 additions & 13 deletions backend/src/Routes/Main/IpAccess/Blacklist/Own/List.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import {DBHelper} from 'flyingfish_core';
import {IpBlacklistServiceDB} from 'flyingfish_core';
import {
IpAccessBlackListOwn,
IpAccessBlackListOwnsResponse,
StatusCodes
} from 'flyingfish_schemas';
import {IpBlacklist as IpBlacklistDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklist.js';
import {UtilsLocation} from '../../UtilsLocation.js';

/**
Expand All @@ -16,16 +15,7 @@ export class List {
* getBlackListOwns
*/
public static async getBlackListOwns(): Promise<IpAccessBlackListOwnsResponse> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

const entries = await ipBlacklistRepository.find({
where: {
is_imported: false
},
order: {
last_block: 'DESC'
}
});
const entries = await IpBlacklistServiceDB.getInstance().findAllOwn('DESC');

const list: IpAccessBlackListOwn[] = [];
const locationIds: number[] = [];
Expand All @@ -39,7 +29,7 @@ export class List {
list.push({
id: entry.id,
ip: entry.ip,
disable: entry.disable,
disabled: entry.disabled,
last_update: entry.last_update,
last_block: entry.last_block,
count_block: entry.count_block,
Expand Down
16 changes: 4 additions & 12 deletions backend/src/Routes/Main/IpAccess/Blacklist/Own/Save.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DateHelper, DBHelper} from 'flyingfish_core';
import {DateHelper, IpBlacklistDB, IpBlacklistServiceDB} from 'flyingfish_core';
import {IpAccessBlackListOwnSaveRequest, IpAccessBlackListOwnSaveResponse, StatusCodes} from 'flyingfish_schemas';
import {IpBlacklist as IpBlacklistDB} from '../../../../../inc/Db/MariaDb/Entity/IpBlacklist.js';

/**
* Save
Expand All @@ -12,26 +11,19 @@ export class Save {
* @param data
*/
public static async saveBlackListOwn(data: IpAccessBlackListOwnSaveRequest): Promise<IpAccessBlackListOwnSaveResponse> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

let entrie = await ipBlacklistRepository.findOne({
where: {
id: data.id,
is_imported: false
}
});
let entrie = await IpBlacklistServiceDB.getInstance().findOwn(data.id);

if (!entrie) {
entrie = new IpBlacklistDB();
}

entrie.ip = data.ip;
entrie.disable = data.disable;
entrie.disabled = data.disabled;
entrie.description = data.description;
entrie.is_imported = false;
entrie.last_update = DateHelper.getCurrentDbTime();

await DBHelper.getDataSource().manager.save(entrie);
await IpBlacklistServiceDB.getInstance().save(entrie);

return {
statusCode: StatusCodes.OK
Expand Down
44 changes: 5 additions & 39 deletions backend/src/Routes/Njs/AddressAccess.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Response, Router} from 'express';
import {DateHelper, DBHelper, DefaultRoute, Logger} from 'flyingfish_core';
import {IpBlacklist as IpBlacklistDB} from '../../inc/Db/MariaDb/Entity/IpBlacklist.js';
import {DateHelper, DBHelper, DefaultRoute, IpBlacklistServiceDB, Logger} from 'flyingfish_core';
import {IpWhitelist as IpWhitelistDB} from '../../inc/Db/MariaDb/Entity/IpWhitelist.js';
import {ListenAddressCheckType, NginxListen as NginxListenDB} from '../../inc/Db/MariaDb/Entity/NginxListen.js';

Expand Down Expand Up @@ -53,13 +52,7 @@ export class AddressAccess extends DefaultRoute {
* @protected
*/
protected async _globalCheckBlacklist(realip_remote_addr: string): Promise<boolean> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);
const address = await ipBlacklistRepository.findOne({
where: {
ip: realip_remote_addr,
disable: false
}
});
const address = await IpBlacklistServiceDB.getInstance().findByIp(realip_remote_addr, false);

if (!address) {
Logger.getLogger().info(`AddressAccess::_globalCheckBlacklist: Address(${realip_remote_addr}) not found in blacklist.`);
Expand All @@ -68,7 +61,7 @@ export class AddressAccess extends DefaultRoute {
}

// update and not await
AddressAccess._updateBlacklistBlock(address.id, address.count_block + 1).then();
IpBlacklistServiceDB.getInstance().updateBlock(address.id, address.count_block + 1).then();

Logger.getLogger().info(`AddressAccess::_globalCheckBlacklist: Address(${realip_remote_addr}) found in blacklist!`);

Expand Down Expand Up @@ -118,14 +111,7 @@ export class AddressAccess extends DefaultRoute {
* @protected
*/
protected async _listCheckBlackList(listenId: number, realip_remote_addr: string): Promise<boolean> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

const address = await ipBlacklistRepository.findOne({
where: {
ip: realip_remote_addr,
disable: false
}
});
const address = await IpBlacklistServiceDB.getInstance().findByIp(realip_remote_addr, false);

if (!address) {
Logger.getLogger().info(`AddressAccess::_listCheckBlackList: Address(${realip_remote_addr}) not found in blacklist.`);
Expand All @@ -136,31 +122,11 @@ export class AddressAccess extends DefaultRoute {
Logger.getLogger().info(`AddressAccess::_listCheckBlackList: Address(${realip_remote_addr}) found in blacklist!`);

// update and not await
AddressAccess._updateBlacklistBlock(address.id, address.count_block + 1).then();
IpBlacklistServiceDB.getInstance().updateBlock(address.id, address.count_block + 1).then();

return false;
}

/**
* _updateBlacklistBlock
* @param ipBlacklistId
* @param newBlockCount
* @protected
*/
protected static async _updateBlacklistBlock(ipBlacklistId: number, newBlockCount: number): Promise<void> {
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

await ipBlacklistRepository
.createQueryBuilder()
.update()
.set({
last_block: DateHelper.getCurrentDbTime(),
count_block: newBlockCount
})
.where('id = :id', {id: ipBlacklistId})
.execute();
}

/**
* _listCheckWhiteList
* @param listenId
Expand Down
14 changes: 4 additions & 10 deletions backend/src/inc/Service/BlacklistService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {DateHelper, DBHelper, Logger} from 'flyingfish_core';
import {DateHelper, DBHelper, IpBlacklistDB, IpBlacklistServiceDB, Logger} from 'flyingfish_core';
import {Job, scheduleJob} from 'node-schedule';
import {IpBlacklist as IpBlacklistDB} from '../Db/MariaDb/Entity/IpBlacklist.js';
import {IpBlacklistCategory as IpBlacklistCategoryDB} from '../Db/MariaDb/Entity/IpBlacklistCategory.js';
import {IpBlacklistMaintainer as IpBlacklistMaintainerDB} from '../Db/MariaDb/Entity/IpBlacklistMaintainer.js';
import {IpListMaintainer as IpListMaintainerDB} from '../Db/MariaDb/Entity/IpListMaintainer.js';
Expand Down Expand Up @@ -72,7 +71,6 @@ export class BlacklistService {
await fh.loadList();

const ipListMaintainerRepository = DBHelper.getRepository(IpListMaintainerDB);
const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);
const ipBlacklistCategoryRepository = DBHelper.getRepository(IpBlacklistCategoryDB);
const ipBlacklistMaintainerRepository = DBHelper.getRepository(IpBlacklistMaintainerDB);

Expand Down Expand Up @@ -107,17 +105,13 @@ export class BlacklistService {
// add ips -------------------------------------------------------------------------------------------------

for await (const ipSet of ipSetParser.getIps()) {
let ipBlacklistEntry = await ipBlacklistRepository.findOne({
where: {
ip: ipSet.ip
}
});
let ipBlacklistEntry = await IpBlacklistServiceDB.getInstance().findByIp(ipSet.ip);

if (!ipBlacklistEntry) {
const blackEntry = new IpBlacklistDB();
blackEntry.ip = ipSet.ip;
blackEntry.is_imported = true;
blackEntry.disable = false;
blackEntry.disabled = false;

ipBlacklistEntry = await DBHelper.getDataSource().manager.save(blackEntry);
}
Expand Down Expand Up @@ -167,7 +161,7 @@ export class BlacklistService {

ipBlacklistEntry!.last_update = DateHelper.getCurrentDbTime();

await DBHelper.getDataSource().manager.save(ipBlacklistEntry);
await IpBlacklistServiceDB.getInstance().save(ipBlacklistEntry);
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions backend/src/inc/Service/IpLocationService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {DBHelper, Logger} from 'flyingfish_core';
import {DBHelper, IpBlacklistServiceDB, Logger} from 'flyingfish_core';
import {Job, scheduleJob} from 'node-schedule';
import {MoreThan} from 'typeorm';
import {IpBlacklist as IpBlacklistDB} from '../Db/MariaDb/Entity/IpBlacklist.js';
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';
Expand Down Expand Up @@ -95,14 +93,7 @@ export class IpLocationService {

// check blacklist ---------------------------------------------------------------------------------------------

const ipBlacklistRepository = DBHelper.getRepository(IpBlacklistDB);

const listB = await ipBlacklistRepository.find({
where: {
count_block: MoreThan(0),
ip_location_id: 0
}
});
const listB = await IpBlacklistServiceDB.getInstance().findAllLastBlock(true);

if (listB) {
for await (const entry of listB) {
Expand Down
Loading

0 comments on commit ece4b08

Please sign in to comment.