utils.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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>Network Interfaces</h3>
  34. <p>Network Interface Card (NIC) currently installed on this host</p>
  35. <table id="network-interfaces-table" class="ui selectable inverted striped celled table">
  36. <thead>
  37. <tr>
  38. <th>Interface Name</th>
  39. <th>ID</th>
  40. <th>IP Address</th>
  41. </tr>
  42. </thead>
  43. <tbody></tbody>
  44. </table>
  45. <h3> IP Address to CIDR</h3>
  46. <p>No experience with CIDR notations? Here are some tools you can use to make setting up easier.</p>
  47. <div class="ui basic segment">
  48. <h5><i class="chevron down icon"></i> IP Range to CIDR Conversion</h5>
  49. <div class="ui message">
  50. <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.
  51. </div>
  52. <div class="ui input">
  53. <input type="text" placeholder="Start IP" id="startIpInput">
  54. </div>
  55. <div class="ui input">
  56. <input type="text" placeholder="End IP" id="endIpInput">
  57. </div>
  58. <br>
  59. <button style="margin-top: 0.6em;" class="ui basic button" onclick="convertToCIDR()">Convert</button>
  60. <p>Results: <div id="cidrOutput">N/A</div></p>
  61. </div>
  62. <div class="ui basic segment">
  63. <h5><i class="chevron down icon"></i> CIDR to IP Range Conversion</h5>
  64. <div class="ui action input">
  65. <input type="text" placeholder="CIDR" id="cidrInput">
  66. <button class="ui basic button" onclick="convertToIPRange()">Convert</button>
  67. </div>
  68. <p>Results: <div id="ipRangeOutput">N/A</div></p>
  69. </div>
  70. </div>
  71. <script>
  72. /*
  73. Account Password utilities
  74. */
  75. $.get("/api/auth/userCount", function(data){
  76. if (data == 0){
  77. //Using external auth manager. Hide options
  78. $(".selfauthOnly").hide();
  79. }
  80. })
  81. function changePassword() {
  82. const oldPassword = document.getElementsByName('oldPassword')[0].value;
  83. const newPassword = document.getElementsByName('newPassword')[0].value;
  84. const confirmNewPassword = document.getElementsByName('confirmNewPassword')[0].value;
  85. $.ajax({
  86. type: "POST",
  87. url: "/api/auth/changePassword",
  88. data: {
  89. oldPassword: oldPassword,
  90. newPassword: newPassword,
  91. confirmPassword: confirmNewPassword,
  92. },
  93. success: function (data) {
  94. if (data.error != undefined){
  95. alert(data.error);
  96. }else{
  97. $("#passwordChangeSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  98. $('[name="oldPassword"]').val('');
  99. $('[name="newPassword"]').val('');
  100. $('[name="confirmNewPassword"]').val('');
  101. }
  102. },
  103. error: function (xhr, status, error) {
  104. alert("Error changing password: " + error);
  105. },
  106. });
  107. }
  108. /*
  109. NIC Info
  110. */
  111. function renderNICInfo(){
  112. $.get("/api/stats/listnic",function(data){
  113. var tbody = document.querySelector("#network-interfaces-table tbody");
  114. data.forEach(function(item) {
  115. var tr = document.createElement("tr");
  116. var name = document.createElement("td");
  117. name.textContent = item.Name;
  118. var id = document.createElement("td");
  119. id.textContent = item.ID;
  120. var ips = document.createElement("td");
  121. ips.innerHTML = item.IPs.join("<br>");
  122. tr.appendChild(name);
  123. tr.appendChild(id);
  124. tr.appendChild(ips);
  125. tbody.appendChild(tr);
  126. });
  127. });
  128. }
  129. renderNICInfo();
  130. /*
  131. IP Address Utilities
  132. */
  133. //events handler
  134. function convertToCIDR() {
  135. const startIp = document.getElementById('startIpInput').value.trim();
  136. const endIp = document.getElementById('endIpInput').value.trim();
  137. const cidrOutput = document.getElementById('cidrOutput');
  138. const cidr = ipRangeToCIDR(startIp, endIp);
  139. const ipRange = cidrToRange(cidr);
  140. cidrOutput.innerHTML = `CIDR: ${cidr} <br> (Cover range: ${ipRange[0]} to ${ipRange[1]})`;
  141. }
  142. // CIDR to IP Range Conversion
  143. function convertToIPRange() {
  144. const cidr = document.getElementById('cidrInput').value.trim();
  145. const ipRangeOutput = document.getElementById('ipRangeOutput');
  146. const ipRange = cidrToRange(cidr);
  147. ipRangeOutput.innerHTML = `Start IP: ${ipRange[0]}<br>End IP: ${ipRange[1]}`;
  148. }
  149. //Ip conversion function
  150. function cidrToRange(cidr) {
  151. var range = [2];
  152. cidr = cidr.split('/');
  153. var cidr_1 = parseInt(cidr[1])
  154. range[0] = long2ip((ip2long(cidr[0])) & ((-1 << (32 - cidr_1))));
  155. start = ip2long(range[0])
  156. range[1] = long2ip( start + Math.pow(2, (32 - cidr_1)) - 1);
  157. return range;
  158. }
  159. function ipRangeToCIDR(ipStart, ipEnd) {
  160. var start = ip2long(ipStart);
  161. var end = ip2long(ipEnd);
  162. var cidr = 32;
  163. while (start != end) {
  164. start >>= 1;
  165. end >>= 1;
  166. cidr--;
  167. }
  168. return ipStart + '/' + cidr;
  169. }
  170. function ip2long (argIP) {
  171. // discuss at: https://locutus.io/php/ip2long/
  172. // original by: Waldo Malqui Silva (https://waldo.malqui.info)
  173. // improved by: Victor
  174. // revised by: fearphage (https://my.opera.com/fearphage/)
  175. // revised by: Theriault (https://github.com/Theriault)
  176. // estarget: es2015
  177. // example 1: ip2long('192.0.34.166')
  178. // returns 1: 3221234342
  179. // example 2: ip2long('0.0xABCDEF')
  180. // returns 2: 11259375
  181. // example 3: ip2long('255.255.255.256')
  182. // returns 3: false
  183. let i = 0
  184. // PHP allows decimal, octal, and hexadecimal IP components.
  185. // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
  186. const pattern = new RegExp([
  187. '^([1-9]\\d*|0[0-7]*|0x[\\da-f]+)',
  188. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  189. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  190. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?$'
  191. ].join(''), 'i')
  192. argIP = argIP.match(pattern) // Verify argIP format.
  193. if (!argIP) {
  194. // Invalid format.
  195. return false
  196. }
  197. // Reuse argIP variable for component counter.
  198. argIP[0] = 0
  199. for (i = 1; i < 5; i += 1) {
  200. argIP[0] += !!((argIP[i] || '').length)
  201. argIP[i] = parseInt(argIP[i]) || 0
  202. }
  203. // Continue to use argIP for overflow values.
  204. // PHP does not allow any component to overflow.
  205. argIP.push(256, 256, 256, 256)
  206. // Recalculate overflow of last component supplied to make up for missing components.
  207. argIP[4 + argIP[0]] *= Math.pow(256, 4 - argIP[0])
  208. if (argIP[1] >= argIP[5] ||
  209. argIP[2] >= argIP[6] ||
  210. argIP[3] >= argIP[7] ||
  211. argIP[4] >= argIP[8]) {
  212. return false
  213. }
  214. return argIP[1] * (argIP[0] === 1 || 16777216) +
  215. argIP[2] * (argIP[0] <= 2 || 65536) +
  216. argIP[3] * (argIP[0] <= 3 || 256) +
  217. argIP[4] * 1
  218. }
  219. function long2ip (ip) {
  220. // discuss at: https://locutus.io/php/long2ip/
  221. // original by: Waldo Malqui Silva (https://fayr.us/waldo/)
  222. // example 1: long2ip( 3221234342 )
  223. // returns 1: '192.0.34.166'
  224. if (!isFinite(ip)) {
  225. return false
  226. }
  227. return [ip >>> 24 & 0xFF, ip >>> 16 & 0xFF, ip >>> 8 & 0xFF, ip & 0xFF].join('.')
  228. }
  229. </script>