Files
2024-07-22 00:43:14 +08:00

544 lines
22 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using AppleBatch_June.ExecuteTasks;
using AppleBatch_June.Model;
using AppleBatch_June.Utils;
using DotNet.Utilities;
namespace AppleBatch_June.AppleWebServace
{
public class ApplePrivacytWeb : AppleWebBase
{
private AppleAcount acount = new AppleAcount();
protected string Apple_Widget_Key = "04659e25236376d440c224638c1cdd6a001abdd7f186cdcfa120abf35417efab";
public string authUrl = "https://privacy.apple.com";
public string redirectURI = "https://idmsa.apple.com/";
private static string homeLanguage = "zh_CN";
public string appleId { get; set; }
private AuthsigninConfig authsigninConfig { get; set; }
public string XAppleIDAccountCountry { get; set; }
protected string iframeId { get; set; }
public ApplePrivacytWeb(bool _openVpn, Action<string, DisplyType, string> _action, Action<string, DisplyType, string> _applyAtion, ITaskRunState _taskState)
: base(_action, _applyAtion, _taskState)
{
taskState = _taskState ?? throw new Exception("任务运行状态不能是空的");
base.openVpn = _openVpn;
iframeId = APPLEHCHelp.getIframeId();
}
public bool applyQueryAuthBalance(AppleAcount _acount, string _noticeKey)
{
acount = _acount;
if (string.IsNullOrEmpty(base.ProxyIp))
{
base.ProxyIp = ProxyAccountCache.getProxyAccountIP(acount.appleId);
}
appleId = acount.appleId;
base.noticeKey = _noticeKey;
authsigninConfig = new AuthsigninConfig
{
Apple_Widget_Key = Apple_Widget_Key,
authUrl = authUrl,
homeLanguage = homeLanguage,
iframeId = iframeId,
redirectURI = redirectURI
};
try
{
HttpResult webContent = getWebContent("https://privacy.apple.com/account", "");
if (webContent.StatusCode != HttpStatusCode.OK)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "页面加载失败");
return false;
}
string value = new Regex(AppSysConfig.getConfig("Regex_apply_CsrfToken")).Match(webContent.Html).Groups[1].Value.Trim();
if (LoginWeb() && appleauthAuth(acount))
{
string value2 = "";
handelDic.TryGetValue("X-Apple-OAuth-Grant-Code", out value2);
string value3 = Tools.EncodeBase64(Encoding.UTF8.GetBytes(Apple_Widget_Key + ":" + value2));
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.Add("x-csrf-token", value);
webHeaderCollection.Add("x-apple-authentication", value3);
action?.Invoke(base.noticeKey, DisplyType.xinxi, "正在获取余额");
HttpResult webJsonContent = getWebJsonContent("https://privacy.apple.com/session/create", "https://privacy.apple.com/", webHeaderCollection);
if (webJsonContent.StatusCode == HttpStatusCode.OK || webJsonContent.StatusCode == HttpStatusCode.Created)
{
WebHeaderCollection webHeaderCollection2 = new WebHeaderCollection();
webHeaderCollection2.Add("x-csrf-token", value);
HttpResult webJsonContent2 = getWebJsonContent("https://privacy.apple.com/section/delete-account", "https://privacy.apple.com/account", webHeaderCollection2);
if (webJsonContent2.Html.Contains("store_balance"))
{
string text = new Regex(AppSysConfig.getConfig("Regex_applyQueryAuthBalance")).Match(webJsonContent2.Html).Groups[1].Value.Trim().TrimEnd(',');
if (string.IsNullOrEmpty(text))
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "余额解析失败");
return false;
}
Dictionary<string, object> dictionary = Tools.Todejosn<Dictionary<string, object>>("{" + text + "}");
decimal num = (dynamic)dictionary["balance"];
string text2 = (dynamic)dictionary["currency"];
action?.Invoke(base.noticeKey, DisplyType.balance, num + " " + text2);
}
else
{
action?.Invoke(base.noticeKey, DisplyType.balance, "0");
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "正在重新获取余额");
webJsonContent = getWebJsonContent("https://privacy.apple.com/session/create", "https://privacy.apple.com/", webHeaderCollection);
if (webJsonContent.StatusCode == HttpStatusCode.OK || webJsonContent.StatusCode == HttpStatusCode.Created)
{
WebHeaderCollection webHeaderCollection3 = new WebHeaderCollection();
webHeaderCollection3.Add("x-csrf-token", value);
HttpResult webJsonContent3 = getWebJsonContent("https://privacy.apple.com/section/delete-account", "https://privacy.apple.com/account", webHeaderCollection3);
if (webJsonContent3.Html.Contains("store_balance"))
{
string text3 = new Regex(AppSysConfig.getConfig("Regex_applyQueryAuthBalance")).Match(webJsonContent3.Html).Groups[1].Value.Trim().TrimEnd(',');
if (string.IsNullOrEmpty(text3))
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "余额解析失败");
return false;
}
Dictionary<string, object> dictionary2 = Tools.Todejosn<Dictionary<string, object>>("{" + text3 + "}");
decimal num2 = (dynamic)dictionary2["balance"];
string text4 = (dynamic)dictionary2["currency"];
action?.Invoke(base.noticeKey, DisplyType.balance, num2 + " " + text4);
}
else
{
action?.Invoke(base.noticeKey, DisplyType.balance, "0");
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "余额获取失败");
}
}
catch (Exception ex)
{
APIUtlis.ApiSeriveError(ex);
action?.Invoke(base.noticeKey, DisplyType.xinxi, ex.Message);
}
return false;
}
public bool LoginWeb()
{
HttpResult result = null;
string errMsg = "未知错误";
if (authsignin(acount.appleId, acount.applePwd, authsigninConfig, ref errMsg, out result))
{
if (result != null)
{
if (result.StatusCode == HttpStatusCode.Conflict || result.StatusCode == HttpStatusCode.OK)
{
if (result.Html.Contains("hsa2"))
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "双重验证账号暂不支持更改");
return false;
}
return true;
}
if (result.StatusCode == HttpStatusCode.PreconditionFailed)
{
string[] allKeys = result.Header.AllKeys;
foreach (string text in allKeys)
{
switch (text)
{
case "X-Apple-Auth-Attributes":
case "X-Apple-Repair-Session-Token":
case "X-Apple-ID-Session-Id":
case "scnt":
case "X-Apple-OAuth-Context":
handelDic.TryAddDic(text, result.Header[text]);
break;
}
if (text == "scnt")
{
handelDic.TryAddDic("scnt2", result.Header[text]);
}
}
return repair();
}
}
else
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "登录异常了");
}
}
return false;
}
private bool appleauthAuth(AppleAcount acount)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "验证密保问题");
new WebHeaderCollection();
string url = "https://idmsa.apple.com/appleauth/auth";
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", Apple_Widget_Key);
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
webHeaderCollection.AddWebHander("X-Apple-Locale", homeLanguage);
webHeaderCollection.AddWebHander("X-Apple-Auth-Attributes", handelDic.getDicVal("X-Apple-Auth-Attributes", ""));
webHeaderCollection.AddWebHander("scnt", handelDic.getDicVal("scnt", ""));
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic.getDicVal("X-Apple-ID-Session-Id", ""));
addOAuthHanede(webHeaderCollection, authsigninConfig);
string appleIFDClientInfo = FDClientInfoHelp.getAppleIFDClientInfo(base.userAgent);
webHeaderCollection.AddWebHander("X-Apple-I-FD-Client-Info", appleIFDClientInfo);
HttpResult webContent = getWebContent(url, authUrl, webHeaderCollection, "text/html", "application/json");
if (webContent.StatusCode == HttpStatusCode.OK)
{
AothQuestions questions = Tools.Todejosn<AothQuestions>(new Regex(AppSysConfig.getConfig("Regex_appleauthAuth")).Match(webContent.Html).Groups[1].Value.Trim() + "}");
return verifyAuthQuestions(questions, new string[3] { acount.appleQt1, acount.appleQt2, acount.appleQt3 });
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "密保获取失败");
return false;
}
private bool verifyAuthQuestions(AothQuestions questions, string[] question)
{
AothQuestions.Question[] questions2 = questions.questions;
int num = 0;
while (true)
{
if (num < questions2.Length)
{
AothQuestions.Question question2 = questions2[num];
int anwerId_Index = Tools.getAnwerId_Index(question2.id);
if (anwerId_Index == 0)
{
break;
}
question2.answer = question[anwerId_Index - 1];
num++;
continue;
}
string postData = Tools.Toenjson(questions);
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", Apple_Widget_Key);
webHeaderCollection.AddWebHander("X-Apple-App-Id", Apple_Widget_Key);
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
webHeaderCollection.AddWebHander("X-Apple-Domain-Id", "1");
webHeaderCollection.AddWebHander("scnt", handelDic["scnt"]);
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic["X-Apple-ID-Session-Id"]);
webHeaderCollection.AddWebHander("X-Apple-Locale", homeLanguage);
webHeaderCollection.AddWebHander("X-Apple-Auth-Attributes", handelDic.getDicVal("X-Apple-Auth-Attributes", ""));
webHeaderCollection.Add("X-Apple-OAuth-Require-Grant-Code", "true");
addOAuthHanede(webHeaderCollection, authsigninConfig);
string appleIFDClientInfo = FDClientInfoHelp.getAppleIFDClientInfo(base.userAgent);
webHeaderCollection.AddWebHander("X-Apple-I-FD-Client-Info", appleIFDClientInfo);
string url = "https://idmsa.apple.com/appleauth/auth/verify/questions";
HttpResult httpResult = postWebContent(url, postData, authUrl, webHeaderCollection);
if (httpResult.StatusCode != HttpStatusCode.BadRequest && httpResult.StatusCode != HttpStatusCode.Unauthorized)
{
if (httpResult.StatusCode == HttpStatusCode.NoContent)
{
string[] allKeys = httpResult.Header.AllKeys;
foreach (string text in allKeys)
{
if (text == "X-Apple-OAuth-Grant-Code" || text == "scnt")
{
handelDic.TryAddDic(text, httpResult.Header[text]);
}
if (text == "scnt")
{
handelDic.TryAddDic("idmsa_scnt", httpResult.Header[text]);
}
}
if (httpResult.Header.AllKeys.Contains("X-Apple-ID-Account-Country"))
{
XAppleIDAccountCountry = AppleUtlis.GetAreaByCode(httpResult.Header["X-Apple-ID-Account-Country"].ToString());
action?.Invoke(base.noticeKey, DisplyType.area, XAppleIDAccountCountry);
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "密保验证成功");
return true;
}
if (httpResult.StatusCode == HttpStatusCode.PreconditionFailed)
{
string[] allKeys = httpResult.Header.AllKeys;
foreach (string text2 in allKeys)
{
switch (text2)
{
case "X-Apple-Repair-Session-Token":
case "X-Apple-ID-Session-Id":
case "scnt":
case "X-Apple-OAuth-Context":
handelDic.TryAddDic(text2, httpResult.Header[text2]);
break;
}
if (text2 == "scnt")
{
handelDic.TryAddDic("idmsa_scnt", httpResult.Header[text2]);
}
}
if (httpResult.Header.AllKeys.Contains("X-Apple-ID-Account-Country"))
{
XAppleIDAccountCountry = AppleUtlis.GetAreaByCode(httpResult.Header["X-Apple-ID-Account-Country"].ToString());
action?.Invoke(base.noticeKey, DisplyType.area, XAppleIDAccountCountry);
}
return repair();
}
return false;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "密保错误");
return false;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "非正常密保问题,无法识别");
return false;
}
private bool repair()
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "获取阅读协议");
string text = "zh_CN_CHN";
if (handelDic.ContainsKey("X-Apple-Locale"))
{
text = handelDic["X-Apple-Locale"];
}
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("Upgrade-Insecure-Requests", "1");
string apple_Widget_Key = Apple_Widget_Key;
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", apple_Widget_Key);
string uRL = "https://appleid.apple.com/widget/account/repair?trustedWidgetDomain=https%3A%2F%2Fidmsa.apple.com&widgetKey=" + apple_Widget_Key + "&rv=1&language=" + text;
HttpItem httpItem = new HttpItem
{
URL = uRL,
Method = "get",
Timeout = 120000,
ReadWriteTimeout = 30000,
UserAgent = base.userAgent,
Cookie = listCookie.toFonmtCookie(),
ContentType = "text/html;charset=UTF-8",
Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
Referer = "https://idmsa.apple.com/appleauth/auth/signin?widgetKey=" + apple_Widget_Key + "&language=zh_CN&rv=1",
ResultType = ResultType.String,
ProtocolVersion = HttpVersion.Version11,
WebProxy = WebRequest.DefaultWebProxy
};
httpItem.Header = webHeaderCollection;
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
if (httpReuslt.StatusCode == HttpStatusCode.OK)
{
string value = new Regex(AppSysConfig.getConfig("Regex_repair_sessionId")).Match(httpReuslt.Html).Groups[1].Value.Trim().Replace("\n", "").Replace("\r", "")
.Replace("\"", "")
.TrimStart(':')
.Trim();
string key = "repairSessionId";
if (!handelDic.ContainsKey(key))
{
handelDic.TryAddDic(key, value);
}
else
{
handelDic[key] = value;
}
string[] allKeys = httpReuslt.Header.AllKeys;
foreach (string text2 in allKeys)
{
if (text2 == "X-Apple-Repair-Session-Token" || text2 == "scnt")
{
handelDic.TryAddDic(text2, httpReuslt.Header[text2]);
}
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议获取成功");
return options();
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议获取失败");
return false;
}
private bool options()
{
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
webHeaderCollection.AddWebHander("scnt", handelDic.getDicVal("scnt", ""));
webHeaderCollection.AddWebHander("X-Apple-Skip-Repair-Attributes", "[\"hsa2_enrollment\"]");
webHeaderCollection.AddWebHander("X-Apple-OAuth-Context", handelDic.getDicVal("X-Apple-OAuth-Context", ""));
string dicVal = handelDic.getDicVal("X-Apple-Session-Token", "");
if (string.IsNullOrEmpty(dicVal))
{
dicVal = handelDic.getDicVal("X-Apple-Repair-Session-Token", "");
}
webHeaderCollection.AddWebHander("X-Apple-Session-Token", dicVal);
webHeaderCollection.AddWebHander("X-Apple-I-TimeZone", "Asia/Shanghai");
webHeaderCollection.AddWebHander("X-Apple-Repair-App-Id", "1879");
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic.getDicVal("repairSessionId", ""));
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", Apple_Widget_Key);
string appleIFDClientInfo = FDClientInfoHelp.getAppleIFDClientInfo(base.userAgent);
webHeaderCollection.AddWebHander("X-Apple-I-FD-Client-Info", appleIFDClientInfo);
string uRL = "https://appleid.apple.com/account/manage/repair/options";
HttpItem httpItem = new HttpItem
{
URL = uRL,
Method = "get",
Timeout = 120000,
ReadWriteTimeout = 30000,
UserAgent = base.userAgent,
Cookie = listCookie.toFonmtCookie(),
ContentType = "application/json; charset=utf-8",
Accept = "application/json, text/javascript, */*; q=0.01",
Referer = "https://appleid.apple.com/",
ResultType = ResultType.String,
ProtocolVersion = HttpVersion.Version11,
WebProxy = WebRequest.DefaultWebProxy
};
httpItem.Header = webHeaderCollection;
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
if (httpReuslt.StatusCode == HttpStatusCode.OK)
{
string[] allKeys = httpReuslt.Header.AllKeys;
foreach (string text in allKeys)
{
if (text == "X-Apple-Session-Token" || text == "scnt")
{
handelDic.TryAddDic(text, httpReuslt.Header[text]);
}
}
Dictionary<string, object> dictionary = Tools.Todejosn<Dictionary<string, object>>(httpReuslt.Html);
if (httpReuslt.Html.Contains("requiredSteps"))
{
foreach (object item in (dynamic)dictionary["requiredSteps"])
{
if ((string)(dynamic)item == "privacy_consent" && !accept())
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议阅读失败#2");
return false;
}
}
}
return complete();
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议获取失败#2");
return false;
}
private bool accept()
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "正在同意协议");
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
webHeaderCollection.AddWebHander("X-Apple-Repair-App-Id", "1879");
webHeaderCollection.AddWebHander("X-Apple-OAuth-Context", handelDic.getDicVal("X-Apple-OAuth-Context", ""));
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", Apple_Widget_Key);
webHeaderCollection.AddWebHander("scnt", handelDic["scnt"]);
webHeaderCollection.AddWebHander("X-Apple-Skip-Repair-Attributes", "[]");
webHeaderCollection.AddWebHander("X-Apple-Session-Token", handelDic["X-Apple-Session-Token"]);
webHeaderCollection.AddWebHander("X-Apple-I-TimeZone", "Asia/Shanghai");
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic["repairSessionId"]);
string appleIFDClientInfo = FDClientInfoHelp.getAppleIFDClientInfo(base.userAgent);
webHeaderCollection.AddWebHander("X-Apple-I-FD-Client-Info", appleIFDClientInfo);
string uRL = "https://appleid.apple.com/account/manage/privacy/accept";
HttpItem httpItem = new HttpItem
{
URL = uRL,
Method = "put",
UserAgent = base.userAgent,
Cookie = listCookie.toFonmtCookie(),
ContentType = "application/json; charset=utf-8",
Accept = "application/json, text/javascript, */*; q=0.01",
Referer = "https://appleid.apple.com/",
ProtocolVersion = HttpVersion.Version11,
WebProxy = WebRequest.DefaultWebProxy
};
httpItem.Header = webHeaderCollection;
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
if (httpReuslt.StatusCode != HttpStatusCode.OK && httpReuslt.StatusCode != HttpStatusCode.MethodNotAllowed)
{
return false;
}
string[] allKeys = httpReuslt.Header.AllKeys;
foreach (string text in allKeys)
{
if (text == "X-Apple-Session-Token" || text == "scnt")
{
handelDic.TryAddDic(text, httpReuslt.Header[text]);
}
}
return true;
}
private bool complete()
{
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
webHeaderCollection.AddWebHander("X-Apple-Locale", homeLanguage);
webHeaderCollection.AddWebHander("scnt", handelDic.getDicVal("idmsa_scnt", ""));
if (handelDic.ContainsKey("X-Apple-ID-Session-Id"))
{
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic.getDicVal("X-Apple-ID-Session-Id", ""));
}
else if (handelDic.ContainsKey("repairSessionId"))
{
webHeaderCollection.AddWebHander("X-Apple-ID-Session-Id", handelDic.getDicVal("repairSessionId", ""));
}
webHeaderCollection.AddWebHander("X-Apple-Domain-Id", "2");
webHeaderCollection.AddWebHander("X-Apple-Trusted-Domain", " https://idmsa.apple.com");
webHeaderCollection.AddWebHander("X-Apple-Repair-Session-Token", handelDic["X-Apple-Session-Token"]);
webHeaderCollection.AddWebHander("Origin", "https://idmsa.apple.com");
webHeaderCollection.AddWebHander("X-Apple-Widget-Key", Apple_Widget_Key);
string appleIFDClientInfo = FDClientInfoHelp.getAppleIFDClientInfo(base.userAgent);
webHeaderCollection.AddWebHander("X-Apple-I-FD-Client-Info", appleIFDClientInfo);
webHeaderCollection.AddWebHander("X-Apple-Auth-Attributes", handelDic.getDicVal("X-Apple-Auth-Attributes", ""));
webHeaderCollection.Add("X-Apple-OAuth-Require-Grant-Code", "true");
addOAuthHanede(webHeaderCollection, authsigninConfig);
string uRL = "https://idmsa.apple.com/appleauth/auth/repair/complete";
HttpItem httpItem = new HttpItem
{
URL = uRL,
Method = "post",
UserAgent = base.userAgent,
Cookie = listCookie.toFonmtCookie(),
ContentType = "application/json; charset=utf-8",
Accept = "application/json, text/javascript, */*; q=0.01",
Referer = "https://idmsa.apple.com/",
ResultType = ResultType.String,
Postdata = "",
ProtocolVersion = HttpVersion.Version11,
WebProxy = WebRequest.DefaultWebProxy
};
httpItem.Header = webHeaderCollection;
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
if (httpReuslt.StatusCode != HttpStatusCode.OK && httpReuslt.StatusCode != HttpStatusCode.NoContent)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议阅读失败");
return false;
}
string[] allKeys = httpReuslt.Header.AllKeys;
foreach (string text in allKeys)
{
if (text == "X-Apple-OAuth-Grant-Code" || text == "scnt")
{
handelDic.TryAddDic(text, httpReuslt.Header[text]);
}
}
if (httpReuslt.Header.AllKeys.Contains("X-Apple-ID-Account-Country"))
{
action?.Invoke(base.noticeKey, DisplyType.area, AppleUtlis.GetAreaByCode(httpReuslt.Header["X-Apple-ID-Account-Country"].ToString()));
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "协议阅读成功");
return true;
}
}
}