summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-wol/htdocs/luci-static/resources/view/wol.js
blob: 0a315d3c3f9016760e4b02982250c9daba1509aa (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
'use strict';
'require view';
'require uci';
'require ui';
'require rpc';
'require form';
'require tools.widgets as widgets';

const ETHERWAKE_BIN = '/usr/bin/etherwake';
const WAKEONLAN_BIN = '/usr/bin/wakeonlan';

const PACKAGES_URL = 'admin/system/package-manager';

return view.extend({
	outputText: '',

	callStat: rpc.declare({
		object: 'luci.wol',
		method: 'stat',
		params: [],
		expect: {}
	}),

	callExec: rpc.declare({
		object: 'luci.wol',
		method: 'exec',
		params: ['name', 'args'],
		expect: {}
	}),

	callHostHints: rpc.declare({
		object: 'luci-rpc',
		method: 'getHostHints',
		expect: {
			'': {}
		}
	}),

	option_install_etherwake() {
		window.open(L.url(PACKAGES_URL) +
			'?query=etherwake', '_blank', 'noopener');
	},

	option_install_wakeonlan() {
		window.open(L.url(PACKAGES_URL) +
			'?query=wakeonlan', '_blank', 'noopener');
	},

	load() {
		return Promise.all([
			L.resolveDefault(this.callStat()),
			this.callHostHints(),
			uci.load('luci-wol')
		]);
	},

	render([stat, hosts]) {
		const has_ewk = stat && stat.etherwake,
			has_wol = stat && stat.wakeonlan;
		let m, s, o;

		// Check if at least one Wake on LAN utility is available, else show install buttons
		if (!has_ewk && !has_wol) {
			m = new form.Map('luci-wol', _('Wake on LAN'),
				_('Wake on LAN is a mechanism to boot computers remotely in the local network.'));

			s = m.section(form.NamedSection, 'packages', 'packages',
				_('Required Packages'),
				_('At least one Wake on LAN utility is needed. Please install one of the following packages (some extra permissions may be required):'));

			s.render = L.bind(function(view) {
				return form.NamedSection.prototype.render.apply(this, arguments)
					.then(L.bind(function(node) {
						node.appendChild(E('div', {
							'class': 'control-group'
						}, [
							E('button', {
								'class': 'btn cbi-button-action',
								'click': ui.createHandlerFn(view, 'option_install_etherwake', this.map),
								'title': _('Install etherwake package')
							}, [_('Install etherwake')]),
							' ',
							E('button', {
								'class': 'btn cbi-button-action',
								'click': ui.createHandlerFn(view, 'option_install_wakeonlan', this.map),
								'title': _('Install wakeonlan package')
							}, [_('Install wakeonlan')])
						]));
						return node;
					}, this));
			}, s, this);

			return m.render();
		}

		m = new form.Map('luci-wol', _('Wake on LAN'),
			_('Wake on LAN is a mechanism to boot computers remotely in the local network.'));

		// Default settings section (used executable)
		s = m.section(form.NamedSection, 'defaults', 'wol', _('Default Settings'));

		if (has_ewk && has_wol) {
			o = s.option(form.ListValue, 'executable', _('Default WoL program'),
				_('Choose the default Wake on LAN utility'));
			o.value(ETHERWAKE_BIN, 'Etherwake');
			o.value(WAKEONLAN_BIN, 'Wakeonlan');
			o.default = ETHERWAKE_BIN;
			o.onchange = function(ev, section_id, value) {
				return m.save(null, true);
			};
		} else {
			// If only one binary is available, show info message with install button for the other
			o = s.option(form.DummyValue, '_info');
			o.rawhtml = true;
			o.default = E('div', {}, [
				E('p', {}, [
					_('Binary used') + ': ',
					E('strong', {}, has_ewk ? 'Etherwake' : 'Wakeonlan')
				]),
				E('p', {
						'style': 'margin-top: 10px'
					},
					_('You can also install the alternative Wake on LAN utility (some extra permissions may be required):')),
				E('div', {
					'class': 'control-group'
				}, [
					E('button', {
						'class': 'btn cbi-button-action',
						'click': ui.createHandlerFn(this, has_ewk ? 'option_install_wakeonlan' : 'option_install_etherwake'),
						'title': _('Install the alternative Wake on LAN package')
					}, [_('Install %s').format(has_ewk ? 'wakeonlan' : 'etherwake')])
				])
			]);

		}

		// Targets section with GridSection
		s = m.section(form.GridSection, 'target', _('Wake on LAN Targets'), _('Configure hosts that can be woken up. Click the Wake button to send a magic packet.') + '<br>' + _('Note: wakeonlan binary does not support interface, broadcast, and password options (etherwake only).') + ' ' + _('These options will be ignored if wakeonlan is used.'));

		s.addremove = true;
		s.anonymous = true;
		s.sortable = true;
		s.nodescriptions = true;

		// Name column
		o = s.option(form.Value, 'name', _('Name'), _('Mandatory'));
		o.rmempty = false;
		o.datatype = 'string';

		// MAC address column
		o = s.option(form.Value, 'mac', _('MAC Address'), _('Mandatory'));
		o.rmempty = false;
		o.datatype = 'macaddr';
		L.sortedKeys(hosts).forEach(function(mac) { // Add host hints, need 'getHostHints' acl (luci-rpc)
			const hint = hosts[mac].name ||
				L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0] ||
				L.toArray(hosts[mac].ip6addrs || hosts[mac].ipv6)[0];
			o.value(mac, hint ? '%s (%s)'.format(mac, hint) : mac);
		});

		// Interface column (only for etherwake)
		if (has_ewk) {
			o = s.option(widgets.DeviceSelect, 'iface', _('Interface'), _('Etherwake only')); // Network device selector widget, needs 'getNetworkDevices' acl (luci-rpc)
			o.noaliases = true;
			o.noinactive = true;
		}

		// Broadcast flag (only for etherwake)
		if (has_ewk) {
			o = s.option(form.Flag, 'broadcast', _('Broadcast'), _('Etherwake only'));
			o.default = o.disabled;
		}

		// Password field (only for etherwake)
		if (has_ewk) {
			o = s.option(form.Value, 'password', _('Password'), _('Etherwake only'));
			o.datatype = 'string';
			o.placeholder = '00:22:44:66:88:aa or 192.168.1.1';
			o.datatype = 'or(macaddr,ip4addr("nomask"))'; // Accept MAC or IPv4 address format
		}

		// When editing, set modal title to include target name
		s.modaltitle = L.bind(function(section_id) {
			var name = uci.get('luci-wol', section_id, 'name');
			return _('Edit target') + (name ? ': ' + name : '');
		}, this);

		// Keep reference to GridSection for button handlers
		const gridSection = s;

		// Take default row actions and add "Wake" button
		s.renderRowActions = L.bind(function(section_id) {
			const defaultButtons = form.GridSection.prototype.renderRowActions.call(gridSection, section_id, _('Edit'));

			const wakeButton = E('button', {
				'class': 'cbi-button cbi-button-action',
				'click': ui.createHandlerFn(this, function() {
					return this.handleWakeup(section_id, has_ewk, has_wol);
				})
			}, _('Wake'));

			const buttonContainer = defaultButtons.querySelector('div');
			if (buttonContainer) {
				buttonContainer.insertBefore(wakeButton, buttonContainer.firstChild);
			}

			return defaultButtons;
		}, this);

		// Output section, for wake results
		s = m.section(form.NamedSection, 'output', 'wol', _('Output'));
		s.anonymous = true;
		s.render = L.bind(function() {
			return E('div', {
				'class': 'cbi-section'
			}, [
				E('h3', {}, _('Output')),
				E('textarea', {
					'readonly': true,
					'rows': 10,
					'style': 'width: 100%; font-family: monospace;',
					'id': 'wol-output-text'
				}, this.outputText)
			]);
		}, this);

		return m.render();
	},

	handleWakeup(section_id, has_ewk, has_wol) {
		const self = this;
		const name = uci.get('luci-wol', section_id, 'name');
		const mac = uci.get('luci-wol', section_id, 'mac');

		// Determine which binary to use and verify availability
		const defaultBin = uci.get('luci-wol', 'defaults', 'executable');
		let bin = defaultBin || (has_ewk ? ETHERWAKE_BIN : WAKEONLAN_BIN);

		if (bin == ETHERWAKE_BIN && !has_ewk)
			bin = WAKEONLAN_BIN;
		else if (bin == WAKEONLAN_BIN && !has_wol)
			bin = ETHERWAKE_BIN;

		// Build argument list based on selected binary
		const args = [];

		if (bin == ETHERWAKE_BIN) {
			args.push('-D');
			const iface = uci.get('luci-wol', section_id, 'iface');
			if (iface)
				args.push('-i', iface);

			const broadcast = uci.get('luci-wol', section_id, 'broadcast');
			if (broadcast == '1')
				args.push('-b');

			const password = uci.get('luci-wol', section_id, 'password');
			if (password)
				args.push('-p', password);

			args.push(mac);
		} else {
			args.push(mac);
		}

		// Execute the wake command and handle output
		this.appendOutput(`Sending wakeup to ${name} (${mac})...\n`);

		return this.callExec(bin, args).then(function(res) {
			if (res.stdout)
				self.appendOutput(res.stdout + '\n');
			if (res.stderr)
				self.appendOutput('Error: ' + res.stderr + '\n');
			if (!res.stdout && !res.stderr)
				self.appendOutput('Command completed with code ' + (res.code || 0) + '\n');
			self.appendOutput('\n');
		}).catch(function(err) {
			self.appendOutput('Error: ' + err + '\n\n');
		});
	},

	appendOutput(text) {
		// Append text to the output textarea and scroll to bottom
		this.outputText += text;
		const textarea = document.getElementById('wol-output-text');
		if (textarea) {
			textarea.value = this.outputText;
			textarea.scrollTop = textarea.scrollHeight;
		}
	}
});