Skip to content

Commit

Permalink
chore: refine
Browse files Browse the repository at this point in the history
  • Loading branch information
vernesong committed May 18, 2024
1 parent 062ddd1 commit 80146d1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 17 deletions.
8 changes: 7 additions & 1 deletion luci-app-openclash/luasrc/controller/openclash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ local function chnroutev6()
end

local function daip()
local daip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
local daip, lan_int_name
lan_int_name = uci:get("openclash", "config", "lan_interface_name") or "0"
if lan_int_name == "0" then
daip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
else
daip = luci.sys.exec(string.format("ip address show %s | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'", lan_int_name))
end
if not daip or daip == "" then
daip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'")
end
Expand Down
15 changes: 13 additions & 2 deletions luci-app-openclash/luasrc/model/cbi/openclash/config-overwrite.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ bold_off = [[</strong>]]

local op_mode = string.sub(luci.sys.exec('uci get openclash.config.operation_mode 2>/dev/null'),0,-2)
if not op_mode then op_mode = "redir-host" end
local lan_ip = SYS.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n' || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1 | tr -d '\n'")

local lan_int_name = uci:get("openclash", "config", "lan_interface_name") or "0"
local lan_ip
if lan_int_name == "0" then
lan_ip = SYS.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
else
lan_ip = SYS.exec(string.format("ip address show %s | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'", lan_int_name))
end
if not lan_ip or lan_ip == "" then
lan_ip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'")
end
if not lan_ip or lan_ip == "" then
lan_ip = luci.sys.exec("ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1 | tr -d '\n'")
end
m = Map("openclash", translate("Overwrite Settings"))
m.pageaction = false
m.description = translate("Note: To restore the default configuration, try accessing:").." <a href='javascript:void(0)' onclick='javascript:restore_config(this)'>http://"..lan_ip.."/cgi-bin/luci/admin/services/openclash/restore</a>"
Expand Down
31 changes: 29 additions & 2 deletions luci-app-openclash/luasrc/model/cbi/openclash/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ bold_off = [[</strong>]]

local op_mode = string.sub(luci.sys.exec('uci get openclash.config.operation_mode 2>/dev/null'),0,-2)
if not op_mode then op_mode = "redir-host" end
local lan_ip = SYS.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n' || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1 | tr -d '\n'")

local lan_int_name = uci:get("openclash", "config", "lan_interface_name") or "0"
local lan_ip
if lan_int_name == "0" then
lan_ip = SYS.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
else
lan_ip = SYS.exec(string.format("ip address show %s | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'", lan_int_name))
end
if not lan_ip or lan_ip == "" then
lan_ip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'")
end
if not lan_ip or lan_ip == "" then
lan_ip = luci.sys.exec("ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1 | tr -d '\n'")
end
m = Map("openclash", translate("Plugin Settings"))
m.pageaction = false
m.description = translate("Note: To restore the default configuration, try accessing:").." <a href='javascript:void(0)' onclick='javascript:restore_config(this)'>http://"..lan_ip.."/cgi-bin/luci/admin/services/openclash/restore</a>"..
Expand Down Expand Up @@ -272,6 +283,22 @@ o = s:taboption("traffic_control", Flag, "intranet_allowed", translate("Only int
o.description = translate("When Enabled, The Control Panel And The Connection Broker Port Will Not Be Accessible From The Public Network")
o.default = 1

o = s:taboption("traffic_control", DynamicList, "intranet_allowed_wan_name", translate("WAN Interface Name"))
o.description = translate("Select WAN Interface Name For The Intranet Allowed")
o:depends("intranet_allowed", "1")
local interfaces = SYS.exec("ls -l /sys/class/net/ 2>/dev/null |awk '{print $9}' 2>/dev/null")
for interface in string.gmatch(interfaces, "%S+") do
o:value(interface)
end

o = s:taboption("traffic_control", ListValue, "lan_interface_name", translate("LAN Interface Name"))
o.description = translate("Select LAN Interface Name")
o:value("0", translate("Disable"))
o.default = "0"
for interface in string.gmatch(interfaces, "%S+") do
o:value(interface)
end

o = s:taboption("traffic_control", Value, "local_network_pass", translate("Local IPv4 Network Bypassed List"))
o.template = "cbi/tvalue"
o.description = translate("The Traffic of The Destination For The Specified Address Will Not Pass The Core")
Expand Down
14 changes: 13 additions & 1 deletion luci-app-openclash/po/zh-cn/openclash.zh-cn.po
Original file line number Diff line number Diff line change
Expand Up @@ -3538,4 +3538,16 @@ msgid "Type CDN URL, Format Like"
msgstr "输入链接地址,格式请参考"

msgid "Please enter a valid URL!"
msgstr "请参考示例输入正确的链接地址!"
msgstr "请参考示例输入正确的链接地址!"

msgid "WAN Interface Name"
msgstr "WAN 接口名称"

msgid "Select WAN Interface Name For The Intranet Allowed"
msgstr "为仅允许内网指定正确的 WAN 接口名称"

msgid "LAN Interface Name"
msgstr "LAN 接口名称"

msgid "Select LAN Interface Name"
msgstr "指定正确的 LAN 接口名称"
62 changes: 57 additions & 5 deletions luci-app-openclash/root/etc/init.d/openclash
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,32 @@ ac_add()
[ -n "$3" ] && ipset add "$3" "$1" 2>/dev/null
}

wan_name_add()
{
if [ -z "$1" ]; then
return
fi

if [ -n "$wan_ints" ]; then
wan_ints="$wan_ints $1"
else
wan_ints="$1"
fi
}

wan6_name_add()
{
if [ -z "$1" ]; then
return
fi

if [ -n "$wan6_ints" ]; then
wan6_ints="$wan6_ints $1"
else
wan6_ints="$1"
fi
}

upnp_exclude()
{
if [ -s "$upnp_lease_file" ]; then
Expand Down Expand Up @@ -1526,7 +1552,12 @@ if [ -n "$FW4" ]; then

#intranet allowed
if [ "$intranet_allowed" -eq 1 ]; then
wan_ints=$(nft list chain inet fw4 input |grep -e "jump input_wan" 2>/dev/null |awk '{for (i=1;i<=NF;i++){if ($i ~ /iifname/ && $(i+1) != "{") {print $(i+1)} if ($i ~ /iifname/ && $(i+1) == "{"){for (j=i+1;j<=NF;j++){if ($j~ /}/) {out="";for (k=i+1;k<=j;k++){out=out" "$k};print out}}}}}' 2>/dev/null |sed 's/"//g'|sed 's/{//g'|sed 's/}//g'|sed 's/,//g')
if [ -n "$intranet_allowed_wan_name" ]; then
config_load "openclash"
config_list_foreach "config" "intranet_allowed_wan_name" wan_name_add
else
wan_ints=$(nft list chain inet fw4 input |grep -e "jump input_wan" 2>/dev/null |awk '{for (i=1;i<=NF;i++){if ($i ~ /iifname/ && $(i+1) != "{") {print $(i+1)} if ($i ~ /iifname/ && $(i+1) == "{"){for (j=i+1;j<=NF;j++){if ($j~ /}/) {out="";for (k=i+1;k<=j;k++){out=out" "$k};print out}}}}}' 2>/dev/null |sed 's/"//g'|sed 's/{//g'|sed 's/}//g'|sed 's/,//g')
fi
if [ -n "$wan_ints" ]; then
nft 'add chain inet fw4 openclash_wan_input'
nft 'flush chain inet fw4 openclash_wan_input'
Expand Down Expand Up @@ -2174,7 +2205,12 @@ if [ -n "$FW4" ]; then

#intranet allowed
if [ "$intranet_allowed" -eq 1 ]; then
wan6_ints=$(nft list chain inet fw4 input |grep -e "jump input_wan" 2>/dev/null |awk '{for (i=1;i<=NF;i++){if ($i ~ /iifname/ && $(i+1) != "{") {print $(i+1)} if ($i ~ /iifname/ && $(i+1) == "{"){for (j=i+1;j<=NF;j++){if ($j~ /}/) {out="";for (k=i+1;k<=j;k++){out=out" "$k};print out}}}}}' 2>/dev/null |sed 's/"//g'|sed 's/{//g'|sed 's/}//g'|sed 's/,//g')
if [ -n "$intranet_allowed_wan_name" ]; then
config_load "openclash"
config_list_foreach "config" "intranet_allowed_wan_name" wan6_name_add
else
wan6_ints=$(nft list chain inet fw4 input |grep -e "jump input_wan" 2>/dev/null |awk '{for (i=1;i<=NF;i++){if ($i ~ /iifname/ && $(i+1) != "{") {print $(i+1)} if ($i ~ /iifname/ && $(i+1) == "{"){for (j=i+1;j<=NF;j++){if ($j~ /}/) {out="";for (k=i+1;k<=j;k++){out=out" "$k};print out}}}}}' 2>/dev/null |sed 's/"//g'|sed 's/{//g'|sed 's/}//g'|sed 's/,//g')
fi
if [ -n "$wan6_ints" ]; then
nft 'add chain inet fw4 openclash_wan6_input'
nft 'flush chain inet fw4 openclash_wan6_input'
Expand Down Expand Up @@ -2294,7 +2330,12 @@ if [ -z "$FW4" ]; then

#intranet allowed
if [ "$intranet_allowed" -eq 1 ]; then
wan_ints=$(iptables-save -t filter |grep -e "-j zone_wan_input" 2>/dev/null |awk '{for (i=1;i<=NF;i++) {if ($i ~ /-i/) {print $(i+1)}}}' 2>/dev/null)
if [ -n "$intranet_allowed_wan_name" ]; then
config_load "openclash"
config_list_foreach "config" "intranet_allowed_wan_name" wan_name_add
else
wan_ints=$(iptables-save -t filter |grep -e "-j zone_wan_input" 2>/dev/null |awk '{for (i=1;i<=NF;i++) {if ($i ~ /-i/) {print $(i+1)}}}' 2>/dev/null)
fi
if [ -n "$wan_ints" ]; then
iptables -t filter -N openclash_wan_input
iptables -t filter -F openclash_wan_input
Expand Down Expand Up @@ -2952,7 +2993,12 @@ if [ -z "$FW4" ]; then

#intranet allowed
if [ "$intranet_allowed" -eq 1 ]; then
wan6_ints=$(ip6tables-save -t filter |grep -e "-j zone_wan_input" 2>/dev/null |awk '{for (i=1;i<=NF;i++) {if ($i ~ /-i/) {print $(i+1)}}}' 2>/dev/null)
if [ -n "$intranet_allowed_wan_name" ]; then
config_load "openclash"
config_list_foreach "config" "intranet_allowed_wan_name" wan6_name_add
else
wan6_ints=$(ip6tables-save -t filter |grep -e "-j zone_wan_input" 2>/dev/null |awk '{for (i=1;i<=NF;i++) {if ($i ~ /-i/) {print $(i+1)}}}' 2>/dev/null)
fi
if [ -n "$wan6_ints" ]; then
ip6tables -t filter -N openclash_wan_input
ip6tables -t filter -F openclash_wan_input
Expand Down Expand Up @@ -3092,7 +3138,12 @@ get_config()
fakeip_range=$(uci -q get openclash.config.fakeip_range)
fi
[ -z "$fakeip_range" ] && fakeip_range="198.18.0.1/16"
lan_ip=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
lan_interface_name=$(uci -q get openclash.config.lan_interface_name || echo "0")
if [ "$lan_interface_name" = "0" ]; then
lan_ip=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
else
lan_ip=$(ip address show $(uci -q -p $lan_interface_name) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}')
fi
wan_ip4s=$(/usr/share/openclash/openclash_get_network.lua "wanip" 2>/dev/null)
wan_ip6s=$(ifconfig | grep 'inet6 addr' | awk '{print $3}' 2>/dev/null)
disable_masq_cache=$(uci -q get openclash.config.disable_masq_cache)
Expand Down Expand Up @@ -3132,6 +3183,7 @@ get_config()
enable_unified_delay=$(uci -q get openclash.config.enable_unified_delay || echo "0")
keep_alive_interval=$(uci -q get openclash.config.keep_alive_interval || echo "0")
proxy_dns_group=$(uci -q get openclash.config.proxy_dns_group || echo "Disable")
intranet_allowed_wan_name=$(uci -q get openclash.config.intranet_allowed_wan_name || echo "0")
[ -z "$dns_port" ] && dns_port=7874 && uci -q set openclash.config.dns_port=7874
uci -q set openclash.config.restricted_mode=0 && uci -q commit openclash
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ router_self_proxy=$(uci -q get openclash.config.router_self_proxy)
core_type=$(uci -q get openclash.config.core_type || echo "Dev")
da_password=$(uci -q get openclash.config.dashboard_password)
cn_port=$(uci -q get openclash.config.cn_port)
lan_ip=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
lan_interface_name=$(uci -q get openclash.config.lan_interface_name || echo "0")
if [ "$lan_interface_name" = "0" ]; then
lan_ip=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
else
lan_ip=$(ip address show $(uci -q -p $lan_interface_name) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}')
fi
dnsmasq_default_resolvfile=$(uci -q get openclash.config.default_resolvfile)

if [ -z "$RAW_CONFIG_FILE" ] || [ ! -f "$RAW_CONFIG_FILE" ]; then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ local addr = arg[1]
local resolve = arg[2]

local function debug_dns()
local info, ip, host
ip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
local info, ip, host, lan_int_name
lan_int_name = uci:get("openclash", "config", "lan_interface_name") or "0"
if lan_int_name == "0" then
ip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
else
ip = luci.sys.exec(string.format("ip address show %s | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'", lan_int_name))
end
if not ip or ip == "" then
ip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ return string.format("%.1f",e)..a[t]
end

local function debug_getcon()
local info, ip, host, diag_info
ip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
local info, ip, host, diag_info, lan_int_name
lan_int_name = uci:get("openclash", "config", "lan_interface_name") or "0"
if lan_int_name == "0" then
ip = luci.sys.exec("uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null |tr -d '\n'")
else
ip = luci.sys.exec(string.format("ip address show %s | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'", lan_int_name))
end
if not ip or ip == "" then
ip = luci.sys.exec("ip address show $(uci -q -p /tmp/state get network.lan.ifname) | grep -w 'inet' 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | tr -d '\n'")
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ del_lock() {

close_all_conection() {
SECRET=$(uci -q get openclash.config.dashboard_password)
LAN_IP=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
lan_interface_name=$(uci -q get openclash.config.lan_interface_name || echo "0")
if [ "$lan_interface_name" = "0" ]; then
LAN_IP=$(uci -q get network.lan.ipaddr |awk -F '/' '{print $1}' 2>/dev/null || ip address show $(uci -q -p /tmp/state get network.lan.ifname || uci -q -p /tmp/state get network.lan.device) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}' || ip addr show 2>/dev/null | grep -w 'inet' | grep 'global' | grep 'brd' | grep -Eo 'inet [0-9\.]+' | awk '{print $2}' | head -n 1)
else
LAN_IP=$(ip address show $(uci -q -p $lan_interface_name) | grep -w "inet" 2>/dev/null |grep -Eo 'inet [0-9\.]+' | awk '{print $2}')
fi
PORT=$(uci -q get openclash.config.cn_port)
curl -m 2 -H "Authorization: Bearer ${SECRET}" -H "Content-Type:application/json" -X DELETE http://"$LAN_IP":"$PORT"/connections >/dev/null 2>&1
}
Expand Down

0 comments on commit 80146d1

Please sign in to comment.