summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-strongswan-swanctl/htdocs/luci-static/resources/view/strongswan-swanctl/status.js
blob: d0a936cc6e179b9ab4e88cf0a0bf8af8107d379f (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'use strict';
'require view';
'require dom';
'require poll';
'require fs';
'require ui';

function formatTime(seconds, selectCount) {
	var days = Math.floor(seconds / (60 * 60 * 24));
	var hours = Math.floor(seconds / (60 * 60)) % 24;
	var minutes = Math.floor(seconds / 60) % 60;
	var seconds = Math.floor(seconds % 60);

	var times = [
		[days, _('Day'), _('Days')],
		[hours, _('Hour'), _('Hours')],
		[minutes, _('Minute'), _('Minutes')],
		[seconds, _('Second'), _('Seconds')]
	].filter(function ([time, singular, plural]) {
		return time > 0;
	});

	var selectedTimes = times.slice(0, selectCount);
	return selectedTimes.map(function ([time, singular, plural]) {
		var unit = time > 1 ? plural : singular;
		return '%d %s'.format(time, unit);
	}).join(', ');
}

function buildSection(name, table) {
	return E('div', { 'class': 'cbi-section' }, [
		E('h2', [name]),
		table
	]);
}

function buildTable(rows) {
	return E('table', { 'class': 'table', }, rows);
}

function buildKeyValueTable(kvPairs) {
	var rows = kvPairs.map(function (row) {
		return E('tr', { 'class': 'tr' }, [
			E('td', { 'class': 'td', 'width': '33%' }, E('strong', [row[0]])),
			E('td', { 'class': 'td' }, [row[1]])
		]);
	});
	return buildTable(rows);
}

function collectErrorMessages(results) {
	var errorMessages = results.reduce(function (messages, result) {
		return messages.concat(result.errors.map(function (error) {
			return error.message;
		}));
	}, []);
	var uniqueErrorMessages = new Set(errorMessages);

	return [...uniqueErrorMessages];
}

return view.extend({
	load: function () {
		return Promise.all([
			fs.exec_direct('/usr/sbin/swanmon', ['version'], 'json'),
			fs.exec_direct('/usr/sbin/swanmon', ['stats'], 'json'),
			fs.exec_direct('/usr/sbin/swanmon', ['list-sas'], 'json')
		]);
	},

	pollData: function (container) {
		poll.add(L.bind(function () {
			return this.load().then(L.bind(function (results) {
				dom.content(container, this.renderContent(results));
			}, this));
		}, this));
	},

	renderContent: function (results) {
		var node = E('div', [E('div')]);
		var firstNode = node.firstElementChild;

		var errorMessages = collectErrorMessages(results);
		if (errorMessages.length > 0) {
			var messageEls = errorMessages.map(function (message) {
				return E('li', message);
			});

			firstNode.appendChild(E('h4', _('Querying strongSwan failed')));
			firstNode.appendChild(E('ul', messageEls));

			return node;
		}

		var [version, stats, sas] = results.map(function (r) {
			return r.data;
		});

		var uptimeSeconds = (new Date() - new Date(stats.uptime.since)) / 1000;
		var statsSection = buildSection(_('Stats'), buildKeyValueTable([
			[_('Version'), version.version],
			[_('Uptime'), formatTime(uptimeSeconds, 2)],
			[_('Daemon'), version.daemon],
			[_('Active IKE_SAs'), stats.ikesas.total],
			[_('Half-Open IKE_SAs'), stats.ikesas['half-open']]
		]));
		firstNode.appendChild(statsSection);

		var tableRows = sas.map(function (conn) {
			var name = Object.keys(conn)[0];
			var data = conn[name];
			var childSas = [];

			Object.entries(data['child-sas']).forEach(function ([name, data]) {
				var table = buildKeyValueTable([
					[_('State'), data.state],
					[_('Mode'), data.mode],
					[_('Protocol'), data.protocol],
					[_('Local Traffic Selectors'), data['local-ts'].join(', ')],
					[_('Remote Traffic Selectors'), data['remote-ts'].join(', ')],
					[_('Encryption Algorithm'), data['encr-alg']],
					[_('Encryption Keysize'), data['encr-keysize']],
					[_('Bytes in'), data['bytes-in']],
					[_('Bytes out'), data['bytes-out']],
					[_('Life Time'), formatTime(data['life-time'], 2)],
					[_('Install Time'), formatTime(data['install-time'], 2)],
					[_('Rekey in'), formatTime(data['rekey-time'], 2)],
					[_('SPI in'), data['spi-in']],
					[_('SPI out'), data['spi-out']]
				]);
				childSas.push(E('div', { 'class': 'cbi-section' }, [
					E('h4', { 'style': 'margin-top: 0; padding-top: 0;' }, [name]),
					table
				]));
			});
			childSas.push(E('button', {
				'class': 'btn cbi-button cbi-button-apply',
				'click': ui.hideModal
			}, _('Close')));

			return E('tr', { 'class': 'tr' }, [
				E('td', { 'class': 'td' }, [name]),
				E('td', { 'class': 'td' }, [data.state]),
				E('td', { 'class': 'td' }, [data['remote-host']]),
				E('td', { 'class': 'td' }, [data.version]),
				E('td', { 'class': 'td' }, [formatTime(data.established, 2)]),
				E('td', { 'class': 'td' }, [formatTime(data['reauth-time'], 2)]),
				E('td', { 'class': 'td' }, [E('button', {
					'class': 'btn cbi-button cbi-button-apply',
					'click': function (ev) {
						ui.showModal(_('CHILD_SAs'), childSas)
					}
				}, _('Show Details'))])
			]);
		});
		var connSection = buildSection(_('Security Associations (SAs)'), buildTable([
			E('tr', { 'class': 'tr' }, [
				E('th', { 'class': 'th' }, [_('Name')]),
				E('th', { 'class': 'th' }, [_('State')]),
				E('th', { 'class': 'th' }, [_('Remote')]),
				E('th', { 'class': 'th' }, [_('IKE Version')]),
				E('th', { 'class': 'th' }, [_('Established for')]),
				E('th', { 'class': 'th' }, [_('Reauthentication in')]),
				E('th', { 'class': 'th' }, [_('Details')])
			]),
			...tableRows
		]));
		firstNode.appendChild(connSection);

		return node;
	},

	render: function (results) {
		var content = E([], [
			E('h2', [_('strongSwan Status')]),
			E('div')
		]);
		var container = content.lastElementChild;

		dom.content(container, this.renderContent(results));
		this.pollData(container);

		return content;
	},

	handleSaveApply: null,
	handleSave: null,
	handleReset: null
});