From 5feeddd3d9c9632bf4c204525fd9017a09475607 Mon Sep 17 00:00:00 2001 From: gitea-action Date: Fri, 22 Nov 2024 19:30:24 +0800 Subject: [PATCH] mihomo: sync upstream last commit: https://github.com/morytyann/OpenWrt-mihomo/commit/f188fce1cca7ade26543010b0d5080332f13755c --- mihomo/Makefile | 6 +- mihomo/files/mihomo.init | 84 ++++++++++++------- .../scripts/{tun.sh => firewall_include.sh} | 2 +- .../scripts/{constants.sh => include.sh} | 28 ++++++- mihomo/files/uci-defaults/firewall.sh | 4 +- mihomo/files/uci-defaults/init.sh | 2 +- mihomo/files/uci-defaults/migrate.sh | 2 +- 7 files changed, 90 insertions(+), 38 deletions(-) rename mihomo/files/scripts/{tun.sh => firewall_include.sh} (92%) rename mihomo/files/scripts/{constants.sh => include.sh} (53%) diff --git a/mihomo/Makefile b/mihomo/Makefile index 624115aed..2ed6cfae9 100644 --- a/mihomo/Makefile +++ b/mihomo/Makefile @@ -1,7 +1,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=mihomo -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_SOURCE_PROTO:=git PKG_SOURCE_URL:=https://github.com/MetaCubeX/mihomo.git @@ -59,8 +59,8 @@ define Package/mihomo/install $(INSTALL_DATA) $(CURDIR)/files/mixin.yaml $(1)/etc/mihomo/mixin.yaml - $(INSTALL_BIN) $(CURDIR)/files/scripts/constants.sh $(1)/etc/mihomo/scripts/constants.sh - $(INSTALL_BIN) $(CURDIR)/files/scripts/tun.sh $(1)/etc/mihomo/scripts/tun.sh + $(INSTALL_BIN) $(CURDIR)/files/scripts/include.sh $(1)/etc/mihomo/scripts/include.sh + $(INSTALL_BIN) $(CURDIR)/files/scripts/firewall_include.sh $(1)/etc/mihomo/scripts/firewall_include.sh $(INSTALL_BIN) $(CURDIR)/files/nftables/hijack.nft $(1)/etc/mihomo/nftables/hijack.nft $(INSTALL_BIN) $(CURDIR)/files/nftables/reserved_ip.nft $(1)/etc/mihomo/nftables/reserved_ip.nft diff --git a/mihomo/files/mihomo.init b/mihomo/files/mihomo.init index 0b834ed46..7dc852360 100644 --- a/mihomo/files/mihomo.init +++ b/mihomo/files/mihomo.init @@ -5,7 +5,7 @@ STOP=10 USE_PROCD=1 . "$IPKG_INSTROOT/lib/functions/network.sh" -. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh" +. "$IPKG_INSTROOT/etc/mihomo/scripts/include.sh" extra_command 'update_subscription' 'Update subscription by section id' @@ -18,7 +18,7 @@ boot() { local enabled start_delay config_get_bool enabled "config" "enabled" 0 config_get start_delay "config" "start_delay" 0 - if [[ "$enabled" == 1 && "$start_delay" > 0 ]]; then + if [[ "$enabled" == 1 && "$start_delay" -gt 0 ]]; then log "App will start after $start_delay seconds." sleep "$start_delay" fi @@ -159,22 +159,8 @@ start_service() { cp -f "$profile_file" "$RUN_PROFILE_PATH" elif [[ "$profile" == "subscription:"* ]]; then local subscription_section; subscription_section="${profile/subscription:/}" - local subscription_name subscription_url subscription_user_agent subscription_prefer - config_get subscription_name "$subscription_section" "name" - config_get subscription_url "$subscription_section" "url" - config_get subscription_user_agent "$subscription_section" "user_agent" - config_get subscription_prefer "$subscription_section" "prefer" "remote" - log "Use Subscription: $subscription_name." - local subscription_tmpfile; subscription_tmpfile="/tmp/$subscription_section.yaml" local subscription_file; subscription_file="$SUBSCRIPTIONS_DIR/$subscription_section.yaml" - if [ "$subscription_prefer" == "remote" ] || [[ "$subscription_prefer" == "local" && ! -f "$subscription_file" ]]; then - if (curl -s -f --connect-timeout 15 --retry 3 -o "$subscription_tmpfile" -L -H "User-Agent: $subscription_user_agent" "$subscription_url"); then - log "Subscription download succeed." - cp -f "$subscription_tmpfile" "$subscription_file" - else - log "Subscription download failed, fallback to subscription file." - fi - fi + update_subscription "$subscription_section" if [ ! -f "$subscription_file" ]; then log "Subscription file not found." log "Exiting..." @@ -244,8 +230,7 @@ start_service() { fi yq -M -i 'del (.bind-address)' "$RUN_PROFILE_PATH" if [ -n "$outbound_interface" ]; then - local outbound_device - network_get_device outbound_device "$outbound_interface" + local outbound_device; network_get_device outbound_device "$outbound_interface" if [ -n "$outbound_device" ]; then outbound_device="$outbound_device" yq -M -i '.interface-name = env(outbound_device)' "$RUN_PROFILE_PATH" fi @@ -313,7 +298,7 @@ start_service() { if [ "$ipv6_proxy" == 1 ]; then ip -6 route add unicast default dev "$TUN_DEVICE" table "$TUN_ROUTE_TABLE" fi - $TUN_SH + $FIREWALL_INCLUDE_SH fi local tcp_route_table if [ "$tcp_transparent_proxy_mode" == "tproxy" ]; then @@ -635,14 +620,55 @@ add_acl_interface() { update_subscription() { local subscription_section; subscription_section="$1" - if [ -n "$subscription_section" ]; then - config_load mihomo - local profile subscription_name subscription_url subscription_user_agent - config_get profile "config" "profile" - config_get subscription_name "$subscription_section" "name" - config_get subscription_url "$subscription_section" "url" - config_get subscription_user_agent "$subscription_section" "user_agent" - local subscription_file; subscription_file="$SUBSCRIPTIONS_DIR/$subscription_section.yaml" - curl -s -f --connect-timeout 15 --retry 3 -o "$subscription_file" -L -H "User-Agent: $subscription_user_agent" "$subscription_url" + if [ -z "$subscription_section" ]; then + return + fi + config_load mihomo + local subscription_name subscription_url subscription_user_agent + config_get subscription_name "$subscription_section" "name" + config_get subscription_url "$subscription_section" "url" + config_get subscription_user_agent "$subscription_section" "user_agent" + log "Update Subscription: $subscription_name." + local subscription_header_tmpfile; subscription_header_tmpfile="/tmp/$subscription_section.header" + local subscription_tmpfile; subscription_tmpfile="/tmp/$subscription_section.yaml" + local subscription_file; subscription_file="$SUBSCRIPTIONS_DIR/$subscription_section.yaml" + if (curl -s -f --connect-timeout 15 --retry 3 -L -X GET -H "User-Agent: $subscription_user_agent" -D "$subscription_header_tmpfile" -o "$subscription_tmpfile" "$subscription_url"); then + log "Subscription update succeed." + local subscription_expire subscription_upload subscription_download subscription_total subscription_used subscription_avaliable + subscription_expire=$(grep "subscription-userinfo: " "$subscription_header_tmpfile" | grep -o -E "expire=[[:digit:]]+" | cut -d '=' -f 2) + subscription_upload=$(grep "subscription-userinfo: " "$subscription_header_tmpfile" | grep -o -E "upload=[[:digit:]]+" | cut -d '=' -f 2) + subscription_download=$(grep "subscription-userinfo: " "$subscription_header_tmpfile" | grep -o -E "download=[[:digit:]]+" | cut -d '=' -f 2) + subscription_total=$(grep "subscription-userinfo: " "$subscription_header_tmpfile" | grep -o -E "total=[[:digit:]]+" | cut -d '=' -f 2) + if [[ -n "$subscription_upload" && -n "$subscription_download" ]]; then + subscription_used=$((subscription_upload + subscription_download)) + if [ -n "$subscription_total" ]; then + subscription_avaliable=$((subscription_total - subscription_upload - subscription_download)) + fi + fi + if [ -n "$subscription_expire" ]; then + uci_set "mihomo" "$subscription_section" "expire" "$(date +%Y-%m-%d -d @$subscription_expire)" + fi + if [ -n "$subscription_upload" ]; then + uci_set "mihomo" "$subscription_section" "upload" "$(format_filesize $subscription_upload)" + fi + if [ -n "$subscription_download" ]; then + uci_set "mihomo" "$subscription_section" "download" "$(format_filesize $subscription_download)" + fi + if [ -n "$subscription_total" ]; then + uci_set "mihomo" "$subscription_section" "total" "$(format_filesize $subscription_total)" + fi + if [ -n "$subscription_used" ]; then + uci_set "mihomo" "$subscription_section" "used" "$(format_filesize $subscription_used)" + fi + if [ -n "$subscription_avaliable" ]; then + uci_set "mihomo" "$subscription_section" "avaliable" "$(format_filesize $subscription_avaliable)" + fi + uci_commit "mihomo" + rm -f "$subscription_header_tmpfile" + mv -f "$subscription_tmpfile" "$subscription_file" + else + log "Subscription update failed." + rm -f "$subscription_header_tmpfile" + rm -f "$subscription_tmpfile" fi } diff --git a/mihomo/files/scripts/tun.sh b/mihomo/files/scripts/firewall_include.sh similarity index 92% rename from mihomo/files/scripts/tun.sh rename to mihomo/files/scripts/firewall_include.sh index 9e99b03b8..9c8ae9605 100644 --- a/mihomo/files/scripts/tun.sh +++ b/mihomo/files/scripts/firewall_include.sh @@ -1,7 +1,7 @@ #!/bin/sh . "$IPKG_INSTROOT/lib/functions.sh" -. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh" +. "$IPKG_INSTROOT/etc/mihomo/scripts/include.sh" config_load mihomo config_get enabled "config" "enabled" 0 diff --git a/mihomo/files/scripts/constants.sh b/mihomo/files/scripts/include.sh similarity index 53% rename from mihomo/files/scripts/constants.sh rename to mihomo/files/scripts/include.sh index 6788428f5..3b12c7bc6 100644 --- a/mihomo/files/scripts/constants.sh +++ b/mihomo/files/scripts/include.sh @@ -29,7 +29,8 @@ CORE_LOG_PATH="$LOG_DIR/core.log" # scripts SH_DIR="$HOME_DIR/scripts" -TUN_SH="$SH_DIR/tun.sh" +INCLUDE_SH="$SH_DIR/include.sh" +FIREWALL_INCLUDE_SH="$SH_DIR/firewall_include.sh" # nftables NFT_DIR="$HOME_DIR/nftables" @@ -38,3 +39,28 @@ RESERVED_IP_NFT="$NFT_DIR/reserved_ip.nft" RESERVED_IP6_NFT="$NFT_DIR/reserved_ip6.nft" GEOIP_CN_NFT="$NFT_DIR/geoip_cn.nft" GEOIP6_CN_NFT="$NFT_DIR/geoip6_cn.nft" + +# functions +format_filesize() { + local kb; kb=1024 + local mb; mb=$((kb * 1024)) + local gb; gb=$((mb * 1024)) + local tb; tb=$((gb * 1024)) + local pb; pb=$((tb * 1024)) + local size; size="$1" + if [ -z "$size" ]; then + echo "" + elif [ "$size" -lt "$kb" ]; then + echo "$size B" + elif [ "$size" -lt "$mb" ]; then + echo "$(awk "BEGIN {print $size / $kb}") KB" + elif [ "$size" -lt "$gb" ]; then + echo "$(awk "BEGIN {print $size / $mb}") MB" + elif [ "$size" -lt "$tb" ]; then + echo "$(awk "BEGIN {print $size / $gb}") GB" + elif [ "$size" -lt "$pb" ]; then + echo "$(awk "BEGIN {print $size / $tb}") TB" + else + echo "$(awk "BEGIN {print $size / $pb}") PB" + fi +} diff --git a/mihomo/files/uci-defaults/firewall.sh b/mihomo/files/uci-defaults/firewall.sh index 3a0cb09b2..f8443ec6a 100644 --- a/mihomo/files/uci-defaults/firewall.sh +++ b/mihomo/files/uci-defaults/firewall.sh @@ -1,12 +1,12 @@ #!/bin/sh -. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh" +. "$IPKG_INSTROOT/etc/mihomo/scripts/include.sh" uci -q batch <<-EOF > /dev/null del firewall.mihomo set firewall.mihomo=include set firewall.mihomo.type=script - set firewall.mihomo.path=$TUN_SH + set firewall.mihomo.path=$FIREWALL_INCLUDE_SH set firewall.mihomo.fw4_compatible=1 commit firewall EOF diff --git a/mihomo/files/uci-defaults/init.sh b/mihomo/files/uci-defaults/init.sh index 615b5a150..3c2912625 100644 --- a/mihomo/files/uci-defaults/init.sh +++ b/mihomo/files/uci-defaults/init.sh @@ -1,6 +1,6 @@ #!/bin/sh -. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh" +. "$IPKG_INSTROOT/etc/mihomo/scripts/include.sh" # check mihomo.config.init init=$(uci -q get mihomo.config.init); [ -z "$init" ] && return diff --git a/mihomo/files/uci-defaults/migrate.sh b/mihomo/files/uci-defaults/migrate.sh index f2b3912d4..ddfb78335 100644 --- a/mihomo/files/uci-defaults/migrate.sh +++ b/mihomo/files/uci-defaults/migrate.sh @@ -1,6 +1,6 @@ #!/bin/sh -. "$IPKG_INSTROOT/etc/mihomo/scripts/constants.sh" +. "$IPKG_INSTROOT/etc/mihomo/scripts/include.sh" # since v1.8.4