From 1e2e4c3848b6d0b46ef65cbc63b9df80c1b982d9 Mon Sep 17 00:00:00 2001 From: CN_SZTL Date: Sat, 19 Sep 2020 00:43:14 +0800 Subject: [PATCH] OpenClash: sync with upstream source --- .../files/etc/init.d/openclash | 386 +++++++----------- .../files/etc/openclash/default.yaml | 127 ++++-- .../usr/share/openclash/openclash_chnroute.sh | 13 +- .../usr/share/openclash/openclash_debug.sh | 34 +- .../usr/share/openclash/openclash_ipdb.sh | 11 +- .../usr/share/openclash/openclash_rule.sh | 41 +- .../files/usr/share/openclash/yml_change.sh | 17 - .../usr/share/openclash/yml_field_cut.sh | 104 +++++ .../usr/share/openclash/yml_groups_get.sh | 35 +- .../share/openclash/yml_groups_name_get.sh | 42 +- .../usr/share/openclash/yml_proxys_get.sh | 37 +- 11 files changed, 376 insertions(+), 471 deletions(-) create mode 100644 package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_field_cut.sh diff --git a/package/ctcgfw/luci-app-openclash/files/etc/init.d/openclash b/package/ctcgfw/luci-app-openclash/files/etc/init.d/openclash index e8501e1be7..d6fedc4dfe 100755 --- a/package/ctcgfw/luci-app-openclash/files/etc/init.d/openclash +++ b/package/ctcgfw/luci-app-openclash/files/etc/init.d/openclash @@ -26,6 +26,9 @@ PROXY_FILE="/tmp/yaml_proxy.yaml" PROXY_PROVIDER_FILE="/tmp/yaml_proxy_provider.yaml" GROUP_FILE="/tmp/yaml_group.yaml" GAME_RULE_FILE="/tmp/yaml_game_rule_group.yaml" +CFW_LATE_FILE="/tmp/yaml_cfw_late_file.yaml" +CFW_PASS_FILE="/tmp/yaml_cfw_pass_file.yaml" +FALLBACK_FILTER_FILE="/tmp/yaml_fallback_filter_file.yaml" HOSTS_FILE="/etc/openclash/custom/openclash_custom_hosts.list" PROXY_FWMARK="0x162" PROXY_ROUTE_TABLE="0x162" @@ -116,44 +119,6 @@ kill_clash() sleep 1 } -field_cut() -{ - local i lines end_len - proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null |sed -n 1p) - provider_len=$(sed -n '/^proxy-providers:/=' "$3" 2>/dev/null) - group_len=$(sed -n '/^proxy-groups:/=' "$3" 2>/dev/null) - rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) - rule_provider_len=$(sed -n '/^rule-providers:/=' "$3" 2>/dev/null) - script_len=$(sed -n '/^script:/=' "$3" 2>/dev/null) - lines="$proxy_len $provider_len $group_len $rule_len $rule_provider_len $script_len" - - for i in $lines; do - if [ -z "$1" ]; then - break - fi - - if [ "$1" -ge "$i" ]; then - continue - fi - - if [ "$end_len" -gt "$i" ] || [ -z "$end_len" ]; then - end_len="$i" - fi - done 2>/dev/null - - if [ -n "$1" ] && [ -z "$end_len" ]; then - end_len=$(sed -n '$=' "$3") - elif [ -n "$end_len" ]; then - end_len=$(expr "$end_len" - 1) - fi - - sed -n "${1},${end_len}p" "$3" > "$2" 2>/dev/null - - if [ -z "$4" ]; then - sed -i "${1},${end_len}d" "$3" 2>/dev/null - fi -} - yml_merge() { #合并文件 @@ -173,14 +138,10 @@ yml_check() sed -i "s/- \{2,\}/- /g" "$3" 2>/dev/null sed -i "s/^\.\.\.$//g" "$3" 2>/dev/null sed -i "s/^---$//g" "$3" 2>/dev/null + sed -i "s/^ \{1,\}dns:/dns:/g" "$3" 2>/dev/null #检查关键字避免后续操作出错 /usr/share/openclash/yml_field_name_ch.sh "$3" 2>/dev/null - - #dns - [ -z "$(grep "^dns:" "$3")" ] && { - sed -i "s/^ \{1,\}dns:/dns:/g" "$3" 2>/dev/null - } #创建启动备份 cp "$3" "$5" @@ -190,164 +151,157 @@ yml_check() cp "$3" "$4" fi - #位置检查 - proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null |sed -n 1p) + #取出general部分 + /usr/share/openclash/yml_field_cut.sh "general" "$CHANGE_FILE" "$3" + + #取出dns部分 + nameserver_len=$(sed -n '/^ \{0,\}nameserver:/=' "$3" 2>/dev/null) + if [ -n "$nameserver_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$nameserver_len" "$DNS_FILE" "$3" + fi 2>/dev/null + + #取出proxy部分 + proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null) + if [ -n "$proxy_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$proxy_len" "$PROXY_FILE" "$3" + fi 2>/dev/null + + #取出proxy-providers部分 provider_len=$(sed -n '/^proxy-providers:/=' "$3" 2>/dev/null) + if [ -n "$provider_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$provider_len" "$PROXY_PROVIDER_FILE" "$3" + fi 2>/dev/null + + #取出group部分 group_len=$(sed -n '/^proxy-groups:/=' "$3" 2>/dev/null) - dns_len=$(sed -n '/^dns:/=' "$3" 2>/dev/null) - rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) - - if [ "$dns_len" -ge "$proxy_len" ] || [ "$dns_len" -ge "$rule_len" ] || [ "$dns_len" -ge "$group_len" ]; then - kill_clash - stop - echo "错误: 不支持的配置文件, General 设置部分应位于开头,请根据模板修改后重试!" > $START_LOG - echo "${LOGTIME} Unsupported Config, Please Check The General Setting's location And Try-again!" >> $LOG_FILE - mv "$START_BACKUP" "$CONFIG_FILE" - exit 0 - fi 2>/dev/null - - if [ "$proxy_len" -ge "$rule_len" ]; then - kill_clash - stop - echo "错误: 不支持的配置文件,服务器节点设置部分应位于规则之前,请根据模板修改后重试!" > $START_LOG - echo "${LOGTIME} Unsupported Config, Please Check The Proxy Setting's location And Try-again!" >> $LOG_FILE - mv "$START_BACKUP" "$CONFIG_FILE" - exit 0 + if [ -n "$group_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$group_len" "$GROUP_FILE" "$3" fi 2>/dev/null - if [ "$group_len" -ge "$rule_len" ] || [ "$proxy_len" -ge "$group_len" ]; then - kill_clash - stop - echo "错误: 不支持的配置文件,策略组设置部分应位于规则之前、节点之后,请根据模板修改后重试!" > $START_LOG - echo "${LOGTIME} Unsupported Config, Please Check The Group Setting's location And Try-again!" >> $LOG_FILE - mv "$START_BACKUP" "$CONFIG_FILE" - exit 0 - fi 2>/dev/null - - #处理proxy_provider位置 - if [ "$provider_len" -ge "$proxy_len" ] || [ "$provider_len" -ge "$group_len" ]; then - field_cut "$provider_len" "$PROXY_PROVIDER_FILE" "$3" - if [ -n "$proxy_len" ]; then - sed -i '/^Proxy:/i\proxy-provider-tag' "$3" 2>/dev/null - sed -i '/proxy-provider-tag/r/tmp/yaml_proxy_provider.yaml' "$3" 2>/dev/null - sed -i '/proxy-provider-tag/d' "$3" 2>/dev/null - elif [ -n "$group_len" ]; then - sed -i '/^proxy-groups:/i\proxy-provider-tag' "$3" 2>/dev/null - sed -i '/proxy-provider-tag/r/tmp/yaml_proxy_provider.yaml' "$3" 2>/dev/null - sed -i '/proxy-provider-tag/d' "$3" 2>/dev/null - fi - fi 2>/dev/null - - #处理rule_provider位置 + #取出rule_provider部分 rule_provider_len=$(sed -n '/^rule-providers:/=' "$3" 2>/dev/null) if [ -n "$rule_provider_len" ]; then - field_cut "$rule_provider_len" "$RULE_PROVIDER_FILE" "$3" + /usr/share/openclash/yml_field_cut.sh "$rule_provider_len" "$RULE_PROVIDER_FILE" "$3" fi 2>/dev/null - #处理备份rule_provider位置 - rule_provider_bak_len=$(sed -n '/^rule-providers:/=' "$4" 2>/dev/null) - if [ -n "$rule_provider_bak_len" ]; then - field_cut "$rule_provider_bak_len" "$RULE_PROVIDER_BAK_FILE" "$4" "backups" - fi 2>/dev/null - - #处理script位置 + #取出script部分 script_len=$(sed -n '/^script:/=' "$3" 2>/dev/null) if [ -n "$script_len" ]; then - field_cut "$script_len" "$SCRIPT_FILE" "$3" + /usr/share/openclash/yml_field_cut.sh "$script_len" "$SCRIPT_FILE" "$3" + fi 2>/dev/null + + #取出rule部分 + rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) + if [ -n "$rule_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$rule_len" "$RULE_FILE" "$3" fi 2>/dev/null - #处理备份script位置 - script_bak_len=$(sed -n '/^script:/=' "$4" 2>/dev/null) - if [ -n "$script_bak_len" ]; then - field_cut "$script_bak_len" "$SCRIPT_BAK_FILE" "$4" "backups" + #取出cfw部分 + cfw_bypass_len=$(sed -n '/^ \{0,\}cfw-bypass:/=' "$DNS_FILE" 2>/dev/null) + if [ -n "$cfw_bypass_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$cfw_bypass_len" "$CFW_LATE_FILE" "$DNS_FILE" "dns" fi 2>/dev/null - #处理备份rule位置 - rule_bak_len=$(sed -n '/^rules:/=' "$4" 2>/dev/null) - if [ -n "$rule_bak_len" ]; then - field_cut "$rule_bak_len" "$RULE_BAK_FILE" "$4" "backups" + #取出cfw部分 + cfw_latency_timeout_len=$(sed -n '/^ \{0,\}cfw-latency-timeout:/=' "$DNS_FILE" 2>/dev/null) + if [ -n "$cfw_latency_timeout_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$cfw_latency_timeout_len" "$CFW_LATE_FILE" "$DNS_FILE" "dns" + fi 2>/dev/null + + #取出fallback-filter部分 + fallback_filter_len=$(sed -n '/^ \{0,\}fallback-filter:/=' "$DNS_FILE" 2>/dev/null) + if [ -n "$fallback_filter_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$fallback_filter_len" "$FALLBACK_FILTER_FILE" "$DNS_FILE" "dns" fi 2>/dev/null - if [ -n "$provider_len" ]; then - proxy_provider_mode=1 - fi + #备份rule_provider部分 + rule_provider_bak_len=$(sed -n '/^rule-providers:/=' "$4" 2>/dev/null) + if [ -n "$rule_provider_bak_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$rule_provider_bak_len" "$RULE_PROVIDER_BAK_FILE" "$4" + fi 2>/dev/null + + #备份script部分 + script_bak_len=$(sed -n '/^script:/=' "$4" 2>/dev/null) + if [ -n "$script_bak_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$script_bak_len" "$SCRIPT_BAK_FILE" "$4" + fi 2>/dev/null + + #备份rule部分 + rule_bak_len=$(sed -n '/^rules:/=' "$4" 2>/dev/null) + if [ -n "$rule_bak_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$rule_bak_len" "$RULE_BAK_FILE" "$4" + fi 2>/dev/null #自定义DNS还原 - if [ "$(grep -c '##Custom DNS##' "$3")" -gt 0 ] && [ "$2" = 0 ] && [ -f "$4" ]; then - if [ "$proxy_provider_mode" -eq 1 ]; then - awk '/^ {0,}nameserver:/,/proxy-providers:/{print}' "$4" | sed '/^proxy-providers:/d' >/tmp/backdns.config 2>/dev/null - else - awk '/^ {0,}nameserver:/,/Proxy:/{print}' "$4" | sed '/^Proxy:/d' >/tmp/backdns.config 2>/dev/null + if [ "$(grep -c '##Custom DNS##' "$CHANGE_FILE")" -gt 0 ] || [ "$(grep -c '##Custom DNS##' "$DNS_FILE")" -gt 0 ]; then + if [ "$2" = 0 ] && [ -f "$4" ]; then + #取出dns备份 + nameserver_len=$(sed -n '/^ \{0,\}nameserver:/=' "$4" 2>/dev/null) + if [ -n "$nameserver_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$nameserver_len" "/tmp/backdns.config" "$4" "dns" + fi 2>/dev/null + sed -i '/^ \{0,\}nameserver:/,$d' "$DNS_FILE" 2>/dev/null + sed -i '/##Custom DNS##/d' "$CHANGE_FILE" 2>/dev/null + cat "/tmp/backdns.config" >> "$DNS_FILE" + rm -rf /tmp/backdns.config 2>/dev/null fi - sed -i '/OpenClash-General-Settings/i\Custom DNS End' "$3" 2>/dev/null - sed -i '/^ \{0,\}nameserver:/,/^Custom DNS End$/d' "$3" 2>/dev/null - sed -i '/##Custom DNS##/r/tmp/bakdns.config' "$3" 2>/dev/null - rm -rf /tmp/backdns.config 2>/dev/null fi 2>/dev/null yml_dns_check - if [ -n "$(grep "^ \{1,\}port:" "$3")" ] || [ -n "$(grep "^ \{1,\}mode:" "$3")" ] || [ -n "$(grep "^ \{1,\}log-level:" "$3")" ]; then - cp "$3" /tmp/config.check 2>/dev/null + if [ -n "$(grep "^ \{1,\}port:" "$CHANGE_FILE")" ] || [ -n "$(grep "^ \{1,\}mode:" "$CHANGE_FILE")" ] || [ -n "$(grep "^ \{1,\}log-level:" "$CHANGE_FILE")" ]; then + cp "$CHANGE_FILE" /tmp/config.check 2>/dev/null sed -i '/^dns:/,$d' /tmp/config.check 2>/dev/null sed -i 's/^[ \t]*//' /tmp/config.check 2>/dev/null - sed -n '/^dns:/,$p' "$3" >> /tmp/config.check 2>/dev/null - mv /tmp/config.check "$3" 2>/dev/null + sed -n '/^dns:/,$p' "$CHANGE_FILE" >> /tmp/config.check 2>/dev/null + mv /tmp/config.check "$CHANGE_FILE" 2>/dev/null fi #添加标识 - sed -i '/OpenClash-General/d' "$3" 2>/dev/null - if [ "$proxy_provider_mode" -eq 1 ]; then - sed -i '/^proxy-providers:/i\#===================== OpenClash-General-Settings =====================#' "$3" 2>/dev/null - else - sed -i '/^Proxy:/i\#===================== OpenClash-General-Settings =====================#' "$3" 2>/dev/null - fi 2>/dev/null + sed -i '/OpenClash-General/d' "$DNS_FILE" 2>/dev/null + sed -i '/OpenClash-General/d' "$FALLBACK_FILTER_FILE" 2>/dev/null + echo '#===================== OpenClash-General-Settings =====================#' >> "$FALLBACK_FILTER_FILE" 2>/dev/null + } #检查DNS设置 yml_dns_check() { - if [ -z "$(grep '^dns:' "$CONFIG_FILE")" ]; then - if [ "$proxy_provider_mode" -eq 1 ]; then - sed -i '/^proxy-providers:/i\dns:' "$CONFIG_FILE" 2>/dev/null - else - sed -i '/^Proxy:/i\dns:' "$CONFIG_FILE" 2>/dev/null - fi 2>/dev/null + if [ -z "$(grep '^dns:' "$CHANGE_FILE")" ]; then + echo "dns:" >> "$CHANGE_FILE" 2>/dev/null fi - if [ -z "$(grep '^ \{0,\}nameserver:' "$CONFIG_FILE")" ]; then - if [ "$proxy_provider_mode" -eq 1 ]; then - sed -i '/^proxy-providers:/i\ nameserver:' "$CONFIG_FILE" 2>/dev/null - else - sed -i '/^Proxy:/i\ nameserver:' "$CONFIG_FILE" 2>/dev/null - fi 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - tls://8.8.8.8:853" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - https://1.1.1.1/dns-query" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - https://dns.google/dns-query" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - https://cloudflare-dns.com/dns-query" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ fallback:" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - https://doh.rixcloud.dev/dns-query" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - 119.29.29.29" "$CONFIG_FILE" 2>/dev/null - sed -i "/^ \{0,\}nameserver:/a\ - 114.114.114.114" "$CONFIG_FILE" 2>/dev/null + if [ -z "$(grep '^ \{0,\}nameserver:' "$DNS_FILE")" ]; then + echo " nameserver:" > "$DNS_FILE" 2>/dev/null + cat >> "$DNS_FILE" <<-EOF + - 114.114.114.114 + - 119.29.29.29 + - https://doh.rixcloud.dev/dns-query + fallback: + - https://cloudflare-dns.com/dns-query + - https://dns.google/dns-query + - https://1.1.1.1/dns-query + - tls://8.8.8.8:853 +EOF fi - dns_port=$(grep "^ \{0,\}listen:" "$CONFIG_FILE" |awk -F ':' '{print $3}' |awk -F '#' '{print $1}' |tr -cd "[0-9]" 2>/dev/null) + dns_port=$(grep "^ \{0,\}listen:" "$CHANGE_FILE" |awk -F ':' '{print $3}' |awk -F '#' '{print $1}' |tr -cd "[0-9]" 2>/dev/null) if [ -z "$dns_port" ] || [ "$dns_port" -eq 53 ]; then dns_port=7874 fi - if [ -n "$(grep "^ \{0,\}listen:" "$CONFIG_FILE")" ]; then + if [ -n "$(grep "^ \{0,\}listen:" "$CHANGE_FILE")" ]; then if [ "$ipv6_enable" != "1" ]; then - sed -i "/^ \{0,\}listen:/c\ listen: 127.0.0.1:${dns_port}" "$CONFIG_FILE" 2>/dev/null + sed -i "/^ \{0,\}listen:/c\ listen: 127.0.0.1:${dns_port}" "$CHANGE_FILE" 2>/dev/null else - sed -i "/^ \{0,\}listen:/c\ listen: 0.0.0.0:${dns_port}" "$CONFIG_FILE" 2>/dev/null + sed -i "/^ \{0,\}listen:/c\ listen: 0.0.0.0:${dns_port}" "$CHANGE_FILE" 2>/dev/null fi else if [ "$ipv6_enable" != "1" ]; then - sed -i "/^dns:/a\ listen: 127.0.0.1:${dns_port}" "$CONFIG_FILE" 2>/dev/null + sed -i "/^dns:/a\ listen: 127.0.0.1:${dns_port}" "$CHANGE_FILE" 2>/dev/null else - sed -i "/^dns:/a\ listen: 0.0.0.0:${dns_port}" "$CONFIG_FILE" 2>/dev/null + sed -i "/^dns:/a\ listen: 0.0.0.0:${dns_port}" "$CHANGE_FILE" 2>/dev/null fi fi 2>/dev/null @@ -452,33 +406,6 @@ fi } -#切割配置文件以分开处理 -yml_cut() -{ - cp "$4" "$1" - sed -i '/^rules:/,$d' "$1" 2>/dev/null - sed -n '/^ \{0,\}nameserver:/,$p' "$1" >"$3" 2>/dev/null - if [ "$proxy_provider_mode" -eq 1 ]; then - sed -n '/^ \{0,\}proxy-groups:/,$p' "$3" >"$7" 2>/dev/null - sed -n '/^ \{0,\}Proxy:/,$p' "$3" >"$5" 2>/dev/null - sed -n '/^ \{0,\}proxy-providers:/,$p' "$3" >"$6" 2>/dev/null - sed -i '/^ \{0,\}proxy-groups:/,$d' "$5" 2>/dev/null - sed -i '/^ \{0,\}Proxy:/,$d' "$6" 2>/dev/null - sed -i '/^ \{0,\}proxy-groups:/,$d' "$6" 2>/dev/null - else - sed -n '/^ \{0,\}proxy-groups:/,$p' "$3" >"$7" 2>/dev/null - sed -n '/^ \{0,\}Proxy:/,$p' "$3" >"$5" 2>/dev/null - sed -i '/^ \{0,\}proxy-groups:/,$d' "$5" 2>/dev/null - fi 2>/dev/null - sed -i '/^ \{0,\}nameserver:/,$d' "$1" 2>/dev/null - if [ "$proxy_provider_mode" -eq 1 ]; then - sed -i '/^ \{0,\}proxy-providers:/,$d' "$3" 2>/dev/null - else - sed -i '/^ \{0,\}Proxy:/,$d' "$3" 2>/dev/null - fi 2>/dev/null - sed -n '/^rules:/,$p' "$4" >"$2" 2>/dev/null -} - #获取自定义DNS设置 yml_dns_get() { @@ -519,47 +446,39 @@ yml_dns_get() if [ -n "$group" ]; then if [ "$group" = "nameserver" ]; then - echo " $dns_type$dns_address" >>/etc/openclash/config.namedns + echo " $dns_type$dns_address" >>/tmp/config.namedns else - if [ -z "$(grep "fallback:$" /etc/openclash/config.falldns 2>/dev/null)" ]; then - echo " fallback:" >/etc/openclash/config.falldns + if [ -z "$(grep "fallback:$" /tmp/config.falldns 2>/dev/null)" ]; then + echo " fallback:" >/tmp/config.falldns fi - echo " $dns_type$dns_address" >>/etc/openclash/config.falldns + echo " $dns_type$dns_address" >>/tmp/config.falldns fi else return fi - } #添加自定义DNS设置 yml_dns_custom() { if [ "$1" = 1 ]; then - echo " nameserver:" >/etc/openclash/config.namedns + echo " nameserver:" >/tmp/config.namedns + echo "##Custom DNS##" >> /tmp/config.namedns 2>/dev/null config_load "openclash" config_foreach yml_dns_get "dns_servers" - sed -i '/^ \{0,\}nameserver:/i\##Custom DNS##' "$2" 2>/dev/null - sed -i '/OpenClash-General-Settings/i\Custom DNS End' "$2" 2>/dev/null - sed -i '/^ \{0,\}nameserver:/,/^Custom DNS End$/d' "$2" 2>/dev/null - sed -i '/##Custom DNS##/r/etc/openclash/config.falldns' "$2" 2>/dev/null - sed -i '/##Custom DNS##/r/etc/openclash/config.namedns' "$2" 2>/dev/null - rm -rf /etc/openclash/config.namedns 2>/dev/null - rm -rf /etc/openclash/config.falldns 2>/dev/null - else + sed -i '/^ \{0,\}nameserver:/,$d' "$2" 2>/dev/null + cat "/tmp/config.namedns" >> "$2" 2>/dev/null + cat "/tmp/config.falldns" >> "$2" 2>/dev/null + rm -rf /tmp/config.namedns 2>/dev/null + rm -rf /tmp/config.falldns 2>/dev/null + elif [ -z "$(grep "##Custom DNS##" "$2" 2>/dev/null)" ]; then sed -i "/^ \{0,\}nameserver:/c\ nameserver:" "$2" 2>/dev/null sed -i "/^ \{0,\}fallback:/c\ fallback:" "$2" 2>/dev/null fi #fallback-filter - if [ -n "$(grep "^ \{0,\}fallback:" "$2")" ]; then - if [ -z "$(grep "^ \{0,\}fallback-filter:" "$2")" ]; then - awk '/^ {0,}fallback:/,/OpenClash-General-Settings/{print}' "$2" |sed '1d' |sed '$d' >/tmp/fallback.cache 2>/dev/null - sed -i '/cfw-bypass:/,$d' /tmp/fallback.cache 2>/dev/null - sed -i '/cfw-latency-timeout:/,$d' /tmp/fallback.cache 2>/dev/null - sed -i '/fallback:/,$d' "$2" 2>/dev/null -cat >> "$2" <<-EOF - fallback: + if [ -n "$(grep "^ \{0,\}fallback:" "$2")" ] && [ -z "$(grep "^ \{0,\}fallback-filter:" "$3")" ]; then +cat >> "$3" <<-EOF fallback-filter: geoip: true ipcidr: @@ -580,38 +499,24 @@ cat >> "$2" <<-EOF - 240.0.0.0/4 - 255.255.255.255/32 EOF - sed -i '/ fallback:/r/tmp/fallback.cache' "$2" 2>/dev/null - rm -rf /tmp/fallback.cache 2>/dev/null - echo "#===================== OpenClash-General-Settings =====================#" >>"$2" - else - if [ -z "$(grep "^ fallback-filter:" "$2")" ]; then - sed -i "/fallback-filter:/c\ fallback-filter:" "$2" 2>/dev/null - fi - if [ -z "$(grep "^ geoip: true" "$2")" ]; then - sed -i "/geoip: true/c\ geoip: true" "$2" 2>/dev/null - fi - if [ -z "$(grep "^ ipcidr:" "$2")" ]; then - sed -i "/ipcidr:/c\ ipcidr:" "$2" 2>/dev/null - fi + sed -i '/OpenClash-General/d' "$3" 2>/dev/null + echo "#===================== OpenClash-General-Settings =====================#" >>"$3" + elif [ -n "$(grep "^ \{0,\}fallback-filter:" "$3")" ]; then + if [ -z "$(grep "^ fallback-filter:" "$3")" ]; then + sed -i "/fallback-filter:/c\ fallback-filter:" "$2" 2>/dev/null fi - if [ -z "$(grep "^ - " "$2")" ]; then - sed -i '/^ \{0,\}nameserver:/,/^ \{0,\}fallback-filter:/ {s/^ \{0,\}- / - /}' "$2" 2>/dev/null #修改参数空格 + if [ -z "$(grep "^ geoip: true" "$3")" ]; then + sed -i "/geoip: true/c\ geoip: true" "$3" 2>/dev/null fi - if [ -z "$(grep "^ - " "$2")" ]; then - sed -i '/^ \{0,\}ipcidr:/,/OpenClash-General-Settings/ {s/^ \{0,\}- / - /}' "$2" 2>/dev/null #修改参数空格 + if [ -z "$(grep "^ ipcidr:" "$3")" ]; then + sed -i "/ipcidr:/c\ ipcidr:" "$3" 2>/dev/null fi -#删除fallback-filter - else - if [ -n "$(grep "^ \{0,\}fallback-filter:" "$2")" ]; then - sed -i '/fallback-filter:/,$d' "$2" 2>/dev/null - echo "#===================== OpenClash-General-Settings =====================#" >>"$2" - fi - if [ -n "$(grep "^ \{0,1\}- " "$2")" ]; then - #添加参数空格 - sed -i "s/^ \{0,\}- / - /" "$2" 2>/dev/null + if [ -z "$(grep "^ - " "$3")" ]; then + sed -i '/^ \{0,\}ipcidr:/,/OpenClash-General-Settings/ {s/^ \{0,\}- / - /}' "$3" 2>/dev/null #修改参数空格 fi fi + cat "$3" >> "$2" 2>/dev/null } #获取认证信息 @@ -630,7 +535,7 @@ yml_auth_get() if [ -z "$username" ] || [ -z "$password" ]; then return else - echo " - $username:$password" >>/etc/openclash/config.auth + echo " - $username:$password" >>/tmp/config.auth fi } @@ -641,10 +546,10 @@ yml_auth_custom() sed -i '/^dns:/i\#authentication' "$1" 2>/dev/null sed -i '/^authentication:/,/^#authentication/d' "$1" 2>/dev/null fi - if [ -f /etc/openclash/config.auth ]; then + if [ -f /tmp/config.auth ]; then sed -i '/^dns:/i\authentication:' "$1" 2>/dev/null - sed -i '/^authentication:/r/etc/openclash/config.auth' "$1" 2>/dev/null - mv /etc/openclash/config.auth /tmp/openclash.auth 2>/dev/null + sed -i '/^authentication:/r/tmp/config.auth' "$1" 2>/dev/null + mv /tmp/config.auth /tmp/openclash.auth 2>/dev/null else rm -rf /tmp/openclash.auth 2>/dev/null fi @@ -1493,15 +1398,15 @@ if [ "$enable" -eq 1 ] && [ -f "$CONFIG_FILE" ]; then get_config echo "第二步: 配置文件检查..." >$START_LOG yml_check "$en_mode" "$enable_custom_dns" "$CONFIG_FILE" "$BACKUP_FILE" "$START_BACKUP" - if [ -n "$(grep "^ \{0,\}proxy-groups:" "$CONFIG_FILE")" ]; then - if [ -z "$(grep "^ \{0,\}Proxy:" "$CONFIG_FILE" 2>/dev/null)" ] && [ -z "$(grep "^ \{0,\}proxy-providers:" "$CONFIG_FILE" 2>/dev/null)" ]; then + if [ -f "$GROUP_FILE" ]; then + if [ ! -f "$PROXY_FILE" ] && [ ! -f "$PROXY_PROVIDER_FILE" ]; then kill_clash stop echo "错误: 配置文件完整性检查不通过,已自动还原配置文件,请对照模板格式检查修改配置文件!" >$START_LOG mv "$START_BACKUP" "$CONFIG_FILE" sleep 5 echo "" >$START_LOG - elif [ -z "$(grep "^ \{0,\}rules:" "$CONFIG_FILE" 2>/dev/null)" ] && [ -z "$(grep "^ \{0,\}script:" "$SCRIPT_FILE" 2>/dev/null)" ]; then + elif [ ! -f "$RULE_FILE" ] && [ ! -f "$SCRIPT_FILE" ]; then kill_clash stop echo "错误: 配置文件完整性检查不通过,已自动还原配置文件,请对照模板格式检查修改配置文件!" >$START_LOG @@ -1512,9 +1417,8 @@ if [ "$enable" -eq 1 ] && [ -f "$CONFIG_FILE" ]; then echo "第三步: 修改配置文件..." >$START_LOG config_load "openclash" config_foreach yml_auth_get "authentication" - yml_auth_custom "$CONFIG_FILE" - yml_cut "$CHANGE_FILE" "$RULE_FILE" "$DNS_FILE" "$CONFIG_FILE" "$PROXY_FILE" "$PROXY_PROVIDER_FILE" "$GROUP_FILE" - yml_dns_custom "$enable_custom_dns" "$DNS_FILE" + yml_auth_custom "$CHANGE_FILE" + yml_dns_custom "$enable_custom_dns" "$DNS_FILE" "$FALLBACK_FILTER_FILE" /usr/share/openclash/yml_change.sh >/dev/null 2>&1 "$LOGTIME" "$en_mode" "$enable_custom_dns" "$da_password" "$cn_port" "$proxy_port" "$CHANGE_FILE" "$ipv6_enable" "$http_port" "$socks_port" "$lan_ip" "$log_level" "$proxy_mode" "$intranet_allowed" "$en_mode_tun" "$stack_type" & /usr/share/openclash/yml_rules_change.sh >/dev/null 2>&1 "$LOGTIME" "$rule_source" "$enable_custom_clash_rules" "$RULE_FILE" "$set_rule_file" "$en_mode" "$enable_rule_proxy" "$BACKUP_FILE" "$RULE_PROVIDER_FILE" & wait @@ -1591,17 +1495,17 @@ if [ "$enable" -eq 1 ] && [ -f "$CONFIG_FILE" ]; then #获取备份rule_provider rule_provider_bak_len=$(sed -n '/^rule-providers:/=' "$START_BACKUP" 2>/dev/null) if [ -n "$rule_provider_bak_len" ]; then - field_cut "$rule_provider_bak_len" "$RULE_PROVIDER_BAK_FILE" "$START_BACKUP" "backups" + /usr/share/openclash/yml_field_cut.sh "$rule_provider_bak_len" "$RULE_PROVIDER_BAK_FILE" "$START_BACKUP" fi 2>/dev/null #获取备份script script_bak_len=$(sed -n '/^script:/=' "$START_BACKUP" 2>/dev/null) if [ -n "$script_bak_len" ]; then - field_cut "$script_bak_len" "$SCRIPT_BAK_FILE" "$START_BACKUP" "backups" + /usr/share/openclash/yml_field_cut.sh "$script_bak_len" "$SCRIPT_BAK_FILE" "$START_BACKUP" fi 2>/dev/null #获取备份rule rule_bak_len=$(sed -n '/^rules:/=' "$START_BACKUP" 2>/dev/null) if [ -n "$rule_bak_len" ]; then - field_cut "$rule_bak_len" "$RULE_BAK_FILE" "$START_BACKUP" "backups" + /usr/share/openclash/yml_field_cut.sh "$rule_bak_len" "$RULE_BAK_FILE" "$START_BACKUP" fi 2>/dev/null yml_rule_bak_merge nohup "$CLASH" -d "$CLASH_CONFIG" -f "$CONFIG_FILE" >> $LOG_FILE 2>&1 & diff --git a/package/ctcgfw/luci-app-openclash/files/etc/openclash/default.yaml b/package/ctcgfw/luci-app-openclash/files/etc/openclash/default.yaml index 6b74e19682..cea74eb28e 100644 --- a/package/ctcgfw/luci-app-openclash/files/etc/openclash/default.yaml +++ b/package/ctcgfw/luci-app-openclash/files/etc/openclash/default.yaml @@ -1,63 +1,120 @@ # Copyright (c) 2018 Dreamacro # 接管:不支持在此页面直接修改,请到全局设置页面进行修改 -# port of HTTP -port: 7890 # 此项将被接管 +# Port of HTTP(S) proxy server on the local end +# 此项将被接管 +port: 7890 -# port of SOCKS5 -socks-port: 7891 # 此项将被接管 +# Port of SOCKS5 proxy server on the local end +# 此项将被接管 +socks-port: 7891 -# redir port for Linux and macOS -redir-port: 7892 # 此项将被接管 +# Transparent proxy server port for Linux and macOS +# 此项将被接管 +redir-port: 7892 + +# HTTP(S) and SOCKS5 server on the same port +mixed-port: 7890 -allow-lan: true # 此项将被接管为true +# Set to true to allow connections to the local-end server from +# other LAN IP addresses +# 此项将被接管为true +allow-lan: true -# Only applicable when setting allow-lan to true -# "*": bind all IP addresses +# This is only applicable when `allow-lan` is `true` +# '*': bind all IP addresses # 192.168.122.11: bind a single IPv4 address # "[aaaa::a8aa:ff:fe09:57d8]": bind a single IPv6 address -bind-address: "*" # HTTP(S)\SOCKS5 监听地址,此项将被接管为all +# HTTP(S)\SOCKS5 监听地址,此项将被接管为all +bind-address: "*" -# rule / global / direct (default is rule) -mode: rule # 此项将被接管 +# Clash router working mode +# rule: rule-based packet routing +# global: all packets will be forwarded to a single endpoint +# direct: directly forward the packets to the Internet +# 此项将被接管 +mode: rule -# set log level to stdout (default is info) +# Clash by default prints logs to STDOUT # info / warning / error / debug / silent -log-level: info # 此项将被接管 +# 此项将被接管 +log-level: info -# A RESTful API for clash -external-controller: 0.0.0.0:9090 # 此项将被接管 +# When set to false, resolver won't translate hostnames to IPv6 addresses +# 此项将被接管 +ipv6: false -# you can put the static web resource (such as clash-dashboard) to a directory, and clash would serve in `${API}/ui` -# input is a relative path to the configuration directory or an absolute path -external-ui: "/usr/share/openclash/dashboard" # 此项将被接管 +# RESTful web API listening address +# 此项将被接管 +external-controller: 0.0.0.0:9090 -# Secret for RESTful API (Optional) -secret: '123456' # 此项将被接管 +# A relative path to the configuration directory or an absolute path to a +# directory in which you put some static web resource. Clash core will then +# serve it at `http://{{external-controller}}/ui`. +# 此项将被接管 +external-ui: "/usr/share/openclash/dashboard" + +# Secret for the RESTful API (optional) +# Authenticate by spedifying HTTP header `Authorization: Bearer ${secret}` +# ALWAYS set a secret if RESTful API is listening on 0.0.0.0 +# 此项将被接管 +secret: '123456' # authentication of local SOCKS5/HTTP(S) server -authentication: # 此项将被接管 +# 此项将被接管 +authentication: - "user1:pass1" - "user2:pass2" + +# Outbound interface name +interface-name: en0 -# # hosts, support wildcard (e.g. *.clash.dev Even *.foo.*.example.com) -# # static domain has a higher priority than wildcard domain (foo.example.com > *.example.com > .example.com) -# # +.foo.com equal .foo.com and foo.com -# hosts: -# '*.clash.dev': 127.0.0.1 -# '.dev': 127.0.0.1 -# 'alpha.clash.dev': '::1' -# '+.foo.dev': 127.0.0.1 +# tun option +# 此项将被接管 +tun: +# enable: false +# stack: system +# dns-hijack: +# - tcp://8.8.8.8:53 +# - tcp://8.8.4.4:53 -dns: # 如订阅配置无包括此项的所有DNS设置,OpenClash将自动添加 +# Static hosts for DNS server and connection establishment, only works +# when `dns.enhanced-mode` is `redir-host`. +# +# Wildcard hostnames are supported (e.g. *.clash.dev, *.foo.*.example.com) +# Non-wildcard domain names have a higher priority than wildcard domain names +# e.g. foo.example.com > *.example.com > .example.com +# P.S. +.foo.com equals to .foo.com and foo.com +hosts: + # '*.clash.dev': 127.0.0.1 + # '.dev': 127.0.0.1 + # 'alpha.clash.dev': '::1' + +# DNS server settings +# This section is optional. When not present, the DNS server will be disabled. +# 如订阅配置无包括此项的所有DNS设置,OpenClash将自动添加 +dns: enable: true # set true to enable dns (default is false) # 此项将被接管为true ipv6: false # default is false # 此项将被接管 listen: 0.0.0.0:53 # 端口为53时将被接管为7874 + # These nameservers are used to resolve the DNS nameserver hostnames below. + # Specify IP addresses only + default-nameserver: + - 114.114.114.114 + - 8.8.8.8 enhanced-mode: redir-host # or fake-ip # 此项将被接管 - fake-ip-range: 198.18.0.1/16 # if you don't know what it is, don't change it # 此项将被接管 -# fake-ip-filter: # fake ip white domain list -# - '*.lan' -# - localhost.ptlogin2.qq.com + fake-ip-range: 198.18.0.1/16 # Fake IP addresses pool CIDR # 此项将被接管 + use-hosts: true # lookup hosts and return IP record # 此项将被接管 + # Hostnames in this list will not be resolved with fake IPs + # i.e. questions to these domain names will always be answered with their + # real IP addresses + # fake-ip-filter: + # - '*.lan' + # - localhost.ptlogin2.qq.com + + # Supports UDP, TCP, DoT, DoH. You can specify the port to connect to. + # All DNS questions are sent directly to the nameserver, without proxies + # involved. Clash answers the DNS question with the first result gathered. nameserver: - 114.114.114.114 - https://1.1.1.1/dns-query # dns over https diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_chnroute.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_chnroute.sh index f609dfd5ef..c215562c38 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_chnroute.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_chnroute.sh @@ -30,13 +30,12 @@ sleep 5 status=$(unify_ps_prevent) done - [ "$china_ip_route" -eq 1 ] && /etc/init.d/openclash stop - echo "大陆IP白名单有更新,开始替换旧版本..." >$START_LOG\ - && mv /tmp/ChinaIP.yaml /etc/openclash/rule_provider/ChinaIP.yaml >/dev/null 2>&1\ - && echo "删除下载缓存..." >$START_LOG\ - && rm -rf /tmp/ChinaIP.yaml >/dev/null 2>&1\ - && rm -rf /etc/openclash/china_ip_route.ipset >/dev/null 2>&1 - [ "$china_ip_route" -eq 1 ] && /etc/init.d/openclash start + echo "大陆IP白名单有更新,开始替换旧版本..." >$START_LOG + mv /tmp/ChinaIP.yaml /etc/openclash/rule_provider/ChinaIP.yaml >/dev/null 2>&1 + echo "删除下载缓存..." >$START_LOG + rm -rf /tmp/ChinaIP.yaml >/dev/null 2>&1 + rm -rf /etc/openclash/china_ip_route.ipset >/dev/null 2>&1 + [ "$china_ip_route" -eq 1 ] && /etc/init.d/openclash restart echo "大陆IP白名单更新成功!" >$START_LOG echo "${LOGTIME} Chnroute Lists Update Successful" >>$LOG_FILE sleep 10 diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_debug.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_debug.sh index 04af490ccc..5aae05b8e6 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_debug.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_debug.sh @@ -7,6 +7,8 @@ status=$(unify_ps_status "openclash_debug.sh") DEBUG_LOG="/tmp/openclash_debug.log" LOGTIME=$(date "+%Y-%m-%d %H:%M:%S") +CHANGE_FILE="/tmp/yaml_change.yaml" +DNS_FILE="/tmp/yaml_dns.yaml" uci commit openclash enable_custom_dns=$(uci get openclash.config.enable_custom_dns 2>/dev/null) @@ -264,32 +266,16 @@ if [ -n "$(grep OpenClash-General-Settings "$CONFIG_FILE")" ]; then sed '/OpenClash-General-Settings/,$d' "$CONFIG_FILE" >> "$DEBUG_LOG" 2>/dev/null else /usr/share/openclash/yml_field_name_ch.sh "$CONFIG_FILE" 2>/dev/null - proxy_len=$(sed -n '/^Proxy:/=' "$CONFIG_FILE" 2>/dev/null) - provider_len=$(sed -n '/^proxy-providers:/=' "$CONFIG_FILE" 2>/dev/null) - group_len=$(sed -n '/^proxy-groups:/=' "$CONFIG_FILE" 2>/dev/null) - dns_len=$(sed -n '/^dns:/=' "$CONFIG_FILE" 2>/dev/null) - rule_len=$(sed -n '/^rules:/=' "$CONFIG_FILE" 2>/dev/null) - if [ "$dns_len" -ge "$proxy_len" ] || [ "$dns_len" -ge "$rule_len" ] || [ "$dns_len" -ge "$group_len" ]; then - echo "错误: 不支持的配置文件, General 设置部分应位于开头,请根据模板修改后重试!" >> "$DEBUG_LOG" - fi 2>/dev/null - - if [ "$proxy_len" -ge "$rule_len" ] || [ "$provider_len" -ge "$rule_len" ]; then - echo "错误: 不支持的配置文件,服务器节点设置部分应位于规则之前,请根据模板修改后重试!" >> "$DEBUG_LOG" + #取出general部分 + /usr/share/openclash/yml_field_cut.sh "general" "$CHANGE_FILE" "$CONFIG_FILE" + + #取出dns部分 + nameserver_len=$(sed -n '/^ \{0,\}nameserver:/=' "$CONFIG_FILE" 2>/dev/null) + if [ -n "$nameserver_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$nameserver_len" "$DNS_FILE" "$CONFIG_FILE" fi 2>/dev/null - if [ "$group_len" -ge "$rule_len" ] || [ "$proxy_len" -ge "$group_len" ]; then - echo "错误: 不支持的配置文件,策略组设置部分应位于规则之前、节点之后,请根据模板修改后重试!" >> "$DEBUG_LOG" - fi 2>/dev/null - - if [ "$proxy_len" -le "$provider_len" ]; then - sed '/^ \{0,\}Proxy:/,$d' "$CONFIG_FILE" >> "$DEBUG_LOG" 2>/dev/null - elif [ "$proxy_len" -ge "$provider_len" ]; then - sed '/^ \{0,\}proxy-providers:/,$d' "$CONFIG_FILE" >> "$DEBUG_LOG" 2>/dev/null - elif [ -n "$proxy_len" ] && [ -z "$provider_len" ]; then - sed '/^ \{0,\}Proxy:/,$d' "$CONFIG_FILE" >> "$DEBUG_LOG" 2>/dev/null - elif [ -z "$proxy_len" ] && [ -n "$provider_len" ]; then - sed '/^ \{0,\}proxy-providers:/,$d' "$CONFIG_FILE" >> "$DEBUG_LOG" 2>/dev/null - fi 2>/dev/null + cat "$CONFIG_FILE" "$DNS_FILE" >> "$DEBUG_LOG" fi #firewall diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_ipdb.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_ipdb.sh index 8bff190b4a..6a6ae9ab86 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_ipdb.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_ipdb.sh @@ -28,12 +28,11 @@ sleep 5 status=$(unify_ps_prevent) done - /etc/init.d/openclash stop - echo "数据库版本有更新,开始替换数据库版本..." >$START_LOG\ - && mv /tmp/Country.mmdb /etc/openclash/Country.mmdb >/dev/null 2>&1\ - && /etc/init.d/openclash start\ - && echo "删除下载缓存..." >$START_LOG\ - && rm -rf /tmp/Country.mmdb >/dev/null 2>&1 + echo "数据库版本有更新,开始替换数据库版本..." >$START_LOG + mv /tmp/Country.mmdb /etc/openclash/Country.mmdb >/dev/null 2>&1 + /etc/init.d/openclash restart + echo "删除下载缓存..." >$START_LOG + rm -rf /tmp/Country.mmdb >/dev/null 2>&1 echo "GEOIP 数据库更新成功!" >$START_LOG echo "${LOGTIME} GEOIP Database Update Successful" >>$LOG_FILE sleep 10 diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_rule.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_rule.sh index e290688a65..47cead78ce 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_rule.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/openclash_rule.sh @@ -2,41 +2,6 @@ . /usr/share/openclash/openclash_ps.sh . /lib/functions.sh -field_cut() -{ - local i lines end_len - rule_len=$(sed -n '/^ \{0,\}rules:/=' "$3" 2>/dev/null) - rule_provider_len=$(sed -n '/^ \{0,\}rule-providers:/=' "$3" 2>/dev/null) - script_len=$(sed -n '/^ \{0,\}script:/=' "$3" 2>/dev/null) - lines="$rule_len $rule_provider_len $script_len" - - for i in $lines; do - if [ -z "$1" ]; then - break - fi - - if [ "$1" -ge "$i" ]; then - continue - fi - - if [ "$end_len" -gt "$i" ] || [ -z "$end_len" ]; then - end_len="$i" - fi - done 2>/dev/null - - if [ -n "$1" ] && [ -z "$end_len" ]; then - end_len=$(sed -n '$=' "$3") - elif [ -n "$end_len" ]; then - end_len=$(expr "$end_len" - 1) - fi - - sed -n "${1},${end_len}p" "$3" > "$2" 2>/dev/null - - if [ -z "$4" ]; then - sed -i "${1},${end_len}d" "$3" 2>/dev/null - fi -} - status=$(unify_ps_status "openclash_rule.sh") [ "$status" -gt 3 ] && exit 0 @@ -86,17 +51,17 @@ field_cut() #处理rule_provider位置 rule_provider_len=$(sed -n '/^ \{0,\}rule-providers:/=' "/tmp/rules.yaml" 2>/dev/null) if [ -n "$rule_provider_len" ]; then - field_cut "$rule_provider_len" "$OTHER_RULE_PROVIDER_FILE" "/tmp/rules.yaml" + /usr/share/openclash/yml_field_cut.sh "$rule_provider_len" "$OTHER_RULE_PROVIDER_FILE" "/tmp/rules.yaml" fi 2>/dev/null #处理script位置 script_len=$(sed -n '/^ \{0,\}script:/=' "/tmp/rules.yaml" 2>/dev/null) if [ -n "$script_len" ]; then - field_cut "$script_len" "$OTHER_SCRIPT_FILE" "/tmp/rules.yaml" + /usr/share/openclash/yml_field_cut.sh "$script_len" "$OTHER_SCRIPT_FILE" "/tmp/rules.yaml" fi 2>/dev/null #处理备份rule位置 rule_bak_len=$(sed -n '/^ \{0,\}rules:/=' "/tmp/rules.yaml" 2>/dev/null) if [ -n "$rule_bak_len" ]; then - field_cut "$rule_bak_len" "$OTHER_RULE_FILE" "/tmp/rules.yaml" + /usr/share/openclash/yml_field_cut.sh "$rule_bak_len" "$OTHER_RULE_FILE" "/tmp/rules.yaml" fi 2>/dev/null #合并 cat "$OTHER_RULE_PROVIDER_FILE" "$OTHER_SCRIPT_FILE" "$OTHER_RULE_FILE" > "/tmp/rules.yaml" 2>/dev/null diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_change.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_change.sh index 83993fd2d3..c55d52080c 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_change.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_change.sh @@ -116,23 +116,6 @@ fi uci commit openclash - - dns_hijack_len=$(sed -n '/dns-hijack:/=' "$7" 2>/dev/null) - if [ -n "$dns_hijack_len" ]; then - dns_hijack_end_len=$dns_hijack_len - while ( [ -n "$(echo "$hijack_line" |grep "^ \{0,\}-")" ] || [ -n "$(echo "$hijack_line" |grep "^ \{0,\}$")" ] || [ -z "$hijack_line" ] ) - do - dns_hijack_end_len=$(expr "$dns_hijack_end_len" + 1) - hijack_line=$(sed -n "${dns_hijack_end_len}p" "$7") - done 2>/dev/null - dns_hijack_end_len=$(expr "$dns_hijack_end_len" - 1) - sed -i "${dns_hijack_len},${dns_hijack_end_len}d" "$7" 2>/dev/null - fi - sed -i '/^ \{0,\}tun:/,/^ \{0,\}enable:/d' "$7" 2>/dev/null - sed -i '/^ \{0,\}dns-hijack:/d' "$7" 2>/dev/null - sed -i '/^ \{0,\}stack:/d' "$7" 2>/dev/null - sed -i '/^ \{0,\}device-url:/d' "$7" 2>/dev/null - sed -i '/^ \{0,\}dns-listen:/d' "$7" 2>/dev/null if [ -z "$(grep "^ enable: true" "$7")" ]; then if [ ! -z "$(grep "^ \{0,\}enable:" "$7")" ]; then diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_field_cut.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_field_cut.sh new file mode 100644 index 0000000000..49731d30dc --- /dev/null +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_field_cut.sh @@ -0,0 +1,104 @@ +#!/bin/sh +. /lib/functions.sh + +field_cut() +{ + local i lines end_len + if [ "$4" != "dns" ]; then + proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null |sed -n 1p) + provider_len=$(sed -n '/^proxy-providers:/=' "$3" 2>/dev/null) + group_len=$(sed -n '/^proxy-groups:/=' "$3" 2>/dev/null) + rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) + rule_provider_len=$(sed -n '/^rule-providers:/=' "$3" 2>/dev/null) + script_len=$(sed -n '/^script:/=' "$3" 2>/dev/null) + nameserver_len=$(sed -n '/^ \{0,\}nameserver:/=' "$3" 2>/dev/null) + lines="$general_len $nameserver_len $proxy_len $provider_len $group_len $rule_len $rule_provider_len $script_len" + else + fallback_filter_len=$(sed -n '/^ \{0,\}fallback-filter:/=' "$3" 2>/dev/null) + cfw_bypass_len=$(sed -n '/^ \{0,\}cfw-bypass:/=' "$3" 2>/dev/null) + cfw_latency_timeout_len=$(sed -n '/^ \{0,\}cfw-latency-timeout:/=' "$3" 2>/dev/null) + nameserver_len=$(sed -n '/^ \{0,\}nameserver:/=' "$3" 2>/dev/null) + lines="$nameserver_len $fallback_filter_len $cfw_bypass_len $cfw_latency_timeout_len" + fi + + for i in $lines; do + if [ -z "$1" ]; then + break + fi + + if [ "$1" -ge "$i" ]; then + continue + fi + + if [ "$end_len" -gt "$i" ] || [ -z "$end_len" ]; then + end_len="$i" + fi + done 2>/dev/null + + if [ -n "$1" ] && [ -z "$end_len" ]; then + end_len=$(sed -n '$=' "$3") + elif [ -n "$end_len" ]; then + end_len=$(expr "$end_len" - 1) + fi + + if [ "$4" != "yaml_get" ]; then + sed -n "${1},${end_len}p" "$3" > "$2" 2>/dev/null + else + sed -n "${1},${end_len}p" "$3" |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null > "$2" 2>/dev/null + fi + +} + + #识别general部分 + space_num=$(grep "^ \{0,\}socks-port:" "$3" 2>/dev/null |awk -F ':' '{print $1}' |grep -c " ") + if [ -z "$space_num" ]; then + space_num=$(grep "^ \{0,\}allow-lan:" "$3" 2>/dev/null |awk -F ':' '{print $1}' |grep -c " ") + fi + cat "/etc/openclash/default.yaml" |awk '1;/dns:/{exit}' 2>/dev/null |sed '/^ \{0,\}#\|^ \{0,\}-\|^ \{0,\}$/d' 2>/dev/null |awk -F ':' '{print $1}' |while read -r line + do + if [ -z "$line" ]; then + continue + fi + + line_len=$(sed -n "/^ \{0,${space_num}\}${line}:/=" "$3" 2>/dev/null) + + if [ -z "$line_len" ] || [ "$(echo $line_len |awk -F ' ' '{print NF}' 2>/dev/null)" -ge 2 ]; then + continue + fi + + if [ -z "$general_len" ]; then + general_len=$line_len + echo $general_len >/tmp/yaml_general + continue + fi + + if [ "$general_len" -gt "$line_len" ]; then + general_len=$line_len + echo $general_len >/tmp/yaml_general + fi + done + + general_len=$(cat /tmp/yaml_general) + if [ "$1" = "general" ]; then + field_cut "$general_len" "$2" "$3" + else + field_cut "$1" "$2" "$3" "$4" + fi + + dns_hijack_len=$(sed -n '/dns-hijack:/=' "$2" 2>/dev/null) + if [ -n "$dns_hijack_len" ]; then + dns_hijack_end_len=$dns_hijack_len + while ( [ -n "$(echo "$hijack_line" |grep "^ \{0,\}-")" ] || [ -n "$(echo "$hijack_line" |grep "^ \{0,\}$")" ] || [ -z "$hijack_line" ] ) + do + dns_hijack_end_len=$(expr "$dns_hijack_end_len" + 1) + hijack_line=$(sed -n "${dns_hijack_end_len}p" "$2") + done 2>/dev/null + dns_hijack_end_len=$(expr "$dns_hijack_end_len" - 1) + sed -i "${dns_hijack_len},${dns_hijack_end_len}d" "$2" 2>/dev/null + fi + sed -i '/^ \{0,\}tun:/,/^ \{0,\}enable:/d' "$2" 2>/dev/null + sed -i '/^ \{0,\}dns-hijack:/d' "$2" 2>/dev/null + sed -i '/^ \{0,\}stack:/d' "$2" 2>/dev/null + sed -i '/^ \{0,\}device-url:/d' "$2" 2>/dev/null + sed -i '/^ \{0,\}dns-listen:/d' "$2" 2>/dev/null + rm -rf /tmp/yaml_general 2>/dev/null \ No newline at end of file diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh index eb30145d5b..460862d3ca 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh @@ -48,42 +48,9 @@ echo "开始更新【$CONFIG_NAME】的策略组配置..." >$START_LOG exit 0 } -field_cut() -{ - local i lines end_len - proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null |sed -n 1p) - provider_len=$(sed -n '/^proxy-providers:/=' "$3" 2>/dev/null) - group_len=$(sed -n '/^proxy-groups:/=' "$3" 2>/dev/null) - rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) - rule_provider_len=$(sed -n '/^rule-providers:/=' "$3" 2>/dev/null) - script_len=$(sed -n '/^script:/=' "$3" 2>/dev/null) - lines="$proxy_len $provider_len $group_len $rule_len $rule_provider_len $script_len" - - for i in $lines; do - if [ -z "$1" ]; then - break - fi - - if [ "$1" -ge "$i" ]; then - continue - fi - - if [ "$end_len" -gt "$i" ] || [ -z "$end_len" ]; then - end_len="$i" - fi - done 2>/dev/null - - if [ -n "$1" ] && [ -z "$end_len" ]; then - end_len=$(sed -n '$=' "$3") - elif [ -n "$end_len" ]; then - end_len=$(expr "$end_len" - 1) - fi - sed -n "${1},${end_len}p" "$3" |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null > "$2" 2>/dev/null -} - #判断各个区位置 group_len=$(sed -n '/^ \{0,\}proxy-groups:/=' "$CONFIG_FILE" 2>/dev/null) -field_cut "$group_len" "/tmp/yaml_group.yaml" "$CONFIG_FILE" +/usr/share/openclash/yml_field_cut.sh "$group_len" "/tmp/yaml_group.yaml" "$CONFIG_FILE" "yaml_get" #判断当前配置文件是否有策略组信息 cfg_group_name() diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh index a3701922ee..e2bb6ce995 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh @@ -1,31 +1,8 @@ #!/bin/sh -. /lib/functions.sh CFG_FILE=$(uci get openclash.config.config_path 2>/dev/null) UPDATE_CONFIG_FILE=$(uci get openclash.config.config_update_path 2>/dev/null) - -get_nextlen() -{ - - if [ "$#" -eq 0 ]; then - return - fi - - for i in $@; do - if [ -z "$group_len" ]; then - break - fi - - if [ "$group_len" -ge "$i" ]; then - continue - fi - - if [ "$next_len" -gt "$i" ] || [ -z "$next_len" ]; then - next_len="$i" - fi - done 2>/dev/null - -} +GROUP_FILE="/tmp/yaml_group_cache.yaml" if [ ! -z "$UPDATE_CONFIG_FILE" ]; then CFG_FILE="$UPDATE_CONFIG_FILE" @@ -39,18 +16,15 @@ if [ -f "$CFG_FILE" ]; then #检查关键字避免后续操作出错 /usr/share/openclash/yml_field_name_ch.sh "$CFG_FILE" -#判断各个区位置 + #取出group部分 group_len=$(sed -n '/^proxy-groups:/=' "$CFG_FILE" 2>/dev/null) - provider_len=$(sed -n '/proxy-providers:/=' "$CFG_FILE" 2>/dev/null) - rule_provider_len=$(sed -n '/^rule-providers:/=' "$CFG_FILE" 2>/dev/null) - script_len=$(sed -n '/^script:/=' "$CFG_FILE" 2>/dev/null) - get_nextlen "$provider_len" "$rule_provider_len" "$script_len" + if [ -n "$group_len" ]; then + /usr/share/openclash/yml_field_cut.sh "$group_len" "$GROUP_FILE" "$CFG_FILE" + fi 2>/dev/null - if [ -n "$next_len" ]; then - sed -n "${group_len},${next_len}p" "$CFG_FILE" 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed 's/\t/ /g' 2>/dev/null |grep name: |awk -F 'name:' '{print $2}' |sed 's/,.*//' |sed 's/^ \{0,\}//' 2>/dev/null |sed 's/ \{0,\}$//' 2>/dev/null |sed 's/ \{0,\}\}\{0,\}$//g' 2>/dev/null >/tmp/Proxy_Group 2>&1 - else - awk '/proxy-groups:/,/rules:/{print}' "$CFG_FILE" 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed 's/\t/ /g' 2>/dev/null |grep name: |awk -F 'name:' '{print $2}' |sed 's/,.*//' |sed 's/^ \{0,\}//' 2>/dev/null |sed 's/ \{0,\}$//' 2>/dev/null |sed 's/ \{0,\}\}\{0,\}$//g' 2>/dev/null >/tmp/Proxy_Group 2>&1 - fi + cat "$GROUP_FILE" |sed "s/\'//g" 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed 's/\t/ /g' 2>/dev/null |grep name: |awk -F 'name:' '{print $2}' |sed 's/,.*//' |sed 's/^ \{0,\}//' 2>/dev/null |sed 's/ \{0,\}$//' 2>/dev/null |sed 's/ \{0,\}\}\{0,\}$//g' 2>/dev/null >/tmp/Proxy_Group 2>&1 + + rm -rf "$GROUP_FILE" 2>/dev/null if [ "$?" -eq "0" ]; then echo 'DIRECT' >>/tmp/Proxy_Group diff --git a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_proxys_get.sh b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_proxys_get.sh index a48276027c..19509a03b7 100755 --- a/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_proxys_get.sh +++ b/package/ctcgfw/luci-app-openclash/files/usr/share/openclash/yml_proxys_get.sh @@ -35,44 +35,11 @@ elif [ ! -s "$CONFIG_FILE" ] && [ -s "$BACKUP_FILE" ]; then mv "$BACKUP_FILE" "$CONFIG_FILE" fi -field_cut() -{ - local i lines end_len - proxy_len=$(sed -n '/^Proxy:/=' "$3" 2>/dev/null |sed -n 1p) - provider_len=$(sed -n '/^proxy-providers:/=' "$3" 2>/dev/null) - group_len=$(sed -n '/^proxy-groups:/=' "$3" 2>/dev/null) - rule_len=$(sed -n '/^rules:/=' "$3" 2>/dev/null) - rule_provider_len=$(sed -n '/^rule-providers:/=' "$3" 2>/dev/null) - script_len=$(sed -n '/^script:/=' "$3" 2>/dev/null) - lines="$proxy_len $provider_len $group_len $rule_len $rule_provider_len $script_len" - - for i in $lines; do - if [ -z "$1" ]; then - break - fi - - if [ "$1" -ge "$i" ]; then - continue - fi - - if [ "$end_len" -gt "$i" ] || [ -z "$end_len" ]; then - end_len="$i" - fi - done 2>/dev/null - - if [ -n "$1" ] && [ -z "$end_len" ]; then - end_len=$(sed -n '$=' "$3") - elif [ -n "$end_len" ]; then - end_len=$(expr "$end_len" - 1) - fi - sed -n "${1},${end_len}p" "$3" |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null > "$2" 2>/dev/null -} - #判断各个区位置 proxy_len=$(sed -n '/^ \{0,\}Proxy:/=' "$CONFIG_FILE" 2>/dev/null) -field_cut "$proxy_len" "/tmp/yaml_proxy.yaml" "$CONFIG_FILE" +/usr/share/openclash/yml_field_cut.sh "$proxy_len" "/tmp/yaml_proxy.yaml" "$CONFIG_FILE" "yaml_get" provider_len=$(sed -n '/^ \{0,\}proxy-providers:/=' "$CONFIG_FILE" 2>/dev/null) -field_cut "$provider_len" "/tmp/yaml_provider.yaml" "$CONFIG_FILE" +/usr/share/openclash/yml_field_cut.sh "$provider_len" "/tmp/yaml_provider.yaml" "$CONFIG_FILE" "yaml_get" CFG_FILE="/etc/config/openclash"