Skip to content

Commit

Permalink
✨ Add GetBlackListUnary unary call
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Jan 18, 2024
1 parent 1866cb8 commit ab8f979
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/blacklist/_utils/blacklist.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ service Blacklist {
rpc PutBlackList (PutBlackListRequest) returns (PutBlackListReply) {}
rpc BlockCountry (BlockCountryRequest) returns (BlockCountryReply) {}
rpc GetBlackList (GetBlackListRequest) returns (stream GetBlackListReply) {}
rpc GetBlackListUnary (GetBlackListRequest) returns (GetBlackListReply) {}
rpc PutWhiteList (PutWhiteListRequest) returns (PutWhiteListReply) {}
}

Expand Down
5 changes: 5 additions & 0 deletions src/blacklist/blacklist.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { BlockCountryRequestDto } from './_utils/dto/request/block-country-reque
export class BlacklistController {
constructor(private readonly blacklistService: BlacklistService) {}

@GrpcMethod('Blacklist', 'GetBlackListUnary')
getBlackListUnary() {
return this.blacklistService.getBlackListUnary();
}

@GrpcMethod('Blacklist', 'GetBlackList')
getBlacklist$(_data: unknown, _metadata: unknown, call: ServerUnaryCall<unknown, GetIdsDto>) {
return this.blacklistService.getBlackList$(call);
Expand Down
19 changes: 19 additions & 0 deletions src/blacklist/blacklist.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,25 @@ export class BlacklistService {
});
}

async getBlackListUnary(): Promise<GetIdsDto> {
const filePath = '/app/honeypot/block.conf';
try {
const blockContent = await readFile(filePath, 'utf8');
const regex = /deny\s+((?:\d{1,3}\.){3}\d{1,3});/g;
const matches = blockContent.match(regex) || [];

const ips = matches.map((entry) => {
const ipRegex = /((?:\d{1,3}\.){3}\d{1,3})/;
const match = entry.match(ipRegex);
return match ? match[1] : null;
}).filter((ip) => ip !== null) as string[];

return { ips };
} catch (err) {
throw new RpcException(`Error reading block.conf: ${err}`);
}
}

getBlackList$(call: ServerUnaryCall<unknown, GetIdsDto>) {
const subject = new Subject<GetIdsDto>();

Expand Down

0 comments on commit ab8f979

Please sign in to comment.