luci-app-wol: add ability to send packet to broadcast address
[project/luci.git] / applications / luci-app-wol / luasrc / model / cbi / wol.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local sys = require "luci.sys"
5 local fs  = require "nixio.fs"
6
7 m = SimpleForm("wol", translate("Wake on LAN"),
8         translate("Wake on LAN is a mechanism to remotely boot computers in the local network."))
9
10 m.submit = translate("Wake up host")
11 m.reset  = false
12
13
14 local has_ewk = fs.access("/usr/bin/etherwake")
15 local has_wol = fs.access("/usr/bin/wol")
16
17
18 s = m:section(SimpleSection)
19
20 if has_ewk and has_wol then
21         bin = s:option(ListValue, "binary", translate("WoL program"),
22                 translate("Sometimes only one of the two tools works. If one fails, try the other one"))
23
24         bin:value("/usr/bin/etherwake", "Etherwake")
25         bin:value("/usr/bin/wol", "WoL")
26 end
27
28 if has_ewk then
29         iface = s:option(ListValue, "iface", translate("Network interface to use"),
30                 translate("Specifies the interface the WoL packet is sent on"))
31
32         if has_wol then
33                 iface:depends("binary", "/usr/bin/etherwake")
34         end
35
36         iface:value("", translate("Broadcast on all interfaces"))
37
38         for _, e in ipairs(sys.net.devices()) do
39                 if e ~= "lo" then iface:value(e) end
40         end
41 end
42
43
44 host = s:option(Value, "mac", translate("Host to wake up"),
45         translate("Choose the host to wake up or enter a custom MAC address to use"))
46
47 sys.net.mac_hints(function(mac, name)
48         host:value(mac, "%s (%s)" %{ mac, name })
49 end)
50
51 if has_ewk then
52         broadcast = s:option(Flag, "broadcast",
53                 translate("Send to broadcast address"))
54         if has_wol then
55                 broadcast:depends("binary", "/usr/bin/etherwake")
56         end
57 end
58
59 function host.write(self, s, val)
60         local host = luci.http.formvalue("cbid.wol.1.mac")
61         if host and #host > 0 and host:match("^[a-fA-F0-9:]+$") then
62                 local cmd
63                 local util = luci.http.formvalue("cbid.wol.1.binary") or (
64                         has_ewk and "/usr/bin/etherwake" or "/usr/bin/wol"
65                 )
66
67                 if util == "/usr/bin/etherwake" then
68                         local iface = luci.http.formvalue("cbid.wol.1.iface")
69                         local broadcast = luci.http.formvalue("cbid.wol.1.broadcast")
70                         cmd = "%s -D%s %s %q" %{
71                                 util, (iface ~= "" and " -i %q" % iface or ""),
72                                 (broadcast == "1" and " -b" or ""), host
73                         }
74                 else
75                         cmd = "%s -v %q" %{ util, host }
76                 end
77
78                 local msg = "<p><strong>%s</strong><br /><br /><code>%s<br /><br />" %{
79                         translate("Starting WoL utility:"), cmd
80                 }
81
82                 local p = io.popen(cmd .. " 2>&1")
83                 if p then
84                         while true do
85                                 local l = p:read("*l")
86                                 if l then
87                                         if #l > 100 then l = l:sub(1, 100) .. "..." end
88                                         msg = msg .. l .. "<br />"
89                                 else
90                                         break
91                                 end
92                         end
93                         p:close()
94                 end
95
96                 msg = msg .. "</code></p>"
97
98                 m.message = msg
99         end
100 end
101
102
103 return m