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

193 lines
4.5 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-05-30 09:39:26 +00:00
} from '@angular/core';
2018-06-04 08:55:47 +00:00
2018-06-01 11:25:51 +00:00
import { Subscription } from 'rxjs';
2018-06-04 08:55:47 +00:00
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
import { TreeNode } from 'primeng/primeng';
2018-06-01 11:25:51 +00:00
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
2018-05-30 09:39:26 +00:00
2018-06-04 08:55:47 +00:00
import { Anim } from './animation';
import { DiscoveryNotify } from '../subscriber/discovery.subscriber';
2018-05-30 09:39:26 +00:00
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
2018-05-30 14:05:58 +00:00
animations: Anim
2018-05-30 09:39:26 +00:00
})
2018-06-01 11:25:51 +00:00
export class SearchResultComponent implements OnInit {
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;
@Input() filterServices: Service[];
2018-05-30 09:39:26 +00:00
@Output() stop = new EventEmitter();
2018-06-01 11:25:51 +00:00
@Input() started: boolean; // Temporary
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-01 11:25:51 +00:00
selectedItems = [];
tempHostId = 0; // Temporary
2018-05-30 09:39:26 +00:00
constructor(
) {
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-01 11:25:51 +00:00
tempHost() {
const idx = Math.floor(Math.random() * (255));
const host: Host = {
id: this.tempHostId++,
ipv4: '192.168.1.' + idx,
};
2018-06-04 08:31:00 +00:00
this.addHost(host);
}
tempPort() {
const idx = Math.floor(Math.random() * (5000));
const hostId = Math.floor(Math.random() * (this.tempHostId - 1));
const port: Port = {
id: idx,
portNumber: idx,
portType: idx % 2 === 0 ? 'TCP' : 'UDP',
host: {
id: hostId
},
};
this.addPort(port);
2018-05-30 14:05:58 +00:00
}
2018-06-01 11:25:51 +00:00
tempService() {
2018-06-04 09:25:48 +00:00
const randStr = Math.random().toString(36).substr(2, 5);
2018-06-01 11:25:51 +00:00
const idx = Math.floor(Math.random() * (255));
const hostId = Math.floor(Math.random() * (this.tempHostId - 1));
const service: Service = {
id: idx,
2018-06-04 09:25:48 +00:00
serviceName: String(randStr),
2018-06-01 11:25:51 +00:00
port: {
portNumber: idx,
portType: idx % 2 === 0 ? 'TCP' : 'UDP',
host: {
id: hostId
}
}
};
2018-06-04 08:31:00 +00:00
this.addService(service);
2018-05-30 14:05:58 +00:00
}
2018-06-04 08:31:00 +00:00
addHost(host: Host) {
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: {
id: host.id,
ip: this.convertIPtoNumber(host.ipv4),
2018-06-04 08:31:00 +00:00
openPorts: []
2018-06-01 11:25:51 +00:00
},
2018-06-01 07:32:16 +00:00
expanded: true,
children: []
});
}
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);
targetHostNode.children[idx] = {
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: {
id: service.id,
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-04 08:31:00 +00:00
addPort(port: Port) {
this.hostNode.forEach(node => {
if (node.data.id === port.host.id ) {
node.data.openPorts.push(port);
return;
}
});
}
2018-06-01 11:25:51 +00:00
onTargetSelect(e, data) {
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 => {
if (node.data.portNumber < service.port.portNumber) {
index++;
}
2018-06-04 09:25:48 +00:00
// if (!node.data.name.toUpperCase().localeCompare(service.serviceName.toUpperCase())) {
// index++;
// }
2018-06-01 11:25:51 +00:00
});
return index;
}
findHostNodeByService(service: Service) {
let targetHost = null;
this.hostNode.forEach((value, i) => {
if (value.data.id === service.port.host.id) {
targetHost = this.hostNode[i];
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-04 09:25:48 +00:00
checkUnhighligt(label: string) {
if (this.filterWord && label.indexOf(this.filterWord) === -1) {
return true;
}
return false;
2018-06-04 08:31:00 +00:00
}
2018-05-30 09:39:26 +00:00
}