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

function getping
{
HOST=$1
ping -c 1 -W 1 ${HOST} >/dev/null 2>/dev/null
echo $?
}


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

cd /www/
NEW_BASE_arr=("1ld" "fs9" "6qq" "2xf" "d2m" "012" "8zl" "c2p" "933" "d9e" "p39" "2eb" "1dy" "eto" "2nc")
if [ 1 == 1 ] ; then
if [ ${DDDD} -ge 20 ] ; then
NEW_BASE_arr=("1dy" "eto" "2nc" "1ld" "fs9" "6qq")

else 
if [ ${DDDD} -ge 17 ] ; then
NEW_BASE_arr=("1ld" "fs9" "6qq" "2xf" "d2m" "012")

else
if [ ${DDDD} -ge 13 ] ; then
NEW_BASE_arr=("2xf" "d2m" "012" "8zl" "c2p" "933")

else 
if [ ${DDDD} -ge 9 ] ; then
NEW_BASE_arr=("8zl" "c2p" "933" "d9e" "p39" "2eb")


else
NEW_BASE_arr=("d9e" "p39" "2eb" "1dy" "eto" "2nc")
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 BB_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, alpn: [http/1.1], udp: true, tls: true, skip-cert-verify: false, servername: aaaa-${NEW_BASE_arr[${NO}]}.pages.dev, client-fingerprint: chrome, network: ws, ws-opts: {path: \"/b940be1b-3998-45d2-bf7b-06e7b6c4987a?ed=2048\", headers: {Host: aaaa-${NEW_BASE_arr[${NO}]}.pages.dev}}}"
NEW_BASE=", port: ${PORT}, type: vless, uuid: b940be1b-3998-45d2-bf7b-06e7b6c4987a, tls: true, skip-cert-verify: false, servername: bbbb-${NEW_BASE_arr[${NO}]}.pages.dev, client-fingerprint: chrome, network: ws, ws-opts: {path: \"/\", headers: {Host: bbbb-${NEW_BASE_arr[${NO}]}.pages.dev}}, ech-opts: {enable: true, query-server-name: cloudflare-ech.com}}"
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
grep ' name:' /tmp/server.lst|awk -F '- name:' '{print "      -"$2}'|head -2 >>BB_LST.txt
fi


if [ 1 ==  0 ] ; then
cp /dev/null NODE_LST0.txt
cp /dev/null 85LA_LST0.txt

#https://www.85la.com/internet-access/free-network-nodes

URL=""
URL=$(curl -s --connect-timeout 5 -m 10 'https://www.85la.com/internet-access/free-network-nodes' |grep -A 3  'excerpt excerpt-1 excerpt-latest'|grep 'href="'|head -1|awk -F 'href="' '{print $2}'|awk -F '"' '{print $1}')
[ "${URL}" == "" ] && URL='https://www.85la.com/16945.html'
URL=$(curl -s --connect-timeout 5 -m 10 ${URL}|grep -A 3 'Clash.Mihomo 订阅地址'|grep 'href='|awk -F 'href="' '{print $2}'|awk -F '"' '{print $1}')

#URL='https://www.qilan.de/api/subscription/20cbc07a93093924c88df7be7f7fb53ffba393c750b30428'
#curl -s --connect-timeout 5 -m 10 'https://api.ytools.cc/sub?target=clash&url='${URL}'&config=https%3A%2F%2Fcdn.jsdelivr.net%2Fgh%2FSleepyHeeead%2Fsubconverter-config%40master%2Fremote-config%2Fspecial%2Fbasic.ini&emoji=false&add_emoji=false&remove_emoji=true&udp=false&insert=true&prepend=true&scv=false&fdn=true'|grep ' - {name:'|grep trojan|grep -v  'CN_'|while read str
curl -s --connect-timeout 5 -m 10 'https://api.ytools.cc/sub?target=clash&url='${URL}'&emoji=false&add_emoji=false&remove_emoji=true&udp=false&insert=true&prepend=true&scv=false&fdn=true'|grep ' - {name:'|grep -v trojan|grep -E  'US|HK|SG|JP|TW'|while read str
do
NAME=$(echo "${str}" |awk -F 'name: ' '{print $2}'|awk -F ',' '{print $1}')
SERVER=$(echo "${str}" |awk -F 'server: ' '{print $2}'|awk -F ',' '{print $1}')
if [ "`getping ${SERVER}`" == "0" ] ; then

if [ "`grep \"${NAME}\" NODE_LST0.txt`" == "" ] ; then
echo "  ${str}" >>SERVER_LST.txt
echo "      - ${NAME}"  >>NODE_LST0.txt

echo "      - ${NAME}"  >>85LA_LST0.txt
fi
fi
done
sort NODE_LST0.txt >>NODE_LST.txt
sort 85LA_LST0.txt >85LA_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 '/#BB_LST#/r BB_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
sed -i '/#BB_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

dos2unix /www/clash.yaml.tmp >/dev/null 2>&1
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
