fix:save_logic

chore:add_guide
This commit is contained in:
dichgrem
2025-10-20 10:10:24 +08:00
parent cd7f41d7a7
commit ef45a9414b

View File

@@ -1,6 +1,6 @@
-- /usr/lib/lua/luci/model/cbi/zzz.lua -- /usr/lib/lua/luci/model/cbi/zzz.lua
local m, s, o local m, s, o
local sys = require "luci.sys" local sys = require("luci.sys")
-- control -- control
local start_action = luci.http.formvalue("cbid.zzz.auth.start_service") local start_action = luci.http.formvalue("cbid.zzz.auth.start_service")
@@ -8,15 +8,14 @@ local stop_action = luci.http.formvalue("cbid.zzz.auth.stop_service")
local restart_action = luci.http.formvalue("cbid.zzz.auth.restart_service") local restart_action = luci.http.formvalue("cbid.zzz.auth.restart_service")
if start_action then if start_action then
sys.call("/etc/rc.d/S99zzz start") sys.call("/etc/rc.d/S99zzz start")
elseif stop_action then elseif stop_action then
sys.call("/etc/rc.d/S99zzz stop") sys.call("/etc/rc.d/S99zzz stop")
elseif restart_action then elseif restart_action then
sys.call("/etc/rc.d/S99zzz stop; sleep 2; /etc/rc.d/S99zzz start") sys.call("/etc/rc.d/S99zzz stop; sleep 2; /etc/rc.d/S99zzz start")
end end
m = Map("zzz", "ZZZ 802.1x 认证客户端", m = Map("zzz", "ZZZ 802.1x 认证客户端", "配置使用 zzz 客户端进行网络访问的 802.1x 认证")
"配置使用 zzz 客户端进行网络访问的 802.1x 认证")
-- Authentication Settings -- Authentication Settings
s = m:section(TypedSection, "auth", "认证设置") s = m:section(TypedSection, "auth", "认证设置")
@@ -27,20 +26,20 @@ s.addremove = false
o = s:option(DummyValue, "_status", "当前状态") o = s:option(DummyValue, "_status", "当前状态")
o.rawhtml = true o.rawhtml = true
o.cfgvalue = function() o.cfgvalue = function()
local sys = require "luci.sys" local sys = require("luci.sys")
local running = sys.call("pgrep zzz >/dev/null") == 0 local running = sys.call("pgrep zzz >/dev/null") == 0
if running then if running then
return "<span style='color:green;font-weight:bold'>✔ 正在运行中</span>" return "<span style='color:green;font-weight:bold'>✔ 正在运行中</span>"
else else
return "<span style='color:red;font-weight:bold'>✘ 未运行</span>" return "<span style='color:red;font-weight:bold'>✘ 未运行</span>"
end end
end end
-- control buttons -- control buttons
control_buttons = s:option(DummyValue, "_control", "服务控制") control_buttons = s:option(DummyValue, "_control", "服务控制")
control_buttons.rawhtml = true control_buttons.rawhtml = true
control_buttons.cfgvalue = function() control_buttons.cfgvalue = function()
return [[ return [[
<div style="display: flex; gap: 10px; align-items: center; flex-wrap: wrap;"> <div style="display: flex; gap: 10px; align-items: center; flex-wrap: wrap;">
<input type="submit" class="cbi-button cbi-button-apply" <input type="submit" class="cbi-button cbi-button-apply"
name="cbid.zzz.auth.start_service" value="启动服务" /> name="cbid.zzz.auth.start_service" value="启动服务" />
@@ -53,17 +52,40 @@ control_buttons.cfgvalue = function()
end end
-- Username -- Username
o = s:option(Value, "username", "用户名", "802.1x 认证用户名") o = s:option(
Value,
"username",
"用户名",
[[802.1x 认证用户名
<span style="cursor: help; color: #007bff; font-weight: bold;" title="用户名为学号@运营商例如212306666@cucc移动为cmcc联通为cucc电信为ctcc">?</span>]]
)
o.password = true o.password = true
o.rmempty = false o.rmempty = false
o.rawhtml = true
-- Password -- Password
o = s:option(Value, "password", "密码", "802.1x 认证密码")
o.password = true o.password = true
o.rmempty = false o.rmempty = false
o = s:option(
Value,
"password",
"密码",
[[802.1x 认证密码
<span style="cursor: help; color: #007bff; font-weight: bold;" title="密码默认为身份证号后六位可以在官方客户端inode中修改">?</span>]]
)
o.password = true
o.rmempty = false
o.rawhtml = true
-- Network Device -- Network Device
o = s:option(ListValue, "device", "网络接口", "用于认证的网络接口") o = s:option(ListValue, "device", "网络接口", "用于认证的网络接口")
o = s:option(
Value,
"device",
"网络接口",
[[用于认证的网络接口
<span style="cursor: help; color: #007bff; font-weight: bold;" title="可以用ip addr命令查看有10.38开头ip的接口">?</span>]]
)
o.rmempty = false o.rmempty = false
o:value("eth0", "eth0") o:value("eth0", "eth0")
o:value("eth1", "eth1") o:value("eth1", "eth1")
@@ -72,9 +94,9 @@ o:value("wan", "WAN")
-- Add network interface -- Add network interface
local interfaces = sys.net.devices() local interfaces = sys.net.devices()
for _, iface in ipairs(interfaces) do for _, iface in ipairs(interfaces) do
if iface ~= "lo" then if iface ~= "lo" then
o:value(iface, iface) o:value(iface, iface)
end end
end end
-- Auto start -- Auto start
@@ -84,34 +106,42 @@ auto_start.rmempty = false
-- Get Status -- Get Status
auto_start.cfgvalue = function(self, section) auto_start.cfgvalue = function(self, section)
local has_cron = sys.call("crontab -l 2>/dev/null | grep 'S99zzz' >/dev/null") == 0 local has_cron = sys.call("crontab -l 2>/dev/null | grep 'S99zzz' >/dev/null") == 0
return has_cron and "1" or "0" return has_cron and "1" or "0"
end end
-- Crontab -- Crontab
auto_start.write = function(self, section, value) auto_start.write = function(self, section, value)
if value == "1" then if value == "1" then
-- 启用定时任务:每周一至周五 7:00 启动 -- 启用定时任务:每周一至周五 7:00 启动
sys.call("(crontab -l 2>/dev/null | grep -v 'S99zzz' | grep -v '# zzz auto') | crontab - 2>/dev/null") sys.call("(crontab -l 2>/dev/null | grep -v 'S99zzz' | grep -v '# zzz auto') | crontab - 2>/dev/null")
sys.call("(crontab -l 2>/dev/null; echo '0 7 * * 1,2,3,4,5 /etc/rc.d/S99zzz start # zzz auto start') | crontab -") sys.call(
sys.call("/etc/init.d/cron enable && /etc/init.d/cron restart") "(crontab -l 2>/dev/null; echo '0 7 * * 1,2,3,4,5 /etc/rc.d/S99zzz start # zzz auto start') | crontab -"
else )
-- 禁用定时任务 sys.call("/etc/init.d/cron enable && /etc/init.d/cron restart")
sys.call("(crontab -l 2>/dev/null | grep -v 'S99zzz' | grep -v '# zzz auto') | crontab - 2>/dev/null") else
sys.call("/etc/init.d/cron restart") -- 禁用定时任务
end sys.call("(crontab -l 2>/dev/null | grep -v 'S99zzz' | grep -v '# zzz auto') | crontab - 2>/dev/null")
sys.call("/etc/init.d/cron restart")
end
end end
-- Crontab Status -- Crontab Status
timer_status_display = s:option(DummyValue, "_timer_status_display", "定时任务状态") timer_status_display = s:option(DummyValue, "_timer_status_display", "定时任务状态")
timer_status_display.rawhtml = true timer_status_display.rawhtml = true
timer_status_display.cfgvalue = function() timer_status_display.cfgvalue = function()
local cron_output = sys.exec("crontab -l 2>/dev/null | grep 'S99zzz' || echo '未设置'") local cron_output = sys.exec("crontab -l 2>/dev/null | grep 'S99zzz' || echo '未设置'")
if cron_output:match("S99zzz") then if cron_output:match("S99zzz") then
return "<span style='color:green;font-weight:bold'>✔ 已启用 (每周一至周五 7:00 自动启动)</span>" return "<span style='color:green;font-weight:bold'>✔ 已启用 (每周一至周五 7:00 自动启动)</span>"
else else
return "<span style='color:red;font-weight:bold'>✘ 未启用</span>" return "<span style='color:red;font-weight:bold'>✘ 未启用</span>"
end end
end
-- 保存后自动重启 zzz 服务
m.on_commit = function(self)
local sys = require("luci.sys")
sys.call("/etc/rc.d/S99zzz restart >/dev/null 2>&1 &")
end end
return m return m