| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function bindTcpAndRelease(port, cb) { | 
					
						
							|  |  |  |   var server = require('net').createServer(); | 
					
						
							|  |  |  |   server.on('error', function (e) { | 
					
						
							|  |  |  |     cb(e); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   server.listen(port, function () { | 
					
						
							|  |  |  |     server.close(); | 
					
						
							|  |  |  |     cb(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 13:55:16 -06:00
										 |  |  | function bindUdpAndRelease(port, cb) { | 
					
						
							|  |  |  |   var socket = require('dgram').createSocket('udp4'); | 
					
						
							|  |  |  |   socket.on('error', function (e) { | 
					
						
							|  |  |  |     cb(e); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   socket.bind(port, function () { | 
					
						
							|  |  |  |     socket.close(); | 
					
						
							|  |  |  |     cb(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function checkTcpPorts(cb) { | 
					
						
							| 
									
										
										
										
											2017-04-26 20:16:47 -06:00
										 |  |  |   var bound = {}; | 
					
						
							|  |  |  |   var failed = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bindTcpAndRelease(80, function (e) { | 
					
						
							|  |  |  |     if (e) { | 
					
						
							|  |  |  |       failed[80] = e; | 
					
						
							|  |  |  |       //console.log(e.code);
 | 
					
						
							|  |  |  |       //console.log(e.message);
 | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       bound['80'] = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bindTcpAndRelease(443, function (e) { | 
					
						
							|  |  |  |       if (e) { | 
					
						
							|  |  |  |         failed[443] = e; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         bound['443'] = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (bound['80'] && bound['443']) { | 
					
						
							|  |  |  |         cb(null, bound); | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       console.warn("default ports 80 and 443 are not available, trying 8443"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       bindTcpAndRelease(8443, function (e) { | 
					
						
							|  |  |  |         if (e) { | 
					
						
							|  |  |  |           failed[8443] = e; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           bound['8443'] = true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         cb(failed, bound); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-03 13:55:16 -06:00
										 |  |  | function checkUdpPorts(cb) { | 
					
						
							|  |  |  |   var bound = {}; | 
					
						
							|  |  |  |   var failed = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   bindUdpAndRelease(53, function (e) { | 
					
						
							|  |  |  |     if (e) { | 
					
						
							|  |  |  |       failed[53] = e; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       bound[53] = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (bound[53]) { | 
					
						
							|  |  |  |       cb(null, bound); | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     console.warn("default DNS port 53 not available, trying 8053"); | 
					
						
							|  |  |  |     bindUdpAndRelease(8053, function (e) { | 
					
						
							|  |  |  |       if (e) { | 
					
						
							|  |  |  |         failed[8053] = e; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         bound[8053] = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 13:00:45 -06:00
										 |  |  |       cb(failed, bound); | 
					
						
							| 
									
										
										
										
											2017-05-03 13:55:16 -06:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports.checkTcpPorts = checkTcpPorts; | 
					
						
							|  |  |  | module.exports.checkUdpPorts = checkUdpPorts; |