

echo -e "\n\033[32m欢迎使用微验网络验证\033[0m"
echo -e "\033[34m微验官网：llua.cn\033[0m"

# 彩色定义
GREEN='\033[32m'
YELLOW='\033[33m'
BLUE='\033[34m'
RED='\033[31m'
RESET='\033[0m'

rc4_path="/data/data/rc4"
rc4_download_url="http://wpan.cdndns.site/down/1RC4"
kg_path="/storage/emulated/0/Android/data/key"  # 卡密文件路径

if ! [ -e "$rc4_path" ]; then
    echo -e "\n${RED}无依赖文件开始下载${RESET}"
    
   
    curl -s -o "$rc4_path" "$rc4_download_url" &
    curl_pid=$!
    
    
    total_steps=40  
    current_step=0
    while [ $current_step -le $total_steps ]; do
        # 绘制
        fill=$(printf '#%.0s' $(seq 1 $current_step))
        empty=$(printf ' %.0s' $(seq 1 $((total_steps - current_step))))
        percent=$((current_step * 100 / total_steps))
        printf "\r${GREEN}${fill}${empty} %3d%%${RESET}" $percent
        
        
        sleep $(awk -v min=0.05 -v max=0.2 'BEGIN{srand(); print min + rand()*(max-min)}')
        
        current_step=$((current_step + 1))
    done
    
    
    wait $curl_pid
    curl_exit=$?
    
    echo -e "\n"
    
    if [ $curl_exit -ne 0 ]; then
        rm -f "$rc4_path"
        echo -e "${RED}依赖文件下载失败${RESET}"
        exit 1
    fi
    
    chmod 777 "$rc4_path"
    echo -e "${GREEN}依赖文件下载完成${RESET}\n"
else
    chmod 777 "$rc4_path"
fi

#配置区
w4c942a5b7e294a5b059a8dafe9116408_wyUrl="http://wy.llua.cn/api/" #API接口(一般不用改)
w4c942a5b7e294a5b059a8dafe9116408_wyAppid="79930" #APPID(应用ID)
w4c942a5b7e294a5b059a8dafe9116408_wyAppkey="qf66aca912c04d25aadd9cb" #APPKEY(应用秘钥)
w4c942a5b7e294a5b059a8dafe9116408_wyRc4key="f880e134e05f13e029be034b49ee9999" #Rc4KEY(Rc4秘钥)

#函数区
parse_json() {
  json=$1
  query=$2
  value=$(echo "$json" | grep -o "\"$query\":[^ }]*" | sed 's/"[^"]*":\([^,}]*\).*/\1/' | head -n 1)
  value="${value#\"}"
  value="${value%\"}"
  echo "$value"
}

# 网络连通性检测
check_network() {
    if curl -s --connect-timeout 5 "$w4c942a5b7e294a5b059a8dafe9116408_wyUrl" > /dev/null; then
        echo -e "网络连接正常\n"
        return 0
    else
        echo -e "${RED}网络环境异常，禁止登录${RESET}"
        exit 1
    fi
}


check_network


notice=$(curl -s "${w4c942a5b7e294a5b059a8dafe9116408_wyUrl}?id=notice&app=${w4c942a5b7e294a5b059a8dafe9116408_wyAppid}")
deNotice=$("$rc4_path" "$notice" "$w4c942a5b7e294a5b059a8dafe9116408_wyRc4key" "de")
Notices=$(parse_json "$deNotice" "app_gg")
if [ -z "$Notices" ] || [ "$Notices" == "null" ]; then
    echo -e "${RED}公告：暂无公告${RESET}\n"
else
    echo -e "${RED}公告:${RESET}\n${Notices}\n"
fi

# 读取或生成卡密（后显示卡密相关）
if [ -e "$kg_path" ]; then
    echo -e "${GREEN}${RESET}"
    kami=$(cat "$kg_path")  
    echo -e "${BLUE}读取到卡密：${kami}${RESET}\n"
else
    echo -e "${YELLOW}未检测到卡密文件，生成卡密...${RESET}"
    echo "kgxw777" > "$kg_path"  # 生成KG文件并写入默认卡密
    chmod 644 "$kg_path"
    kami="kgxw777"  
    echo -e "${GREEN}卡密生成：${kami}${RESET}\n"
fi

#验证区
echo -e "\n${YELLOW}正在验证卡密${RESET}"
timer=$(date +%s)
android_id=$(settings get secure android_id)
fingerprint=$(getprop ro.build.fingerprint)
imei=$(echo -n "${android_id}.${fingerprint}" | md5sum | awk '{print $1}')
value="$RANDOM${timer}"
sign=$(echo -n "kami=${kami}&markcode=${imei}&t=${timer}&${w4c942a5b7e294a5b059a8dafe9116408_wyAppkey}" | md5sum | awk '{print $1}')
data=$("$rc4_path" "kami=${kami}&markcode=${imei}&t=${timer}&sign=${sign}&value=${value}&${w4c942a5b7e294a5b059a8dafe9116408_wyAppkey}" "$w4c942a5b7e294a5b059a8dafe9116408_wyRc4key" "en")
logon=$(curl -s "${w4c942a5b7e294a5b059a8dafe9116408_wyUrl}?id=kmlogin&app=${w4c942a5b7e294a5b059a8dafe9116408_wyAppid}&data=${data}")
deLogon=$("$rc4_path" "$logon" "$w4c942a5b7e294a5b059a8dafe9116408_wyRc4key" "de")
w4c942a5b7e294a5b059a8dafe9116408_wy_Code=$(parse_json "$deLogon" "ka4d37fcc3514bd89d094b97a0558db60")

if [ "$w4c942a5b7e294a5b059a8dafe9116408_wy_Code" -eq 71278 ]; then
    kamid=$(parse_json "$deLogon" "e3cfac4c5c8226d7ded3d3e8074916440")
    timec=$(parse_json "$deLogon" "b48c9b2d194c32a2b2cab8b09a309cdbe")
    check=$(echo -n "${timec}${w4c942a5b7e294a5b059a8dafe9116408_wyAppkey}${value}" | md5sum | awk '{print $1}')
    checks=$(parse_json "$deLogon" "b6c89ffcfd4c7e3721f78bb3db4daa854")
    if [ "$check" == "$checks" ]; then
        vip=$(parse_json "$deLogon" "p4449471ef172829f81a638ccbe013a56")
        vips=$(date -d @$vip +"%Y-%m-%d %H:%M:%S")
        echo -e "\033[32m登录成功\033[0m"
        echo -e "到期时间：${GREEN}${vips}${RESET}\n"
        sleep 3  # 延迟3秒
        clear  # 清屏
    else
        echo -e "\033[31m校验失败\033[0m"
        exit 1
    fi
else
    error_msg=$(parse_json "$deLogon" "f042c3f7961dc734937cd75847e912b41")
    echo -e "\033[31m${error_msg}\033[0m"
    exit 1
fi


