Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

range_check.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. (function ()
  2. {
  3. var ipaddr = require('ipaddr.js'),
  4. ip6 = require('ip6');
  5. function isIP(addr)
  6. {
  7. return ipaddr.isValid(addr);
  8. }
  9. function isRange(range)
  10. {
  11. try {
  12. var cidr = ipaddr.parseCIDR(range);
  13. return true;
  14. } catch(err) {
  15. return false;
  16. }
  17. }
  18. function ver(addr)
  19. {
  20. try {
  21. var parse_addr = ipaddr.parse(addr);
  22. var kind = parse_addr.kind();
  23. if (kind === 'ipv4')
  24. {
  25. return 4; //IPv4
  26. }
  27. else if (kind === 'ipv6')
  28. {
  29. return 6; //IPv6
  30. }
  31. else
  32. {
  33. return 0; //not 4 or 6
  34. }
  35. }
  36. catch(err) {
  37. return 0; //not 4 or 6
  38. }
  39. }
  40. function isV4(addr)
  41. {
  42. return (ver(addr) === 4);
  43. }
  44. function isV6(addr)
  45. {
  46. return (ver(addr) === 6);
  47. }
  48. function storeIP(addr)
  49. {
  50. try {
  51. var parse_addr = ipaddr.parse(addr);
  52. var kind = parse_addr.kind();
  53. if (kind === 'ipv4') //is a plain v4 address
  54. {
  55. return addr;
  56. }
  57. else if (kind === 'ipv6')
  58. {
  59. if (parse_addr.isIPv4MappedAddress()) //convert v4 mapped to v6 addresses to a v4 in it's original format
  60. {
  61. return parse_addr.toIPv4Address().toString();
  62. }
  63. else //is a v6, abbreviate it
  64. {
  65. return ip6.abbreviate(addr);
  66. }
  67. }
  68. else
  69. {
  70. return null; //invalid IP address
  71. }
  72. }
  73. catch(err) {
  74. return null; //invalid IP address
  75. }
  76. }
  77. function displayIP(addr)
  78. {
  79. try {
  80. var parse_addr = ipaddr.parse(addr);
  81. var kind = parse_addr.kind();
  82. if (kind === 'ipv4') //is a plain v4 address
  83. {
  84. return addr;
  85. }
  86. else if (kind === 'ipv6')
  87. {
  88. if (parse_addr.isIPv4MappedAddress()) //convert v4 mapped to v6 addresses to a v4 in it's original format
  89. {
  90. return parse_addr.toIPv4Address().toString();
  91. }
  92. else //is a v6, normalize it
  93. {
  94. return ip6.normalize(addr);
  95. }
  96. }
  97. else
  98. {
  99. return ''; //invalid IP address
  100. }
  101. }
  102. catch(err) {
  103. return ''; //invalid IP address
  104. }
  105. }
  106. function inRange(addr, range)
  107. {
  108. if (typeof range === 'string')
  109. {
  110. if (range.indexOf('/') !== -1)
  111. {
  112. try {
  113. var range_data = range.split('/');
  114. var parse_addr = ipaddr.parse(addr);
  115. var parse_range = ipaddr.parse(range_data[0]);
  116. return parse_addr.match(parse_range, range_data[1]);
  117. }
  118. catch(err) {
  119. return false;
  120. }
  121. }
  122. else
  123. {
  124. addr = (isV6(addr)) ? ip6.normalize(addr) : addr; //v6 normalize addr
  125. range = (isV6(range)) ? ip6.normalize(range) : range; //v6 normalize range
  126. return isIP(range) && addr === range;
  127. }
  128. }
  129. else if (range && typeof range === 'object') //list
  130. {
  131. for (var check_range in range)
  132. {
  133. if (inRange(addr, range[check_range]) === true)
  134. {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. else
  141. {
  142. return false;
  143. }
  144. }
  145. // Export public API
  146. var range_check = {};
  147. //Validate IP Address
  148. range_check.vaild_ip = range_check.vaildIp = isIP;
  149. range_check.valid_ip = range_check.validIp = isIP;
  150. range_check.isIP = isIP;
  151. //isV4 and isV6
  152. range_check.isV4 = isV4;
  153. range_check.isV6 = isV6;
  154. //storeIP, searchIP and displayIP
  155. range_check.storeIP = storeIP;
  156. range_check.searchIP = storeIP;
  157. range_check.displayIP = displayIP;
  158. //Validate Range
  159. range_check.valid_range = range_check.validRange = isRange;
  160. range_check.isRange = isRange;
  161. //Others
  162. range_check.ver = ver;
  163. range_check.in_range = range_check.inRange = inRange;
  164. module.exports = range_check;
  165. }());