Files
kami_itunes_june/AppleBatch_June.ExecuteTasks/ActivateiCloudTask.cs
2024-07-22 00:43:14 +08:00

504 lines
16 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using AppleBatch_June.AppleWebServace;
using AppleBatch_June.Model;
using AppleBatch_June.SMS;
using AppleBatch_June.StartTaskModel;
using DotNet.Utilities;
namespace AppleBatch_June.ExecuteTasks
{
public class ActivateiCloudTask : BaseTask
{
private static readonly object lockActClientObj;
private readonly Dictionary<string, object> actClientLockObjs = new Dictionary<string, object>();
public static ActivateiCloudTask Instance { get; private set; }
public static ActivateiCloudTask InstanceSub { get; private set; }
public int nasType { get; set; } = 1;
private ActiotiCloudConfig baseCloudConfig { get; set; }
private Action<string, DisplyType, string> baseAction { get; set; }
static ActivateiCloudTask()
{
Instance = null;
InstanceSub = null;
lockActClientObj = new object();
Instance = new ActivateiCloudTask("激活iCloud");
InstanceSub = new ActivateiCloudTask("激活iCloudSub");
}
public ActivateiCloudTask(string _taskName)
: base(_taskName)
{
}
public void ExecuteLeaveFamily(int TaskSize, bool openVpn, ActiotiCloudConfig cloudConfig, List<AppleAcount> lvItems, Action startAct, Action endTakAct, Action<string> sucessAct, Action<string, DisplyType, string> _action)
{
TaskSize = Tools.getTaskSize(openVpn, lvItems.Count);
if (!base.isRun)
{
AppleExecuteTask.TaskRunAppleIdCount = 0L;
AppleExecuteTask.TaskRunModels.Clear();
startAct?.Invoke();
base.isRun = true;
base.TaskIsRun = true;
base.baseEndTakAct = endTakAct;
Task.Run(delegate
{
ApiNetReq.startNowFun = 30;
TaskSize = 4;
Task[] array = new Task[TaskSize];
List<Task> openAuthActionTask = new List<Task>();
for (int i = 0; i < TaskSize; i++)
{
array[i] = Task.Run(delegate
{
string guid = Guid.NewGuid().ToString("n");
TaskRunModel taskRunModel = new TaskRunModel
{
guid = guid,
isRuning = false
};
AppleExecuteTask.TaskRunModels.Add(taskRunModel);
while (base.isRun)
{
AppleAcount listView = null;
try
{
lock (lvItems)
{
listView = lvItems.Where((AppleAcount c) => !c.isAct).FirstOrDefault();
if (listView == null)
{
break;
}
listView.isAct = true;
}
if (BaseTask.startRuning(taskRunModel, _action, listView.appleId))
{
int type = 40;
iCloundUtils icloud = new iCloundUtils(this, listView.appleId, _action, openVpn);
if (icloud.getIsLogin(40, _action))
{
_action?.Invoke(listView.appleId, DisplyType.chongzhi, "");
icloud.appleId = listView.appleId;
NasQualifyModel nasQualifyInfo = icloud.GetNasQualifyInfo(nasType);
bool flag = false;
if (nasQualifyInfo != null && !string.IsNullOrEmpty(nasQualifyInfo.nasQualify) && !string.IsNullOrEmpty(nasQualifyInfo.clientInfo) && !string.IsNullOrEmpty(nasQualifyInfo.userAgent))
{
string text = icloud.GetKeyValue("<key>client-id</key>", "<string>", "</string>", nasQualifyInfo.postData);
if (string.IsNullOrEmpty(text))
{
text = "9B134D07-1389-4872-A249-9DF756337B97";
}
lock (lockActClientObj)
{
if (!actClientLockObjs.ContainsKey(text))
{
actClientLockObjs.Add(text, new object());
}
}
string reflocalizedError = "";
bool flag2 = false;
lock (actClientLockObjs[text])
{
flag2 = icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref reflocalizedError);
}
if (!flag2)
{
int num = 0;
while ((reflocalizedError == "MOBILEME_CREATE_UNAVAILABLE" || reflocalizedError == "MOBILEME_TERMS_OF_SERVICE_UPDATE" || string.IsNullOrEmpty(reflocalizedError)) && base.isRun && num <= 5)
{
num++;
nasQualifyInfo = icloud.GetNasQualifyInfo(nasType);
if (nasQualifyInfo != null && !string.IsNullOrEmpty(nasQualifyInfo.nasQualify) && !string.IsNullOrEmpty(nasQualifyInfo.clientInfo) && !string.IsNullOrEmpty(nasQualifyInfo.userAgent))
{
if (flag2 = icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref reflocalizedError))
{
ApiApplyAct(type, "激活iCloud");
reflocalizedError = "Success";
break;
}
Thread.Sleep(1000);
if (reflocalizedError == "MOBILEME_CREATE_UNAVAILABLE" || reflocalizedError == "MOBILEME_TERMS_OF_SERVICE_UPDATE" || (string.IsNullOrEmpty(reflocalizedError) && base.isRun && num <= 5))
{
_action?.Invoke(listView.appleId, DisplyType.xinxi, "重新激活..." + num);
Thread.Sleep(1000);
}
}
}
if (reflocalizedError == "ACCOUNT_INVALID_HSA_TOKEN" || reflocalizedError == "HSA2_NEEDS_2FA_AUTH" || (reflocalizedError == "ACCOUNT_UNAVAILABLE" && cloudConfig.isOpenAuthAction))
{
AppleManageWeb webUtlis = new AppleManageWeb(openVpn, _action, _action, this);
baseCloudConfig = cloudConfig;
baseAction = _action;
flag = true;
openAuthActionTask.Add(Task.Run(delegate
{
try
{
if (reflocalizedError == "ACCOUNT_UNAVAILABLE" && AuthActionTask(webUtlis, listView, cloudConfig))
{
Thread.Sleep(5000);
AutoActioniCloud(icloud, listView, forgotAttempt: false, nasQualifyInfo);
}
if (reflocalizedError == "ACCOUNT_INVALID_HSA_TOKEN" || reflocalizedError == "HSA2_NEEDS_2FA_AUTH")
{
AutoActioniCloud(icloud, listView, forgotAttempt: false, nasQualifyInfo);
}
}
catch (Exception ex3)
{
APIUtlis.ApiSeriveError(ex3);
_action?.Invoke(listView.appleId, DisplyType.xinxi, ex3.Message);
}
finally
{
sucessAct?.Invoke(listView.appleId);
}
}));
}
}
}
else
{
_action?.Invoke(listView.appleId, DisplyType.xinxi, "数据加载失败..");
}
if (!flag)
{
sucessAct?.Invoke(listView.appleId);
}
}
goto IL_07d3;
}
}
catch (Exception ex2)
{
APIUtlis.ApiSeriveError(ex2);
if (listView != null)
{
_action?.Invoke(listView.appleId, DisplyType.xinxi, "请求失败:" + ex2.Message);
}
goto IL_07d3;
}
continue;
IL_07d3:
BaseTask.endRuning(taskRunModel);
}
});
}
Task.WaitAll(array);
Task.WaitAll(openAuthActionTask.ToArray());
try
{
Task[] array2 = array;
foreach (Task task in array2)
{
if (task.Status == TaskStatus.RanToCompletion)
{
task.Dispose();
}
}
array = null;
foreach (Task item in openAuthActionTask)
{
item.Dispose();
}
openAuthActionTask = null;
}
catch (Exception)
{
}
AppleExecuteTask.TaskRunModels.Clear();
base.TaskIsRun = false;
base.isRun = false;
endTakAct?.Invoke();
});
}
else
{
endTakAct?.Invoke();
}
}
public bool AuthActionTask(AppleManageWeb webUtlis, AppleAcount listView, ActiotiCloudConfig _cloudConfig)
{
int num = 0;
while (true)
{
if (num < _cloudConfig.openNumber)
{
if (base.isRun)
{
listView.moblie = "18888888888";
webUtlis.listCookie.Clear();
webUtlis.ProxyIp = "";
listView.isAccountException = false;
if (webUtlis.OpenAuthen(listView, new AppleChangeItem
{
countryCode = _cloudConfig.smsCountry,
mode = "sms"
}, actGetCode, listView.appleId))
{
return true;
}
if (listView.isHsa2)
{
return true;
}
if (listView.isAccountException)
{
return false;
}
if (_cloudConfig.smsService == null)
{
break;
}
}
num++;
continue;
}
return false;
}
return false;
}
public bool AutoActioniCloud(iCloundUtils icloud, AppleAcount listView, bool forgotAttempt, NasQualifyModel nasQualifyInfo)
{
string moblie = listView.moblie;
if (!string.IsNullOrEmpty(moblie))
{
if (nasQualifyInfo != null && !string.IsNullOrEmpty(nasQualifyInfo.nasQualify) && !string.IsNullOrEmpty(nasQualifyInfo.clientInfo) && !string.IsNullOrEmpty(nasQualifyInfo.userAgent))
{
string text = icloud.GetKeyValue("<key>client-id</key>", "<string>", "</string>", nasQualifyInfo.postData);
if (string.IsNullOrEmpty(text))
{
text = "9B134D07-1389-4872-A249-9DF756337B97";
}
string localizedError = "";
CookieItem cookieItem = icloud.listCookie.Where((CookieItem c) => c.Key == "repairSteps").FirstOrDefault();
if (cookieItem == null)
{
if (icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref localizedError))
{
return true;
}
if (localizedError == "MOBILEME_CREATE_UNAVAILABLE" || localizedError == "MOBILEME_UNSUPPORTED_DEVICE" || string.IsNullOrEmpty(localizedError))
{
int num = 0;
do
{
nasQualifyInfo = icloud.GetNasQualifyInfo(nasType);
if (!icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref localizedError))
{
Thread.Sleep(2000);
num++;
continue;
}
return true;
}
while ((localizedError == "MOBILEME_CREATE_UNAVAILABLE" || localizedError == "MOBILEME_UNSUPPORTED_DEVICE" || string.IsNullOrEmpty(localizedError)) && num < 5);
}
if (localizedError != "ACCOUNT_INVALID_HSA_TOKEN" && localizedError != "HSA2_NEEDS_2FA_AUTH")
{
return false;
}
}
string errmess = "";
string icMsmCode = "";
if (icloud.iCloudVerifyauthen(listView.appleId, listView.applePwd, moblie, nasQualifyInfo, actGetCode, ref errmess, ref icMsmCode))
{
if (localizedError != "HSA2_NEEDS_2FA_AUTH")
{
icMsmCode = "";
}
if (!icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref localizedError, isAgree: false, icMsmCode))
{
for (int i = 0; i < 5; i++)
{
if (!(localizedError == "MOBILEME_CREATE_UNAVAILABLE") && !(localizedError == "MOBILEME_UNSUPPORTED_DEVICE") && !string.IsNullOrEmpty(localizedError))
{
break;
}
nasQualifyInfo = icloud.GetNasQualifyInfo(nasType);
if (nasQualifyInfo != null && !string.IsNullOrEmpty(nasQualifyInfo.nasQualify) && !string.IsNullOrEmpty(nasQualifyInfo.clientInfo) && !string.IsNullOrEmpty(nasQualifyInfo.userAgent) && icloud.ActioniCloud(listView.appleId, listView.applePwd, nasQualifyInfo.nasQualify, nasQualifyInfo.clientInfo, text, ref localizedError, isAgree: false, icMsmCode))
{
break;
}
}
}
if (!string.IsNullOrEmpty(baseCloudConfig.ActivaNewPwd))
{
AppleiForgotWeb appleiForgotWeb = new AppleiForgotWeb(icloud.openVpn, baseAction, null, this);
bool flag = false;
int num2 = 0;
string text2 = baseCloudConfig.ActivaNewPwd;
while (!(flag = appleiForgotWeb.RetrievePassword(listView, text2, listView.appleId, isCloseAuthen: true)))
{
if (!listView.notAuthen)
{
if (listView.notRepeatPwd)
{
Thread.Sleep(2000);
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "关双失败,切换备用密码....");
text2 = baseCloudConfig.ActivaStandbyPwd;
}
else
{
Thread.Sleep(2000);
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "关双失败,重新关闭....");
}
Thread.Sleep(1000);
num2++;
if (flag || !base.isRun || num2 >= 5)
{
break;
}
continue;
}
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "已没有双重,请验证密码[" + text2 + "]");
break;
}
}
}
else if (!string.IsNullOrEmpty(baseCloudConfig.ActivaStandbyPwd) && !forgotAttempt && errmess.Contains("输入的代码无效"))
{
Thread.Sleep(2000);
string text3 = baseCloudConfig.ActivaStandbyPwd;
int num3 = 0;
bool flag2 = false;
AppleiForgotWeb appleiForgotWeb2 = new AppleiForgotWeb(icloud.openVpn, baseAction, null, this);
do
{
if (!(flag2 = appleiForgotWeb2.RetrievePassword(listView, text3, listView.appleId, isCloseAuthen: true)))
{
if (listView.notRepeatPwd)
{
Thread.Sleep(2000);
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "验证不通过,关双失败,切换备用密码....");
text3 = baseCloudConfig.ActivaNewPwd;
}
else
{
Thread.Sleep(2000);
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "验证不通过,关双失败,重新关闭....");
}
Thread.Sleep(1000);
num3++;
continue;
}
listView.applePwd = text3;
cookieItem = icloud.listCookie.Where((CookieItem c) => c.Key == "repairSteps").FirstOrDefault();
if (cookieItem != null)
{
try
{
icloud.listCookie.Remove(cookieItem);
}
catch (Exception)
{
}
}
return AutoActioniCloud(icloud, listView, forgotAttempt: true, nasQualifyInfo);
}
while (!flag2 && base.isRun && num3 < 5);
}
}
else
{
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "数据加载失败..");
}
}
else
{
baseAction?.Invoke(listView.appleId, DisplyType.xinxi, "未输入手机号码");
}
return false;
}
public string actGetCode(string appleId, int type)
{
if (baseCloudConfig.smsService == null)
{
return baseCloudConfig.actGetCode?.Invoke(appleId, type);
}
if (type == 10)
{
baseCloudConfig.smsService.blackPhone(appleId);
}
switch (type)
{
case 11:
{
SmsRespon molieNumber = baseCloudConfig.smsService.getMolieNumber(ref appleId);
if (molieNumber.code == "0000")
{
return appleId;
}
return molieNumber.ErrMessage + "[" + baseCloudConfig.smsService.smsName + "]";
}
case 9:
{
string phone = "";
SmsRespon molieNumber2 = baseCloudConfig.smsService.getMolieNumber(ref phone);
if (molieNumber2.code == "0000")
{
return phone;
}
baseAction?.Invoke(appleId, DisplyType.xinxi, molieNumber2.ErrMessage + "[" + baseCloudConfig.smsService.smsName + "]");
return "";
}
case 1:
case 15:
case 16:
{
string value = new Regex("<([\\s\\S]*?)>").Match(appleId).Groups[1].Value;
string value2 = new Regex(":([\\s\\S]*?),").Match(appleId).Groups[1].Value;
if (string.IsNullOrEmpty(value))
{
break;
}
int num = ((type == 15) ? 15 : baseCloudConfig.getSmsMsgMaxCount);
string text = "";
for (int i = 0; i < num; i++)
{
if (!base.isRun)
{
continue;
}
SmsRespon sms = baseCloudConfig.smsService.getSms(value);
if (sms.code == "0000" && !string.IsNullOrEmpty(sms.Data))
{
string value3 = new Regex("([\\d]{6})").Match(sms.Data).Groups[1].Value;
if (!string.IsNullOrEmpty(value3))
{
return value3;
}
}
if (type == 15 || type == 16)
{
text = "ic";
}
baseAction?.Invoke(value2, DisplyType.xinxi, text + " 获取[" + value + "]短信...." + i + "[" + baseCloudConfig.smsService.smsName + "]");
Thread.Sleep(3000);
}
baseCloudConfig.smsService.releasePhone(value);
break;
}
}
return "";
}
}
}