member_webapp/@overflow/discovery/component/search-result.component.ts
2018-06-14 12:11:56 +09:00

216 lines
5.4 KiB
TypeScript

import {
Component, Input,
SimpleChanges,
OnInit,
OnChanges,
} from '@angular/core';
import { Subscription, Observable } from 'rxjs';
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';
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
animations: Anim,
})
export class SearchResultComponent implements OnInit, OnChanges {
@Input() probeHost: ProbeHost;
@Input() filterWord: string;
@Input() filterServices: Map<string, boolean>;
@Input() finished: boolean;
discoverySubscription: Subscription;
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
selectedItems = [];
msgs: Message[];
error$: Observable<any>;
targetSaveSucceed: boolean;
displayTargetDone: boolean;
constructor(
private targetService: TargetService
) {
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가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.',
});
}
}
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: []
});
}
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
},
});
}
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.ipv4) {
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();
}
}