2018-05-18 06:57:43 +00:00
|
|
|
import {
|
2018-05-29 12:18:43 +00:00
|
|
|
Component, EventEmitter, Input,
|
|
|
|
Output, OnChanges, SimpleChanges
|
2018-05-18 06:57:43 +00:00
|
|
|
} from '@angular/core';
|
2018-05-29 12:18:43 +00:00
|
|
|
import { ProbeHost } from '@overflow/commons-typescript/model/probe';
|
|
|
|
import * as CIDR from 'ip-cidr';
|
|
|
|
import * as ipRangeCheck from 'ip-range-check';
|
|
|
|
import { DiscoverPort, DiscoverService, DiscoverZone } from '@overflow/commons-typescript/model/discovery';
|
|
|
|
import { Checkbox } from 'primeng/primeng';
|
2018-06-14 03:11:56 +00:00
|
|
|
import { MetaIPTypeEnum, toMetaIPType } from '@overflow/commons-typescript/model/meta';
|
2018-05-18 06:57:43 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'of-discovery-search-config',
|
|
|
|
templateUrl: './search-config.component.html',
|
|
|
|
})
|
2018-05-29 12:18:43 +00:00
|
|
|
export class SearchConfigComponent implements OnChanges {
|
2018-05-25 10:56:28 +00:00
|
|
|
|
2018-05-30 09:39:26 +00:00
|
|
|
SUPPORT_V6 = false;
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
@Input() probeHost: ProbeHost;
|
|
|
|
@Output() requestDiscovery = new EventEmitter<DiscoverZone>();
|
2018-05-24 09:18:38 +00:00
|
|
|
|
2018-06-14 03:11:56 +00:00
|
|
|
ipType: MetaIPTypeEnum;
|
2018-05-24 09:18:38 +00:00
|
|
|
startIP: string;
|
|
|
|
endIP: string;
|
|
|
|
startPort: string;
|
|
|
|
endPort: string;
|
2018-05-30 14:05:58 +00:00
|
|
|
excludeIPs = [];
|
|
|
|
excludePorts = [];
|
2018-05-29 12:18:43 +00:00
|
|
|
includeServices = [];
|
2018-05-18 06:57:43 +00:00
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
hostChecked = true;
|
|
|
|
portChecked = true;
|
|
|
|
serviceChecked = true;
|
|
|
|
tcpChecked = true;
|
|
|
|
udpChecked = true;
|
2018-05-25 10:56:28 +00:00
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
validation = false;
|
|
|
|
ipErrMsg: string;
|
|
|
|
portErrMsg: string;
|
2018-05-28 05:48:17 +00:00
|
|
|
|
2018-05-30 09:39:26 +00:00
|
|
|
fixedIPrange: number;
|
2018-05-30 14:05:58 +00:00
|
|
|
ipArray = [];
|
2018-05-31 07:03:59 +00:00
|
|
|
portExcludeDisplay = false;
|
|
|
|
portChips = [];
|
2018-05-30 09:39:26 +00:00
|
|
|
|
2018-05-18 06:57:43 +00:00
|
|
|
constructor(
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
2018-05-30 09:39:26 +00:00
|
|
|
if (changes['probeHost']) {
|
|
|
|
this.initByProbe();
|
|
|
|
}
|
2018-05-29 12:18:43 +00:00
|
|
|
this.validation = true;
|
2018-05-18 06:57:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
initByProbe() {
|
|
|
|
const cidr = new CIDR(this.probeHost.probe.cidr);
|
|
|
|
if (!cidr.isValid()) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-14 03:11:56 +00:00
|
|
|
this.ipType = MetaIPTypeEnum.V4;
|
2018-05-30 14:05:58 +00:00
|
|
|
this.startIP = cidr.start();
|
|
|
|
this.endIP = cidr.end();
|
2018-05-29 12:18:43 +00:00
|
|
|
this.startPort = '1';
|
|
|
|
this.endPort = '1024';
|
|
|
|
// TODO: Initialize services
|
2018-05-30 09:39:26 +00:00
|
|
|
|
|
|
|
this.checkEditableIPrange(cidr);
|
2018-05-18 06:57:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-30 14:05:58 +00:00
|
|
|
onIPExcludeFocus() {
|
|
|
|
const cidr = new CIDR(this.probeHost.probe.cidr);
|
|
|
|
this.ipArray = cidr.toArray().filter((value) => {
|
|
|
|
return this.inRange(value) || this.excludeIPs.find(obj => obj.ip === value);
|
|
|
|
}).map(value => {
|
|
|
|
return { ip: value };
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inRange(value) {
|
|
|
|
const min = this.startIP;
|
|
|
|
const max = this.endIP;
|
|
|
|
if (this.ipToNum(min) <= this.ipToNum(value) && this.ipToNum(max) >= this.ipToNum(value)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ipToNum(ip) {
|
|
|
|
return Number(
|
|
|
|
ip.split('.')
|
|
|
|
.map(d => ('000' + d).substr(-3))
|
|
|
|
.join('')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
onRequestDiscovery() {
|
|
|
|
let discoverPort: DiscoverPort = null;
|
|
|
|
let discoverService: DiscoverService = null;
|
|
|
|
|
|
|
|
if (this.serviceChecked) {
|
2018-05-31 13:26:14 +00:00
|
|
|
const services = [];
|
2018-05-29 12:18:43 +00:00
|
|
|
for (const service of this.includeServices) {
|
|
|
|
services.push(service.description);
|
|
|
|
}
|
|
|
|
discoverService = {
|
|
|
|
includeServices: services,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (this.portChecked) {
|
|
|
|
discoverPort = {
|
|
|
|
firstScanRange: Number(this.startPort),
|
|
|
|
lastScanRange: Number(this.endPort),
|
|
|
|
includeTCP: this.tcpChecked,
|
|
|
|
includeUDP: this.udpChecked,
|
2018-05-31 07:03:59 +00:00
|
|
|
excludePorts: this.excludePorts,
|
2018-05-29 12:18:43 +00:00
|
|
|
discoverService: discoverService
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const discoverZone: DiscoverZone = {
|
|
|
|
discoverHost: {
|
2018-06-14 03:11:56 +00:00
|
|
|
metaIPType: toMetaIPType(this.ipType),
|
|
|
|
firstScanRange: this.startIP,
|
|
|
|
lastScanRange: this.endIP,
|
2018-05-31 07:03:59 +00:00
|
|
|
discoverPort: discoverPort,
|
2018-06-14 03:11:56 +00:00
|
|
|
excludeHosts: this.excludeIPs,
|
2018-05-29 12:18:43 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-05-31 13:26:14 +00:00
|
|
|
console.log(discoverZone);
|
2018-05-29 12:18:43 +00:00
|
|
|
this.requestDiscovery.emit(discoverZone);
|
|
|
|
}
|
|
|
|
|
2018-05-30 09:39:26 +00:00
|
|
|
checkEditableIPrange(cidr) {
|
|
|
|
const startIPArray = cidr.addressStart.parsedAddress;
|
|
|
|
const endIPArray = cidr.addressEnd.parsedAddress;
|
|
|
|
let count = 0;
|
|
|
|
endIPArray.forEach((element, idx) => {
|
|
|
|
if (element === startIPArray[idx]) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this.fixedIPrange = count;
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
onPortCheckChange(serviceCheckbox: Checkbox, checked: boolean) {
|
|
|
|
if (!checked) {
|
|
|
|
serviceCheckbox.checked = false;
|
|
|
|
this.serviceChecked = false;
|
2018-05-30 09:39:26 +00:00
|
|
|
} else {
|
|
|
|
if (!this.tcpChecked && !this.udpChecked) {
|
|
|
|
this.tcpChecked = true;
|
|
|
|
this.udpChecked = true;
|
|
|
|
}
|
2018-05-29 12:18:43 +00:00
|
|
|
}
|
2018-05-24 03:16:10 +00:00
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
onServiceCheckChange(portCheckbox: Checkbox, checked: boolean) {
|
|
|
|
if (checked) {
|
|
|
|
portCheckbox.checked = true;
|
|
|
|
this.portChecked = true;
|
|
|
|
}
|
2018-05-18 06:57:43 +00:00
|
|
|
}
|
2018-05-23 03:34:16 +00:00
|
|
|
|
2018-05-30 09:39:26 +00:00
|
|
|
onPortTypeCheck(portCheckbox: Checkbox, serviceCheckbox: Checkbox): void {
|
|
|
|
if (!this.tcpChecked && !this.udpChecked) {
|
|
|
|
this.portChecked = false;
|
|
|
|
portCheckbox.checked = false;
|
|
|
|
this.serviceChecked = false;
|
|
|
|
serviceCheckbox.checked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-29 12:18:43 +00:00
|
|
|
onInputIP(e, idx: number) {
|
|
|
|
const value = e.value;
|
|
|
|
if (idx === 0) {
|
|
|
|
this.startIP = value;
|
|
|
|
} else {
|
|
|
|
this.endIP = value;
|
|
|
|
}
|
|
|
|
this.ipErrMsg = this.validateIP(value, idx);
|
|
|
|
|
|
|
|
if (this.ipErrMsg) {
|
|
|
|
this.validation = false;
|
|
|
|
} else {
|
|
|
|
this.validation = true;
|
|
|
|
}
|
2018-05-25 10:56:28 +00:00
|
|
|
}
|
2018-05-29 12:18:43 +00:00
|
|
|
|
|
|
|
validateIP(value: string, idx): string {
|
|
|
|
if (!this.isValidIPregex(value)) {
|
|
|
|
return 'Not valid IP format.';
|
|
|
|
}
|
|
|
|
if (!ipRangeCheck(value, this.probeHost.probe.cidr)) {
|
|
|
|
return 'Not valid IP range.';
|
|
|
|
}
|
|
|
|
const ipArray = [this.startIP, this.endIP];
|
|
|
|
const sortedIpArray = this.sortIP([this.startIP, this.endIP]);
|
|
|
|
if (ipArray[0] !== sortedIpArray[0]) {
|
|
|
|
return 'Not valiad range';
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
isValidIPregex(ip: string): boolean {
|
2018-06-14 03:11:56 +00:00
|
|
|
if (this.ipType === MetaIPTypeEnum.V4) {
|
2018-05-29 12:18:43 +00:00
|
|
|
return /^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(ip);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
sortIP(ipAddressArray) {
|
2018-05-30 14:05:58 +00:00
|
|
|
return ipAddressArray.sort((a, b) => {
|
2018-05-29 12:18:43 +00:00
|
|
|
a = a.split('.');
|
|
|
|
b = b.split('.');
|
|
|
|
for (let i = 0; i < a.length; i++) {
|
|
|
|
// tslint:disable-next-line:radix
|
|
|
|
if ((a[i] = parseInt(a[i])) < (b[i] = parseInt(b[i]))) {
|
|
|
|
return -1;
|
|
|
|
} else if (a[i] > b[i]) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
onInputPort(portNum: string, idx: number) {
|
|
|
|
if (idx === 0) {
|
|
|
|
this.startPort = portNum;
|
|
|
|
} else {
|
|
|
|
this.endPort = portNum;
|
|
|
|
}
|
|
|
|
this.portErrMsg = this.validatePort(portNum);
|
|
|
|
|
|
|
|
if (this.portErrMsg) {
|
|
|
|
this.validation = false;
|
|
|
|
} else {
|
|
|
|
this.validation = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
validatePort(portNum): string {
|
|
|
|
if (Number(this.startPort) > Number(this.endPort)) {
|
|
|
|
return 'Not valid port range.';
|
|
|
|
}
|
|
|
|
if (Number(portNum) <= 0 || Number(portNum) > 65535) {
|
|
|
|
return 'Not valid port range.';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-30 14:05:58 +00:00
|
|
|
|
2018-05-31 07:03:59 +00:00
|
|
|
onAddExcludePort(event) {
|
|
|
|
const port = event.value.replace(/\s/g, '');
|
|
|
|
if (port.indexOf('~') > 0) { // e.g. 1~3000
|
|
|
|
const splited = port.split('~');
|
|
|
|
this.portChips.pop();
|
|
|
|
const from = Number(splited[0]);
|
|
|
|
const to = Number(splited[1]);
|
|
|
|
if (this.checkInvalidPort(from)
|
|
|
|
|| this.checkInvalidPort(to)
|
|
|
|
|| !Number.isInteger(from)
|
|
|
|
|| !Number.isInteger(to)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const chipItem = 'All ' + from + ' ~ ' + to;
|
|
|
|
this.portChips.push(chipItem);
|
|
|
|
for (let i = from; i <= to; i++) {
|
|
|
|
this.excludePorts.push(String(i));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const num = Number(event.value);
|
|
|
|
if (!Number.isInteger(num) || this.checkInvalidPort(num) || this.checkExistExcludePort(event.value)) {
|
|
|
|
this.portChips.pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.excludePorts.push(event.value);
|
|
|
|
}
|
|
|
|
}
|
2018-05-30 14:05:58 +00:00
|
|
|
|
2018-05-31 07:03:59 +00:00
|
|
|
onRemoveExcludePort(event) {
|
|
|
|
if (event.value.indexOf('~') > 0) {
|
|
|
|
// e.g. 'All 1 ~ 30'
|
|
|
|
const splited = event.value.replace(/\s/g, '').replace('All', '').split('~');
|
|
|
|
for (let i = Number(splited[0]); i <= Number(splited[1]); i++) {
|
|
|
|
console.log(String(i));
|
|
|
|
const index = this.excludePorts.indexOf(String(i));
|
|
|
|
this.excludePorts.splice(index, 1);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const idx = this.excludePorts.indexOf(event.value);
|
|
|
|
this.excludePorts.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkExistExcludePort(portNum) {
|
|
|
|
return this.excludePorts.find((value) => {
|
|
|
|
return value === portNum;
|
|
|
|
});
|
2018-05-30 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 07:03:59 +00:00
|
|
|
checkInvalidPort(port) {
|
|
|
|
return port <= 0 || port > 65535;
|
2018-05-30 14:05:58 +00:00
|
|
|
}
|
|
|
|
|
2018-05-18 06:57:43 +00:00
|
|
|
}
|