Skip to content

Commit

Permalink
✨ Add GetContainers unary call
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinbreiz committed Jan 18, 2024
1 parent 62fdaa0 commit b960c5a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/containers/_utils/containers.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package containers;

service Containers {
rpc StreamContainers (ContainersRequest) returns (stream ContainersReply) {}
rpc GetContainers (ContainersRequest) returns (ContainersReply) {}
}

message ContainersRequest {}
Expand Down
5 changes: 5 additions & 0 deletions src/containers/containers.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { GrpcMethod } from '@nestjs/microservices';
export class ContainersController {
constructor(private readonly containersService: ContainersService) {}

@GrpcMethod('Containers', 'GetContainers')
getContainers() {
return this.containersService.getContainers();
}

@GrpcMethod('Containers', 'StreamContainers')
streamContainers() {
return this.containersService.streamContainers();
Expand Down
31 changes: 31 additions & 0 deletions src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@ import * as Docker from 'dockerode';
export class ContainersService {
private docker = new Docker();

private async getContainerData(): Promise<ContainersReplyDto> {
const network = await this.docker.getNetwork('honeypot_network').inspect();

const networkContainers = network.Containers;
const containerIds = Object.keys(networkContainers);

const containers = await this.docker.listContainers({ all: true });
const buildContainers = containers.filter((container) => container.Names[0].startsWith('/honeypot_'));

const containersData = buildContainers.map((container): Container => {
const networkContainerKey = containerIds.find((key) => key.startsWith(container.Id));
const networkContainer = networkContainerKey ? networkContainers[networkContainerKey] : undefined;

return {
ip: networkContainer ? networkContainer.IPv4Address.split('/')[0] : 'Not found',
name: container.Names[0].substring(1),
status: container.State,
};
});

return { containers: containersData };
}

public async getContainers(): Promise<ContainersReplyDto> {
try {
return await this.getContainerData();
} catch (error) {
throw new Error(`Error fetching container data: ${error}`);
}
}

private async handleContainerEvent(subject: Subject<ContainersReplyDto>) {
const network = await this.docker.getNetwork('honeypot_network').inspect();

Expand Down

0 comments on commit b960c5a

Please sign in to comment.