#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

DDDD=`date '+%H'`
[ ${DDDD:0:1} == "0" ] && DDDD=${DDDD:1:1}

cd /www/
NEW_BASE_arr=("ezr" "c5l" "2nf" "423" "2zc" "f3b" "7lf" "01u" "7ym" "eyr" "83j" "bq7" "xcd" "8oc" "bn1")
if [ 1 == 1 ] ; then
if [ ${DDDD} -ge 20 ] ; then
NEW_BASE_arr=("xcd" "8oc" "bn1" "ezr" "c5l" "2nf")

else 
if [ ${DDDD} -ge 17 ] ; then
NEW_BASE_arr=("ezr" "c5l" "2nf" "423" "2zc" "f3b")

else
if [ ${DDDD} -ge 13 ] ; then
NEW_BASE_arr=("423" "2zc" "f3b" "7lf" "01u" "7ym")

else 
if [ ${DDDD} -ge 9 ] ; then
NEW_BASE_arr=("7lf" "01u" "7ym" "eyr" "83j" "bq7")


else
NEW_BASE_arr=("eyr" "83j" "bq7" "xcd" "8oc" "bn1")
fi
fi
fi
fi
fi




cp /www/clash.yaml.source /www/clash.yaml.tmp
rm -rf NODE_LST.txt SERVER_LST.txt BEST_LST.txt  SF_LST.txt 85LA_LST.txt
echo "">NODE_LST.txt

if [ 1 == 0 ]; then
echo "162.159.198.2
162.159.199.2
162.159.192.23:8319
162.159.192.198:8319
162.159.192.77:3854
162.159.192.0:939
162.159.195.74:1018
162.159.195.64:939
162.159.195.173:1014
162.159.195.36:4500
162.159.192.82:4177
162.159.192.10:7559"|grep -v '#'|while read str
do
[ "${str}" == "" ] && continue

SERVER=`echo $str|awk -F ':' '{print $1}'`
PORT=`echo $str|awk -F ':' '{print $2}'`  
[ "${PORT}" == "" ] && PORT=443
NAME="WARP-${SERVER}"
echo "  - {name: ${NAME}, port: ${PORT}, server: ${SERVER}, type: masque, private-key: MHcCAQEEIK9dznfrIrk/4+3lziALqbXrFUXWiEcwKXaS0cqjyj7HoAoGCCqGSM49AwEHoUQDQgAEBnAAhZmaaqqNqUTAIej6GSBsDdXwIJBa9jiN6laX63QqyhyDPFxcFZosqDnyAMOGaQn3r1ASFLxSLp6I3bJFzg==, public-key: MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEIaU7MToJm9NKp8YfGxR6r+/h4mcG7SxI8tsW8OR1A5tv/zCzVbCRRh2t87/kxnP6lAy0lkr7qYwu+ox+k3dr6w==, ip: 172.16.0.2, mtu: 1280, udp: true, remote-dns-resolve: true, dns: [1.1.1.1]}" >>SERVER_LST.txt

echo "      - ${NAME}" >>NODE_LST.txt  
echo "      - ${NAME}" >>BEST_LST.txt 
done
fi 
 
LEN=${#NEW_BASE_arr[@]}
rm -f /tmp/tmp_ip.txt 2>/dev/null
echo "162.159.235.22#US
104.19.190.137#US
172.64.148.102#SG
#saas.sin.fan
#cf.090227.xyz
#ct.877774.xyz
#bestcf.030101.xyz
#www.shopify.com
#cloudflare.182682.xyz
#BestCF.pages.dev"|grep -E -v '^#'|while read str
do
#nslookup ${str}|grep Address:|grep -v '#53'|awk '{print $2}'|grep -v ':'|head -1>>/tmp/tmp_ip.txt
[ "${str}" == "" ] && continue
echo ${str} >>/tmp/tmp_ip.txt
done
 
cat /tmp/tmp_ip.txt|while read str
do
[ "${str}" == "" ] && continue

IPIP=`echo $str|awk -F '#' '{print $1}'|awk -F ':' '{print $1}'`
PORT=`echo $str|awk -F '#' '{print $1}'|awk -F ':' '{print $2}'`
[ "${PORT}" == "" ] && PORT=443
PRE=`echo $str|awk -F '#' '{print $2}'`  
[ "${PRE}" != "" ] && PRE="_${PRE}"
[ "`echo ${IP}|grep -E '^#'`" != "" ] && continue
NO=0
for IP in  ${NEW_BASE_arr[@]}; do 
NEW_BASE=", ech-opts: {enable: true}, port: ${PORT}, type: vless, uuid: b940be1b-3998-45d2-bf7b-06e7b6c4987a, udp: true, tls: true, skip-cert-verify: true, servername: kkkk-${NEW_BASE_arr[${NO}]}.pages.dev, client-fingerprint: chrome, network: ws, ws-opts: {path: \"/?ed=2048\", headers: {Host: kkkk-${NEW_BASE_arr[${NO}]}.pages.dev}}}"
echo "  - {name: ${IPIP}-${NEW_BASE_arr[${NO}]}${PRE}, server: ${IPIP}${NEW_BASE}" >>SERVER_LST.txt
echo "      - ${IPIP}-${NEW_BASE_arr[${NO}]}${PRE}" >>NODE_LST.txt  
echo "      - ${IPIP}-${NEW_BASE_arr[${NO}]}${PRE}" >>BEST_LST.txt  
let NO=${NO}+1
done
done





if [ 1 == 1 ]; then
curl -s --connect-timeout 5 -m 10 -kL  https://www.gitlabip.xyz/Alvin9999/PAC/refs/heads/master/backup/img/1/2/ipp/clash.meta2/1/config.yaml|grep -A 300 'proxies:'|grep -B 300 'proxy-groups:'|grep -v -E '^prox'|sed 's/55 M/500 M/g'>/tmp/server.lst
cat /tmp/server.lst >>SERVER_LST.txt
grep ' name:' /tmp/server.lst|awk -F '- name:' '{print "      -"$2}'|head -2 >>NODE_LST.txt
grep ' name:' /tmp/server.lst|awk -F '- name:' '{print "      -"$2}'|head -2 >>BEST_LST.txt
fi





sed -i '/#SERVER_LST#/r SERVER_LST.txt' /www/clash.yaml.tmp
sed -i '/#NODE_LST#/r NODE_LST.txt' /www/clash.yaml.tmp
sed -i '/#BEST_LST#/r BEST_LST.txt' /www/clash.yaml.tmp
sed -i '/#SF_LST#/r SF_LST.txt' /www/clash.yaml.tmp
sed -i '/#85LA_LST#/r 85LA_LST.txt' /www/clash.yaml.tmp
sed -i '/#SERVER_LST#/d'  /www/clash.yaml.tmp
sed -i '/#NODE_LST#/d'  /www/clash.yaml.tmp
sed -i '/#BEST_LST#/d'  /www/clash.yaml.tmp
sed -i '/#SF_LST#/d'  /www/clash.yaml.tmp
sed -i '/#85LA_LST#/d'  /www/clash.yaml.tmp
rm -rf SG_LST.xt  JP_LST.txt US_LST.txt
grep -E '新加坡|SG'  NODE_LST.txt >SG_LST.txt
[ `wc -l SG_LST.txt|awk '{print $1}'` == 0 ] && echo '      - 🔰 节点选择' >SG_LST.txt
sed -i '/#SG_BEST_LST#/r SG_LST.txt' /www/clash.yaml.tmp
sed -i '/#SG_BEST_LST#/d'  /www/clash.yaml.tmp

grep -E '日本|JP'  NODE_LST.txt >JP_LST.txt
[ `wc -l JP_LST.txt|awk '{print $1}'` == 0 ] && echo '      - 🔰 节点选择' >JP_LST.txt
sed -i '/#JP_BEST_LST#/r JP_LST.txt' /www/clash.yaml.tmp
sed -i '/#JP_BEST_LST#/d'  /www/clash.yaml.tmp


grep -E '美国|US'  NODE_LST.txt >US_LST.txt
[ `wc -l US_LST.txt|awk '{print $1}'` == 0 ] && echo '      - 🔰 节点选择' >US_LST.txt
sed -i '/#US_BEST_LST#/r US_LST.txt' /www/clash.yaml.tmp
sed -i '/#US_BEST_LST#/d'  /www/clash.yaml.tmp


#sed -i 's/_ECH//g' /www/clash.yaml.tmp

#sed -i '/_ECH/d'  /www/clash.yaml.tmp


cp /www/clash.yaml.tmp /www/clash.yaml

cp /www/clash.yaml /www/openclash.yaml
sed -i  '/备用节点/d'  /www/openclash.yaml
sed -i  '/home1/d'  /www/openclash.yaml
sed -i 's/home0, server: 127.0.0.1/home0, server: 192.168.1.254/'  /www/openclash.yaml

sed '/proxies:/,$d' clash.yaml >clash_new.yaml
curl -s --connect-timeout 5 -m 10 https://raw.githubusercontent.com/shuaidaoya/FreeNodes/refs/heads/main/nodes/mihomo.yaml >>clash_new.yaml
##############TV

#curl -s -kL -o IPTV.txt https://raw.githubusercontent.com/fuxinyi0505/Ku9-IPTV-source/refs/heads/main/Ku9-IPTV-source.txt
#curl -s -o Gather.m3u https://tv.iill.top/m3u/Gather
#curl -s -o mytv.txt https://tv.iill.top/m3u/MyTV
