tunnels.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* This is free software, licensed under the Apache License, Version 2.0
  2. *
  3. * Copyright (C) 2024 Sergey Ponomarev <stokito@gmail.com>
  4. */
  5. 'use strict';
  6. 'require view';
  7. 'require fs';
  8. function listTunnels() {
  9. let command = '/usr/bin/cloudflared';
  10. let commandArgs = ['tunnel', 'list', '-o', 'json'];
  11. return fs.exec(command, commandArgs).then(function (res) {
  12. if (res.code === 0) {
  13. return JSON.parse(res.stdout);
  14. } else {
  15. throw new Error(res.stdout + ' ' + res.stderr);
  16. }
  17. });
  18. }
  19. return view.extend({
  20. handleSaveApply: null,
  21. handleSave: null,
  22. handleReset: null,
  23. load: function () {
  24. return Promise.all([
  25. listTunnels()
  26. ]);
  27. },
  28. render: function (data) {
  29. var tunnels = data[0];
  30. var tunnelRows = tunnels.map(function (tunnel, index) {
  31. var rowClass = index % 2 === 0 ? 'cbi-rowstyle-1' : 'cbi-rowstyle-2';
  32. var tunneldate = new Date(tunnel.created_at).toLocaleString();
  33. return E('tr', { 'class': 'tr ' + rowClass }, [
  34. E('td', {'class': 'td'}, tunnel.name),
  35. E('td', {'class': 'td'}, tunnel.id),
  36. E('td', {'class': 'td'}, tunneldate),
  37. E('td', {'class': 'td'}, tunnel.connections.length)
  38. ]);
  39. });
  40. var tunnelTable = [
  41. E('h3', _('Tunnels Information')),
  42. E('table', { 'class': 'table cbi-section-table' }, [
  43. E('tr', { 'class': 'tr table-titles' }, [
  44. E('th', {'class': 'th'}, _('Name')),
  45. E('th', {'class': 'th'}, _('ID')),
  46. E('th', {'class': 'th'}, _('Created At')),
  47. E('th', {'class': 'th'}, _('Connections'))
  48. ]),
  49. E(tunnelRows)
  50. ])
  51. ];
  52. var connectionsTables = tunnels.map(function (tunnel) {
  53. var connectionsTable;
  54. if (tunnel.connections.length > 0) {
  55. var connectionRows = tunnel.connections.map(function (connection, index) {
  56. var rowClass = index % 2 === 0 ? 'cbi-rowstyle-1' : 'cbi-rowstyle-2';
  57. var connectiondate = new Date(connection.opened_at).toLocaleString();
  58. return E('tr', { 'class': 'tr ' + rowClass }, [
  59. E('td', {'class': 'td'}, connection.id),
  60. E('td', {'class': 'td'}, connection.origin_ip),
  61. E('td', {'class': 'td'}, connectiondate),
  62. E('td', {'class': 'td'}, connection.colo_name)
  63. ]);
  64. });
  65. connectionsTable = E('table', { 'class': 'table cbi-section-table' }, [
  66. E('tr', { 'class': 'tr table-titles' }, [
  67. E('th', {'class': 'th'}, _('Connection ID')),
  68. E('th', {'class': 'th'}, _('Origin IP')),
  69. E('th', {'class': 'th'}, _('Opened At')),
  70. E('th', {'class': 'th'}, _('Data Center'))
  71. ]),
  72. E(connectionRows)
  73. ]);
  74. } else {
  75. connectionsTable = E('div', {'class':'cbi-value center'}, [
  76. E('em', _('No connections'))
  77. ]);
  78. }
  79. return E('div', {'class': 'cbi-section'}, [
  80. E('h3', _('Connections') + ' ' + tunnel.name),
  81. E(connectionsTable)
  82. ]);
  83. });
  84. return E([], [
  85. E('h2', { 'class': 'section-title' }, _('Tunnels')),
  86. E('div', {'class': 'cbi-section'}, tunnelTable),
  87. E(connectionsTables)
  88. ]);
  89. }
  90. });