utils.html 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Utilities</h2>
  4. <p>You might find these tools or information helpful when setting up your gateway server</p>
  5. </div>
  6. <div class="ui divider"></div>
  7. <div class="selfauthOnly">
  8. <h3>Account Management</h3>
  9. <p>Functions to help management the current account</p>
  10. <div class="ui basic segment">
  11. <h5><i class="chevron down icon"></i> Change Password</h5>
  12. <div class="ui form">
  13. <div class="field">
  14. <label>Current Password</label>
  15. <input type="password" name="oldPassword" placeholder="Current Password">
  16. </div>
  17. <div class="field">
  18. <label>New Password</label>
  19. <input type="password" name="newPassword" placeholder="New Password">
  20. </div>
  21. <div class="field">
  22. <label>Confirm New Password</label>
  23. <input type="password" name="confirmNewPassword" placeholder="Confirm New Password">
  24. </div>
  25. <button class="ui basic button" onclick="changePassword()"><i class="ui teal key icon"></i> Change Password</button>
  26. </div>
  27. <div id="passwordChangeSuccMsg" class="ui green message" style="display:none;">
  28. <i class="ui circle checkmark green icon "></i> Password Updated
  29. </div>
  30. </div>
  31. <div class="ui divider"></div>
  32. </div>
  33. <h3> IP Address to CIDR</h3>
  34. <p>No experience with CIDR notations? Here are some tools you can use to make setting up easier.</p>
  35. <div class="ui basic segment">
  36. <h5><i class="chevron down icon"></i> IP Range to CIDR Conversion</h5>
  37. <div class="ui message">
  38. <i class="info circle icon"></i> Note that the CIDR generated here covers additional IP address before or after the given range. If you need more details settings, please use CIDR with a smaller range and add additional IPs for detail range adjustment.
  39. </div>
  40. <div class="ui input">
  41. <input type="text" placeholder="Start IP" id="startIpInput">
  42. </div>
  43. <div class="ui input">
  44. <input type="text" placeholder="End IP" id="endIpInput">
  45. </div>
  46. <br>
  47. <button style="margin-top: 0.6em;" class="ui basic button" onclick="convertToCIDR()">Convert</button>
  48. <p>Results: <div id="cidrOutput">N/A</div></p>
  49. </div>
  50. <div class="ui basic segment">
  51. <h5><i class="chevron down icon"></i> CIDR to IP Range Conversion</h5>
  52. <div class="ui action input">
  53. <input type="text" placeholder="CIDR" id="cidrInput">
  54. <button class="ui basic button" onclick="convertToIPRange()">Convert</button>
  55. </div>
  56. <p>Results: <div id="ipRangeOutput">N/A</div></p>
  57. </div>
  58. </div>
  59. <script>
  60. /*
  61. Account Password utilities
  62. */
  63. $.get("/api/auth/userCount", function(data){
  64. if (data == 0){
  65. //Using external auth manager. Hide options
  66. $(".selfauthOnly").hide();
  67. }
  68. })
  69. function changePassword() {
  70. const oldPassword = document.getElementsByName('oldPassword')[0].value;
  71. const newPassword = document.getElementsByName('newPassword')[0].value;
  72. const confirmNewPassword = document.getElementsByName('confirmNewPassword')[0].value;
  73. $.ajax({
  74. type: "POST",
  75. url: "/api/auth/changePassword",
  76. data: {
  77. oldPassword: oldPassword,
  78. newPassword: newPassword,
  79. confirmPassword: confirmNewPassword,
  80. },
  81. success: function (data) {
  82. if (data.error != undefined){
  83. alert(data.error);
  84. }else{
  85. $("#passwordChangeSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  86. $('[name="oldPassword"]').val('');
  87. $('[name="newPassword"]').val('');
  88. $('[name="confirmNewPassword"]').val('');
  89. }
  90. },
  91. error: function (xhr, status, error) {
  92. alert("Error changing password: " + error);
  93. },
  94. });
  95. }
  96. /*
  97. IP Address Utilities
  98. */
  99. //events handler
  100. function convertToCIDR() {
  101. const startIp = document.getElementById('startIpInput').value.trim();
  102. const endIp = document.getElementById('endIpInput').value.trim();
  103. const cidrOutput = document.getElementById('cidrOutput');
  104. const cidr = ipRangeToCIDR(startIp, endIp);
  105. const ipRange = cidrToRange(cidr);
  106. cidrOutput.innerHTML = `CIDR: ${cidr} <br> (Cover range: ${ipRange[0]} to ${ipRange[1]})`;
  107. }
  108. // CIDR to IP Range Conversion
  109. function convertToIPRange() {
  110. const cidr = document.getElementById('cidrInput').value.trim();
  111. const ipRangeOutput = document.getElementById('ipRangeOutput');
  112. const ipRange = cidrToRange(cidr);
  113. ipRangeOutput.innerHTML = `Start IP: ${ipRange[0]}<br>End IP: ${ipRange[1]}`;
  114. }
  115. //Ip conversion function
  116. function cidrToRange(cidr) {
  117. var range = [2];
  118. cidr = cidr.split('/');
  119. var cidr_1 = parseInt(cidr[1])
  120. range[0] = long2ip((ip2long(cidr[0])) & ((-1 << (32 - cidr_1))));
  121. start = ip2long(range[0])
  122. range[1] = long2ip( start + Math.pow(2, (32 - cidr_1)) - 1);
  123. return range;
  124. }
  125. function ipRangeToCIDR(ipStart, ipEnd) {
  126. var start = ip2long(ipStart);
  127. var end = ip2long(ipEnd);
  128. var cidr = 32;
  129. while (start != end) {
  130. start >>= 1;
  131. end >>= 1;
  132. cidr--;
  133. }
  134. return ipStart + '/' + cidr;
  135. }
  136. function ip2long (argIP) {
  137. // discuss at: https://locutus.io/php/ip2long/
  138. // original by: Waldo Malqui Silva (https://waldo.malqui.info)
  139. // improved by: Victor
  140. // revised by: fearphage (https://my.opera.com/fearphage/)
  141. // revised by: Theriault (https://github.com/Theriault)
  142. // estarget: es2015
  143. // example 1: ip2long('192.0.34.166')
  144. // returns 1: 3221234342
  145. // example 2: ip2long('0.0xABCDEF')
  146. // returns 2: 11259375
  147. // example 3: ip2long('255.255.255.256')
  148. // returns 3: false
  149. let i = 0
  150. // PHP allows decimal, octal, and hexadecimal IP components.
  151. // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
  152. const pattern = new RegExp([
  153. '^([1-9]\\d*|0[0-7]*|0x[\\da-f]+)',
  154. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  155. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  156. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?$'
  157. ].join(''), 'i')
  158. argIP = argIP.match(pattern) // Verify argIP format.
  159. if (!argIP) {
  160. // Invalid format.
  161. return false
  162. }
  163. // Reuse argIP variable for component counter.
  164. argIP[0] = 0
  165. for (i = 1; i < 5; i += 1) {
  166. argIP[0] += !!((argIP[i] || '').length)
  167. argIP[i] = parseInt(argIP[i]) || 0
  168. }
  169. // Continue to use argIP for overflow values.
  170. // PHP does not allow any component to overflow.
  171. argIP.push(256, 256, 256, 256)
  172. // Recalculate overflow of last component supplied to make up for missing components.
  173. argIP[4 + argIP[0]] *= Math.pow(256, 4 - argIP[0])
  174. if (argIP[1] >= argIP[5] ||
  175. argIP[2] >= argIP[6] ||
  176. argIP[3] >= argIP[7] ||
  177. argIP[4] >= argIP[8]) {
  178. return false
  179. }
  180. return argIP[1] * (argIP[0] === 1 || 16777216) +
  181. argIP[2] * (argIP[0] <= 2 || 65536) +
  182. argIP[3] * (argIP[0] <= 3 || 256) +
  183. argIP[4] * 1
  184. }
  185. function long2ip (ip) {
  186. // discuss at: https://locutus.io/php/long2ip/
  187. // original by: Waldo Malqui Silva (https://fayr.us/waldo/)
  188. // example 1: long2ip( 3221234342 )
  189. // returns 1: '192.0.34.166'
  190. if (!isFinite(ip)) {
  191. return false
  192. }
  193. return [ip >>> 24 & 0xFF, ip >>> 16 & 0xFF, ip >>> 8 & 0xFF, ip & 0xFF].join('.')
  194. }
  195. </script>