1 -- Copyright 2017 Yousong Zhou <yszhou4tech@gmail.com>
2 -- Licensed to the public under the Apache License 2.0.
4 local ds = require "luci.dispatcher"
5 local ss = require "luci.model.shadowsocks-libev"
6 local ut = require "luci.util"
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."))
17 local cfgtypes = { "ss_local", "ss_redir", "ss_server", "ss_tunnel" }
19 for sname, sdata in pairs(m:get()) do
20 local key, value = ss.cfgvalue_overview(sdata)
22 instances[key] = value
26 s = m:section(Table, instances)
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",
37 s.parse = function(self, ...)
38 Table.parse(self, ...)
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
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)
57 local stype = m:formvalue("_newinst.type")
58 local sname = m:formvalue("_newinst.name")
59 if ut.contains(cfgtypes, stype) then
61 if sname and #sname > 0 then
62 created = m:set(sname, nil, stype)
64 created = m:add(stype)
68 m.uci:save("shadowsocks-libev")
69 luci.http.redirect(ds.build_url(
70 "admin/services/shadowsocks-libev/instances", sname
76 o = s:option(DummyValue, "name", translate("Name"))
78 o = s:option(DummyValue, "overview", translate("Overview"))
81 s:option(DummyValue, "running", translate("Running"))
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"
89 self.title = translate("Enabled")
90 self.inputstyle = "save"
92 Button.render(self, section, scope)
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))