summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-wol/root/usr/share/rpcd/ucode/luci.wol
blob: fd9e9f0485ab6baec96ae089caf1089a6d471c99 (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
#!/usr/bin/ucode

'use strict';

import { access, stat, popen } from 'fs';

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


function shellquote(s) {
    return "'" + replace(s, "'", "'\\''") + "'";
}


const methods = {
	stat: {
		call: function(request) {
			const result = {};

			result.etherwake = false;
			result.wakeonlan = false;

			if (access(etherwake, "x")) {
				result.etherwake = true;
			}

			if (access(wakeonlan, "x")) {
				result.wakeonlan = true;
			}

			return result;
		}
	},

	exec: {
		args: { name: 'string', args: [] },
		call: function(request) {
			const result = {};
			if (request.args.name == etherwake || request.args.name == wakeonlan) {
				let parts = map(request.args.args, shellquote);
				const fd = popen(request.args.name + ' ' + join(' ', parts));

				result.stdout = fd.read('all');
				result.stderr = '';
				result.code = fd.close();
			} else {
				result.stdout = '';
				result.stderr = 'disallowed';
				result.code = 1;
			}
			return result;
		}
	}
};

return { "luci.wol": methods };