import { extendedPingOptions , pingResponse } from "../types" ; import parser from "./parser.interface" ; class windows implements parser { config : extendedPingOptions | undefined ; response : pingResponse ; constructor ( response , options ? ) { this . config = options ; this . response = response ; this . response . times = [ ] ; } processHeader ( line ) { let isPingNumeric = line . indexOf ( '[' ) === - 1 ; const ipv4v6Regex = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/ ; let words = line . split ( ' ' ) ; if ( isPingNumeric ) { this . response . host = words . find ( ( word ) => { return ipv4v6Regex . test ( word ) ; } ) ; this . response . numericHost = this . response . host ; } else { let numericHost = words . find ( function ( word ) { return word . indexOf ( '[' ) !== - 1 ; } ) ; if ( numericHost ) { let numericHostIndex = words . indexOf ( numericHost ) ; let match = /\[(.*)\]/ . exec ( numericHost ) ; if ( match ) { this . response . numericHost = match [ 1 ] ; } this . response . host = words [ numericHostIndex - 1 ] ; } } } processBody ( line ) { let isIPV6 = this . config ?. IPV6 ; let words = line . split ( ' ' ) ; let dataFields = words . filter ( ( word ) => { let isDataField = word . indexOf ( '=' ) >= 0 || word . indexOf ( '<' ) >= 0 ; return isDataField ; } ) ; if ( isIPV6 ) { this . processIPV6Body ( dataFields ) ; } else { this . processIPV4Body ( dataFields ) ; } } processFooter ( line ) { let packetLoss = line . match ( /([\d.]+)%/ ) ; if ( packetLoss ) { this . response . packetLoss = packetLoss [ 1 ] ; } if ( line . search ( /(ms|мсек)/i ) >= 0 ) { let regExp = /([0-9.]+)/g ; let m1 = regExp . exec ( line ) ; let m2 = regExp . exec ( line ) ; let m3 = regExp . exec ( line ) ; m1 ? this . response . min = parseFloat ( m1 [ 1 ] ) : null ; m2 ? this . response . max = parseFloat ( m2 [ 1 ] ) : null ; m3 ? this . response . avg = parseFloat ( m3 [ 1 ] ) : null ; } return this . response ; } processIPV4Body ( dataFields ) { const expectDataFieldInReplyLine = 3 ; let isReplyLine = dataFields . length >= expectDataFieldInReplyLine ; if ( isReplyLine ) { let timeField = dataFields . find ( ( field ) => { return field . search ( /(ms|мс)/i ) >= 0 ; } ) ; if ( timeField ) { let regExp = /([0-9.]+)/ ; let match = regExp . exec ( timeField ) ; let timeFieldIndex = dataFields . indexOf ( timeField ) ; match ? this . response ?. times ?. push ( parseFloat ( match [ 1 ] ) ) : null ; let bytesField = dataFields [ timeFieldIndex - 1 ] ; match = regExp . exec ( bytesField ) ; match ? this . response . bufferSize = parseFloat ( match [ 1 ] ) : null ; } } } processIPV6Body ( dataFields ) { let expectDataFieldInReplyLine = 1 ; if ( dataFields . length >= expectDataFieldInReplyLine ) { let timeField = dataFields . find ( ( dataField ) => { return dataField . search ( /(ms|мс)/i ) >= 0 ; } ) ; const regExp = /([0-9.]+)/ ; if ( timeField ) { let match = regExp . exec ( timeField ) ; match ? this . response ?. times ?. push ( parseFloat ( match [ 1 ] ) ) : null ; } } } } export default windows ;