#!/bin/sh
#author:Wos
#simplify clash configuration file or public subscribes 
WORK_DIR=$(dirname $0)
SCRIPT_NAME=$(basename $0)
CFG_PATH=${WORK_DIR}/clash.yml
CFG_TEMP=/tmp/clash.yml
CFG_TEMP_2=/tmp/clash2.yml
CFG_P2C_PATH=${WORK_DIR}/p2c.yml
NODE_NAME=/tmp/nodename
DEL_NODE=/tmp/delnode
CLASH_PATH=/usr/bin/clash
SUB1="https://clashnode.com/wp-content/uploads/$(date -d now +%Y)/$(date -d now +%m)/$(date -d now +%Y%m%d).yaml"
SUB2="https://clashnode.com/wp-content/uploads/$(date -d yesterday +%Y)/$(date -d yesterday +%m)/$(date -d yesterday +%Y%m%d).yaml" 

function logo()
{
    echo -e "\033[96;40m"
    base64 -d <<<"H4sIAAAAAAAAA4WOwQ3AIAwD/0zhX/uoxEJIZpAMXzsBiU9VRyQHOAEgRfyKaM5kIFzMyGD1L/aV
LDJ3pFkFQ2nq+BK6P4onat+0vWuyerp4mxQnr8nD32BkUXqYpmIs9qLNXwr6qVPtBYezLN0OAQAA"|gunzip
    echo -e "\033[0m"

}
function usage()
{
    echo "${SCRIPT_NAME} [clash_config_link]"
    echo "${SCRIPT_NAME} [local_clash_config_file_path]"
    echo "${SCRIPT_NAME} -f [proxy_list_file] #convert proxy list to clash configuration file and connect"
    echo "${SCRIPT_NAME} -f #direct connect previous p2c configuration file"
    echo "${SCRIPT_NAME}    #direct connect inner subscribe  clash link"
    exit -1
}
####################################################################
#convert normal proxy list to clash configuration file
#proxy list foramt,you can combine http and socks5 in one file,one server one line
#recommend socks5 type 
# http ip port
# socks5 ip port
function p2c()
{
cat ${CFG_TEMP}|sed '/^$/d'|awk '{print "      - "$1"_"NR"_"}' > ${NODE_NAME}
cat ${CFG_TEMP}|sed '/^$/d'|awk '{print "  - {name: "$1"_"NR"_, type: "$1", skip-cert-verify: true, server: "$2", port: "$3"}"}' > ${CFG_TEMP_2}
cat <<BOF  > ${CFG_P2C_PATH}
mixed-port: 7891
allow-lan: false
mode: Rule
log-level: info
dns:
  enable: true
  ipv6: false
  nameserver:
    - 127.0.0.1 
  fallback: 
    - 127.0.0.1 
proxies:
BOF
cat ${CFG_TEMP_2} >> ${CFG_P2C_PATH}
cat <<BOF  >> ${CFG_P2C_PATH}
proxy-groups:
  - name: auto
    type: url-test 
    url: 'https://www.gstatic.com/generate/204'
    interval: 300
    proxies:
BOF
cat ${NODE_NAME} >> ${CFG_P2C_PATH}
cat <<BOF  >> ${CFG_P2C_PATH}
rules:
 - GEOIP,CN,DIRECT
 - MATCH,auto
BOF
}
###################################################################
#speicial binary configuration file cleaner

function clean_bin_yml()
{
sed -i 's/外部控制器/external-controller/g' ${CFG_TEMP_2}
sed -i 's/代理组/proxy-groups/g' ${CFG_TEMP_2}
sed -i 's/代理/proxies/g' ${CFG_TEMP_2}
sed -i 's/名称/name/g' ${CFG_TEMP_2}
sed -i 's/服务器/server/g' ${CFG_TEMP_2}
sed -i 's/端口/port/g' ${CFG_TEMP_2}
sed -i 's/类型/type/g' ${CFG_TEMP_2}
sed -i 's/真/true/g' ${CFG_TEMP_2}
sed -i 's/假/false/g' ${CFG_TEMP_2}
sed -i 's/网络/network/g' ${CFG_TEMP_2}
sed -i 's/跳过证书验证/skip-cert-verify/g' ${CFG_TEMP_2}
sed -i 's/验证/verify/g' ${CFG_TEMP_2}
sed -i 's/路径/path/g' ${CFG_TEMP_2}
sed -i 's/标头/headers/g' ${CFG_TEMP_2}
sed -i 's/主机/Host/g' ${CFG_TEMP_2}
sed -i 's/密码:auto/cipher: auto/g' ${CFG_TEMP_2}
sed -i 's/密码:aes/cipher: aes/g' ${CFG_TEMP_2}
sed -i 's/密码/password/g' ${CFG_TEMP_2}
sed -i 's/ :/: /g' ${CFG_TEMP_2}
sed -i 's/:/: /g' ${CFG_TEMP_2}
sed -i 's/,/, /g' ${CFG_TEMP_2}
sed -i 's/&quot;/"/g' ${CFG_TEMP_2}
sed -i '/\"network\":\"http\"/d' ${CFG_TEMP_2}
sed -i '/mrarsha/d' ${CFG_TEMP_2}

#get all node name
cat ${CFG_TEMP_2}|grep ", server:"|awk -F "server:" '{print "      - ""server_"NR"_"}' > ${NODE_NAME}
cat ${CFG_TEMP_2}|grep ",\"type\":"|awk -F "\"type\":" '{print "      - ""server_"NR"_"}' >> ${NODE_NAME}
cat ${CFG_TEMP_2}|grep ", server:"|awk -F "server:" '{print "  - {name: ""server_"NR"_, server: "$2}' > ${CFG_TEMP}
cat ${CFG_TEMP_2}|grep ",\"type\":"|awk -F "\"type\":" '{print "  - {name: ""server_"NR"_, \"type\": "$2}' >> ${CFG_TEMP}
CNT=$(cat ${NODE_NAME}|wc -l)

#special node name
if [ $CNT == 0 ];then
    cat ${CFG_TEMP_2} |grep "\- name: \""|awk -F\" '{print "      - "$2}' > ${NODE_NAME}
    cat ${CFG_TEMP_2} |grep -oE "\"name\":.*"|awk -F\" '{print "      - "$4}' >> ${NODE_NAME}
    ST=$(($(sed -n "/proxies:/=" ${CFG_TEMP_2}|head -1)+1))
    ED=$(($(sed -n "/proxy-groups:/=" ${CFG_TEMP_2}|head -1)-1))
    sed -n ${ST},${ED}p ${CFG_TEMP_2} > ${CFG_TEMP}
fi

#get unsupport plugin node

cat ${CFG_TEMP}|grep "v2ray-plugin"|awk -F, '{print $1}'|awk -F: '{print $2}' > ${DEL_NODE}
cat ${CFG_TEMP}|grep "microsoft.com"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "cloudflare"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "singapore.com"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "127.0.0.1"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "::ffff"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "vless"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "sni:"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}
cat ${CFG_TEMP}|grep "argo-vmess?"|awk -F, '{print $1}'|awk -F: '{print $2}' >> ${DEL_NODE}

cat <<BOF  > ${CFG_PATH}
mixed-port: 7891
allow-lan: false
mode: Rule
log-level: info
dns:
  enable: true
  ipv6: false
  nameserver:
    - 127.0.0.1 
  fallback: 
    - 127.0.0.1 
proxies:
BOF
cat ${CFG_TEMP} >> ${CFG_PATH}
cat <<BOF  >> ${CFG_PATH}
proxy-groups:
  - name: auto
    type: url-test 
    url: 'https://www.gstatic.com/generate/204'
    interval: 300
    proxies:
BOF
cat ${NODE_NAME} >> ${CFG_PATH}
cat <<BOF  >> ${CFG_PATH}
rules:
  - GEOIP,CN,DIRECT
  - MATCH,auto
BOF


#delete unsupport node
cat ${DEL_NODE}|xargs -I {} sh -c "sed -i \"/{}/d\" ${CFG_PATH}"
sed -i '/^ *- *$/d' ${CFG_PATH}
}

###################################################################
clear
logo
if [ $# == 0 ]; then
    echo "connect inner subscribe server..."
    echo -e "^C:\033[96;40mNext Subscribe\033[0m"
    ${CLASH_PATH} -f ${CFG_PATH}
    for i in {1..2};do
        clear
        logo
        SUB=`eval echo '$'"SUB"$i`
        echo -e "Subscribe Link:\033[96;40m${SUB}\033[0m"
        echo -e "^C:\033[96;40mNext Subscribe\033[0m"
        rm /tmp/clash.yml 2>/dev/null 
        aria2c --connect-timeout=3 ${SUB} --dir=/tmp --out=clash.yml -q
        strings -s "" --include-all-whitespace ${CFG_TEMP} > ${CFG_TEMP_2} 2>/dev/null && clean_bin_yml && ${CLASH_PATH} -f ${CFG_PATH} 
	sleep 1
    done
    exit -1
elif [ $# == 1 ]; then
    if [ ${1} == '-f' ] && [ -f "${CFG_P2C_PATH}" ]; then
        echo "connect p2c config file..."
        ${CLASH_PATH} -f ${CFG_P2C_PATH}
        exit -1
    elif [[ "${1}" =~ ^(http(s)?:\/\/).* ]]; then
        echo "connect clash config link..."
        rm /tmp/clash.yml 2>/dev/null
        aria2c --connect-timeout=3 ${1} --dir=/tmp --out=clash.yml -q
        strings -s "" --include-all-whitespace ${CFG_TEMP} > ${CFG_TEMP_2} 2>/dev/null && clean_bin_yml && ${CLASH_PATH} -f ${CFG_PATH} 
        exit -1
    elif [ -n "${1}" ] && [ -f "${1}" ]; then
        echo "connect local clash file..."
        strings -s "" --include-all-whitespace ${1} > ${CFG_TEMP_2}
        clean_bin_yml
        ${CLASH_PATH} -f ${CFG_PATH} 
        exit -1
    else
        usage
    fi
elif [ $# == 2 ] && [ ${1} == '-f' ]; then
    if [ -n "${2}" ] && [ -f "${2}" ]; then
        echo "convert proxy list to clash config..."
        CFG_TEMP="${2}"
        p2c
        echo "connect p2c config file..."
        ${CLASH_PATH} -f ${CFG_P2C_PATH}
        exit -1
    else
        usage
    fi
else
    usage
fi