1
0

utils.html 8.1 KB

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