#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
cd /www/
function getping
{
str=$*
str=${str//\"/}
str=${str// /}
HOST=`echo "${str}"|awk -F 'server:' '{print $2}'|awk -F ',' '{print $1}'`
[ "${HOST}" == "" ] && HOST=${str}
ping -c 1 -W 1 ${HOST} >/dev/null
echo $?
}


S=$(curl -s -kL 'https://skill-note.blogspot.com/2023/09/21-find-isnumber-len-substitute.html'|grep 'Clash-meta 20.37版以后'|awk -F 'Clash-meta 20.37版以后' '{print $2}'|awk -F 'https://drive.google.com' '{print $2}'|awk -F '<' '{print $1}')
S="https://drive.google.com${S}"
curl -kL -s -o /www/sf.yaml.1 "${S}"
[ "`grep 'mixed-port' /www/sf.yaml.1`" != "" ] && mv /www/sf.yaml.1 /www/sf.yaml

cat >/www/v2ray.txt <<EOF
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@bestcf.yzqiang.site:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#bestcf.yzqiang.site
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@usa.visa.com:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#usa.visa.com
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@myanmar.visa.com:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#myanmar.visa.com
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@www.visa.com.tw:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#www.visa.com.tw
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@www.visaeurope.ch:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#www.visaeurope.ch
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@www.visa.com.br:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#www.visa.com.br
vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@www.visasoutheasteurope.com:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#vwww.visasoutheasteurope.com
EOF
S=$(curl -s -kL 'https://skill-note.blogspot.com/2023/09/21-find-isnumber-len-substitute.html'|grep 'Quantumult X订阅'|awk -F 'Quantumult X订阅' '{print $2}'|awk -F 'https://drive.google.com' '{print $2}'|awk -F '<' '{print $1}')
S="https://drive.google.com${S}"
curl -kL -s -o /www/v2ray.txt.1 "${S}"
[ "`grep 'vless:' /www/v2ray.txt.1`" != "" ] && cat  /www/v2ray.txt.1 >>/www/v2ray.txt




cp /www/clash.yaml.source /www/clash.yaml.tmp
rm -rf NODE_LST.txt SERVER_LST.txt 



BASE=', port: 443, type: vless, uuid: b940be1b-3998-45d2-bf7b-06e7b6c4987a, network: ws, tls: true, udp: false, sni: c379b284.vs-494.pages.dev, client-fingerprint: chrome, ws-opts: {path: "/?ed=2560", headers: {host: c379b284.vs-494.pages.dev}}}'
BASE=', port: 443, type: vless, uuid: b940be1b-3998-45d2-bf7b-06e7b6c4987a, network: ws, tls: true, udp: false, sni: vvv.yzqiang.site, client-fingerprint: chrome, ws-opts: {path: "/?ed=2560", headers: {host: vvv.yzqiang.site}}}'

BB=11
curl -s -kL https://ghproxy.cn/https://raw.githubusercontent.com/ymyuuu/IPDB/refs/heads/main/bestcf.txt|head -14|while read str
do
  if [ `getping "${str}"` == 0 ] ; then
  SSSS="  - {name: S${BB}, server: ${str}"
  echo "${SSSS}${BASE}" >>SERVER_LST.txt
  echo "      - S${BB}" >>NODE_LST.txt
  fi
#  echo "vless://b940be1b-3998-45d2-bf7b-06e7b6c4987a@${}:443?encryption=none&security=tls&type=ws&host=vvv.yzqiang.site&sni=vvv.yzqiang.site&fp=random&path=%2F%3Fed%3D2560#vvv.yzqiang.site" >>/tmp/v2ray.txt.tmp
  let BB=${BB}+1
done
BB=25
curl -s 'https://api.hostmonit.com/get_optimization_ip' -d '{"key":"iDetkOys"}'|jq '.info[]'|jq -r '[.ip,.speed]|@csv'|tr '"' ' '|tr ',' ' '|sort -rk2 -n|awk '{print $1}'|head -15|while read str
do
  if [ `getping "${str}"` == 0 ] ; then
  SSSS="  - {name: S${BB}, server: ${str}"
  echo "${SSSS}${BASE}" >>SERVER_LST.txt
  echo "      - S${BB}" >>NODE_LST.txt
  fi
  let BB=${BB}+1

done

rm -f NODE_LST0.txt
grep '{"name"' sf.yaml|grep -E '美国|日本|新加坡|香港|台湾|韩国'|sort|while read str
do
if [ `getping "${str}"` == 0 ] ; then
str=${str//YouTube顺丰资源 /}
str=${str//YouTube顺丰资源/}
str=${str//Youtube顺丰资源 /}
NAME=$(echo "${str}" |awk -F '"name": "' '{print $2}'|awk -F '"' '{print $1}')

echo "  ${str}" >>SERVER_LST.txt
echo "      - ${NAME}"  >>NODE_LST0.txt
fi
done
sort NODE_LST0.txt >>NODE_LST.txt


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 '/#SERVER_LST#/d'  /www/clash.yaml.tmp
sed -i '/#NODE_LST#/d'  /www/clash.yaml.tmp
cp /www/clash.yaml.tmp /www/clash.yaml

cp /www/clash.yaml /www/openclash.yaml
sed -i  '/SOCKS5/d'  /www/openclash.yaml
