#!/bin/bash

clear
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
PLAIN='\033[0m'
BLUE="\033[36m"

SUPPORT_COUNTRY=(AL DZ AD AO AG AR AM AU AT AZ BS BD BB BE BZ BJ BT BO BA BW BR BN BG BF CV CA CL CO KM CG CR CI HR CY CZ DK DJ DM DO EC SV EE FJ FI FR GA GM GE DE GH GR GD GT GN GW GY HT VA HN HU IS IN ID IQ IE IL IT JM JP JO KZ KE KI KW KG LV LB LS LR LI LT LU MG MW MY MV ML MT MH MR MU MX FM MD MC MN ME MA MZ MM NA NR NP NL NZ NI NE NG MK NO OM PK PW PS PA PG PY PE PH PL PT QA RO RW KN LC VC WS SM ST SN RS SC SL SG SK SI SB ZA KR ES LK SR SE CH TW TZ TH TL TG TO TT TN TR TV UG UA AE GB US UY VU ZM)

UA_Browser="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"
curlArgs="-sS --max-time 10"

testTime=$(date +"%Y-%m-%d %H:%M:%S")

echo -e "${BLUE}-------------------------------------${PLAIN}"
echo -e "${GREEN}测试时间：${testTime}${PLAIN}"
echo -e "${BLUE}-------------------------------------${PLAIN}"

# Check if jq is installed, if not, install it
if ! command -v jq &>/dev/null; then
    echo -e "${GREEN}Installing jq${PLAIN}"
    if [ -x "$(command -v apt)" ]; then
        apt update >/dev/null 2>&1
        apt install jq -y >/dev/null 2>&1
    elif [ -x "$(command -v yum)" ]; then
        yum makecache >/dev/null 2>&1
        yum install jq -y >/dev/null 2>&1
    elif [ -x "$(command -v pkg)" ]; then
        pkg update -y >/dev/null 2>&1
        pkg install jq -y >/dev/null 2>&1
    elif [ -x "$(command -v brew)" ]; then
        brew install jq >/dev/null 2>&1
    else
        echo -e "${RED}无法安装jq,请手动安装!${PLAIN}"
        exit 1
    fi
fi

MediaUnlockTest_ChatGPT() {
    local tmpresult=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsLI --max-time 10 "https://chat.openai.com" 2>&1)
    local tmpresult1=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsL --max-time 10 "https://ios.chat.openai.com" 2>&1)
    local cf_details=$(echo "$tmpresult1" | jq .cf_details)
    if [[ "$tmpresult" == "curl"* ]]; then
        echo -n -e "\r${RED}ChatGPT:\t失败 (网络连接)${PLAIN}\n"
        return
    fi

    local result1=$(echo "$tmpresult" | grep 'location' )
    if [ ! -n "$result1" ]; then
        if [[ "$tmpresult1" == *"blocked_why_headline"* ]]; then
            echo -n -e "\r${RED}ChatGPT:\t不可用 (已阻止)${PLAIN}\n"
            return
        fi
        if [[ "$cf_details" == *"(1)"* ]]; then
            echo -n -e "\r${RED}ChatGPT:\t不可用 (禁止 ISP[1])${PLAIN}\n"
            return
        fi
        if [[ "$cf_details" == *"(2)"* ]]; then
            echo -n -e "\r${RED}ChatGPT:\t不可用 (禁止 ISP[2])${PLAIN}\n"
            return
        fi
        echo -n -e "\r${RED}ChatGPT:\t不可用${PLAIN}\n"
    else
        local region1=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsL --max-time 10 "https://chat.openai.com/cdn-cgi/trace" 2>&1 | grep "loc=" | awk -F= '{print $2}')
        if [[ "$cf_details" == *"(1)"* ]]; then
            echo -n -e "\r${YELLOW}ChatGPT:\t仅限 Web 使用 (禁止 ISP[1])${PLAIN}\n"
            return
        fi
        if [[ "$cf_details" == *"(2)"* ]]; then
            echo -n -e "\r${YELLOW}ChatGPT:\t仅限 Web 使用 (禁止 ISP[2])${PLAIN}\n"
            return
        fi
        echo -n -e "\r${GREEN}ChatGPT:\t可用 (地区: ${region1})${PLAIN}\n"
    fi
}

AIUnlockTest_Gemini_location() {
    local tmp=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsL --max-time 10 'https://gemini.google.com/_/BardChatUi/data/batchexecute'   -H 'accept-language: en-US'   --data-raw 'f.req=[[["K4WWud","[[0],[\"en-US\"]]",null,"generic"]]]' 2>&1)
    if [[ "$tmp" == "curl"* ]]; then
        echo -n -e "\r${RED}Gemini:\t失败 (网络连接)${PLAIN}\n"
        return
    fi
    local region=$(echo "$tmp" | grep K4WWud | jq .[0][2] | grep -Eo '\[\[\\"(.*)\\",\\"S' )
    echo -n -e "\r${YELLOW}Gemini:\t\t(地区: ${region:4:-6})${PLAIN}\n"
}

AIUnlockTest_Copilot() {
    local tmp=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsL --max-time 10 "https://copilot.microsoft.com/" 2>&1)
    local tmp2=$(curl $curlArgs -${1} --user-agent "${UA_Browser}" -SsL --max-time 10 "https://copilot.microsoft.com/turing/conversation/chats?bundleVersion=1.1342.3-cplt.12"  2>&1)
    if [[ "$tmp" == "curl"* ]]; then
        echo -n -e "\r${RED}Copilot:\t失败 (网络连接)${PLAIN}\n"
        return
    fi
    local result=$(echo "$tmp2" | jq .result.value  2>&1 | tr -d '"' 2>&1) 
    local region=$(echo "$tmp" | sed -n 's/.*RevIpCC:"\([^"]*\)".*/\1/p' )
    if [[ "$result" == "Success" ]];then
        echo -n -e "\r${GREEN}Copilot:\t可用 (地区: ${region^^})${PLAIN}\n"
    else 
        echo -n -e "\r${RED}Copilot:\t不可用  (地区: ${region^^})${PLAIN}\n"
    fi
}

if [[ $(curl -sS https://chat.openai.com/ -I | grep "text/plain") != "" ]]; then
    echo "您的 IP 已被封锁！"
else
    check4=`ping 1.1.1.1 -c 1 2>&1`;
    if [[ "$check4" != *"received"* ]] && [[ "$check4" != *"transmitted"* ]]; then
        echo -e "${BLUE}IPv4 不受当前主机支持。跳过...${PLAIN}";
    else
    	echo -e "${BLUE}[IPv4]$(echo -n -e "\t\t")$(hostname -I | awk '{print $1}')${PLAIN}"
        MediaUnlockTest_ChatGPT "4"
        AIUnlockTest_Gemini_location "4"
        AIUnlockTest_Copilot "4"
    fi

    echo -e "${BLUE}-------------------------------------${PLAIN}"
    check6=`ping6 240c::6666 -c 1 2>&1`;
    if [[ "$check6" != *"received"* ]] && [[ "$check6" != *"transmitted"* ]]; then
        echo -e "${BLUE}IPv6 不受当前主机支持。跳过...${PLAIN}";    
    else
    	echo -e "${BLUE}[IPv6]$(echo -n -e "\t\t")$(hostname -I | awk '{print $2}')${PLAIN}"
        MediaUnlockTest_ChatGPT "6"
        AIUnlockTest_Gemini_location "6"
        AIUnlockTest_Copilot "6"
    fi

    echo -e "${BLUE}-------------------------------------${PLAIN}"
fi
