member_webapp/@overflow/discovery/component/search-result.component.ts
crusader 9990650925 ing
2018-06-04 17:55:47 +09:00

185 lines
4.2 KiB
TypeScript

import {
AfterContentInit, Component, Input,
EventEmitter,
Output,
SimpleChanges,
OnInit,
} from '@angular/core';
import { Subscription } from 'rxjs';
import { DiscoverZone, Zone, Host, Port, Service } from '@overflow/commons-typescript/model/discovery';
import { TreeNode } from 'primeng/primeng';
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
import { Anim } from './animation';
import { DiscoveryNotify } from '../subscriber/discovery.subscriber';
@Component({
selector: 'of-discovery-result',
templateUrl: './search-result.component.html',
animations: Anim
})
export class SearchResultComponent implements OnInit {
@Input() probeHost: ProbeHost;
@Input() filterWord: string;
@Input() filterServices: Service[];
@Output() stop = new EventEmitter();
@Input() started: boolean; // Temporary
discoverySubscription: Subscription;
zoneNode: TreeNode[] = [];
hostNode: TreeNode[] = [];
selectedItems = [];
tempHostId = 0; // Temporary
constructor(
) {
}
ngOnInit(): void {
this.zoneNode.push({
label: this.probeHost.probe.cidr,
type: 'ZONE',
data: {
},
children: this.hostNode,
expanded: true
});
}
tempHost() {
const idx = Math.floor(Math.random() * (255));
const host: Host = {
id: this.tempHostId++,
ipv4: '192.168.1.' + idx,
};
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);
}
tempService() {
const idx = Math.floor(Math.random() * (255));
const hostId = Math.floor(Math.random() * (this.tempHostId - 1));
const service: Service = {
id: idx,
serviceName: 'Service',
port: {
portNumber: idx,
portType: idx % 2 === 0 ? 'TCP' : 'UDP',
host: {
id: hostId
}
}
};
this.addService(service);
}
addHost(host: Host) {
const idx = this.findHostIndex(host);
this.hostNode.splice(idx, 0, {
type: 'HOST',
label: host.ipv4,
data: {
id: host.id,
ip: this.convertIPtoNumber(host.ipv4),
openPorts: []
},
expanded: true,
children: []
});
}
addService(service: Service) {
const targetHostNode = this.findHostNodeByService(service);
const idx = this.findServiceIndex(targetHostNode.children, service);
targetHostNode.children[idx] = {
type: 'SERVICE',
label: service.serviceName,
data: {
id: service.id,
portType: service.port.portType,
portNumber: service.port.portNumber,
},
};
}
addPort(port: Port) {
this.hostNode.forEach(node => {
if (node.data.id === port.host.id ) {
node.data.openPorts.push(port);
return;
}
});
}
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++;
}
});
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];
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;
});
}
checkFilter(label: string) {
return label.indexOf(this.filterWord) > 0 ? true : false;
}
}