summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-cloudflared/htdocs/luci-static/resources/view/cloudflared/tunnels.js
blob: 28acf4452fae3e7c4e779a544381563809cc572c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* This is free software, licensed under the Apache License, Version 2.0
 *
 * Copyright (C) 2024 Sergey Ponomarev <stokito@gmail.com>
 */

'use strict';
'require view';
'require fs';

function listTunnels() {
	let command = '/usr/bin/cloudflared';
	let commandArgs = ['tunnel', 'list', '-o', 'json'];
	return fs.exec(command, commandArgs).then(function (res) {
		if (res.code === 0) {
			return JSON.parse(res.stdout);
		} else {
			throw new Error(res.stdout + ' ' + res.stderr);
		}
	});
}


return view.extend({
	handleSaveApply: null,
	handleSave: null,
	handleReset: null,

	load: function () {
		return Promise.all([
			listTunnels()
		]);
	},

	render: function (data) {
		var tunnels = data[0];

		var tunnelsElList = [];
		for (var tunnel of tunnels) {
			var connectionsSection = [];
			if (tunnel.connections.length > 0) {
				var connectionsElList = [];
				for (let connection of tunnel.connections) {
					var dateOpenedAt = new Date(connection.opened_at).toLocaleString();
					connectionsElList.push(
						E('tr', [
							E('td', connection.id),
							E('td', connection.origin_ip),
							E('td', dateOpenedAt),
							E('td', connection.colo_name)
						])
					);
				}

				connectionsSection = [
					E('h5', _('Connections')),
					E('table', {'class': 'table cbi-section-table'}, [
						E('thead', [
							E('tr', {'class': 'tr table-titles'}, [
								E('th', {'class': 'th'}, 'ID'),
								E('th', {'class': 'th'}, _('Origin IP')),
								E('th', {'class': 'th'}, _('Opened At')),
								E('th', {'class': 'th'}, _('Data center')),
							]),
						]),
						E('tbody', connectionsElList)
					])
				];
			} else {
				connectionsSection = [E('em', _('No connections'))];
			}

			var tunnelEl = E('div', [
					E('h4', tunnel.name),
					E('span', 'ID '),
					E('span', tunnel.id),
					E('div', connectionsSection)
				]
			);
			tunnelsElList.push(tunnelEl);
		}
		return E([], [
			E('h2', {'class': 'section-title'}, _('Tunnels')),
			E('div', {'id': 'tunnels'}, tunnelsElList),
		]);
	}
});