diff --git a/package/cnsztl/luci-app-openclash/files/etc/init.d/openclash b/package/cnsztl/luci-app-openclash/files/etc/init.d/openclash index 8f7dc47e5c..221bd4901f 100755 --- a/package/cnsztl/luci-app-openclash/files/etc/init.d/openclash +++ b/package/cnsztl/luci-app-openclash/files/etc/init.d/openclash @@ -100,24 +100,45 @@ yml_check() yml_dns_check - #检查行首空格避免后续操作出错 - [ ! -z "$(grep "^ \{1,\}Proxy:" $3)" ] && { + #检查关键字避免后续操作出错 + + [ ! -z "$(grep "^ \{0,\}'Proxy':" "$3")" ] || [ ! -z "$(grep '^ \{0,\}"Proxy":' "$3")" ] && { + sed -i "/^ \{0,\}\'Proxy\'/c\Proxy:" "$3" + sed -i '/^ \{0,\}\"Proxy\":/c\Proxy:' "$3" + } + + [ ! -z "$(grep "^ \{0,\}'Proxy Group':" "$3")" ] || [ ! -z "$(grep '^ \{0,\}"Proxy Group":' "$3")" ] && { + sed -i "/^ \{0,\}\'Proxy Group\'/c\Proxy Group:" "$3" + sed -i '/^ \{0,\}\"Proxy Group\":/c\Proxy Group:' "$3" + } + + [ ! -z "$(grep "^ \{0,\}'Rule':" "$3")" ] || [ ! -z "$(grep '^ \{0,\}"Rule":' "$3")" ] && { + sed -i "/^ \{0,\}\'Rule\'/c\Rule:" "$3" + sed -i '/^ \{0,\}\"Rule\":/c\Rule:' "$3" + } + + [ ! -z "$(grep "^ \{0,\}'dns':" "$3")" ] || [ ! -z "$(grep '^ \{0,\}"dns":' "$3")" ] && { + sed -i "/^ \{0,\}\'dns\'/c\dns:" "$3" + sed -i '/^ \{0,\}\"dns\":/c\dns:' "$3" + } + + [ ! -z "$(grep "^ \{1,\}Proxy:" "$3")" ] && { sed -i "/^ \{1,\}Proxy:/c\Proxy:" "$3" } - [ ! -z "$(grep "^ \{1,\}Proxy Group:" $3)" ] && { + [ ! -z "$(grep "^ \{1,\}Proxy Group:" "$3")" ] && { sed -i "/^ \{1,\}Proxy Group:/c\Proxy Group:" "$3" } - [ ! -z "$(grep "^ \{1,\}Rule:" $3)" ] && { + [ ! -z "$(grep "^ \{1,\}Rule:" "$3")" ] && { sed -i "/^ \{1,\}Rule:/c\Rule:" "$3" } - [ ! -z "$(grep "^ \{1,\}dns:" $3)" ] && { + [ ! -z "$(grep "^ \{1,\}dns:" "$3")" ] && { sed -i "/^ \{1,\}dns:/c\dns:" "$3" } - if [ ! -z "$(grep "^ \{1,\}port:" $3)" ] || [ ! -z "$(grep "^ \{1,\}mode:" $3)" ] || [ ! -z "$(grep "^ \{1,\}log-level:" $3)" ]; then + if [ ! -z "$(grep "^ \{1,\}port:" "$3")" ] || [ ! -z "$(grep "^ \{1,\}mode:" "$3")" ] || [ ! -z "$(grep "^ \{1,\}log-level:" "$3")" ]; then cp "$3" /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 diff --git a/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/config.lua b/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/config.lua index 612a504151..ad475b0787 100644 --- a/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/config.lua +++ b/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/config.lua @@ -40,8 +40,8 @@ HTTP.setfilehandler( fd:close() fd = nil if (meta.file == "config.yml") then - SYS.call("cp /etc/openclash/config.yml /etc/openclash/config.bak") SYS.call("mv /etc/openclash/config.yml /etc/openclash/config.yaml") + SYS.call("cp /etc/openclash/config.yaml /etc/openclash/config.bak") elseif (meta.file == "config.yaml") then SYS.call("cp /etc/openclash/config.yaml /etc/openclash/config.bak") end diff --git a/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/servers.lua b/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/servers.lua index 61bf271155..0f908a729e 100644 --- a/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/servers.lua +++ b/package/cnsztl/luci-app-openclash/files/usr/lib/lua/luci/model/cbi/openclash/servers.lua @@ -42,7 +42,7 @@ o.inputstyle = "apply" o.write = function() m.uci:set("openclash", "config", "enable", 0) m.uci:commit("openclash") - luci.sys.call("/usr/share/openclash/yml_proxys_set.sh >/dev/null 2>&1 &") + luci.sys.call("/usr/share/openclash/yml_groups_set.sh >/dev/null 2>&1 &") luci.http.redirect(luci.dispatcher.build_url("admin", "services", "openclash")) end diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/openclash_watchdog.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/openclash_watchdog.sh index 78ec97365c..caf2487aa7 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/openclash_watchdog.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/openclash_watchdog.sh @@ -15,8 +15,6 @@ if [ "$enable" -eq 1 ]; then nohup /etc/init.d/openclash restart & exit 0 fi -else - exit 0 fi ## Log File Size Manage: diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh index d2f2667dbc..fea157487f 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_get.sh @@ -11,16 +11,24 @@ elif [ ! -f "/etc/openclash/config.yaml" ] && [ "$(ls -l /etc/openclash/config.y mv "/etc/openclash/config.yml" "/etc/openclash/config.yaml" fi echo "开始更新策略组配置..." >$START_LOG + +/usr/share/openclash/yml_groups_name_get.sh +[ ! -z "$(grep "读取错误" /tmp/Proxy_Group)"] && { + echo "读取错误,配置文件异常!" >$START_LOG + sleep 5 + echo "" >$START_LOG + exit 0 +} +awk '/Proxy Group:/,/Rule:/{print}' /etc/openclash/config.yaml 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null >/tmp/yaml_group.yaml 2>&1 + +#删除旧配置 while ( [ ! -z "$(grep "config groups" "$CFG_FILE")" ] || [ ! -z "$(grep "config servers" "$CFG_FILE")" ] ) do uci delete openclash.@groups[0] 2>/dev/null uci delete openclash.@servers[0] 2>/dev/null uci commit openclash done -awk '/Proxy Group:/,/Rule:/{print}' /etc/openclash/config.yaml 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null >/tmp/yaml_group.yaml 2>&1 -awk '/Proxy Group:/,/Rule:/{print}' /etc/openclash/config.yaml 2>/dev/null |sed 's/\"//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed 's/\t/ /g' 2>/dev/null |grep name: 2>/dev/null |awk -F 'name:' '{print $2}' 2>/dev/null |sed 's/,.*//' 2>/dev/null |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 -echo "DIRECT" >>/tmp/Proxy_Group -echo "REJECT" >>/tmp/Proxy_Group + count=1 file_count=1 match_group_file="/tmp/Proxy_Group" diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh index 9421f35877..d0916d8b31 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_name_get.sh @@ -1,10 +1,35 @@ #!/bin/sh +CFG_FILE="/etc/openclash/config.yaml" -if [ -f "/etc/openclash/config.yaml" ]; then +if [ -f "$CFG_FILE" ]; then + #检查关键字避免后续操作出错 + [ ! -z "$(grep "^ \{0,\}'Proxy':" "$CFG_FILE")" ] || [ ! -z "$(grep '^ \{0,\}"Proxy":' "$CFG_FILE")" ] && { + sed -i "/^ \{0,\}\'Proxy\':/c\Proxy:" "$CFG_FILE" + sed -i '/^ \{0,\}\"Proxy\":/c\Proxy:' "$CFG_FILE" + } + + [ ! -z "$(grep "^ \{0,\}'Proxy Group':" "$CFG_FILE")" ] || [ ! -z "$(grep '^ \{0,\}"Proxy Group":' "$CFG_FILE")" ] && { + sed -i "/^ \{0,\}\'Proxy Group\':/c\Proxy Group:" "$CFG_FILE" + sed -i '/^ \{0,\}\"Proxy Group\":/c\Proxy Group:' "$CFG_FILE" + } + + [ ! -z "$(grep "^ \{0,\}'Rule':" "$CFG_FILE")" ] || [ ! -z "$(grep '^ \{0,\}"Rule":' "$CFG_FILE")" ] && { + sed -i "/^ \{0,\}\'Rule\':/c\Rule:" "$CFG_FILE" + sed -i '/^ \{0,\}\"Rule\":/c\Rule:' "$CFG_FILE" + } + + [ ! -z "$(grep "^ \{0,\}'dns':" "$CFG_FILE")" ] || [ ! -z "$(grep '^ \{0,\}"dns":' "$CFG_FILE")" ] && { + sed -i "/^ \{0,\}\'dns\':/c\dns:" "$CFG_FILE" + sed -i '/^ \{0,\}\"dns\":/c\dns:' "$CFG_FILE" + } + awk '/Proxy Group:/,/Rule:/{print}' /etc/openclash/config.yaml 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 - echo 'DIRECT' >>/tmp/Proxy_Group - echo 'REJECT' >>/tmp/Proxy_Group + if [ "$?" -eq "0" ]; then + echo 'DIRECT' >>/tmp/Proxy_Group + echo 'REJECT' >>/tmp/Proxy_Group + else + echo '读取错误,配置文件异常!' >/tmp/Proxy_Group + fi else echo '读取错误,配置文件异常!' >/tmp/Proxy_Group - exit 0 fi \ No newline at end of file diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_set.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_set.sh index 25594689ac..78fa942ddc 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_set.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_groups_set.sh @@ -65,8 +65,8 @@ yml_groups_set() #名字变化时处理规则部分 if [ "$name" != "$old_name" ]; then - sed -i "s/,${old_name}$/,${name}#d/g" $CONFIG_FILE 2>/dev/null - sed -i "s/:${old_name}$/:${name}#d/g" $CONFIG_FILE 2>/dev/null + sed -i "s/,${old_name}/,${name}#d/g" $CONFIG_FILE 2>/dev/null + sed -i "s/:${old_name}$/:${name}#d/g" $CONFIG_FILE 2>/dev/null #修改自定义规则分组对应标签 sed -i "s/\'${old_name}\'/\'${name}\'/g" $CFG_FILE 2>/dev/null config_load "openclash" fi @@ -84,20 +84,19 @@ yml_groups_set() create_config=$(uci get openclash.config.create_config 2>/dev/null) if [ "$create_config" = "0" ]; then - - if [ -z "$(grep "^ \{0,\}Proxy:" /etc/openclash/config.yaml)" ] || [ -z "$(grep "^ \{0,\}Proxy Group:" /etc/openclash/config.yaml)" ]; then - echo "配置文件不完整,开始使用ConnersHua规则创建..." >$START_LOG - uci set openclash.config.create_config=1 - uci set openclash.config.rule_sources="ConnersHua" - uci set openclash.config.rule_source="ConnersHua" - uci commit openclash + /usr/share/openclash/yml_groups_name_get.sh + if [ -z "$(grep "^ \{0,\}Proxy:" /etc/openclash/config.yaml)" ] || [ -z "$(grep "^ \{0,\}Rule:" /etc/openclash/config.yaml)" ]; then + echo "配置文件信息读取失败,无法进行修改,请选择一键创建配置文件..." >$START_LOG + sleep 5 + echo "" >$START_LOG + exit 0 else echo "开始更新配置文件策略组信息..." >$START_LOG config_load "openclash" config_foreach yml_groups_set "groups" - sed -i "s/#d$//g" $CONFIG_FILE 2>/dev/null + sed -i "s/#d//g" $CONFIG_FILE 2>/dev/null echo "Rule:" >>$GROUP_FILE + echo "配置文件策略组更新完成!" >$START_LOG fi - fi -echo "配置文件策略组写入完成!" >$START_LOG +/usr/share/openclash/yml_proxys_set.sh >/dev/null 2>&1 \ No newline at end of file diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_proxys_set.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_proxys_set.sh index 84a6f22c02..364bc6aa85 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_proxys_set.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_proxys_set.sh @@ -164,7 +164,7 @@ EOF #创建配置文件 -echo "开始更新配置文件节点..." >$START_LOG +echo "开始更新配置文件服务器节点信息..." >$START_LOG echo "Proxy:" >$SERVER_FILE config_load "openclash" config_foreach yml_servers_set "servers" @@ -321,19 +321,18 @@ uci set openclash.config.Others="Others" fi if [ "$create_config" != "0" ]; then echo "Rule:" >>$SERVER_FILE - echo "服务器节点写入配置文件完成,开始更新配置文件..." >$START_LOG + echo "配置文件创建完成,正在更新服务器、策略组信息..." >$START_LOG cat "$SERVER_FILE" > "/etc/openclash/config.yaml" 2>/dev/null /usr/share/openclash/yml_groups_get.sh >/dev/null 2>&1 else - echo "正在更新配置文件..." >$START_LOG - /usr/share/openclash/yml_groups_set.sh >/dev/null 2>&1 + echo "服务器、策略组信息修改完成,正在更新配置文件..." >$START_LOG sed -i '/^ \{0,\}Proxy:/i\#change server#' "/etc/openclash/config.yaml" 2>/dev/null sed -i '/^ \{0,\}Proxy:/,/^ \{0,\}Rule:/d' "/etc/openclash/config.yaml" 2>/dev/null sed -i '/#change server#/r/tmp/yaml_servers.yaml' "/etc/openclash/config.yaml" 2>/dev/null sed -i '/Proxy Group:/r/tmp/yaml_groups.yaml' "/etc/openclash/config.yaml" 2>/dev/null sed -i '/#change server#/d' "/etc/openclash/config.yaml" 2>/dev/null fi -echo "配置文件服务器节点写入完成!" >$START_LOG +echo "配置文件更新完成!" >$START_LOG rm -rf $SERVER_FILE 2>/dev/null rm -rf /tmp/Proxy_Server 2>/dev/null rm -rf /tmp/yaml_groups.yaml 2>/dev/null diff --git a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_rules_change.sh b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_rules_change.sh index 093b716500..08ae8a227e 100755 --- a/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_rules_change.sh +++ b/package/cnsztl/luci-app-openclash/files/usr/share/openclash/yml_rules_change.sh @@ -56,29 +56,29 @@ if [ "$2" != 0 ]; then if [ "$2" = "lhie1" ]; then sed -i '/^Rule:/,$d' "$4" cat /etc/openclash/lhie1.yaml >> "$4" - sed -i -e "s/,GlobalTV$/,${GlobalTV}#d/g" -e "/Rule:/a\##GlobalTV:${GlobalTV}"\ - -e "s/,AsianTV$/,${AsianTV}#d/g" -e "/Rule:/a\##AsianTV:${AsianTV}"\ - -e "s/,Proxy$/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ - -e "s/,Domestic$/,${Domestic}#d/g" -e "/Rule:/a\##Domestic:${Domestic}"\ - -e "s/,Others$/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ - -e "s/#d$//g" "$4" + sed -i -e "s/,GlobalTV/,${GlobalTV}#d/g" -e "/Rule:/a\##GlobalTV:${GlobalTV}"\ + -e "s/,AsianTV/,${AsianTV}#d/g" -e "/Rule:/a\##AsianTV:${AsianTV}"\ + -e "s/,Proxy/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ + -e "s/,Domestic/,${Domestic}#d/g" -e "/Rule:/a\##Domestic:${Domestic}"\ + -e "s/,Others/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ + -e "s/#d//g" "$4" elif [ "$2" = "ConnersHua" ]; then sed -i '/^Rule:/,$d' "$4" cat /etc/openclash/ConnersHua.yaml >> "$4" - sed -i -e "s/,ForeignMedia$/,${GlobalTV}#d/g" -e "/Rule:/a\##GlobalTV:${GlobalTV}"\ - -e "s/,DomesticMedia$/,${AsianTV}#d/g" -e "/Rule:/a\##AsianTV:${AsianTV}"\ - -e "s/,PROXY$/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ - -e "s/,Apple$/,${Apple}#d/g" -e "/Rule:/a\##Apple:${Apple}"\ - -e "s/,Hijacking$/,${AdBlock}#d/g" -e "/Rule:/a\##AdBlock:${AdBlock}"\ - -e "s/,DIRECT$/,${Domestic}#d/g" -e "/Rule:/a\##Domestic:${Domestic}"\ - -e "s/,Final$/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ - -e "s/#d$//g" "$4" + sed -i -e "s/,ForeignMedia/,${GlobalTV}#d/g" -e "/Rule:/a\##GlobalTV:${GlobalTV}"\ + -e "s/,DomesticMedia/,${AsianTV}#d/g" -e "/Rule:/a\##AsianTV:${AsianTV}"\ + -e "s/,PROXY/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ + -e "s/,Apple/,${Apple}#d/g" -e "/Rule:/a\##Apple:${Apple}"\ + -e "s/,Hijacking/,${AdBlock}#d/g" -e "/Rule:/a\##AdBlock:${AdBlock}"\ + -e "s/,DIRECT/,${Domestic}#d/g" -e "/Rule:/a\##Domestic:${Domestic}"\ + -e "s/,Final/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ + -e "s/#d//g" "$4" else sed -i '/^Rule:/,$d' "$4" cat /etc/openclash/ConnersHua_return.yaml >> "$4" - sed -i -e "s/,PROXY$/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ - -e "s/,DIRECT$/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ - -e "s/#d$//g" "$4" + sed -i -e "s/,PROXY/,${Proxy}#d/g" -e "/Rule:/a\##Proxy:${Proxy}"\ + -e "s/,DIRECT/,${Others}#d/g" -e "/Rule:/a\##Others:${Others}"\ + -e "s/#d//g" "$4" fi fi fi