#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
cd /www/wwwroot/default
cp /www/wwwroot/default/clash.yaml.source /www/wwwroot/default/clash.yaml.tmp
BB=11
curl -s https://ghproxy.cn/https://raw.githubusercontent.com/ymyuuu/IPDB/refs/heads/main/bestcf.txt|head -14|while read str
do
sed -i "s/SS${BB}/${str}/g" /www/wwwroot/default/clash.yaml.tmp
let BB=${BB}+1
done
BB=31
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
sed -i "s/SS${BB}/${str}/g" /www/wwwroot/default/clash.yaml.tmp
let BB=${BB}+1
done

BB=25
curl -s https://ghproxy.cn/https://raw.githubusercontent.com/Alvin9999/pac2/refs/heads/master/clash.meta2/config.yaml|grep -a1 'type: vmess'|grep server|awk '{print $2}'|head -4|while read str
do
sed -i "s/SS${BB}/${str}/g" /www/wwwroot/default/clash.yaml.tmp
let BB=${BB}+1
done


rm -f SERVER_LST.txt
rm -f NODE_LST.txt

python3 vpn.py|grep 'ss://'|while read str 
do 
  str=${str:5}
  [ "${str}" == "" ] && continue
  S1=`echo "${str}"|awk -F '#' '{print $1}'|base64 -d`
  S2=`echo "${str}"|awk -F '#' '{print $2}'`
  V1=`echo "${S1}"|awk -F '@' '{print $1}'`
  V2=`echo "${S1}"|awk -F '@' '{print $2}'`
  W1=`echo "${V1}"|awk -F ':' '{print $1}'`
  W2=`echo "${V1}"|awk -F ':' '{print $2}'`  
  W3=`echo "${V2}"|awk -F ':' '{print $1}'`
  W4=`echo "${V2}"|awk -F ':' '{print $2}'`    
#  echo $S2 $W1 $W2 $W3 $W4
  echo "  - {name: \"$S2\", server: $W3, port: $W4, type: ss, cipher: $W1, password: $W2}" >>SERVER_LST.txt
  echo "      - $S2" >>NODE_LST.txt
done

sed -i '/#SERVER_LST#/r SERVER_LST.txt' /www/wwwroot/default/clash.yaml.tmp
sed -i '/#NODE_LST#/r NODE_LST.txt' /www/wwwroot/default/clash.yaml.tmp
sed -i '/#SERVER_LST#/d'  /www/wwwroot/default/clash.yaml.tmp
sed -i '/#NODE_LST#/d'  /www/wwwroot/default/clash.yaml.tmp
cp /www/wwwroot/default/clash.yaml.tmp /www/wwwroot/default/clash.yaml
