diff --git a/luci-app-passwall/Makefile b/luci-app-passwall/Makefile index 3997cd8b7..b83e4c1a4 100644 --- a/luci-app-passwall/Makefile +++ b/luci-app-passwall/Makefile @@ -1,5 +1,5 @@ # Copyright (C) 2018-2020 L-WRT Team -# Copyright (C) 2021-2023 xiaorouji +# Copyright (C) 2021-2025 xiaorouji # # This is free software, licensed under the GNU General Public License v3. diff --git a/luci-app-passwall/luasrc/controller/passwall.lua b/luci-app-passwall/luasrc/controller/passwall.lua index 620d8b0c3..8df6d62d0 100644 --- a/luci-app-passwall/luasrc/controller/passwall.lua +++ b/luci-app-passwall/luasrc/controller/passwall.lua @@ -1,10 +1,10 @@ -- Copyright (C) 2018-2020 L-WRT Team --- Copyright (C) 2021-2023 xiaorouji +-- Copyright (C) 2021-2025 xiaorouji module("luci.controller.passwall", package.seeall) local api = require "luci.passwall.api" -local appname = "passwall" -- not available -local uci = api.libuci -- in funtion index() +local appname = "passwall" -- not available +local uci = api.uci -- in funtion index() local fs = api.fs local http = require "luci.http" local util = require "luci.util" @@ -17,8 +17,8 @@ function index() else return end end local api = require "luci.passwall.api" - local appname = "passwall" -- global definitions not available - local uci = api.libuci -- in function index() + local appname = "passwall" -- global definitions not available + local uci = api.uci -- in function index() local fs = api.fs entry({"admin", "services", appname}).dependent = true entry({"admin", "services", appname, "reset_config"}, call("reset_config")).leaf = true @@ -110,7 +110,7 @@ end function show_menu() uci:delete(appname, "@global[0]", "hide_from_luci") - uci:commit(appname) + api.uci_save(uci, appname, true) luci.sys.call("rm -rf /tmp/luci-*") luci.sys.call("/etc/init.d/rpcd restart >/dev/null") luci.http.redirect(api.url()) @@ -118,7 +118,7 @@ end function hide_menu() uci:set(appname, "@global[0]", "hide_from_luci","1") - uci:commit(appname) + api.uci_save(uci, appname, true) luci.sys.call("rm -rf /tmp/luci-*") luci.sys.call("/etc/init.d/rpcd restart >/dev/null") luci.http.redirect(luci.dispatcher.build_url("admin", "status", "overview")) @@ -135,6 +135,7 @@ function socks_autoswitch_add_node() local id = luci.http.formvalue("id") local key = luci.http.formvalue("key") if id and id ~= "" and key and key ~= "" then + uci:set(appname, id, "enable_autoswitch", "1") local new_list = uci:get(appname, id, "autoswitch_backup_node") or {} for i = #new_list, 1, -1 do if (uci:get(appname, new_list[i], "remarks") or ""):find(key) then @@ -146,8 +147,8 @@ function socks_autoswitch_add_node() table.insert(new_list, e.id) end end - api.uci_set_list(uci, appname, id, "autoswitch_backup_node", new_list) - uci:commit(appname) + uci:set_list(appname, id, "autoswitch_backup_node", new_list) + api.uci_save(uci, appname) end luci.http.redirect(api.url("socks_config", id)) end @@ -156,14 +157,15 @@ function socks_autoswitch_remove_node() local id = luci.http.formvalue("id") local key = luci.http.formvalue("key") if id and id ~= "" and key and key ~= "" then + uci:set(appname, id, "enable_autoswitch", "1") local new_list = uci:get(appname, id, "autoswitch_backup_node") or {} for i = #new_list, 1, -1 do if (uci:get(appname, new_list[i], "remarks") or ""):find(key) then table.remove(new_list, i) end end - api.uci_set_list(uci, appname, id, "autoswitch_backup_node", new_list) - uci:commit(appname) + uci:set_list(appname, id, "autoswitch_backup_node", new_list) + api.uci_save(uci, appname) end luci.http.redirect(api.url("socks_config", id)) end @@ -353,7 +355,7 @@ function set_node() local protocol = luci.http.formvalue("protocol") local section = luci.http.formvalue("section") uci:set(appname, "@global[0]", protocol .. "_node", section) - uci:commit(appname) + api.uci_save(uci, appname, true) luci.sys.call("/etc/init.d/passwall restart > /dev/null 2>&1 &") luci.http.redirect(api.url("log")) end @@ -361,7 +363,7 @@ end function copy_node() local section = luci.http.formvalue("section") local uuid = api.gen_short_uuid() - api.uci_section(uci, appname, "nodes", uuid) + uci:section(appname, "nodes", uuid) for k, v in pairs(uci:get_all(appname, section)) do local filter = k:find("%.") if filter and filter == 1 then @@ -375,7 +377,7 @@ function copy_node() end uci:delete(appname, uuid, "add_from") uci:set(appname, uuid, "add_mode", 1) - uci:commit(appname) + api.uci_save(uci, appname) luci.http.redirect(api.url("node_config", uuid)) end @@ -385,7 +387,7 @@ function clear_all_nodes() uci:delete(appname, '@global[0]', "udp_node") uci:foreach(appname, "socks", function(t) uci:delete(appname, t[".name"]) - api.uci_set_list(uci, appname, t[".name"], "autoswitch_backup_node", {}) + uci:set_list(appname, t[".name"], "autoswitch_backup_node", {}) end) uci:foreach(appname, "haproxy_config", function(t) uci:delete(appname, t[".name"]) @@ -398,7 +400,7 @@ function clear_all_nodes() uci:delete(appname, node['.name']) end) - uci:commit(appname) + api.uci_save(uci, appname, true) luci.sys.call("/etc/init.d/" .. appname .. " stop") end @@ -421,7 +423,7 @@ function delete_select_nodes() table.remove(auto_switch_node_list, i) end end - api.uci_set_list(uci, appname, t[".name"], "autoswitch_backup_node", auto_switch_node_list) + uci:set_list(appname, t[".name"], "autoswitch_backup_node", auto_switch_node_list) end) uci:foreach(appname, "haproxy_config", function(t) if t["lbss"] == w then @@ -448,7 +450,7 @@ function delete_select_nodes() end) uci:delete(appname, w) end) - uci:commit(appname) + api.uci_save(uci, appname, true) luci.sys.call("/etc/init.d/" .. appname .. " restart > /dev/null 2>&1 &") end diff --git a/luci-app-passwall/luasrc/model/cbi/passwall/client/socks_config.lua b/luci-app-passwall/luasrc/model/cbi/passwall/client/socks_config.lua index bc1ab22d8..1e70f23fa 100644 --- a/luci-app-passwall/luasrc/model/cbi/passwall/client/socks_config.lua +++ b/luci-app-passwall/luasrc/model/cbi/passwall/client/socks_config.lua @@ -123,6 +123,8 @@ for k, v in pairs(nodes_table) do socks_node:value(v.id, v["remark"]) end -m:append(Template(appname .. "/socks_auto_switch/footer")) +o = s:option(DummyValue, "btn", " ") +o.template = appname .. "/socks_auto_switch/btn" +o:depends("enable_autoswitch", true) return m diff --git a/luci-app-passwall/luasrc/passwall/api.lua b/luci-app-passwall/luasrc/passwall/api.lua index c866607ce..1dbeb84ad 100644 --- a/luci-app-passwall/luasrc/passwall/api.lua +++ b/luci-app-passwall/luasrc/passwall/api.lua @@ -3,6 +3,7 @@ local com = require "luci.passwall.com" bin = require "nixio".bin fs = require "nixio.fs" sys = require "luci.sys" +uci = require "luci.model.uci".cursor() libuci = require "uci".cursor() util = require "luci.util" datatypes = require "luci.cbi.datatypes" @@ -30,6 +31,10 @@ function log(...) end end +function is_js_luci() + return sys.call('[ -f "/www/luci-static/resources/uci.js" ]') == 0 +end + function uci_set_list(cursor, config, section, option, value) if config and section and option then if not value or #value == 0 then @@ -55,24 +60,48 @@ function uci_section(cursor, config, type, name, values) return stat and name end +function uci_save(cursor, config, commit, apply) + if is_js_luci() then + commit = true + if commit then + if apply then + cursor:commit(config) + else + sh_uci_commit(config) + end + end + else + cursor:save(config) + if commit then + cursor:commit(config) + if apply then + sys.call("/etc/init.d/" .. config .. " reload > /dev/null 2>&1 &") + end + end + end +end + function sh_uci_get(config, section, option) exec_call(string.format("uci -q get %s.%s.%s", config, section, option)) - exec_call(string.format("uci -q commit %s", config)) end -function sh_uci_set(config, section, option, val) +function sh_uci_set(config, section, option, val, commit) exec_call(string.format("uci -q set %s.%s.%s=\"%s\"", config, section, option, val)) - exec_call(string.format("uci -q commit %s", config)) + if commit then sh_uci_commit(config) end end -function sh_uci_del(config, section, option) +function sh_uci_del(config, section, option, commit) exec_call(string.format("uci -q delete %s.%s.%s", config, section, option)) - exec_call(string.format("uci -q commit %s", config)) + if commit then sh_uci_commit(config) end end -function sh_uci_add_list(config, section, option, val) +function sh_uci_add_list(config, section, option, val, commit) exec_call(string.format("uci -q del_list %s.%s.%s=\"%s\"", config, section, option, val)) exec_call(string.format("uci -q add_list %s.%s.%s=\"%s\"", config, section, option, val)) + if commit then sh_uci_commit(config) end +end + +function sh_uci_commit(config) exec_call(string.format("uci -q commit %s", config)) end @@ -175,7 +204,7 @@ end function curl_direct(url, file, args) --直连访问 - local chn_list = libuci:get(appname, "@global[0]", "chn_list") or "direct" + local chn_list = uci:get(appname, "@global[0]", "chn_list") or "direct" local Dns = (chn_list == "proxy") and "1.1.1.1" or "223.5.5.5" if not args then args = {} end local tmp_args = clone(args) @@ -419,30 +448,10 @@ function get_domain_from_url(url) return url end -function get_node_name(node_id) - local e - if type(node_id) == "table" then - e = node_id - else - e = libuci:get_all(appname, node_id) - end - if e then - if e.type and e.remarks then - if e.protocol and (e.protocol == "_balancing" or e.protocol == "_shunt" or e.protocol == "_iface") then - local type = e.type - if type == "sing-box" then type = "Sing-Box" end - local remark = "%s:[%s] " % {type .. " " .. i18n.translatef(e.protocol), e.remarks} - return remark - end - end - end - return "" -end - function get_valid_nodes() local show_node_info = uci_get_type("@global_other[0]", "show_node_info", "0") local nodes = {} - libuci:foreach(appname, "nodes", function(e) + uci:foreach(appname, "nodes", function(e) e.id = e[".name"] if e.type and e.remarks then if e.protocol and (e.protocol == "_balancing" or e.protocol == "_shunt" or e.protocol == "_iface") then @@ -539,7 +548,7 @@ function gen_short_uuid() end function uci_get_type(type, config, default) - local value = libuci:get(appname, type, config) or default + local value = uci:get(appname, type, config) or default if (value == nil or value == "") and (default and default ~= "") then value = default end diff --git a/luci-app-passwall/luasrc/passwall/server_app.lua b/luci-app-passwall/luasrc/passwall/server_app.lua index ef267c649..50357056b 100644 --- a/luci-app-passwall/luasrc/passwall/server_app.lua +++ b/luci-app-passwall/luasrc/passwall/server_app.lua @@ -3,7 +3,7 @@ local action = arg[1] local api = require "luci.passwall.api" local sys = api.sys -local uci = api.libuci +local uci = api.uci local jsonc = api.jsonc local CONFIG = "passwall_server" diff --git a/luci-app-passwall/luasrc/passwall/util_hysteria2.lua b/luci-app-passwall/luasrc/passwall/util_hysteria2.lua index 12afe6874..b818aadbf 100644 --- a/luci-app-passwall/luasrc/passwall/util_hysteria2.lua +++ b/luci-app-passwall/luasrc/passwall/util_hysteria2.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_hysteria2", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local jsonc = api.jsonc function gen_config_server(node) diff --git a/luci-app-passwall/luasrc/passwall/util_naiveproxy.lua b/luci-app-passwall/luasrc/passwall/util_naiveproxy.lua index b6ef39b3b..ee095c1e6 100644 --- a/luci-app-passwall/luasrc/passwall/util_naiveproxy.lua +++ b/luci-app-passwall/luasrc/passwall/util_naiveproxy.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_naiveproxy", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local jsonc = api.jsonc function gen_config(var) diff --git a/luci-app-passwall/luasrc/passwall/util_shadowsocks.lua b/luci-app-passwall/luasrc/passwall/util_shadowsocks.lua index 5e724e77b..e6080001b 100644 --- a/luci-app-passwall/luasrc/passwall/util_shadowsocks.lua +++ b/luci-app-passwall/luasrc/passwall/util_shadowsocks.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_shadowsocks", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local jsonc = api.jsonc function gen_config_server(node) diff --git a/luci-app-passwall/luasrc/passwall/util_sing-box.lua b/luci-app-passwall/luasrc/passwall/util_sing-box.lua index e6cdbc7d3..bfa24b46c 100644 --- a/luci-app-passwall/luasrc/passwall/util_sing-box.lua +++ b/luci-app-passwall/luasrc/passwall/util_sing-box.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_sing-box", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local sys = api.sys local jsonc = api.jsonc local appname = "passwall" diff --git a/luci-app-passwall/luasrc/passwall/util_trojan.lua b/luci-app-passwall/luasrc/passwall/util_trojan.lua index 31bd67a4a..848b689a5 100644 --- a/luci-app-passwall/luasrc/passwall/util_trojan.lua +++ b/luci-app-passwall/luasrc/passwall/util_trojan.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_trojan", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local json = api.jsonc function gen_config_server(node) diff --git a/luci-app-passwall/luasrc/passwall/util_tuic.lua b/luci-app-passwall/luasrc/passwall/util_tuic.lua index be00dcb8c..e138b6da5 100644 --- a/luci-app-passwall/luasrc/passwall/util_tuic.lua +++ b/luci-app-passwall/luasrc/passwall/util_tuic.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_tuic", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local json = api.jsonc function gen_config(var) diff --git a/luci-app-passwall/luasrc/passwall/util_xray.lua b/luci-app-passwall/luasrc/passwall/util_xray.lua index c3ac56c20..828b7681f 100644 --- a/luci-app-passwall/luasrc/passwall/util_xray.lua +++ b/luci-app-passwall/luasrc/passwall/util_xray.lua @@ -1,6 +1,6 @@ module("luci.passwall.util_xray", package.seeall) local api = require "luci.passwall.api" -local uci = api.libuci +local uci = api.uci local sys = api.sys local jsonc = api.jsonc local appname = "passwall" diff --git a/luci-app-passwall/luasrc/view/passwall/socks_auto_switch/footer.htm b/luci-app-passwall/luasrc/view/passwall/socks_auto_switch/btn.htm similarity index 63% rename from luci-app-passwall/luasrc/view/passwall/socks_auto_switch/footer.htm rename to luci-app-passwall/luasrc/view/passwall/socks_auto_switch/btn.htm index 067b97876..287c1d2f4 100644 --- a/luci-app-passwall/luasrc/view/passwall/socks_auto_switch/footer.htm +++ b/luci-app-passwall/luasrc/view/passwall/socks_auto_switch/btn.htm @@ -19,5 +19,7 @@ local api = require "luci.passwall.api" } //]]> - - \ No newline at end of file +