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" }
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"]
23 for sname, sdata in pairs(m:get()) do
24 local key, value = ss.cfgvalue_overview(sdata)
26 if instances_data and instances_data[key] and instances_data[key]["running"] then
27 value["running"] = "yes"
29 value["running"] = "no"
31 instances[key] = value
35 s = m:section(Table, instances)
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",
46 s.parse = function(self, ...)
47 Table.parse(self, ...)
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
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)
66 local stype = m:formvalue("_newinst.type")
67 local sname = m:formvalue("_newinst.name")
68 if ut.contains(cfgtypes, stype) then
70 if sname and #sname > 0 then
71 created = m:set(sname, nil, stype)
73 created = m:add(stype)
77 m.uci:save("shadowsocks-libev")
78 luci.http.redirect(ds.build_url(
79 "admin/services/shadowsocks-libev/instances", sname
85 o = s:option(DummyValue, "name", translate("Name"))
87 o = s:option(DummyValue, "overview", translate("Overview"))
90 s:option(DummyValue, "running", translate("Running"))
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"
98 self.title = translate("Enabled")
99 self.inputstyle = "save"
101 Button.render(self, section, scope)
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))