summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-sshtunnel/htdocs/luci-static/resources/view/sshtunnel/ssh_hosts.js
blob: 0313e8024a2de1f129b08584a73cffd4e15becb4 (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
'use strict';
'require form';
'require fs';
'require ui';
'require view';


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

	load: function () {
		return Promise.all([
			fs.lines('/root/.ssh/known_hosts'),
		]);
	},

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

		let m, s, o;

		m = new form.Map('sshtunnel', _('SSH Tunnels'),
			_('This configures <a %s>SSH Tunnels</a>.')
				.format('href="https://openwrt.org/docs/guide-user/services/ssh/sshtunnel"')
		);

		s = m.section(form.GridSection, '_known_hosts');
		s.render = L.bind(_renderKnownHosts, this, knownHosts);

		return m.render();
	},
});

function _renderKnownHosts(knownHosts) {
	var table = E('table', {'class': 'table cbi-section-table', 'id': 'known_hosts'}, [
		E('tr', {'class': 'tr table-titles'}, [
			E('th', {'class': 'th'}, _('Hostname')),
			E('th', {'class': 'th'}, _('Public Key')),
		])
	]);

	var rows = _splitKnownHosts(knownHosts);
	cbi_update_table(table, rows);

	return E('div', {'class': 'cbi-section cbi-tblsection'}, [
		E('h3', _('Known hosts ')),
		E('div', {'class': 'cbi-section-descr'},
			_('Keys of SSH servers found in %s.').format('<code>/root/.ssh/known_hosts</code>')
		),
		table
	]);
}

function _splitKnownHosts(knownHosts) {
	var knownHostsMap = [];
	for (var i = 0; i < knownHosts.length; i++) {
		var sp = knownHosts[i].indexOf(' ');
		if (sp < 0) {
			continue;
		}
		var hostname = knownHosts[i].substring(0, sp);
		var pub = knownHosts[i].substring(sp + 1);
		knownHostsMap.push([hostname, '<small><code>' + pub + '</code></small>']);
	}
	return knownHostsMap;
}