From b913e0bc572750eb24e742a92f83c9b8b87519b1 Mon Sep 17 00:00:00 2001 From: gitea-action Date: Thu, 8 May 2025 01:00:30 +0800 Subject: [PATCH] luci-app-passwall: sync upstream last commit: https://github.com/xiaorouji/openwrt-passwall/commit/079691261d4c6345f0bf676c50bd956b6e1525b1 --- .../root/usr/share/passwall/app.sh | 5 ++ .../usr/share/passwall/helper_dnsmasq.lua | 6 +++ .../root/usr/share/passwall/lease2hosts.sh | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100755 luci-app-passwall/root/usr/share/passwall/lease2hosts.sh diff --git a/luci-app-passwall/root/usr/share/passwall/app.sh b/luci-app-passwall/root/usr/share/passwall/app.sh index c90d95302..d644f54a5 100755 --- a/luci-app-passwall/root/usr/share/passwall/app.sh +++ b/luci-app-passwall/root/usr/share/passwall/app.sh @@ -1704,6 +1704,8 @@ start_dns() { ln_run "$(first_type dnsmasq)" "dnsmasq_default" "/dev/null" -C ${GLOBAL_DNSMASQ_CONF} -x ${GLOBAL_ACL_PATH}/dnsmasq.pid set_cache_var "ACL_default_dns_port" "${GLOBAL_DNSMASQ_PORT}" DNS_REDIRECT_PORT=${GLOBAL_DNSMASQ_PORT} + #dhcp.leases to hosts + $APP_PATH/lease2hosts.sh > /dev/null 2>&1 & fi } @@ -1928,6 +1930,8 @@ acl_app() { ln_run "$(first_type dnsmasq)" "dnsmasq_${sid}" "/dev/null" -C ${dnsmasq_conf} -x ${acl_path}/dnsmasq.pid set_cache_var "ACL_${sid}_dns_port" "${dnsmasq_port}" set_cache_var "node_${tcp_node}_$(echo -n "${tcp_proxy_mode}${remote_dns}" | md5sum | cut -d " " -f1)" "${dnsmasq_port}" + #dhcp.leases to hosts + $APP_PATH/lease2hosts.sh > /dev/null 2>&1 & } _redir_port=$(get_cache_var "node_${tcp_node}_redir_port") _socks_port=$(get_cache_var "node_${tcp_node}_socks_port") @@ -2155,6 +2159,7 @@ stop() { } rm -rf $TMP_PATH rm -rf /tmp/lock/${CONFIG}_socks_auto_switch* + rm -rf /tmp/lock/${CONFIG}_lease2hosts* echolog "清空并关闭相关程序和缓存完成。" exit 0 } diff --git a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq.lua b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq.lua index 165ed51fc..0247edb1c 100644 --- a/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq.lua +++ b/luci-app-passwall/root/usr/share/passwall/helper_dnsmasq.lua @@ -693,6 +693,12 @@ function add_rule(var) api.set_cache_var("DEFAULT_DNS", DEFAULT_DNS) end end + + --dhcp.leases to hosts + local hosts = "/tmp/etc/" .. appname .. "_tmp/dhcp-hosts" + sys.call("touch " .. hosts) + tinsert(conf_lines, "addn-hosts=" .. hosts) + if #conf_lines > 0 then local conf_out = io.open(DNSMASQ_CONF_FILE, "a") conf_out:write(table.concat(conf_lines, "\n")) diff --git a/luci-app-passwall/root/usr/share/passwall/lease2hosts.sh b/luci-app-passwall/root/usr/share/passwall/lease2hosts.sh new file mode 100755 index 000000000..359297509 --- /dev/null +++ b/luci-app-passwall/root/usr/share/passwall/lease2hosts.sh @@ -0,0 +1,47 @@ +#!/bin/sh + +# dhcp.leases to hosts + +CONFIG=passwall +TMP_PATH=/tmp/etc/${CONFIG} +TMP_PATH2=/tmp/etc/${CONFIG}_tmp +LOCK_FILE=/tmp/lock/${CONFIG}_lease2hosts.lock +LEASE_FILE="/tmp/dhcp.leases" +HOSTS_FILE="$TMP_PATH2/dhcp-hosts" +TMP_FILE="/tmp/dhcp-hosts.tmp" + +exec 99>"$LOCK_FILE" +flock -n 99 +if [ "$?" != 0 ]; then + exit 0 +fi + +reload_dnsmasq_pids() { + local pidfile pid + find $TMP_PATH/acl -type f -name 'dnsmasq.pid' 2>/dev/null | while read pidfile; do + if [ -s "$pidfile" ]; then + read pid < "$pidfile" + if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then + kill -HUP "$pid" + fi + fi + done +} + +while true; do + + if [ -s "$LEASE_FILE" ]; then + awk 'NF >= 4 {print $3" "$4}' "$LEASE_FILE" | sort > "$TMP_FILE" + if [ -f "$TMP_FILE" ]; then + if [ ! -f "$HOSTS_FILE" ] || [ "$(md5sum "$TMP_FILE" | awk '{print $1}')" != "$(md5sum "$HOSTS_FILE" | awk '{print $1}')" ]; then + mv "$TMP_FILE" "$HOSTS_FILE" + reload_dnsmasq_pids + else + rm -rf "$TMP_FILE" + fi + fi + fi + + sleep 60 + +done 2>/dev/null