member_webapp/@overflow/discovery/component/search-result.component.ts

222 lines
5.7 KiB
TypeScript
Raw Normal View History

2018-05-30 09:39:26 +00:00
import {
AfterContentInit, Component, Input,
EventEmitter,
2018-05-30 14:05:58 +00:00
Output,
2018-06-01 11:25:51 +00:00
SimpleChanges,
OnInit,
2018-06-05 02:56:45 +00:00
OnChanges,
2018-05-30 09:39:26 +00:00
} from '@angular/core';
2018-06-05 13:01:01 +00:00
import { catchError, exhaustMap, map, tap, take } from 'rxjs/operators';
import { Subscription, Observable, of } from 'rxjs';
2018-06-04 08:55:47 +00:00
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
2018-06-05 06:07:12 +00:00
import { TreeNode, Message, Tree } from 'primeng/primeng';
2018-06-01 11:25:51 +00:00
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
2018-06-04 08:55:47 +00:00
import { Anim } from './animation';
import { DiscoveryNotify } from '../subscriber/discovery.subscriber';
2018-06-05 06:07:12 +00:00
import { InfraHost, InfraService } from '@overflow/commons-typescript/model/infra';
2018-06-06 03:19:44 +00:00
import { TargetService } from '@overflow/target/service/target.service';
2018-06-05 13:01:01 +00:00
import { Target } from '@overflow/commons-typescript/model/target';
2018-06-04 08:55:47 +00:00
2018-05-30 09:39:26 +00:00
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
2018-06-06 03:19:44 +00:00
animations: Anim,
2018-05-30 09:39:26 +00:00
})
2018-06-05 02:56:45 +00:00
export class SearchResultComponent implements OnInit, OnChanges {
2018-05-30 09:39:26 +00:00
2018-06-01 11:25:51 +00:00
@Input() probeHost: ProbeHost;
2018-06-04 08:31:00 +00:00
@Input() filterWord: string;
2018-06-05 02:56:45 +00:00
@Input() filterServices: Map<string, boolean>;
@Input() finished: boolean;
2018-05-30 14:05:58 +00:00
2018-06-01 11:25:51 +00:00
discoverySubscription: Subscription;
2018-06-01 07:32:16 +00:00
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
2018-06-05 13:01:01 +00:00
selectedItems = [];
2018-06-05 02:56:45 +00:00
msgs: Message[];
2018-06-05 13:01:01 +00:00
error$: Observable<any>;
2018-06-05 06:07:12 +00:00
2018-06-06 08:57:19 +00:00
targetSaveSucceed: boolean;
displayTargetDone: boolean;
2018-05-30 09:39:26 +00:00
constructor(
2018-06-05 13:01:01 +00:00
private targetService: TargetService
2018-05-30 09:39:26 +00:00
) {
2018-06-06 08:57:19 +00:00
this.targetSaveSucceed = false;
this.displayTargetDone = false;
2018-06-01 11:25:51 +00:00
}
ngOnInit(): void {
2018-06-01 07:32:16 +00:00
this.zoneNode.push({
2018-06-01 11:25:51 +00:00
label: this.probeHost.probe.cidr,
type: 'ZONE',
data: {
},
2018-06-01 07:32:16 +00:00
children: this.hostNode,
expanded: true
});
2018-05-30 09:39:26 +00:00
}
2018-05-30 14:05:58 +00:00
2018-06-05 02:56:45 +00:00
ngOnChanges(changes: SimpleChanges): void {
if (changes['finished'] && changes['finished'].currentValue === true) {
this.msgs = [];
this.msgs.push({
severity: 'success',
summary: 'Discovery가 완료되었습니다. 모니터링 대상(들)을 선택 후 저장하세요.',
});
}
2018-05-30 14:05:58 +00:00
}
2018-06-04 08:31:00 +00:00
addHost(host: Host) {
2018-06-05 13:01:01 +00:00
// this.targetService.findExistHostTarget(this.probeHost.probe.id, host.ipv4)
// .pipe(
// map((target: Target) => {
// }),
// catchError(error => {
// this.error$ = of(error);
// return of();
// }),
// ).subscribe();
2018-06-01 11:25:51 +00:00
const idx = this.findHostIndex(host);
2018-06-01 07:32:16 +00:00
this.hostNode.splice(idx, 0, {
2018-06-01 11:25:51 +00:00
type: 'HOST',
label: host.ipv4,
data: {
2018-06-05 13:01:01 +00:00
exist: false,
2018-06-01 11:25:51 +00:00
ip: this.convertIPtoNumber(host.ipv4),
2018-06-05 06:07:12 +00:00
ipv6: host.ipv6,
mac: host.mac,
openPorts: [],
2018-06-05 09:32:36 +00:00
target: host
2018-06-01 11:25:51 +00:00
},
2018-06-01 07:32:16 +00:00
expanded: true,
children: []
});
}
2018-06-04 12:50:46 +00:00
2018-06-04 08:31:00 +00:00
addService(service: Service) {
2018-06-01 11:25:51 +00:00
const targetHostNode = this.findHostNodeByService(service);
const idx = this.findServiceIndex(targetHostNode.children, service);
2018-06-04 12:50:46 +00:00
targetHostNode.children.splice(idx, 0, {
2018-06-01 11:25:51 +00:00
type: 'SERVICE',
2018-06-04 09:25:48 +00:00
label: service.serviceName + ' (' + service.port.portNumber + ')',
2018-06-01 11:25:51 +00:00
data: {
2018-06-04 09:25:48 +00:00
name: service.serviceName,
2018-06-01 11:25:51 +00:00
portType: service.port.portType,
portNumber: service.port.portNumber,
2018-06-05 09:32:36 +00:00
target: service
2018-06-01 11:25:51 +00:00
},
2018-06-04 12:50:46 +00:00
});
2018-06-01 11:25:51 +00:00
}
2018-06-04 08:31:00 +00:00
addPort(port: Port) {
this.hostNode.forEach(node => {
2018-06-04 12:50:46 +00:00
if (node.data.id === port.host.id) {
2018-06-04 08:31:00 +00:00
node.data.openPorts.push(port);
return;
}
});
}
2018-06-01 11:25:51 +00:00
2018-06-06 03:19:44 +00:00
onTargetSelect(e, node: TreeNode) {
const data = node.data.target;
2018-06-01 11:25:51 +00:00
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.ipv4)) {
index++;
}
});
return index;
}
findServiceIndex(serviceNodes: TreeNode[], service: Service) {
let index = 0;
serviceNodes.forEach(node => {
2018-06-04 12:50:46 +00:00
// if (node.data.portNumber < service.port.portNumber) {
2018-06-04 09:25:48 +00:00
// index++;
// }
2018-06-04 12:50:46 +00:00
if (node.data.name.toUpperCase().localeCompare(service.serviceName.toUpperCase()) === -1) {
index++;
}
2018-06-01 11:25:51 +00:00
});
return index;
}
findHostNodeByService(service: Service) {
let targetHost = null;
this.hostNode.forEach((value, i) => {
2018-06-04 12:50:46 +00:00
if (value.data.ip === this.convertIPtoNumber(service.port.host.ipv4)) {
targetHost = value;
2018-06-04 04:13:06 +00:00
return;
2018-06-01 11:25:51 +00:00
}
});
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;
2018-06-01 07:32:16 +00:00
});
}
2018-06-04 08:31:00 +00:00
2018-06-06 03:19:44 +00:00
checkHighlight(label: string, type: number) {
2018-06-05 09:32:36 +00:00
let highlight = true;
if (this.filterWord && (label.toUpperCase().indexOf(this.filterWord.toUpperCase()) < 0)) {
highlight = false;
2018-06-05 02:56:45 +00:00
}
2018-06-06 03:19:44 +00:00
if (type === 1 && this.filterServices[label] === false) {
2018-06-05 09:32:36 +00:00
highlight = false;
2018-06-04 09:25:48 +00:00
}
2018-06-05 09:32:36 +00:00
return highlight;
2018-06-04 08:31:00 +00:00
}
2018-06-04 12:50:46 +00:00
2018-06-05 06:07:12 +00:00
2018-06-05 02:56:45 +00:00
saveTargets() {
2018-06-05 13:01:01 +00:00
const hosts: Host[] = [];
const services: Service[] = [];
this.selectedItems.forEach(value => {
if (value.ipv4) {
hosts.push(value);
} else {
services.push(value);
2018-06-05 06:07:12 +00:00
}
2018-06-05 13:01:01 +00:00
});
2018-06-06 08:57:19 +00:00
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();
2018-06-04 12:50:46 +00:00
}
2018-06-06 08:57:19 +00:00
2018-05-30 09:39:26 +00:00
}