import { Component, Input, OnInit, ViewChild, Output, EventEmitter } from '@angular/core'; import { DropdownPanelComponent } from '@overflow/commons/ui/component/primeng'; import { DiscoverHost, Zone } from '@overflow/model/discovery'; import { toMetaIPType, MetaIPTypeEnum, MetaPortTypeEnum } from '@overflow/model/meta'; import { DiscoveryConfigService } from '../service/discovery-config.service'; @Component({ selector: 'app-scanner-setting-dropdown', templateUrl: './scanner-setting-dropdown.component.html', styleUrls: ['./scanner-setting-dropdown.component.scss'], }) export class ScannerSettingDropdownComponent implements OnInit { @Input() blockTarget: any; @Output() ready = new EventEmitter(); @ViewChild('panel') panel: DropdownPanelComponent; valid: boolean; ipSummary: string; portSummary: string; ipType: MetaIPTypeEnum; firstIP: string; lastIP: string; includePortType: MetaPortTypeEnum[]; firstPort: string; lastPort: string; ipErrMsg: string; portErrMsg: string; constructor( private discoveryConfigService: DiscoveryConfigService ) { } ngOnInit(): void { this.discoveryConfigService.zone.subscribe(res => { var zone = res as Zone; this.setDefault(zone); }); } setDefault(zone: Zone): void { if (zone === null) { return; } this.ipType = MetaIPTypeEnum.V4; this.firstIP = zone.address; this.lastIP = '192.168.1.254' this.includePortType = [MetaPortTypeEnum.TCP, MetaPortTypeEnum.UDP] this.firstPort = '1' this.lastPort = '65535' 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))) { this.ipErrMsg = 'INVALID_IP_FORMAT'; return; } let from = idx === 0 ? value : this.firstIP; let to = idx === 1 ? value : this.lastIP; if (this.ipToNum(from) > this.ipToNum(to)) { this.ipErrMsg = 'INVALID_IP_RANGE'; return; } break; case 'V6': break; default: break; } } ipPressed(evt: any) { if (!Number.isInteger(Number(evt.key)) && evt.key !== '.') { return false; } } ipToNum(ip: string): number { return ip.split('.').reduce(function (ipInt, octet) { return (ipInt << 8) + parseInt(octet, 10) }, 0) >>> 0; } validatePort(value: string, idx: number) { this.portErrMsg = ''; let fromStr = idx === 0 ? value : this.firstPort; let toStr = idx === 1 ? value : this.lastPort; let from = Number(fromStr); let to = Number(toStr) if (from === NaN || to === NaN) { this.portErrMsg = 'INVALID_PORT_TYPE'; return; } if (from <= 0) { this.firstPort = '1'; return; } if (to > 65535) { this.lastPort = '65535'; return; } if (from > to) { this.portErrMsg = 'INVALID_PORT_RANGE'; return; } } validateTCP(checked: boolean) { if (!checked && this.includePortType.indexOf(MetaPortTypeEnum.UDP) === -1) { this.includePortType = [MetaPortTypeEnum.TCP]; } } validateUDP(checked: boolean) { if (!checked && this.includePortType.indexOf(MetaPortTypeEnum.TCP) === -1) { this.includePortType = [MetaPortTypeEnum.UDP]; } } done() { // TODO: re-validation this.setSummary(); } setSummary(): void { this.ipSummary = this.ipType + ' (' + this.firstIP + ' - ' + this.lastIP + ')'; this.portSummary = this.includePortType.join(','); this.portSummary += ' (' + this.firstPort + ' - ' + this.lastPort + ')'; this.panel.hide(); this.configDiscoverHost(); } configDiscoverHost(): void { const discoverHost: DiscoverHost = { metaIPType: toMetaIPType(this.ipType), firstScanRange: this.firstIP, lastScanRange: this.lastIP, 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, } } this.ready.emit(discoverHost); } }