summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-shadowsocks-libev/luasrc/model/cbi/shadowsocks-libev/instances.lua
blob: 62a90fb416e78d3ff5077715519a37af15af06e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
-- Copyright 2017 Yousong Zhou <yszhou4tech@gmail.com>
-- Licensed to the public under the Apache License 2.0.

local ds = require "luci.dispatcher"
local ss = require "luci.model.shadowsocks-libev"
local ut = require "luci.util"
local m, s, o

m = Map("shadowsocks-libev",
	translate("Local Instances"),
	translate("Instances of shadowsocks-libev components, e.g. ss-local, \
			   ss-redir, ss-tunnel, ss-server, etc.  To enable an instance it \
			   is required to enable both the instance itself and the remote \
			   server it refers to."))

local instances = {}
local cfgtypes = { "ss_local", "ss_redir", "ss_server", "ss_tunnel" }

for sname, sdata in pairs(m:get()) do
	local key, value = ss.cfgvalue_overview(sdata)
	if key ~= nil then
		instances[key] = value
	end
end

s = m:section(Table, instances)
s.addremove = true
s.template_addremove = "shadowsocks-libev/add_instance"
s.extedit = function(self, section)
	local value = instances[section]
	if type(value) == "table" then
		return ds.build_url(unpack(ds.context.requestpath),
					"services/shadowsocks-libev/instances",
					value[".name"])
	end
end
s.parse = function(self, ...)
	Table.parse(self, ...)

	local crval = REMOVE_PREFIX .. self.config
	local name = self.map:formvaluetable(crval)
	for k,v in pairs(name) do
		local value = instances[k]
		local sname = value[".name"]
		if type(value) == "table" then
			m:del(sname)
			instances[k] = nil
			for _, oname in ipairs({"redir_tcp", "redir_udp"}) do
				local ovalue = m:get("ss_rules", oname)
				if ovalue == sname then
					m:del("ss_rules", oname)
				end
			end
		end
	end

	local stype = m:formvalue("_newinst.type")
	local sname = m:formvalue("_newinst.name")
	if ut.contains(cfgtypes, stype) then
		local created
		if sname and #sname > 0 then
			created = m:set(sname, nil, stype)
		else
			created = m:add(stype)
			sname = created
		end
		if created then
			m.uci:save("shadowsocks-libev")
			luci.http.redirect(ds.build_url(
				"admin/services/shadowsocks-libev/instances", sname
			))
		end
	end
end

o = s:option(DummyValue, "name", translate("Name"))
o.rawhtml = true
o = s:option(DummyValue, "overview", translate("Overview"))
o.rawhtml = true

s:option(DummyValue, "running", translate("Running"))

o = s:option(Button, "disabled", translate("Enable/Disable"))
o.render = function(self, section, scope)
	if instances[section].disabled then
		self.title = translate("Disabled")
		self.inputstyle = "reset"
	else
		self.title = translate("Enabled")
		self.inputstyle = "save"
	end
	Button.render(self, section, scope)
end
o.write = function(self, section)
	local sdata = instances[section]
	if type(sdata) == "table" then
		local sname = sdata[".name"]
		local disabled = not sdata["disabled"]
		sdata["disabled"] = disabled
		m:set(sname, "disabled", tostring(disabled))
	end
end

return m