@@ -37,34 +37,53 @@ export class networkHelper {
3737 return Promise . resolve ( result ) ;
3838 }
3939
40- static async PingHost ( hostName :string , numberOfEchos = 3 , timeout = 1 ) : Promise < IotResult > {
40+ static async PingHost ( hostName :string , numberOfEchos = 3 , timeout = 1 , getHostname : boolean = false ) : Promise < IotResult > {
4141 let result :IotResult ;
4242 result = await this . GetIpAddress ( hostName ) ;
4343 if ( result . Status == StatusResult . Error ) return Promise . resolve ( result ) ;
4444 const ipAddress = < string > result . returnObject ;
4545 const msg = `The host is unavailable. Host: ${ hostName } IP-Address: ${ ipAddress } .` ;
46+ const option :pingOptions = {
47+ numberOfEchos : numberOfEchos ,
48+ timeout : timeout ,
49+ logToFile : false ,
50+ IPV4 : true ,
51+ numeric : getHostname
52+ } ;
4653 try
4754 {
48- const response = await ping ( ipAddress , { logToFile : false , numberOfEchos : numberOfEchos , timeout : timeout , IPV4 : true } ) ;
55+ const response = await ping ( ipAddress , option ) ;
4956 const packetLoss = + ( response . packetLoss ?? "100" ) ;
50- if ( packetLoss > 50 )
51- result = new IotResult ( StatusResult . Error , msg ) ; else result = new IotResult ( StatusResult . Ok , `Host ${ ipAddress } is available.` ) ;
57+ if ( packetLoss > 50 ) {
58+ result = new IotResult ( StatusResult . Error , msg ) ;
59+ } else {
60+ result = new IotResult ( StatusResult . Ok , `Host ${ ipAddress } is available.` ) ;
61+ if ( getHostname && response . host && response . host != `` ) result . returnObject = response . host ;
62+ }
5263 } catch ( err :any ) {
5364 result = new IotResult ( StatusResult . Error , msg , err ) ;
5465 }
5566 return Promise . resolve ( result ) ;
5667 }
5768
58- static async CheckTcpPortUsed ( hostName :string , port : number ) : Promise < IotResult > {
69+ static async CheckTcpPortUsed ( hostName :string , port : number , retryTimeMs :number = 200 , timeOutMs :number = 1000 ) : Promise < IotResult > {
70+ //[retryTimeMs] the retry interval in milliseconds - defaultis is 200ms
71+ //[timeOutMs] the amount of time to wait until port is free default is 1000ms
5972 let result :IotResult ;
6073 result = await this . GetIpAddress ( hostName ) ;
6174 if ( result . Status == StatusResult . Error ) return Promise . resolve ( result ) ;
6275 const ipAddress = < string > result . returnObject ;
6376 //next
6477 const msg = `${ port } port unavailable. Host: ${ hostName } IP-Address: ${ ipAddress } .` ;
78+ const tcpPortUsedOptions :tcpPortUsed . TcpPortUsedOptions = {
79+ port : port ,
80+ host : ipAddress ,
81+ retryTimeMs : retryTimeMs ,
82+ timeOutMs : timeOutMs
83+ } ;
6584 try
6685 {
67- const inUse = await tcpPortUsed . check ( port , ipAddress ) ;
86+ const inUse = await tcpPortUsed . check ( tcpPortUsedOptions ) ;
6887 if ( inUse )
6988 result = new IotResult ( StatusResult . Ok , `Network port ${ port } host ${ ipAddress } available.` ) ; else result = new IotResult ( StatusResult . Error , msg ) ;
7089 } catch ( err :any ) {
@@ -88,19 +107,22 @@ export class networkHelper {
88107 }
89108
90109 static GetLocalIPaddress ( ) : Map < string , string > {
110+ //otput: 1. wireless => 192.168.10.24
91111 let results :Map < string , string > = new Map < string , string > ( ) ;
92- const { networkInterfaces } = require ( 'os' ) ;
93- const nets = networkInterfaces ( ) ;
94- for ( const name of Object . keys ( nets ) ) {
95- for ( const net of nets [ name ] ) {
96- // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
97- // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
98- const familyV4Value = typeof net . family === 'string' ? 'IPv4' : 4
99- if ( net . family === familyV4Value && ! net . internal ) {
100- results . set ( name , net . address ) ;
112+ try {
113+ const { networkInterfaces } = require ( 'os' ) ;
114+ const nets = networkInterfaces ( ) ;
115+ for ( const name of Object . keys ( nets ) ) {
116+ for ( const net of nets [ name ] ) {
117+ // Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
118+ // 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
119+ const familyV4Value = typeof net . family === 'string' ? 'IPv4' : 4
120+ if ( net . family === familyV4Value && ! net . internal ) {
121+ results . set ( name , net . address ) ;
122+ }
101123 }
102124 }
103- }
125+ } catch ( err : any ) { }
104126 return results ;
105127 }
106128
@@ -123,5 +145,4 @@ export class networkHelper {
123145 return Promise . resolve ( rangeIP ) ;
124146 }
125147
126-
127148}
0 commit comments