alias decodeURIComponent="sed 's/%/\\\\x/g' xargs -0 printf '%b'"
encodeURIComponent() { local data=$1 if [ -n "$1" ]; then local data="$(echo $data curl -Gso /dev/null -w %{url_effective} --data-urlencode @- "" sed -E 's/..(.*).../\1/')" else echo "Usage: encodeURIComponent " fi echo "${data##/?}" }
get_json_value() { local json=$1 local key=$2 if [ -z "$3" ]; then local num=1 else local num=$3 fi local value=$(echo "${json}" awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'${key}'\042/){print $(i+1)}}}' tr -d '"' sed -n "${num}"p) echo "${value}" }
reflush_TIME() { TIME_CUR=$(date +%s) TIME=$(date '+%Y-%m-%d %H:%M:%S') } check_SHOULD_STOP() { reflush_TIME TIME_STOP=$(date -d "$(date '+%Y-%m-%d 23:15:00')" +%s) if [ $TIME_CUR -gt $TIME_STOP ]; then if [ "${portServIncludeFailedCode}" = "63027" ]; then SHOULD_STOP=true else SHOULD_STOP=false fi else SHOULD_STOP=false if [ "${portServIncludeFailedCode}" = "63027" ]; then SLEEP_TIME="60" else SLEEP_TIME="1" fi fi } reflush_CONNECT_TIME() { CONNECT_TIME=$(date +%s) reflush_TIME } check_connect() { if [ $(curl -sI -w "%{http_code}" -o /dev/null connect.rom.miui.com/generate_204) = 204 ]; then CONNECT=true else CONNECT=false fi } doHeartBeat() { echo "Start do HeartBeat ${TIME}" userDevPort=$(get_json_value $v_json userDevPort) userDevPort_ENCODEURL=$(encodeURIComponent $userDevPort) userStatus=$(get_json_value $v_json userStatus) serialNo=$(get_json_value $v_json serialNo) v_Language=$(get_json_value $v_json clientLanguage)
if [ "${v_errorNumber}" = "1" ]; then #认证成功 reflush_CONNECT_TIME portalLink=$(get_json_value "${JSON}" portalLink) v_jsonStr=${portalLink} echo "Login Success "${TIME}""
if [ ! -n "${v_jsonStr}" ]; then #解码失败 echo "ERROR: portalLink DEBASE64 error" start_auth fi
portalLink_ENCODEURL_DEBASE64="$(printf "%s" "${v_jsonStr}" base64 -d)" v_json=$(echo $portalLink_ENCODEURL_DEBASE64 decodeURIComponent) if [ ! -n "${v_json}" ]; then #解码失败 echo "ERROR: portalLink DecodeURL error" start_auth fi #echo v_json: "${v_json}" #debug ifNeedModifyPwd=$(get_json_value $v_json ifNeedModifyPwd) if [ "${ifNeedModifyPwd}" = true ]; then #要求修改密码 echo Need Modify Pwd
fi
heartBeatCyc=$(get_json_value $v_json heartBeatCyc) if [ "$heartBeatCyc" -gt 0 ]; then #要求心跳 requires_heartBeat=true heartBeatCyc_TRUE=$(expr $heartBeatCyc / 1000) SLEEP_TIME=$(expr $heartBeatCyc_TRUE / 2 - 1) echo "The connection requires a heartbeat every ${heartBeatCyc_TRUE} seconds. Please do not terminate the script." #doHeartBeat #debug else requires_heartBeat=false fi portServIncludeFailedCode="" portServErrorCode=""
elif [ -n "${portServErrorCode}" ]; then echo Error: "${v_errorInfo}" SLEEP_TIME="1" if [ "${portServErrorCode}" = 2 ]; then reflush_CONNECT_TIME elif [ "${portServErrorCode}" = 3 ]; then start_auth
else #未知错误 echo "Unknown error, login failed." exit fi fi fi fi } start_auth while [ true ]; do check_connect if [ "$CONNECT" = true ]; then reflush_TIME TMP=$(($TIME_CUR - $CONNECT_TIME)) if [ "${requires_heartBeat}" = true ]; then if [ $TMP -gt "$heartBeatCyc_TRUE" ]; then doHeartBeat fi fi elif [ "$CONNECT" = false ]; then check_SHOULD_STOP if [ "$SHOULD_STOP" = true ]; then break elif [ "$SHOULD_STOP" = false ]; then echo Reconnecting start_auth fi fi sleep $SLEEP_TIME done