Skip to content

Commit

Permalink
luci: fixed known bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaorouji committed Nov 17, 2024
1 parent 593290a commit 6d4b0ca
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 96 deletions.
2 changes: 1 addition & 1 deletion luci-app-passwall2/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=luci-app-passwall2
PKG_VERSION:=24.11.17
PKG_RELEASE:=1
PKG_RELEASE:=2

PKG_CONFIG_DEPENDS:= \
CONFIG_PACKAGE_$(PKG_NAME)_Iptables_Transparent_Proxy \
Expand Down
196 changes: 105 additions & 91 deletions luci-app-passwall2/luasrc/passwall2/util_xray.lua
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,20 @@ function gen_config(var)
return inboundTag
end

local function gen_balancer(_node, loopbackTag)
local function gen_balancer(_node, loopback_tag)
local balancer_id = _node[".name"]
local balancer_tag = "balancer-" .. balancer_id
local loopback_dst = balancer_id -- route destination for the loopback outbound
if not loopback_tag or loopback_tag == "" then loopback_tag = balancer_id end
-- existing balancer
for _, v in ipairs(balancers) do
if v.tag == balancer_tag then
gen_loopback(loopback_tag, loopback_dst)
return balancer_tag
end
end
-- new balancer
local blc_nodes = _node.balancing_node
local fallback_node_id = _node.fallback_node
local length = #blc_nodes
local valid_nodes = {}
for i = 1, length do
Expand All @@ -698,6 +709,10 @@ function gen_config(var)
end
end
end
if #valid_nodes == 0 then return nil end

-- fallback node
local fallback_node_id = _node.fallback_node
if fallback_node_id == "" then fallback_node_id = nil end
if fallback_node_id then
local is_new_node = true
Expand All @@ -718,41 +733,33 @@ function gen_config(var)
fallback_node_id = nil
end
else
local valid = gen_balancer(fallback_node)
if not valid then
if not gen_balancer(fallback_node) then
fallback_node_id = nil
end
end
end
end

local valid = nil
if #valid_nodes > 0 then
local balancerTag = get_balancer_tag(_node[".name"])
table.insert(balancers, {
tag = balancerTag,
selector = valid_nodes,
fallbackTag = fallback_node_id,
strategy = { type = _node.balancingStrategy or "random" }
})
if _node.balancingStrategy == "leastPing" or fallback_node_id then
if not observatory then
observatory = {
subjectSelector = { "blc-" },
probeUrl = _node.useCustomProbeUrl and _node.probeUrl or nil,
probeInterval = _node.probeInterval or "1m",
enableConcurrency = true
}
end
table.insert(balancers, {
tag = balancer_tag,
selector = valid_nodes,
fallbackTag = fallback_node_id,
strategy = { type = _node.balancingStrategy or "random" }
})
if _node.balancingStrategy == "leastPing" or fallback_node_id then
if not observatory then
observatory = {
subjectSelector = { "blc-" },
probeUrl = _node.useCustomProbeUrl and _node.probeUrl or nil,
probeInterval = _node.probeInterval or "1m",
enableConcurrency = true
}
end
if loopbackTag == nil or loopbackTag =="" then loopbackTag = _node[".name"] end
local inboundTag = gen_loopback(loopbackTag, _node[".name"])
table.insert(rules, { inboundTag = { inboundTag }, balancerTag = balancerTag })
valid = true
end
return valid
local inbound_tag = gen_loopback(loopback_tag, loopback_dst)
table.insert(rules, { inboundTag = { inbound_tag }, balancerTag = balancer_tag })
return balancer_tag
end

local function set_outbound_detour(node, outbound, outbounds_table, shunt_rule_name)
if not node or not outbound or not outbounds_table then return nil end
local default_outTag = outbound.tag
Expand Down Expand Up @@ -787,31 +794,25 @@ function gen_config(var)
node.port = server_port
end
if node.protocol == "_shunt" then
local preproxy_enabled = node.preproxy_enabled == "1"
local preproxy_rule_name = "main"
local preproxy_tag = "main"
local preproxy_rule_name = node.preproxy_enabled == "1" and "main" or nil
local preproxy_tag = preproxy_rule_name
local preproxy_node_id = node["main_node"]
local preproxy_node = preproxy_enabled and preproxy_node_id and uci:get_all(appname, preproxy_node_id) or nil
if preproxy_node then
preproxy_tag = preproxy_tag .. ":" .. preproxy_node.remarks
end

local proxy_outboundTag, proxy_balancerTag
local preproxy_outbound_tag, preproxy_balancer_tag
local preproxy_nodes

local function gen_shunt_node(rule_name, _node_id)
if not rule_name then return nil, nil end
if not _node_id then _node_id = node[rule_name] or "nil" end
local rule_outboundTag
local rule_balancerTag
if _node_id == "_direct" then
rule_outboundTag = "direct"
return "direct", nil
elseif _node_id == "_blackhole" then
rule_outboundTag = "blackhole"
return "blackhole", nil
elseif _node_id == "_default" then
rule_outboundTag = "default"
return "default", nil
elseif _node_id:find("Socks_") then
local socks_id = _node_id:sub(1 + #"Socks_")
local socks_node = uci:get_all(appname, socks_id) or nil
local socks_tag
if socks_node then
local _node = {
type = "Xray",
Expand All @@ -823,35 +824,33 @@ function gen_config(var)
}
local outbound = gen_outbound(flag, _node, rule_name)
if outbound then
table.insert(outbounds, outbound)
rule_outboundTag = outbound.tag
if rule_name == "default" then
table.insert(outbounds, 1, outbound)
else
table.insert(outbounds, outbound)
end
socks_tag = outbound.tag
end
end
return socks_tag, nil
elseif _node_id ~= "nil" then
local _node = uci:get_all(appname, _node_id)
if not _node then return nil, nil end

if api.is_normal_node(_node) then
local use_proxy = preproxy_node and node[rule_name .. "_proxy_tag"] == preproxy_rule_name and _node_id ~= preproxy_node_id
if use_proxy and proxy_balancerTag then
for _, blc_node_id in ipairs(preproxy_node.balancing_node) do
if _node_id == blc_node_id then
use_proxy = false
break
end
end
end
local use_proxy = preproxy_tag and node[rule_name .. "_proxy_tag"] == preproxy_rule_name and _node_id ~= preproxy_node_id
if use_proxy and preproxy_balancer_tag and preproxy_nodes[_node_id] then use_proxy = false end
local copied_outbound
for index, value in ipairs(outbounds) do
if value["_id"] == _node_id and value["_flag_proxy_tag"] == preproxy_tag then
if value["_id"] == _node_id and value["_flag_proxy_tag"] == (use_proxy and preproxy_tag or nil) then
copied_outbound = api.clone(value)
break
end
end
if copied_outbound then
copied_outbound.tag = rule_name .. ":" .. _node.remarks
table.insert(outbounds, copied_outbound)
rule_outboundTag = rule_name
return copied_outbound.tag, nil
else
if use_proxy and (_node.type ~= "Xray" or _node.flow == "xtls-rprx-vision") then
new_port = get_new_port()
Expand All @@ -869,8 +868,8 @@ function gen_config(var)
_node.port = new_port
table.insert(rules, 1, {
inboundTag = {"proxy_" .. rule_name},
outboundTag = proxy_outboundTag,
balancerTag = proxy_balancerTag
outboundTag = not preproxy_balancer_tag and preproxy_tag or nil,
balancerTag = preproxy_balancer_tag
})
end
local proxy_table = {
Expand All @@ -885,33 +884,22 @@ function gen_config(var)
end
end
local outbound = gen_outbound(flag, _node, rule_name, proxy_table)
local outbound_tag
if outbound then
outbound.tag = outbound.tag .. ":" .. _node.remarks
rule_outboundTag = set_outbound_detour(_node, outbound, outbounds, rule_name)
outbound_tag = set_outbound_detour(_node, outbound, outbounds, rule_name)
if rule_name == "default" then
table.insert(outbounds, 1, outbound)
else
table.insert(outbounds, outbound)
end
end
return outbound_tag, nil
end
elseif _node.protocol == "_balancing" then
local is_new_balancer = true
rule_balancerTag = get_balancer_tag(_node_id)
for _, v in ipairs(balancers) do
if v.tag == rule_balancerTag then
is_new_balancer = false
gen_loopback(rule_name, _node_id)
break
end
end
if is_new_balancer then
local valid = gen_balancer(_node, rule_name)
if not valid then
rule_balancerTag = nil
end
end
return nil, gen_balancer(_node, rule_name)
elseif _node.protocol == "_iface" then
local outbound_tag
if _node.iface then
local outbound = {
protocol = "freedom",
Expand All @@ -923,26 +911,40 @@ function gen_config(var)
}
}
}
outbound_tag = outbound.tag
table.insert(outbounds, outbound)
rule_outboundTag = rule_name
sys.call("touch /tmp/etc/passwall2/iface/" .. _node.iface)
end
return outbound_tag, nil
end
end
return rule_outboundTag, rule_balancerTag
end

if preproxy_node then
proxy_outboundTag, proxy_balancerTag = gen_shunt_node(preproxy_rule_name, preproxy_node_id)
if not proxy_outboundTag and not proxy_balancerTag then
preproxy_node = nil
if preproxy_tag and preproxy_node_id then
preproxy_outbound_tag, preproxy_balancer_tag = gen_shunt_node(preproxy_rule_name, preproxy_node_id)
if preproxy_balancer_tag then
local _node_id = preproxy_node_id
preproxy_nodes = {}
while _node_id do
_node = uci:get_all(appname, _node_id)
if not _node then break end
if _node.protocol ~= "_balancing" then
preproxy_nodes[_node_id] = true
break
end
local _blc_nodes = _node.balancing_node
for i = 1, #_blc_nodes do preproxy_nodes[_blc_nodes[i]] = true end
_node_id = _node.fallback_node
end
elseif preproxy_outbound_tag then
preproxy_tag = preproxy_outbound_tag
end
end
--default_node
local default_node_id = node.default_node or "_direct"
local default_outboundTag, default_balancerTag = gen_shunt_node("default", default_node_id)
COMMON.default_outbound_tag = default_outbound_tag
COMMON.default_balancer_tag = default_balancer_tag
COMMON.default_outbound_tag = default_outboundTag
COMMON.default_balancer_tag = default_balancerTag
--shunt rule
uci:foreach(appname, "shunt_rules", function(e)
local outboundTag, balancerTag = gen_shunt_node(e[".name"])
Expand Down Expand Up @@ -1053,15 +1055,15 @@ function gen_config(var)
}
elseif node.protocol == "_balancing" then
if node.balancing_node then
local valid = gen_balancer(node)
if valid then
table.insert(rules, { network = "tcp,udp", balancerTag = get_balancer_tag(node_id) })
local balancer_tag = gen_balancer(node)
if balancer_tag then
table.insert(rules, { network = "tcp,udp", balancerTag = balancer_tag })
end
routing = {
balancers = balancers,
rules = rules
}
COMMON.default_balancer_tag = get_balancer_tag(node_id)
COMMON.default_balancer_tag = balancer_tag
end
elseif node.protocol == "_iface" then
if node.iface then
Expand Down Expand Up @@ -1358,7 +1360,7 @@ function gen_config(var)

local default_rule_index = #routing.rules > 0 and #routing.rules or 1
for index, value in ipairs(routing.rules) do
if value["_flag"] == "default" then
if value.ruleTag == "default" then
default_rule_index = index
break
end
Expand Down Expand Up @@ -1461,7 +1463,7 @@ function gen_config(var)
})
end

table.insert(outbounds, {
local direct_outbound = {
protocol = "freedom",
tag = "direct",
settings = {
Expand All @@ -1472,11 +1474,23 @@ function gen_config(var)
mark = 255
}
}
})
table.insert(outbounds, {
}
if COMMON.default_outbound_tag == "direct" then
table.insert(outbounds, 1, direct_outbound)
else
table.insert(outbounds, direct_outbound)
end

local blackhole_outbound = {
protocol = "blackhole",
tag = "blackhole"
})
}
if COMMON.default_outbound_tag == "blackhole" then
table.insert(outbounds, 1, blackhole_outbound)
else
table.insert(outbounds, blackhole_outbound)
end

for index, value in ipairs(config.outbounds) do
for k, v in pairs(config.outbounds[index]) do
if k:find("_") == 1 then
Expand Down
4 changes: 2 additions & 2 deletions luci-app-passwall2/root/usr/share/passwall2/iptables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ load_acl() {
}

_acl_list=${TMP_ACL_PATH}/${sid}/rule_list
[ $use_interface = "1" ] && _acl_list=${TMP_ACL_PATH}/${sid}/interface_list
[ "$use_interface" = "1" ] && _acl_list=${TMP_ACL_PATH}/${sid}/interface_list

for i in $(cat $_acl_list); do
if [ $use_interface = "0" ]; then
if [ "$use_interface" = "0" ]; then
if [ -n "$(echo ${i} | grep '^iprange:')" ]; then
_iprange=$(echo ${i} | sed 's#iprange:##g')
_ipt_source=$(factor ${_iprange} "-m iprange --src-range")
Expand Down
4 changes: 2 additions & 2 deletions luci-app-passwall2/root/usr/share/passwall2/nftables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ load_acl() {
}

_acl_list=${TMP_ACL_PATH}/${sid}/rule_list
[ $use_interface = "1" ] && _acl_list=${TMP_ACL_PATH}/${sid}/interface_list
[ "$use_interface" = "1" ] && _acl_list=${TMP_ACL_PATH}/${sid}/interface_list

for i in $(cat $_acl_list); do
if [ $use_interface = "0" ]; then
if [ "$use_interface" = "0" ]; then
if [ -n "$(echo ${i} | grep '^iprange:')" ]; then
_iprange=$(echo ${i} | sed 's#iprange:##g')
_ipt_source=$(factor ${_iprange} "ip saddr")
Expand Down

0 comments on commit 6d4b0ca

Please sign in to comment.