1
0

utils.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. <h3>Forget Password Email</h3>
  33. <p>The following SMTP settings help you to reset your password in case you have lost your management account.</p>
  34. <form id="email-form" class="ui form">
  35. <div class="field">
  36. <label>Sender Address</label>
  37. <input type="text" name="senderAddr" placeholder="E.g. [email protected]">
  38. </div>
  39. <div class="field">
  40. <p><i class="caret down icon"></i> Connection setup for email service provider</p>
  41. <div class="fields">
  42. <div class="twelve wide field">
  43. <label>SMTP Provider Hostname</label>
  44. <input type="text" name="hostname" placeholder="E.g. mail.gandi.net">
  45. </div>
  46. <div class="four wide field">
  47. <label>Port</label>
  48. <input type="number" name="port" placeholder="E.g. 587" value="587">
  49. </div>
  50. </div>
  51. </div>
  52. <div class="field">
  53. <p><i class="caret down icon"></i> Credentials for SMTP server authentications</p>
  54. <div class="two fields">
  55. <div class="field">
  56. <label>Sender Username</label>
  57. <input type="text" name="username" placeholder="E.g. admin">
  58. </div>
  59. <div class="field">
  60. <label>Sender Domain</label>
  61. <div class="ui labeled input">
  62. <div class="ui basic label">
  63. @
  64. </div>
  65. <input type="text" name="domain" min="1" max="65534" placeholder="E.g. arozos.com">
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <div class="field">
  71. <label>Sender Password</label>
  72. <input type="password" name="password" placeholder="Password of the email account">
  73. <small>Leave empty to use the old password</small>
  74. </div>
  75. <p> <i class="caret down icon"></i> Email for sending account reset link</p>
  76. <div class="field">
  77. <label>Admin / Receiver Address</label>
  78. <input type="text" name="recvAddr" placeholder="E.g. [email protected]">
  79. </div>
  80. <button class="ui basic button" type="submit"><i class="blue save icon"></i> Set SMTP Configs</button>
  81. <button class="ui basic button" onclick="event.preventDefault(); sendTestEmail(this);"><i class="teal mail icon"></i> Send Test Email</button>
  82. </form>
  83. </div>
  84. <div class="ui divider"></div>
  85. <h3> IP Address to CIDR</h3>
  86. <p>No experience with CIDR notations? Here are some tools you can use to make setting up easier.</p>
  87. <div class="ui basic segment">
  88. <h5><i class="chevron down icon"></i> IP Range to CIDR Conversion</h5>
  89. <div class="ui message">
  90. <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.
  91. </div>
  92. <div class="ui input">
  93. <input type="text" placeholder="Start IP" id="startIpInput">
  94. </div>
  95. <div class="ui input">
  96. <input type="text" placeholder="End IP" id="endIpInput">
  97. </div>
  98. <br>
  99. <button style="margin-top: 0.6em;" class="ui basic button" onclick="convertToCIDR()">Convert</button>
  100. <p>Results: <div id="cidrOutput">N/A</div></p>
  101. </div>
  102. <div class="ui basic segment">
  103. <h5><i class="chevron down icon"></i> CIDR to IP Range Conversion</h5>
  104. <div class="ui action input">
  105. <input type="text" placeholder="CIDR" id="cidrInput">
  106. <button class="ui basic button" onclick="convertToIPRange()">Convert</button>
  107. </div>
  108. <p>Results: <div id="ipRangeOutput">N/A</div></p>
  109. </div>
  110. <!-- Config Tools -->
  111. <div class="ui divider"></div>
  112. <h3>System Backup & Restore</h3>
  113. <p>Options related to system backup, migrate and restore.</p>
  114. <button class="ui basic button" onclick="showSideWrapper('snippet/configTools.html');">Open Config Tools</button>
  115. <!-- System Information -->
  116. <div class="ui divider"></div>
  117. <div id="zoraxyinfo">
  118. <h3 class="ui header">
  119. System Information
  120. </h3>
  121. <p>Basic information about this zoraxy host</p>
  122. <table class="ui very basic collapsing celled table">
  123. <tbody>
  124. <tr>
  125. <td>Host UUID</td>
  126. <td class="uuid"></td>
  127. </tr>
  128. <tr>
  129. <td>Version</td>
  130. <td class="version"></td>
  131. </tr>
  132. <tr>
  133. <td>Build</td>
  134. <td class="development"></td>
  135. </tr>
  136. <tr>
  137. <td>Running Since</td>
  138. <td class="boottime"></td>
  139. </tr>
  140. <tr>
  141. <td>ZeroTier Linked</td>
  142. <td class="zt"></td>
  143. </tr>
  144. <tr>
  145. <td>Enable SSH Loopback</td>
  146. <td class="sshlb"></td>
  147. </tr>
  148. </tbody>
  149. </table>
  150. <p>Zoraxy is developed by tobychui for <a href="//imuslab.com" target="_blank">imuslab</a> and open source under <a href="https://www.gnu.org/licenses/agpl-3.0.txt">AGPL</a></p>
  151. </div>
  152. <br>
  153. </div>
  154. <script>
  155. /*
  156. Account Password utilities
  157. */
  158. $.get("/api/auth/userCount", function(data){
  159. if (data == 0){
  160. //Using external auth manager. Hide options
  161. $(".selfauthOnly").hide();
  162. }
  163. });
  164. $.get("/api/info/x", function(data){
  165. function timeConverter(UNIX_timestamp){
  166. var a = new Date(UNIX_timestamp * 1000);
  167. var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  168. var year = a.getFullYear();
  169. var month = months[a.getMonth()];
  170. var date = a.getDate();
  171. var hour = a.getHours();
  172. var min = a.getMinutes();
  173. var sec = a.getSeconds();
  174. var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
  175. return time;
  176. }
  177. function secondsToDhms(seconds) {
  178. seconds = Number(seconds);
  179. var d = Math.floor(seconds / (3600*24));
  180. var h = Math.floor(seconds % (3600*24) / 3600);
  181. var m = Math.floor(seconds % 3600 / 60);
  182. var s = Math.floor(seconds % 60);
  183. var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : "";
  184. var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : "";
  185. var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : "";
  186. var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : "";
  187. return dDisplay + hDisplay + mDisplay + sDisplay;
  188. }
  189. console.log(data);
  190. $("#zoraxyinfo .uuid").text(data.NodeUUID);
  191. $("#zoraxyinfo .development").text(data.Development?"Development":"Release");
  192. $("#zoraxyinfo .version").text(data.Version);
  193. $("#zoraxyinfo .boottime").text(timeConverter(data.BootTime) + ` ( ${secondsToDhms(parseInt(Date.now()/1000) - data.BootTime)} ago)`);
  194. $("#zoraxyinfo .zt").html(data.ZerotierConnected?`<i class="ui green check icon"></i> Connected`:`<i class="ui red times icon"></i> Link Error`);
  195. $("#zoraxyinfo .sshlb").html(data.EnableSshLoopback?`<i class="ui yellow exclamation triangle icon"></i> Enabled`:`Disabled`);
  196. });
  197. function changePassword() {
  198. const oldPassword = document.getElementsByName('oldPassword')[0].value;
  199. const newPassword = document.getElementsByName('newPassword')[0].value;
  200. const confirmNewPassword = document.getElementsByName('confirmNewPassword')[0].value;
  201. $.ajax({
  202. type: "POST",
  203. url: "/api/auth/changePassword",
  204. data: {
  205. oldPassword: oldPassword,
  206. newPassword: newPassword,
  207. confirmPassword: confirmNewPassword,
  208. },
  209. success: function (data) {
  210. if (data.error != undefined){
  211. alert(data.error);
  212. }else{
  213. $("#passwordChangeSuccMsg").stop().finish().slideDown("fast").delay(3000).slideUp("fast");
  214. $('[name="oldPassword"]').val('');
  215. $('[name="newPassword"]').val('');
  216. $('[name="confirmNewPassword"]').val('');
  217. }
  218. },
  219. error: function (xhr, status, error) {
  220. alert("Error changing password: " + error);
  221. },
  222. });
  223. }
  224. /*
  225. SMTP Settings
  226. */
  227. //Bind events to the form
  228. $('#email-form').submit(function(e) {
  229. e.preventDefault();
  230. var data = {
  231. hostname: $('input[name=hostname]').val(),
  232. domain: $('input[name=domain]').val(),
  233. port: parseInt($('input[name=port]').val()),
  234. username: $('input[name=username]').val(),
  235. password: $('input[name=password]').val(),
  236. senderAddr: $('input[name=senderAddr]').val(),
  237. adminAddr: $('input[name=recvAddr]').val()
  238. };
  239. var inputValid = validateSMTPInputs();
  240. if (!inputValid){
  241. msgbox("SMTP input not valid", false, 5000);
  242. return;
  243. }
  244. $.ajax({
  245. type: "POST",
  246. url: "/api/tools/smtp/set",
  247. data: data,
  248. success: function(data) {
  249. if (data.error != undefined){
  250. msgbox(data.error, false, 5000);
  251. }else{
  252. msgbox("SMTP Account Updated")
  253. }
  254. },
  255. error: function(xhr, status, error) {
  256. msgbox(xhr.responseText, false, 5000);
  257. }
  258. });
  259. });
  260. function initSMTPSettings(){
  261. $.get("/api/tools/smtp/get", function(data){
  262. $('#email-form input[name=hostname]').val(data.Hostname);
  263. $('#email-form input[name=domain]').val(data.Domain);
  264. $('#email-form input[name=port]').val(data.Port);
  265. $('#email-form input[name=username]').val(data.Username);
  266. $('#email-form input[name=senderAddr]').val(data.SenderAddr);
  267. });
  268. $.get("/api/tools/smtp/admin", function(data){
  269. $('#email-form input[name=recvAddr]').val(data);
  270. });
  271. }
  272. initSMTPSettings();
  273. function sendTestEmail(btn){
  274. $(btn).addClass("loading").addClass("disabled");
  275. $.get("/api/tools/smtp/test", function(data){
  276. if (data.error !== undefined){
  277. msgbox(data.error, false, 5000);
  278. }else{
  279. msgbox("Test Email Sent")
  280. }
  281. $(btn).removeClass("loading").removeClass("disabled");
  282. })
  283. }
  284. function validateSMTPInputs() {
  285. let isValid = true;
  286. const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; // email regex pattern
  287. const domainRegex = /^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$/i; // domain/subdomain regex pattern
  288. const form = $('#email-form');
  289. // validate hostname
  290. const hostname = form.find('input[name="hostname"]').val().trim();
  291. if (!domainRegex.test(hostname)) {
  292. form.find('input[name="hostname"]').parent().addClass('error');
  293. isValid = false;
  294. } else {
  295. form.find('input[name="hostname"]').parent().removeClass('error');
  296. }
  297. // validate domain
  298. const domain = form.find('input[name="domain"]').val().trim();
  299. if (!domainRegex.test(domain)) {
  300. form.find('input[name="domain"]').parent().addClass('error');
  301. isValid = false;
  302. } else {
  303. form.find('input[name="domain"]').parent().removeClass('error');
  304. }
  305. // validate username
  306. const username = form.find('input[name="username"]').val().trim();
  307. if (username === '') {
  308. form.find('input[name="username"]').parent().addClass('error');
  309. isValid = false;
  310. } else {
  311. form.find('input[name="username"]').parent().removeClass('error');
  312. }
  313. // validate password
  314. const password = form.find('input[name="password"]').val().trim();
  315. if (password === '') {
  316. form.find('input[name="password"]').parent().addClass('error');
  317. isValid = false;
  318. } else {
  319. form.find('input[name="password"]').parent().removeClass('error');
  320. }
  321. // validate sender address
  322. const senderAddr = form.find('input[name="senderAddr"]').val().trim();
  323. if (!emailRegex.test(senderAddr)) {
  324. form.find('input[name="senderAddr"]').parent().addClass('error');
  325. isValid = false;
  326. } else {
  327. form.find('input[name="senderAddr"]').parent().removeClass('error');
  328. }
  329. // validate receiver address
  330. const recvAddr = form.find('input[name="recvAddr"]').val().trim();
  331. if (!emailRegex.test(recvAddr)) {
  332. form.find('input[name="recvAddr"]').parent().addClass('error');
  333. isValid = false;
  334. } else {
  335. form.find('input[name="recvAddr"]').parent().removeClass('error');
  336. }
  337. return isValid;
  338. }
  339. /*
  340. IP Address Utilities
  341. */
  342. //events handler
  343. function convertToCIDR() {
  344. const startIp = document.getElementById('startIpInput').value.trim();
  345. const endIp = document.getElementById('endIpInput').value.trim();
  346. const cidrOutput = document.getElementById('cidrOutput');
  347. const cidr = ipRangeToCIDR(startIp, endIp);
  348. const ipRange = cidrToRange(cidr);
  349. cidrOutput.innerHTML = `CIDR: ${cidr} <br> (Cover range: ${ipRange[0]} to ${ipRange[1]})`;
  350. }
  351. // CIDR to IP Range Conversion
  352. function convertToIPRange() {
  353. const cidr = document.getElementById('cidrInput').value.trim();
  354. const ipRangeOutput = document.getElementById('ipRangeOutput');
  355. const ipRange = cidrToRange(cidr);
  356. ipRangeOutput.innerHTML = `Start IP: ${ipRange[0]}<br>End IP: ${ipRange[1]}`;
  357. }
  358. //Ip conversion function
  359. function cidrToRange(cidr) {
  360. var range = [2];
  361. cidr = cidr.split('/');
  362. var cidr_1 = parseInt(cidr[1])
  363. range[0] = long2ip((ip2long(cidr[0])) & ((-1 << (32 - cidr_1))));
  364. start = ip2long(range[0])
  365. range[1] = long2ip( start + Math.pow(2, (32 - cidr_1)) - 1);
  366. return range;
  367. }
  368. function ipRangeToCIDR(ipStart, ipEnd) {
  369. var start = ip2long(ipStart);
  370. var end = ip2long(ipEnd);
  371. var cidr = 32;
  372. while (start != end) {
  373. start >>= 1;
  374. end >>= 1;
  375. cidr--;
  376. }
  377. return ipStart + '/' + cidr;
  378. }
  379. function ip2long (argIP) {
  380. // discuss at: https://locutus.io/php/ip2long/
  381. // original by: Waldo Malqui Silva (https://waldo.malqui.info)
  382. // improved by: Victor
  383. // revised by: fearphage (https://my.opera.com/fearphage/)
  384. // revised by: Theriault (https://github.com/Theriault)
  385. // estarget: es2015
  386. // example 1: ip2long('192.0.34.166')
  387. // returns 1: 3221234342
  388. // example 2: ip2long('0.0xABCDEF')
  389. // returns 2: 11259375
  390. // example 3: ip2long('255.255.255.256')
  391. // returns 3: false
  392. let i = 0
  393. // PHP allows decimal, octal, and hexadecimal IP components.
  394. // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
  395. const pattern = new RegExp([
  396. '^([1-9]\\d*|0[0-7]*|0x[\\da-f]+)',
  397. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  398. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?',
  399. '(?:\\.([1-9]\\d*|0[0-7]*|0x[\\da-f]+))?$'
  400. ].join(''), 'i')
  401. argIP = argIP.match(pattern) // Verify argIP format.
  402. if (!argIP) {
  403. // Invalid format.
  404. return false
  405. }
  406. // Reuse argIP variable for component counter.
  407. argIP[0] = 0
  408. for (i = 1; i < 5; i += 1) {
  409. argIP[0] += !!((argIP[i] || '').length)
  410. argIP[i] = parseInt(argIP[i]) || 0
  411. }
  412. // Continue to use argIP for overflow values.
  413. // PHP does not allow any component to overflow.
  414. argIP.push(256, 256, 256, 256)
  415. // Recalculate overflow of last component supplied to make up for missing components.
  416. argIP[4 + argIP[0]] *= Math.pow(256, 4 - argIP[0])
  417. if (argIP[1] >= argIP[5] ||
  418. argIP[2] >= argIP[6] ||
  419. argIP[3] >= argIP[7] ||
  420. argIP[4] >= argIP[8]) {
  421. return false
  422. }
  423. return argIP[1] * (argIP[0] === 1 || 16777216) +
  424. argIP[2] * (argIP[0] <= 2 || 65536) +
  425. argIP[3] * (argIP[0] <= 3 || 256) +
  426. argIP[4] * 1
  427. }
  428. function long2ip (ip) {
  429. // discuss at: https://locutus.io/php/long2ip/
  430. // original by: Waldo Malqui Silva (https://fayr.us/waldo/)
  431. // example 1: long2ip( 3221234342 )
  432. // returns 1: '192.0.34.166'
  433. if (!isFinite(ip)) {
  434. return false
  435. }
  436. return [ip >>> 24 & 0xFF, ip >>> 16 & 0xFF, ip >>> 8 & 0xFF, ip & 0xFF].join('.')
  437. }
  438. </script>