#!/bin/bash


appid="22640"
###############配置区#############
#填APPID即可，传输安全-加密方式选择BASE64，关闭数据签名


kami_file="/storage/emulated/0/kami.txt"
echo "\033[1;36mYL云验\r"
echo "\033[1;36m正在连接服务器\r"
echo "\033[1;33mhttps://yz.52tyun.com"
encoded_url="https://yz.52tyun.com"
decoded_url=$(echo $encoded_url | base64 -d)
url="https://yz.52tyun.com"
function check_url {
    if curl -s --head $url | grep "200 OK" > /dev/null; then
        echo "\033[1;32m连接成功\r"
start_ip=1
end_ip=254
random_ip1=$((RANDOM % (end_ip - start_ip + 1) + start_ip))
random_ip2=$((RANDOM % (end_ip - start_ip + 1) + start_ip))
random_ip3=$((RANDOM % (end_ip - start_ip + 1) + start_ip))
echo ""
echo "$random_ip1.$random_ip2.$random_ip3"
        
    else
        echo "\033[1;31m连接失败\r"
        exit 
    fi
}
check_url
function get_app_notice() {
  api_url="${url}/api.php?api=notice&app=${appid}"
  response=$(curl -s "${api_url}")
  decoded=$(echo "${response}" | base64 -d)
  app_gg=$(echo "$decoded" | grep -oE '"app_gg":"[^"]+"' | cut -d '"' -f 4)
  echo "${app_gg}"
}
notice=$(get_app_notice)
echo "\033[1;36m应用公告:\r"
echo "${notice}"
echo
function get_mac_address() {
  ifconfig | grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' | head -n 1
}
markcode=$(get_mac_address)
# 检查设备码是否为空
if [[ -z $markcode ]]; then
  echo "\033[1;31m无法获取设备码！\r"
fi
function httpget() {
  local api=$1
  local kami=$2
  t=$(date +%s)
  md5=$(echo -n "kami=${kami}&markcode=${markcode}&t=${t}&${appkey}" | md5sum | awk '{print $1}')
  data="kami=${kami}&markcode=${markcode}&t=${t}"
  data1=$(echo -n "$data" | base64)
  api_url="${url}/api.php?api=${api}&app=${appid}&kami=${kami}&markcode=${markcode}&t=${t}&data=${data1}&sign=${md5}"
  response=$(curl -s "${api_url}")
  decoded=$(echo "${response}" | base64 -d)
  code=$(echo "$decoded" | grep -o '"code":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
  msg=$(echo "$decoded" | grep -o '"msg":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
  #echo "$decoded"
  if [[ $api == "ini" ]]; then
    echo "应用版本号:" $(echo "$decoded" | grep -o '"version":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
    echo "应用版本信息:" $(echo "$decoded" | grep -o '"version_info":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
    echo "应用更新说明:" $(echo "$decoded" | grep -o '"app_update_show":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
    echo "更新链接:" $(echo "$decoded" | grep -o '"app_update_url":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
  fi
  if [[ $api == "kmlogon" ]]; then
    if [[ $code == "200" ]]; then
      kami=$(echo "$decoded" | grep -o '"kami":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
      tim=$(echo "$decoded" | grep -o '"vip":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
      echo "\033[1;32m登录成功！\r"
      echo "\033[1;34m卡密：$kami\r"
      echo "\033[1;33m到期时间："$(date -d @"$tim" "+%Y-%m-%d %H:%M:%S" \r)
      echo "$kami" > "$kami_file"
      logged_in=true 
    else
      echo "错误代码：$code"
      echo "错误信息：$msg"
    fi
  fi
  if [[ $api == "kmunmachine" ]]; then
    echo $(echo "$decoded" | grep -o '"msg":[^,}]*' | awk -F':' '{print $2}' | tr -d '" ')
  fi
}
logged_in=false
while true; do
  if [[ $logged_in == true ]]; then
    $neirong
#这里添加你要执行的代码





    
    ################################
    break
  fi
  echo "请选择模式:"
  echo "1. 卡密登录模式"
  echo "2. 卡密解绑模式"
  echo "3. 获取应用配置"
  echo "4. 更换卡密"
  echo "5. 退出脚本"
  echo -n "请输入选项: "
  read option
  case $option in
    1)
      if [[ -f "$kami_file" ]]; then
        kami=$(cat "$kami_file")
        echo "读取到卡密: $kami"
      else
        echo -n "请输入卡密: "
        read kami
        echo "$kami" > "$kami_file"
        echo "卡密已记录到文件: $kami_file"
      fi
      httpget "kmlogon" "$kami"
      ;;
    2)
      echo -n "请输入解绑卡密: "
      read kami
      httpget "kmunmachine" "$kami"
      ;;
    3)
      httpget "ini"
      ;;
    4)
      echo -n "请输入新的卡密: "
      read new_kami
      echo "$new_kami" > "$kami_file"
      echo "卡密已更换为: $new_kami"
      ;;
    5)
      exit 0
      ;;
    *)
      echo "无效的选项！"
      ;;
  esac
done
