luci-app-shadowsocks-libev: rewrite for shadowsocks-libev 3.0.6-2
[project/luci.git] / applications / luci-app-shadowsocks-libev / luasrc / model / cbi / shadowsocks-libev / instances.lua
1 -- Copyright 2017 Yousong Zhou <yszhou4tech@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local ds = require "luci.dispatcher"
5 local ss = require "luci.model.shadowsocks-libev"
6 local ut = require "luci.util"
7 local m, s, o
8
9 m = Map("shadowsocks-libev",
10 translate("Local Instances"),
11 translate("Instances of shadowsocks-libev components, e.g. ss-local, \
12 ss-redir, ss-tunnel, ss-server, etc. To enable an instance it \
13 is required to enable both the instance itself and the remote \
14 server it refers to."))
15
16 local instances = {}
17 local cfgtypes = { "ss_local", "ss_redir", "ss_server", "ss_tunnel" }
18 local instances_data = ut.ubus("service", "list", {name = "shadowsocks-libev"})["shadowsocks-libev"]
19 if instances_data ~= nil then
20 instances_data = instances_data["instances"]
21 end
22
23 for sname, sdata in pairs(m:get()) do
24 local key, value = ss.cfgvalue_overview(sdata)
25 if key ~= nil then
26 if instances_data and instances_data[key] and instances_data[key]["running"] then
27 value["running"] = "yes"
28 else
29 value["running"] = "no"
30 end
31 instances[key] = value
32 end
33 end
34
35 s = m:section(Table, instances)
36 s.addremove = true
37 s.template_addremove = "shadowsocks-libev/add_instance"
38 s.extedit = function(self, section)
39 local value = instances[section]
40 if type(value) == "table" then
41 return ds.build_url(unpack(ds.context.requestpath),
42 "services/shadowsocks-libev/instances",
43 value[".name"])
44 end
45 end
46 s.parse = function(self, ...)
47 Table.parse(self, ...)
48
49 local crval = REMOVE_PREFIX .. self.config
50 local name = self.map:formvaluetable(crval)
51 for k,v in pairs(name) do
52 local value = instances[k]
53 local sname = value[".name"]
54 if type(value) == "table" then
55 m:del(sname)
56 instances[k] = nil
57 for _, oname in ipairs({"redir_tcp", "redir_udp"}) do
58 local ovalue = m:get("ss_rules", oname)
59 if ovalue == sname then
60 m:del("ss_rules", oname)
61 end
62 end
63 end
64 end
65
66 local stype = m:formvalue("_newinst.type")
67 local sname = m:formvalue("_newinst.name")
68 if ut.contains(cfgtypes, stype) then
69 local created
70 if sname and #sname > 0 then
71 created = m:set(sname, nil, stype)
72 else
73 created = m:add(stype)
74 sname = created
75 end
76 if created then
77 m.uci:save("shadowsocks-libev")
78 luci.http.redirect(ds.build_url(
79 "admin/services/shadowsocks-libev/instances", sname
80 ))
81 end
82 end
83 end
84
85 o = s:option(DummyValue, "name", translate("Name"))
86 o.rawhtml = true
87 o = s:option(DummyValue, "overview", translate("Overview"))
88 o.rawhtml = true
89
90 s:option(DummyValue, "running", translate("Running"))
91
92 o = s:option(Button, "disabled", translate("Enable/Disable"))
93 o.render = function(self, section, scope)
94 if instances[section].disabled then
95 self.title = translate("Disabled")
96 self.inputstyle = "reset"
97 else
98 self.title = translate("Enabled")
99 self.inputstyle = "save"
100 end
101 Button.render(self, section, scope)
102 end
103 o.write = function(self, section)
104 local sdata = instances[section]
105 if type(sdata) == "table" then
106 local sname = sdata[".name"]
107 local disabled = not sdata["disabled"]
108 sdata["disabled"] = disabled
109 m:set(sname, "disabled", tostring(disabled))
110 end
111 end
112
113 return m