import Os from 'os' ; export class Network { static masks ( cidr ) { const subnet = parseInt ( cidr . split ( '/' ) . pop ( ) || '24' , 10 ) ; const ipaddress = ( cidr . split ( '/' ) . shift ( ) || '' ) . split ( '.' ) . map ( ( b ) => parseInt ( b , 10 ) ) ; const masks = ( `${ ( '1' ) . repeat ( subnet ) }${ '0' . repeat ( 32 - subnet ) }` ) . match ( /.{1,8}/g ) ?. map ( ( b ) => parseInt ( b , 2 ) ) ; const inverted = masks ?. map ( ( b ) => b ^ 255 ) ; const base = ipaddress . map ( ( block , index ) => block & masks ! [ index ] ) ; const broadcast = base . map ( ( block , index ) => block | inverted ! [ index ] ) ; return { base : base . join ( '.' ) , broadcast : broadcast . join ( '.' ) , } ; } static network ( ) { const ifaces = Os . networkInterfaces ( ) ; const results = [ ] ; Object . keys ( ifaces ) . forEach ( ( ifname ) => { ifaces [ ifname ] ! . forEach ( ( iface ) => { if ( iface . family !== 'IPv4' || iface . internal !== false ) { return ; } if ( results . indexOf ( iface . address ) === - 1 ) { results . push ( `${ iface . cidr }` ) ; } } ) ; } ) ; return results ; } static subnets ( ) { const network = Network . network ( ) ; const masks = network . map ( ( n ) => Network . masks ( n ) ) ; return masks ; } }