instances.lua 2.8 KB

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