import { Component, Input, SimpleChanges, OnInit, OnChanges, } from '@angular/core'; import { Host, Port, Service } from '@overflow/commons-typescript/model/discovery'; import { TreeNode, Message, Tree } from 'primeng/primeng'; import { ProbeHost } from '@overflow/commons-typescript/model/probe'; import { Anim } from './animation'; import { TargetService } from '@overflow/target/service/target.service'; import { InfraService, InfraHost, MetaTargetHostTypeEnum, toMetaTargetHostType, Infra } from '@overflow/commons-typescript'; import { InfraService as InfraRegistService } from '../../infra/service/infra.service'; import { Observable, of, Subscription } from 'rxjs'; import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators'; @Component({ selector: 'of-discovery-result', templateUrl: './search-result.component.html', animations: Anim, providers: [ TargetService, InfraRegistService ] }) export class SearchResultComponent implements OnInit, OnChanges { @Input() probeHost: ProbeHost; @Input() filterWord: string; @Input() filterServices: Map; @Input() finished: boolean; discoverySubscription: Subscription; zoneNode: TreeNode[] = []; hostNode: TreeNode[] = []; selectedItems = []; msgs: Message[]; error$: Observable; targetSaveSucceed: boolean; displayTargetDone: boolean; discoveredHosts: Host[] = []; discoveredServices: Service[] = []; pending$: Observable; constructor( private targetService: TargetService, private infraRegistService: InfraRegistService ) { this.targetSaveSucceed = false; this.displayTargetDone = false; } ngOnInit(): void { this.zoneNode.push({ label: this.probeHost.probe.cidr, type: 'ZONE', data: { }, children: this.hostNode, expanded: true }); } ngOnChanges(changes: SimpleChanges): void { if (changes['finished'] && changes['finished'].currentValue === true) { this.msgs = []; this.msgs.push({ severity: 'success', summary: 'Discovery가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.', }); this.saveDiscoveredInfras(); } } saveDiscoveredInfras() { this.infraRegistService.registDiscoverd( this.probeHost.probe.id, this.discoveredHosts, this.discoveredServices) .pipe( tap(() => { this.pending$ = of(true); }), map((infras: Infra[]) => { console.log(infras); }), catchError(error => { this.error$ = of(error); return of(); }), tap(() => { this.pending$ = of(false); }), take(1), ).subscribe(); } addHost(host: Host) { // this.targetService.findExistHostTarget(this.probeHost.probe.id, host.ipv4) // .pipe( // map((target: Target) => { // }), // catchError(error => { // this.error$ = of(error); // return of(); // }), // ).subscribe(); const idx = this.findHostIndex(host); this.hostNode.splice(idx, 0, { type: 'HOST', label: host.address, data: { exist: false, ip: this.convertIPtoNumber(host.address), ipv6: host.address, mac: host.mac, openPorts: [], target: host }, expanded: true, children: [] }); this.discoveredHosts.push(host); } addService(service: Service) { const targetHostNode = this.findHostNodeByService(service); const idx = this.findServiceIndex(targetHostNode.children, service); targetHostNode.children.splice(idx, 0, { type: 'SERVICE', label: service.name + ' (' + service.port.portNumber + ')', data: { name: service.name, portType: service.port.metaPortType, portNumber: service.port.portNumber, target: service }, }); this.discoveredServices.push(service); } addPort(port: Port) { // this.hostNode.forEach(node => { // if (node.data.id === port.host.id) { // node.data.openPorts.push(port); // return; // } // }); } onTargetSelect(e, node: TreeNode) { const data = node.data.target; if (e.checked) { this.selectedItems.push(data); } else { const index = this.selectedItems.indexOf(data); this.selectedItems.splice(index, 1); } } findHostIndex(host: Host): number { let index = 0; this.hostNode.forEach(node => { if (node.data.ip < this.convertIPtoNumber(host.address)) { index++; } }); return index; } findServiceIndex(serviceNodes: TreeNode[], service: Service) { let index = 0; serviceNodes.forEach(node => { // if (node.data.portNumber < service.port.portNumber) { // index++; // } if (node.data.name.toUpperCase().localeCompare(service.name.toUpperCase()) === -1) { index++; } }); return index; } findHostNodeByService(service: Service) { let targetHost = null; this.hostNode.forEach((value, i) => { if (value.data.ip === this.convertIPtoNumber(service.port.host.address)) { targetHost = value; return; } }); return targetHost; } convertIPtoNumber(ip: string) { return ip.split('.').map((octet, index, array) => { // tslint:disable-next-line:radix return parseInt(octet) * Math.pow(256, (array.length - index - 1)); }).reduce((prev, curr) => { return prev + curr; }); } checkHighlight(label: string, type: number) { let highlight = true; if (this.filterWord && (label.toUpperCase().indexOf(this.filterWord.toUpperCase()) < 0)) { highlight = false; } if (type === 1 && this.filterServices[label] === false) { highlight = false; } return highlight; } saveTargets() { // const hosts: Host[] = []; // const services: Service[] = []; // this.selectedItems.forEach(value => { // if (!value.port) { // hosts.push(value); // } else { // services.push(value); // } // }); // this.targetService.registDiscoveredTargets(this.probeHost.probe.id, hosts, services) // .pipe( // tap(() => { // this.targetSaveSucceed = false; // }), // map((targets: Target[]) => { // if (targets) { // this.displayTargetDone = true; // } // }), // catchError(error => { // this.error$ = of(error); // return of(); // }), // tap(() => { // this.targetSaveSucceed = true; // }), // take(1), // ).subscribe(); } }