app/src/commons/component/scanner-setting-dropdown.component.ts

228 lines
5.9 KiB
TypeScript
Raw Normal View History

2018-09-09 10:20:21 +00:00
import { Component, Input, OnInit, ViewChild, Output, EventEmitter } from '@angular/core';
2018-09-05 13:55:18 +00:00
import { DropdownPanelComponent } from '@overflow/commons/ui/component/primeng';
2018-09-10 07:37:42 +00:00
import { DiscoverHost, Zone } from '@overflow/model/discovery';
2018-09-09 10:20:21 +00:00
import { toMetaIPType, MetaIPTypeEnum, MetaPortTypeEnum } from '@overflow/model/meta';
2018-09-10 07:37:42 +00:00
import { DiscoveryConfigService } from '../service/discovery-config.service';
2018-08-17 10:53:40 +00:00
2018-09-11 01:40:28 +00:00
const IPCIDR = require('ip-cidr');
2018-09-10 07:45:54 +00:00
2018-08-17 10:53:40 +00:00
@Component({
selector: 'app-scanner-setting-dropdown',
templateUrl: './scanner-setting-dropdown.component.html',
styleUrls: ['./scanner-setting-dropdown.component.scss'],
})
2018-09-05 13:55:18 +00:00
export class ScannerSettingDropdownComponent implements OnInit {
2018-08-18 01:04:59 +00:00
@Input() blockTarget: any;
2018-09-09 10:20:21 +00:00
@Output() ready = new EventEmitter<DiscoverHost>();
2018-09-05 13:55:18 +00:00
@ViewChild('panel') panel: DropdownPanelComponent;
valid: boolean;
ipSummary: string;
portSummary: string;
2018-09-09 10:20:21 +00:00
ipType: MetaIPTypeEnum;
2018-09-05 13:55:18 +00:00
firstIP: string;
lastIP: string;
2018-09-09 10:20:21 +00:00
includePortType: MetaPortTypeEnum[];
2018-09-05 13:55:18 +00:00
firstPort: string;
lastPort: string;
ipErrMsg: string;
portErrMsg: string;
2018-09-10 08:51:14 +00:00
zone: Zone;
2018-09-10 09:19:07 +00:00
lastCondition: Condition = null;
2018-09-10 08:51:14 +00:00
2018-09-21 02:10:39 +00:00
validIPArray: string[];
2018-08-17 10:53:40 +00:00
constructor(
2018-09-10 07:37:42 +00:00
private discoveryConfigService: DiscoveryConfigService
2018-08-17 10:53:40 +00:00
) {
2018-09-05 13:55:18 +00:00
}
ngOnInit(): void {
2018-09-10 07:37:42 +00:00
this.discoveryConfigService.zone.subscribe(res => {
2018-09-10 08:51:14 +00:00
this.zone = res as Zone;
this.setDefault();
2018-09-10 07:37:42 +00:00
});
2018-09-05 13:55:18 +00:00
}
2018-09-10 08:51:14 +00:00
setDefault(): void {
if (this.zone === null) {
2018-09-10 07:37:42 +00:00
return;
}
2018-09-21 02:10:39 +00:00
const cidrUtil = new IPCIDR(this.zone.network);
this.validIPArray = cidrUtil.toArray();
2018-09-09 10:20:21 +00:00
this.ipType = MetaIPTypeEnum.V4;
2018-09-05 13:55:18 +00:00
2018-09-21 02:10:39 +00:00
this.firstIP = cidrUtil.start();
this.lastIP = cidrUtil.end();
2018-09-11 01:40:28 +00:00
this.includePortType = [MetaPortTypeEnum.TCP, MetaPortTypeEnum.UDP];
this.firstPort = '1';
this.lastPort = '65535';
2018-09-05 13:55:18 +00:00
this.ipErrMsg = '';
this.portErrMsg = '';
this.setSummary();
}
validateIP(value: string, idx: number) {
this.ipErrMsg = '';
switch (this.ipType) {
case 'V4':
if (!(/^(?=\d+\.\d+\.\d+\.\d+$)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])\.?){4}$/.test(value))) {
2018-09-10 09:19:07 +00:00
this.ipErrMsg = 'Invalid IP format.';
2018-09-05 13:55:18 +00:00
return;
}
2018-09-21 02:10:39 +00:00
if (this.validIPArray.indexOf(value) === -1) {
this.ipErrMsg = 'Invalid IP range.';
return;
}
2018-09-11 01:40:28 +00:00
const from = idx === 0 ? value : this.firstIP;
const to = idx === 1 ? value : this.lastIP;
2018-09-05 13:55:18 +00:00
if (this.ipToNum(from) > this.ipToNum(to)) {
2018-09-10 09:19:07 +00:00
this.ipErrMsg = 'Invalid IP range.';
2018-09-05 13:55:18 +00:00
return;
}
break;
case 'V6':
break;
default:
break;
}
}
2018-09-10 09:32:41 +00:00
ipPressed(evt: KeyboardEvent) {
2018-09-05 13:55:18 +00:00
if (!Number.isInteger(Number(evt.key)) && evt.key !== '.') {
return false;
}
}
2018-09-10 09:32:41 +00:00
portPressed(evt: KeyboardEvent) {
if (!Number.isInteger(Number(evt.key))) {
return false;
}
}
2018-09-05 13:55:18 +00:00
ipToNum(ip: string): number {
2018-09-13 05:47:22 +00:00
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-09-05 13:55:18 +00:00
}
validatePort(value: string, idx: number) {
this.portErrMsg = '';
2018-09-11 01:40:28 +00:00
const fromStr = idx === 0 ? value : this.firstPort;
const toStr = idx === 1 ? value : this.lastPort;
const from = Number(fromStr);
const to = Number(toStr);
2018-09-05 13:55:18 +00:00
if (from === NaN || to === NaN) {
2018-09-10 09:19:07 +00:00
this.portErrMsg = 'Invalid Port Type.';
2018-09-05 13:55:18 +00:00
return;
}
if (from <= 0) {
this.firstPort = '1';
return;
}
if (to > 65535) {
this.lastPort = '65535';
return;
}
if (from > to) {
2018-09-10 09:19:07 +00:00
this.portErrMsg = 'Invalid Port range.';
2018-09-05 13:55:18 +00:00
return;
}
}
validateTCP(checked: boolean) {
2018-09-09 10:20:21 +00:00
if (!checked && this.includePortType.indexOf(MetaPortTypeEnum.UDP) === -1) {
this.includePortType = [MetaPortTypeEnum.TCP];
2018-09-05 13:55:18 +00:00
}
}
validateUDP(checked: boolean) {
2018-09-09 10:20:21 +00:00
if (!checked && this.includePortType.indexOf(MetaPortTypeEnum.TCP) === -1) {
this.includePortType = [MetaPortTypeEnum.UDP];
2018-09-05 13:55:18 +00:00
}
}
done() {
// TODO: re-validation
2018-09-10 09:19:07 +00:00
this.saveLastCondition();
2018-09-05 13:55:18 +00:00
this.setSummary();
2018-09-10 08:51:14 +00:00
this.panel.hide();
2018-09-10 09:19:07 +00:00
}
cancel() {
if (null === this.lastCondition) {
this.setDefault();
} else {
this.restore();
}
}
saveLastCondition() {
2018-09-11 01:40:28 +00:00
const c: Condition = {
2018-09-10 09:19:07 +00:00
ipType: this.ipType,
firstIP: this.firstIP,
lastIP: this.lastIP,
includePortType: this.includePortType,
firstPort: this.firstPort,
lastPort: this.lastPort,
2018-09-11 01:40:28 +00:00
};
2018-09-10 09:19:07 +00:00
this.lastCondition = c;
}
2018-09-10 08:51:14 +00:00
2018-09-10 09:19:07 +00:00
restore() {
this.ipType = this.lastCondition.ipType;
this.firstIP = this.lastCondition.firstIP;
this.lastIP = this.lastCondition.lastIP;
this.includePortType = this.lastCondition.includePortType;
this.firstPort = this.lastCondition.firstPort;
this.lastPort = this.lastCondition.lastPort;
2018-09-10 09:32:41 +00:00
this.ipErrMsg = '';
this.portErrMsg = '';
2018-09-05 13:55:18 +00:00
}
2018-08-17 10:53:40 +00:00
2018-09-05 13:55:18 +00:00
setSummary(): void {
this.ipSummary = this.ipType + ' (' + this.firstIP + ' - ' + this.lastIP + ')';
this.portSummary = this.includePortType.join(',');
this.portSummary += ' (' + this.firstPort + ' - ' + this.lastPort + ')';
2018-09-09 10:20:21 +00:00
this.configDiscoverHost();
}
configDiscoverHost(): void {
const discoverHost: DiscoverHost = {
metaIPType: toMetaIPType(this.ipType),
firstScanRange: this.firstIP,
lastScanRange: this.lastIP,
2018-09-11 01:40:28 +00:00
discoveryConfig: {},
2018-09-09 10:20:21 +00:00
discoverPort: {
firstScanRange: Number(this.firstPort),
lastScanRange: Number(this.lastPort),
includeTCP: this.includePortType.indexOf(MetaPortTypeEnum.TCP) !== -1 ? true : false,
includeUDP: this.includePortType.indexOf(MetaPortTypeEnum.UDP) !== -1 ? true : false,
2018-09-11 01:52:43 +00:00
discoverService: {
includeServices: null,
}
2018-09-09 10:20:21 +00:00
}
2018-09-11 01:40:28 +00:00
};
2018-09-09 10:20:21 +00:00
this.ready.emit(discoverHost);
2018-08-17 10:53:40 +00:00
}
2018-09-10 08:51:14 +00:00
2018-08-17 10:53:40 +00:00
}
2018-09-10 09:19:07 +00:00
class Condition {
ipType: MetaIPTypeEnum;
firstIP: string;
lastIP: string;
includePortType: MetaPortTypeEnum[];
firstPort: string;
lastPort: string;
}