OpenClash: sync with upstream source

This commit is contained in:
CN_SZTL 2020-10-07 03:10:51 +08:00
parent 241a0c31e2
commit a170cd3be7
No known key found for this signature in database
GPG Key ID: 6850B6345C862176
9 changed files with 197 additions and 125 deletions

View File

@ -74,6 +74,10 @@ local function is_web()
return luci.sys.call("pidof clash >/dev/null") == 0
end
local function restricted_mode()
return luci.sys.exec("uci get openclash.config.restricted_mode 2>/dev/null |tr -d '\n'")
end
local function is_watchdog()
local ps_version = luci.sys.exec("ps --version 2>&1 |grep -c procps-ng |tr -d '\n'")
if ps_version == "0" then
@ -306,6 +310,7 @@ function action_status()
uh_port = uh_port(),
web = is_web(),
cn_port = cn_port(),
restricted_mode = restricted_mode(),
mode = mode();
})
end

View File

@ -173,9 +173,16 @@
});
XHR.poll(2, '<%=luci.dispatcher.build_url("admin", "services", "openclash", "status")%>', null, function(x, status) {
if ( x && x.status == 200 ) {
clash.innerHTML = status.clash ? '<b><font color=green><%:RUNNING%></font></b>' : '<b><font color=red><%:NOT RUNNING%></font></b>';
if ( status.restricted_mode != "1" )
{
clash.innerHTML = status.clash ? '<b><font color=green><%:RUNNING%></font></b>' : '<b><font color=red><%:NOT RUNNING%></font></b>';
}
else
{
clash.innerHTML = status.clash ? '<b><font color=green><%:RUNNING%><%: <功能受限状态> %></font></b>' : '<b><font color=red><%:NOT RUNNING%></font></b>';
}
if ( status.mode == "fake-ip\n" )
{
{
mode.innerHTML = status.clash ? "<b><font color=green><%: Fake-IP增强模式 %></font></b>" : '<b><font color=red><%:NOT RUNNING%></font></b>';
}
else if ( status.mode == "redir-host\n" )

View File

@ -29,11 +29,11 @@
if ( x && x.status == 200 ) {
if ( status.switch_mode == "redir-host" ) {
alert('页面已切换为Fake-IP模式')
window.location.href='<%="http://'+window.location.hostname+'/cgi-bin/luci/admin/services/openclash/settings"%>';
window.location.href='<%="settings"%>';
}
else {
alert('页面已切换为Redir-Host模式')
window.location.href='<%="http://'+window.location.hostname+'/cgi-bin/luci/admin/services/openclash/settings"%>';
window.location.href='<%="settings"%>';
}
}
});

View File

@ -323,11 +323,11 @@
XHR.get('<%=luci.dispatcher.build_url("admin", "services", "openclash", "restore")%>', null, function(x, status) {
if ( x && x.status == 200 ) {
alert('还原默认配置成功!')
window.location.href='<%="http://'+window.location.hostname+'/cgi-bin/luci/admin/services/openclash/settings"%>';
window.location.href='<%="settings"%>';
}
else {
alert('还原默认配置失败!')
window.location.href='<%="http://'+window.location.hostname+'/cgi-bin/luci/admin/services/openclash/settings"%>';
window.location.href='<%="settings"%>';
}
});
} else {

View File

@ -903,35 +903,6 @@ yml_custom_rule_provider()
}
ac_add()
{
if [ -z "$1" ]; then
return
fi
ipset add "$2" "$1" 2>/dev/null
}
firewall_redirect_exclude()
{
local section="$1"
local src_dport
config_get "src_dport" "$section" "src_dport" ""
if [ -z "$src_dport" ]; then
return
fi
if [ -z "$en_mode_tun" ]; then
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
elif [ "$en_mode_tun" -ne 3 ]; then
iptables -t mangle -A openclash -p tcp --dport "$src_dport" -j RETURN >/dev/null 2>&1
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
else
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
fi
}
#配置文件选择
config_choose()
{
@ -1113,6 +1084,164 @@ start_run_core()
uci commit openclash
}
check_core_status()
{
check_time=1
while ( [ "$check_time" -le 3 ] && [ -z "$(pidof clash)" ] )
do
sleep 2
check_time=$(expr "$check_time" + 1)
done
}
#不修改配置文件启动
raw_config_start()
{
mv "$START_BACKUP" "$CONFIG_FILE"
dns_port=$(grep "^ \{0,\}listen:" "$CONFIG_FILE" |awk -F ':' '{print $3}' |awk -F '#' '{print $1}' |tr -cd "[0-9]" 2>/dev/null)
en_mode=$(grep "^ \{0,\}enhanced-mode:" "$CONFIG_FILE" |awk -F ': ' '{print $2}' |awk -F '#' '{print $1}' 2>/dev/null |sed 's/^ \{0,\}//g' 2>/dev/null |sed 's/ \{0,\}$//g' 2>/dev/null)
proxy_port=$(grep "^ \{0,\}redir-port:" "$CONFIG_FILE" |awk -F ': ' '{print $2}' |awk -F '#' '{print $1}' 2>/dev/null |tr -cd "[0-9]")
if [ -z "$dns_port" ] || [ -z "$en_mode" ] || [ -z "$proxy_port" ]; then
if [ -z "$dns_port" ]; then
echo "错误: 无法获取DNS部分的监听端口设置, OpenClash 使用原始配置文件启动失败" >$START_LOG
echo "${LOGTIME} Get DNS 'listen' Option Error, OpenClash Can Not Start With Raw Config File" >> $LOG_FILE
sleep 3
fi
if [ -z "$en_mode" ]; then
echo "错误: 无法获取DNS部分的运行模式设置, OpenClash 使用原始配置文件启动失败" >$START_LOG
echo "${LOGTIME} Get DNS 'enhanced-mode' Option Error, OpenClash Can Not Start With Raw Config File" >> $LOG_FILE
sleep 3
fi
if [ -z "$proxy_port" ]; then
echo "错误: 无法获取General部分的转发端口设置, OpenClash 使用原始配置文件启动失败" >$START_LOG
echo "${LOGTIME} Get General 'redir-port' Option Error, OpenClash Can Not Start With Raw Config File" >> $LOG_FILE
sleep 3
fi
start_fail
fi
start_run_core
check_core_status
if ! pidof clash >/dev/null; then
echo "错误: OpenClash 启动失败,请到日志页面查看详细错误信息!" >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Please Check The Error Info And Try Again" >> $LOG_FILE
sleep 5
start_fail
fi
if [ "$en_mode" = "redir-host" ]; then
case $en_mode_tun in
"1")
uci set openclash.config.en_mode=redir-host-tun
;;
"2")
uci set openclash.config.en_mode=redir-host-vpn
;;
"3")
uci set openclash.config.en_mode=redir-host-mix
;;
*)
uci set openclash.config.en_mode=redir-host
esac
elif [ "$en_mode" = "fake-ip" ]; then
case $en_mode_tun in
"1")
uci set openclash.config.en_mode=fake-ip-tun
;;
"2")
uci set openclash.config.en_mode=fake-ip-vpn
;;
"3")
uci set openclash.config.en_mode=fake-ip-mix
;;
*)
uci set openclash.config.en_mode=fake-ip
esac
fi
dase=$(grep "^ \{0,\}Secret:" "$CONFIG_FILE" |awk -F ': ' '{print $2}' |awk -F '#' '{print $1}' |sed 's/^ \{0,\}//g' 2>/dev/null |sed 's/ \{0,\}$//g' 2>/dev/null |sed "s/\'//g" 2>/dev/null |sed "s/\'//g" 2>/dev/null)
uci set openclash.config.dashboard_password=dase 2>/dev/null
cn_port=$(grep "^ \{0,\}external-controller:" "$CONFIG_FILE" |awk -F ':' '{print $3}' |awk -F '#' '{print $1}' |tr -cd "[0-9]" 2>/dev/null)
uci set openclash.config.dashboard_password=cn_port 2>/dev/null
uci set openclash.config.restricted_mode=1
uci commit openclash
}
try_restore_start()
{
if [ -z "$(pidof clash)" ]; then
if [ "$rule_source" = 0 ] && [ "$enable_custom_clash_rules" = 0 ]; then
echo "错误: OpenClash 启动失败,尝试使用原始配置文件启动..." >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Try Use Raw Config Restart Again" >> $LOG_FILE
sleep 3
raw_config_start
else
echo "错误: OpenClash 启动失败,尝试还原第三方规则并重新启动 Clash 主程序..." >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Try Use Backup Rules Start Again" >> $LOG_FILE
rm -rf /tmp/yaml_general 2>/dev/null
#获取备份rule_provider
rule_provider_bak_len=$(sed -n '/^rule-providers:/=' "$START_BACKUP" 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" "$START_BACKUP"
fi 2>/dev/null
#获取备份script
script_bak_len=$(sed -n '/^script:/=' "$START_BACKUP" 2>/dev/null)
if [ -n "$script_bak_len" ]; then
/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
/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 &
check_core_status
if ! pidof clash >/dev/null; then
echo "错误: OpenClash 启动失败,尝试使用原始配置文件启动..." >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Try Use Raw Config Restart Again" >> $LOG_FILE
sleep 3
raw_config_start
fi
fi
fi
}
#防火墙设置部分
ac_add()
{
if [ -z "$1" ]; then
return
fi
ipset add "$2" "$1" 2>/dev/null
}
firewall_redirect_exclude()
{
local section="$1"
local src_dport
config_get "src_dport" "$section" "src_dport" ""
if [ -z "$src_dport" ]; then
return
fi
if [ -z "$en_mode_tun" ]; then
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
elif [ "$en_mode_tun" -ne 3 ]; then
iptables -t mangle -A openclash -p tcp --dport "$src_dport" -j RETURN >/dev/null 2>&1
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
else
iptables -t mangle -A openclash -p udp --dport "$src_dport" -j RETURN >/dev/null 2>&1
fi
}
set_firewall()
{
if [ "$(iptables -t nat -nL PREROUTING --line-number |grep "udp dpt:53" |grep "0.0.0.0/0 \{0,\}0.0.0.0/0" |wc -l)" -gt 1 ] && [ "$enable_redirect_dns" -eq "1" ]; then
@ -1462,29 +1591,7 @@ get_config()
stack_type=$(uci get openclash.config.stack_type 2>/dev/null)
china_ip_route=$(uci get openclash.config.china_ip_route 2>/dev/null)
small_flash_memory=$(uci get openclash.config.small_flash_memory 2>/dev/null)
}
yml_field_check()
{
if [ -f "$GROUP_FILE" ]; then
if [ ! -f "$PROXY_FILE" ] && [ ! -f "$PROXY_PROVIDER_FILE" ]; then
echo "错误: 配置文件完整性检查不通过,已自动还原配置文件,请对照模板格式检查修改配置文件!" >$START_LOG
mv "$START_BACKUP" "$CONFIG_FILE"
sleep 5
start_fail
elif [ ! -f "$RULE_FILE" ] && [ ! -f "$SCRIPT_FILE" ]; then
echo "错误: 配置文件完整性检查不通过,已自动还原配置文件,请对照模板格式检查修改配置文件!" >$START_LOG
mv "$START_BACKUP" "$CONFIG_FILE"
sleep 5
start_fail
fi
else
nohup "$CLASH" -d "$CLASH_CONFIG" -f "$CONFIG_FILE" >> $LOG_FILE 2>&1 &
echo "错误: 配置文件完整性检查不通过,已自动还原配置文件,请根据日志信息对照模板格式检查修改配置文件!" >$START_LOG
mv "$START_BACKUP" "$CONFIG_FILE"
sleep 5
start_fail
fi
uci set openclash.config.restricted_mode=0 && uci commit openclash
}
start()
@ -1510,7 +1617,6 @@ start()
echo "第二步: 配置文件检查..." >$START_LOG
yml_check "$en_mode" "$enable_custom_dns" "$CONFIG_FILE" "$BACKUP_FILE" "$START_BACKUP"
yml_dns_check
yml_field_check
echo "第三步: 修改配置文件..." >$START_LOG
config_load "openclash"
@ -1543,68 +1649,40 @@ start()
echo "第六步: 等待主程序下载外部文件..." >$START_LOG
yml_provider_check "$PROXY_PROVIDER_FILE" "proxy_provider"
yml_provider_check "$RULE_PROVIDER_FILE" "rule_provider"
echo "第七步: 设置控制面板..." >$START_LOG
echo "第七步: 检查内核启动状态..." >$START_LOG
check_core_status
try_restore_start
echo "第八步: 设置控制面板..." >$START_LOG
ln -s /usr/share/openclash/yacd /www/luci-static/openclash 2>/dev/null
echo "第八步: 设置防火墙规则..." >$START_LOG
echo "第步: 设置防火墙规则..." >$START_LOG
set_firewall
echo "第步: 重启 Dnsmasq 程序..." >$START_LOG
echo "第步: 重启 Dnsmasq 程序..." >$START_LOG
change_dns "$enable_redirect_dns" "$disable_masq_cache"
/etc/init.d/dnsmasq restart >/dev/null 2>&1
echo "第十步: 检查内核启动状态..." >$START_LOG
if [ -z "$(pidof clash)" ] && [ "$rule_source" = 0 ] && [ "$enable_custom_clash_rules" = 0 ]; then
echo "错误: OpenClash 启动失败,请到日志页面查看详细错误信息!" >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Please Check The Error Info And Try Again" >> $LOG_FILE
sleep 5
start_fail
fi
if ! pidof clash >/dev/null; then
echo "错误: OpenClash 启动失败,尝试还原第三方规则并重新启动 Clash 主程序..." >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Try Use Backup Rules Start Again" >> $LOG_FILE
rm -rf /tmp/yaml_general 2>/dev/null
#获取备份rule_provider
rule_provider_bak_len=$(sed -n '/^rule-providers:/=' "$START_BACKUP" 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" "$START_BACKUP"
fi 2>/dev/null
#获取备份script
script_bak_len=$(sed -n '/^script:/=' "$START_BACKUP" 2>/dev/null)
if [ -n "$script_bak_len" ]; then
/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
/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 &
sleep 3
if ! pidof clash >/dev/null; then
echo "错误: OpenClash 启动失败,请到日志页面查看详细错误信息!" >$START_LOG
echo "${LOGTIME} OpenClash Can Not Start, Please Check The Error Info And Try Again" >> $LOG_FILE
sleep 5
start_fail
fi
fi
echo "第十一步: 还原策略组节点状态..." >$START_LOG
/usr/share/openclash/openclash_history_set.sh
echo "第十二步: 添加计划任务,启动进程守护程序..." >$START_LOG
add_cron
if [ -z "$(uci get dhcp.lan.dhcpv6 2>/dev/null)" ]; then
if [ -z "$(uci get dhcp.lan.dhcpv6 2>/dev/null)" ] && [ "$(uci get openclash.config.restricted_mode 2>/dev/null)" != "1" ]; then
echo "OpenClash 启动成功,请等待服务器上线!" >$START_LOG
echo "${LOGTIME} OpenClash Start Successful" >> $LOG_FILE
sleep 5
else
elif [ -n "$(uci get dhcp.lan.dhcpv6 2>/dev/null)" ]; then
echo "OpenClash 启动成功检测到您启用了IPV6的DHCP服务可能会造成连接异常" >$START_LOG
echo "${LOGTIME} OpenClash Start Successful, Please Note That Network May Abnormal With IPV6's DHCP Server" >> $LOG_FILE
sleep 5
fi
if [ "$(uci get openclash.config.restricted_mode 2>/dev/null)" = "1" ]; then
echo "OpenClash 使用原始配置文件启动成功,部分设置可能未生效!" >$START_LOG
echo "${LOGTIME} OpenClash Start Successful With Raw Config File, Please Note That It's Restricted Mode Now" >> $LOG_FILE
sleep 5
fi
echo "OpenClash Already Start"
echo "" >$START_LOG
rm -rf $START_BACKUP 2>/dev/null

View File

@ -24,18 +24,12 @@
echo "大陆IP白名单下载成功检查版本是否更新..." >$START_LOG
cmp -s /tmp/ChinaIP.yaml /etc/openclash/rule_provider/ChinaIP.yaml
if [ "$?" -ne "0" ]; then
status=$(unify_ps_prevent)
while ( [ "$status" -gt 1 ] )
do
sleep 5
status=$(unify_ps_prevent)
done
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 /usr/share/openclash/res/china_ip_route.ipset >/dev/null 2>&1
[ "$china_ip_route" -eq 1 ] && /etc/init.d/openclash restart
[ "$china_ip_route" -eq 1 ] && [ "$(unify_ps_prevent)" -eq 0 ] && /etc/init.d/openclash restart
echo "大陆IP白名单更新成功" >$START_LOG
echo "${LOGTIME} Chnroute Lists Update Successful" >>$LOG_FILE
sleep 10

View File

@ -30,12 +30,6 @@
echo "GEOIP 数据库下载成功,检查数据库版本是否更新..." >$START_LOG
cmp -s /tmp/Country.mmdb "$geoip_path"
if [ "$?" -ne "0" ]; then
status=$(unify_ps_prevent)
while ( [ "$status" -gt 1 ] )
do
sleep 5
status=$(unify_ps_prevent)
done
echo "数据库版本有更新,开始替换数据库版本..." >$START_LOG
mv /tmp/Country.mmdb "$geoip_path" >/dev/null 2>&1
echo "删除下载缓存..." >$START_LOG
@ -43,7 +37,7 @@
echo "GEOIP 数据库更新成功!" >$START_LOG
echo "${LOGTIME} GEOIP Database Update Successful" >>$LOG_FILE
sleep 5
/etc/init.d/openclash restart
[ "$(unify_ps_prevent)" -eq 0 ] && /etc/init.d/openclash restart
echo "" >$START_LOG
else
echo "数据库版本没有更新,停止继续操作..." >$START_LOG

View File

@ -18,9 +18,9 @@ unify_ps_pids() {
unify_ps_prevent() {
if [ "$(ps --version 2>&1 |grep -c procps-ng)" -eq 1 ];then
echo "$(ps -ef |grep -v openclash_watchdog |grep -c openclash.sh 2>/dev/null)"
echo "$(ps -ef |grep -v grep |grep -c "/etc/init.d/openclash")"
else
echo "$(ps |grep -v openclash_watchdog |grep -c openclash.sh 2>/dev/null)"
echo "$(ps |grep -v grep |grep -c "/etc/init.d/openclash")"
fi
}

View File

@ -75,14 +75,8 @@
mv /tmp/rules.yaml /usr/share/openclash/res/"$RUlE_SOURCE".yaml >/dev/null 2>&1
sed -i '/^rules:/a\##updated' /usr/share/openclash/res/"$RUlE_SOURCE".yaml >/dev/null 2>&1
echo "替换成功,重新加载 OpenClash 应用新规则..." >$START_LOG
status=$(unify_ps_prevent)
while ( [ "$status" -gt 1 ] )
do
sleep 5
status=$(unify_ps_prevent)
done
/etc/init.d/openclash restart 2>/dev/null
echo "${LOGTIME} Other Rules 【$RUlE_SOURCE】 Update Successful" >>$LOG_FILE
[ "$(unify_ps_prevent)" -eq 0 ] && /etc/init.d/openclash restart
else
echo "检测到下载的规则文件没有更新,停止继续操作..." >$START_LOG
rm -rf /tmp/rules.yaml >/dev/null 2>&1