Merge pull request #1249 from yousong/shadowsocks-libev
[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
19 for sname, sdata in pairs(m:get()) do
20         local key, value = ss.cfgvalue_overview(sdata)
21         if key ~= nil then
22                 instances[key] = value
23         end
24 end
25
26 s = m:section(Table, instances)
27 s.addremove = true
28 s.template_addremove = "shadowsocks-libev/add_instance"
29 s.extedit = function(self, section)
30         local value = instances[section]
31         if type(value) == "table" then
32                 return ds.build_url(unpack(ds.context.requestpath),
33                                         "services/shadowsocks-libev/instances",
34                                         value[".name"])
35         end
36 end
37 s.parse = function(self, ...)
38         Table.parse(self, ...)
39
40         local crval = REMOVE_PREFIX .. self.config
41         local name = self.map:formvaluetable(crval)
42         for k,v in pairs(name) do
43                 local value = instances[k]
44                 local sname = value[".name"]
45                 if type(value) == "table" then
46                         m:del(sname)
47                         instances[k] = nil
48                         for _, oname in ipairs({"redir_tcp", "redir_udp"}) do
49                                 local ovalue = m:get("ss_rules", oname)
50                                 if ovalue == sname then
51                                         m:del("ss_rules", oname)
52                                 end
53                         end
54                 end
55         end
56
57         local stype = m:formvalue("_newinst.type")
58         local sname = m:formvalue("_newinst.name")
59         if ut.contains(cfgtypes, stype) then
60                 local created
61                 if sname and #sname > 0 then
62                         created = m:set(sname, nil, stype)
63                 else
64                         created = m:add(stype)
65                         sname = created
66                 end
67                 if created then
68                         m.uci:save("shadowsocks-libev")
69                         luci.http.redirect(ds.build_url(
70                                 "admin/services/shadowsocks-libev/instances", sname
71                         ))
72                 end
73         end
74 end
75
76 o = s:option(DummyValue, "name", translate("Name"))
77 o.rawhtml = true
78 o = s:option(DummyValue, "overview", translate("Overview"))
79 o.rawhtml = true
80
81 s:option(DummyValue, "running", translate("Running"))
82
83 o = s:option(Button, "disabled", translate("Enable/Disable"))
84 o.render = function(self, section, scope)
85         if instances[section].disabled then
86                 self.title = translate("Disabled")
87                 self.inputstyle = "reset"
88         else
89                 self.title = translate("Enabled")
90                 self.inputstyle = "save"
91         end
92         Button.render(self, section, scope)
93 end
94 o.write = function(self, section)
95         local sdata = instances[section]
96         if type(sdata) == "table" then
97                 local sname = sdata[".name"]
98                 local disabled = not sdata["disabled"]
99                 sdata["disabled"] = disabled
100                 m:set(sname, "disabled", tostring(disabled))
101         end
102 end
103
104 return m