-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
337 additions
and
6 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,24 @@ | ||
import { Entity, Column, PrimaryColumn, CreateDateColumn, UpdateDateColumn, Index } from 'typeorm' | ||
|
||
import { FailToBanModel } from '../../models/req-res.model'; | ||
|
||
@Entity({ name: 'fail_to_ban' }) | ||
export class FailToBan implements FailToBanModel { | ||
|
||
@PrimaryColumn({ type: 'text' }) | ||
ip_domain: string; | ||
|
||
@Column({ type: 'int', default: 0 }) | ||
fail_count: number; | ||
|
||
@Column({ type: 'text', nullable: true }) | ||
rule_id: string; | ||
|
||
@CreateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) | ||
@Index() | ||
created_at: number | Date; | ||
|
||
@UpdateDateColumn({ type: 'timestamp with time zone', default: () => 'CURRENT_TIMESTAMP' }) | ||
updated_at: number | Date; | ||
|
||
} |
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,77 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity'; | ||
import { EntityMetadata, FindManyOptions, FindOneOptions, FindConditions, InsertResult, Repository, UpdateResult } from 'typeorm'; | ||
|
||
import { FailToBan } from '../entities/FailToBan'; | ||
|
||
import { GlobalService } from '../services/global.service'; | ||
|
||
@Injectable() | ||
export class FailToBanService { | ||
|
||
constructor( | ||
@InjectRepository(FailToBan) private failToBanRepo: Repository<FailToBan>, | ||
private gs: GlobalService | ||
) { | ||
// | ||
} | ||
|
||
new(): FailToBan { | ||
return new FailToBan(); | ||
} | ||
|
||
instance(): Repository<FailToBan> { | ||
return this.failToBanRepo; | ||
} | ||
|
||
getMetaData(): EntityMetadata { | ||
return this.failToBanRepo.metadata; | ||
} | ||
|
||
find(options: FindManyOptions<FailToBan>): Promise<FailToBan[]> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-FIND_ALL] 🧱', options); | ||
return this.failToBanRepo.find(options); | ||
} | ||
|
||
findAndCount(options: FindManyOptions<FailToBan>): Promise<[FailToBan[], number]> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-FIND_AND_COUNT] 🧱', options); | ||
return this.failToBanRepo.findAndCount(options); | ||
} | ||
|
||
findOneOrFail(options: FindOneOptions<FailToBan>): Promise<FailToBan> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-GET_BY] 🧱', options); | ||
return this.failToBanRepo.findOneOrFail(options); | ||
} | ||
|
||
save<T = FailToBan | FailToBan[]>(failToBan: T): Promise<T> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-SAVE] 🧱', failToBan); | ||
return this.failToBanRepo.save(failToBan); | ||
} | ||
|
||
count(options: FindManyOptions<FailToBan>): Promise<number> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-COUNT] 🧱', options); | ||
return this.failToBanRepo.count(options); | ||
} | ||
|
||
remove(failToBan: FailToBan | FailToBan[]): Promise<FailToBan | FailToBan[]> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-REMOVE] 🧱', failToBan); | ||
return this.failToBanRepo.remove(failToBan as any); | ||
} | ||
|
||
query(query: string, parameters: any = []): Promise<any> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-QUERY] 🧱', query); | ||
return this.failToBanRepo.query(query, parameters); | ||
} | ||
|
||
update(criteria: FindConditions<FailToBan>, partialEntity: QueryDeepPartialEntity<FailToBan>): Promise<UpdateResult> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-UPDATE] 🧱', criteria); | ||
return this.failToBanRepo.update(criteria, partialEntity); | ||
} | ||
|
||
insert(failToBan: FailToBan): Promise<InsertResult> { | ||
this.gs.log('[FAIL_TO_BAN_SERVICE-INSERT] 🧱', failToBan); | ||
return this.failToBanRepo.insert(failToBan); | ||
} | ||
|
||
} |
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,73 @@ | ||
// NodeJS Library | ||
import cluster from 'node:cluster'; | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { Cron, CronExpression, SchedulerRegistry } from '@nestjs/schedule'; | ||
|
||
import { CONSTANTS } from '../../constants'; | ||
|
||
import { GlobalService } from '../services/global.service'; | ||
import { CloudflareService } from '../services/cloudflare.service'; | ||
import { FailToBanService } from '../repository/fail-to-ban.service'; | ||
|
||
@Injectable() | ||
export class CfWafService { | ||
|
||
constructor( | ||
private sr: SchedulerRegistry, | ||
private gs: GlobalService, | ||
private cfs: CloudflareService, | ||
private ftb: FailToBanService | ||
) { | ||
// | ||
} | ||
|
||
@Cron( | ||
CronExpression.EVERY_MINUTE, | ||
{ | ||
name: CONSTANTS.cronCloudflareBan | ||
} | ||
) | ||
async unBanIp(): Promise<void> { | ||
if (cluster.isMaster) { | ||
const job = this.sr.getCronJob(CONSTANTS.cronCloudflareBan); | ||
job.stop(); | ||
const startTime = new Date(); | ||
this.gs.log('[CRON_TASK_CLOUDFLARE_BAN-START] 🐾', `${startTime}`); | ||
try { | ||
const failToBan = await this.ftb.find({ | ||
order: { | ||
ip_domain: 'ASC' | ||
} | ||
}); | ||
for (const _ftb of failToBan) { | ||
try { | ||
if (_ftb.rule_id) { | ||
const diffDateMs = startTime.getTime() - new Date(_ftb.updated_at).getTime(); | ||
// Approx. Min 1 Hours IP Ban (+ 1 Min Max Cron Delay) | ||
if (diffDateMs >= 1 * 60 * 60 * 1000) { | ||
const resBan = await this.cfs.deleteFailToBan(_ftb.rule_id); | ||
if (resBan && resBan.status >= 200 && resBan.status < 400) { | ||
await this.ftb.remove(_ftb); | ||
} | ||
} | ||
} else { | ||
await this.ftb.remove(_ftb); | ||
} | ||
} catch (e) { | ||
this.gs.log('[CRON_TASK_CLOUDFLARE_BAN-ERROR_DELETE] 🐾', e, 'error'); | ||
} | ||
} | ||
} catch (error) { | ||
this.gs.log('[CRON_TASK_CLOUDFLARE_BAN-ERROR] 🐾', error, 'error'); | ||
} | ||
const endTime = new Date(); | ||
const elapsedTime = endTime.getTime() - startTime.getTime(); | ||
this.gs.log('[CRON_TASK_CLOUDFLARE_BAN-END] 🐾', `${endTime} @ ${elapsedTime} ms`); | ||
job.start(); | ||
} else { | ||
this.sr.getCronJob(CONSTANTS.cronCloudflareBan).stop(); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.