diff --git a/luci-app-openclash/Makefile b/luci-app-openclash/Makefile index 89feadb0e2..d8daa89400 100644 --- a/luci-app-openclash/Makefile +++ b/luci-app-openclash/Makefile @@ -100,6 +100,7 @@ define Package/$(PKG_NAME)/postrm rm -rf /tmp/dler* >/dev/null 2>&1 rm -rf /tmp/etc/openclash >/dev/null 2>&1 rm -rf /tmp/openclash_edit_file_name >/dev/null 2>&1 + sed -i '/OpenClash Append/,/OpenClash Append End/d' "/usr/lib/lua/luci/model/network.lua" >/dev/null 2>&1 uci -q delete firewall.openclash uci -q commit firewall uci -q delete ucitrack.@openclash[-1] diff --git a/luci-app-openclash/root/etc/uci-defaults/luci-openclash b/luci-app-openclash/root/etc/uci-defaults/luci-openclash index 983c9aeb7d..830e217622 100644 --- a/luci-app-openclash/root/etc/uci-defaults/luci-openclash +++ b/luci-app-openclash/root/etc/uci-defaults/luci-openclash @@ -33,27 +33,6 @@ cat > "/lib/upgrade/keep.d/luci-app-openclash" <<-EOF /etc/openclash/ EOF -if [ -f "/tmp/openclash.bak" ]; then - mv -f "/tmp/openclash.bak" "/etc/config/openclash" >/dev/null 2>&1 - cp -rf "/tmp/openclash/." "/etc/openclash/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_config/." "/etc/openclash/config/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_core/." "/etc/openclash/core/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_history/." "/etc/openclash/history/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_proxy_provider/." "/etc/openclash/proxy_provider/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_rule_provider/." "/etc/openclash/rule_provider/" >/dev/null 2>&1 - cp -rf "/tmp/openclash_game_rules/." "/etc/openclash/game_rules/" >/dev/null 2>&1 - mv -f "/tmp/openclash_custom_rules.list.bak" "/etc/openclash/custom/openclash_custom_rules.list" >/dev/null 2>&1 - mv -f "/tmp/openclash_custom_rules_2.list.bak" "/etc/openclash/custom/openclash_custom_rules_2.list" >/dev/null 2>&1 - mv -f "/tmp/openclash_custom_hosts.list.bak" "/etc/openclash/custom/openclash_custom_hosts.list" >/dev/null 2>&1 - mv -f "/tmp/openclash_custom_fake_filter.list.bak" "/etc/openclash/custom/openclash_custom_fake_filter.list" >/dev/null 2>&1 - mv -f "/tmp/openclash_custom_domain_dns.list.bak" "/etc/openclash/custom/openclash_custom_domain_dns.list" >/dev/null 2>&1 - rm -rf "/etc/openclash/openclash" >/dev/null 2>&1 - rm -rf "/etc/openclash/fake_filter.list" >/dev/null 2>&1 - rm -rf "/etc/openclash/openclash_servers_fake_filter.conf" >/dev/null 2>&1 - rm -rf "/tmp/openclash" >/dev/null 2>&1 - rm -rf "/tmp/openclash.bak" >/dev/null 2>&1 -fi - source "/etc/openwrt_release" case "${DISTRIB_ARCH}" in aarch64_*) @@ -93,6 +72,81 @@ if [ -n "$(uci -q get dhcp.@dnsmasq[0].resolvfile)" ]; then uci -q set openclash.config.default_resolvfile=$(uci -q get dhcp.@dnsmasq[0].resolvfile) fi +if [ -f "/tmp/openclash.bak" ]; then + mv -f "/tmp/openclash.bak" "/etc/config/openclash" >/dev/null 2>&1 + cp -rf "/tmp/openclash/." "/etc/openclash/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_config/." "/etc/openclash/config/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_core/." "/etc/openclash/core/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_history/." "/etc/openclash/history/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_proxy_provider/." "/etc/openclash/proxy_provider/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_rule_provider/." "/etc/openclash/rule_provider/" >/dev/null 2>&1 + cp -rf "/tmp/openclash_game_rules/." "/etc/openclash/game_rules/" >/dev/null 2>&1 + mv -f "/tmp/openclash_custom_rules.list.bak" "/etc/openclash/custom/openclash_custom_rules.list" >/dev/null 2>&1 + mv -f "/tmp/openclash_custom_rules_2.list.bak" "/etc/openclash/custom/openclash_custom_rules_2.list" >/dev/null 2>&1 + mv -f "/tmp/openclash_custom_hosts.list.bak" "/etc/openclash/custom/openclash_custom_hosts.list" >/dev/null 2>&1 + mv -f "/tmp/openclash_custom_fake_filter.list.bak" "/etc/openclash/custom/openclash_custom_fake_filter.list" >/dev/null 2>&1 + mv -f "/tmp/openclash_custom_domain_dns.list.bak" "/etc/openclash/custom/openclash_custom_domain_dns.list" >/dev/null 2>&1 + rm -rf "/etc/openclash/openclash" >/dev/null 2>&1 + rm -rf "/etc/openclash/fake_filter.list" >/dev/null 2>&1 + rm -rf "/etc/openclash/openclash_servers_fake_filter.conf" >/dev/null 2>&1 + rm -rf "/tmp/openclash" >/dev/null 2>&1 + rm -rf "/tmp/openclash.bak" >/dev/null 2>&1 +fi + +if [ -f "/usr/lib/lua/luci/model/network.lua" ]; then + sed -i '/OpenClash Append/,/OpenClash Append End/d' "/usr/lib/lua/luci/model/network.lua" >/dev/null 2>&1 + cat >> "/usr/lib/lua/luci/model/network.lua" << EOF +-------------- OpenClash Append -------------- + +function get_all_wan_networks(self) + local k, v + local wan_nets = { } + local route_statuses = self:get_all_status_by_route("0.0.0.0", 0) + + for k, v in pairs(route_statuses) do + wan_nets[#wan_nets+1] = network(k, v.proto) + end + + return wan_nets +end + +function get_all_wan6_networks(self) + local k, v + local wan6_nets = { } + local route_statuses = self:get_all_status_by_route("::", 0) + + for k, v in pairs(route_statuses) do + wan6_nets[#wan6_nets+1] = network(k, v.proto) + end + + return wan6_nets +end + +function get_all_status_by_route(self, addr, mask) + local route_statuses = { } + local _, object + for _, object in ipairs(utl.ubus()) do + local net = object:match("^network%.interface%.(.+)") + if net then + local s = utl.ubus(object, "status", {}) + if s and s.route then + local rt + for _, rt in ipairs(s.route) do + if not rt.table and rt.target == addr and rt.mask == mask then + route_statuses[net] = s + end + end + end + end + end + + return route_statuses +end + +-------------- OpenClash Append End -------------- +EOF +fi + uci -q set openclash.config.enable=0 uci -q commit openclash diff --git a/luci-app-openclash/root/usr/share/openclash/openclash_get_network.lua b/luci-app-openclash/root/usr/share/openclash/openclash_get_network.lua index 81f43e2df7..dfe2bf201e 100644 --- a/luci-app-openclash/root/usr/share/openclash/openclash_get_network.lua +++ b/luci-app-openclash/root/usr/share/openclash/openclash_get_network.lua @@ -10,14 +10,17 @@ local wan, wan6 if not type then os.exit(0) end -if pcall(function() local x = ntm:get_wannet(); local y = ntm:get_wan6net(); end) then +if pcall(function() local x = ntm:get_all_wan_networks(); local y = ntm:get_all_wan6_networks(); end) then + wan = ntm:get_all_wan_networks() + wan6 = ntm:get_all_wan6_networks() +elseif pcall(function() local x = ntm:get_wan_networks(); local y = ntm:get_wan6_networks(); end) then + wan = ntm:get_wan_networks() + wan6 = ntm:get_wan6_networks() +elseif pcall(function() local x = ntm:get_wannet(); local y = ntm:get_wan6net(); end) then wan = {} wan6 = {} wan[1] = ntm:get_wannet() wan6[1] = ntm:get_wan6net() -elseif pcall(function() local x = ntm:get_wan_networks(); local y = ntm:get_wan6_networks(); end) then - wan = ntm:get_wan_networks() - wan6 = ntm:get_wan6_networks() else os.exit(0) end