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

462 lines
18 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
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 AppleReportaproblemWeb : AppleWebBase
{
private static string homeLanguage = "zh_CN";
public string Apple_Widget_Key = "6f59402f11d3e2234be5b88bf1c96e1e453a875aec205272add55157582a9f61";
public string authUrl = "https://idmsa.apple.com";
public string redirectURI = "https://idmsa.apple.com";
private string dataR = "";
protected string iframeId { get; set; }
private AuthsigninConfig authsigninConfig { get; set; }
public bool idIsNormal { get; set; }
public string appleId { get; set; }
public AppleReportaproblemWeb(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().Replace("auth-", "daw-");
}
public bool checkDisable(string _appleId, string applePwd, string _notityKey)
{
listCookie.Add(new CookieItem
{
Key = "geo",
Value = "CN"
});
homeLanguage = "CN-ZH";
authsigninConfig = new AuthsigninConfig
{
Apple_Widget_Key = Apple_Widget_Key,
authUrl = authUrl,
homeLanguage = homeLanguage,
iframeId = iframeId,
redirectURI = authUrl,
refererUrl = redirectURI
};
HttpResult result = null;
appleId = _appleId;
base.noticeKey = _notityKey;
new Dictionary<string, string>().Add("X-Apple-App-Id", "1879");
HttpResult webContent = getWebContent("https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=" + Apple_Widget_Key + "&language=" + homeLanguage, "");
if (webContent.StatusCode == HttpStatusCode.OK)
{
string text = new Regex("iframeId\":\"([\\s\\S]*?)\"").Match(webContent.Html).Groups[1].Value.Trim().TrimEnd(',');
if (!string.IsNullOrEmpty(text))
{
AuthsigninConfig obj = authsigninConfig;
string text3 = (iframeId = text);
obj.iframeId = text3;
}
if (webContent.Header.AllKeys.Contains("scnt"))
{
handelDic.Add("login_scnt", webContent.Header["scnt"]);
}
}
string errMsg = "未知错误";
if (authsignin(appleId, applePwd, authsigninConfig, ref errMsg, out result))
{
if (result != null)
{
if (result.StatusCode == HttpStatusCode.Unauthorized)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "ID 或密码不正确");
return true;
}
if (result.StatusCode == HttpStatusCode.Forbidden)
{
RespErrorJson respErrorJson = Tools.Todejosn<RespErrorJson>(result.Html);
if (respErrorJson != null && respErrorJson.serviceErrors != null && respErrorJson.serviceErrors.Length != 0)
{
string message = respErrorJson.serviceErrors[0].message;
action?.Invoke(base.noticeKey, DisplyType.xinxi, message);
return false;
}
return false;
}
if (result.StatusCode == HttpStatusCode.OK)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "密码验证成功");
string[] allKeys = result.Header.AllKeys;
foreach (string text4 in allKeys)
{
switch (text4)
{
case "X-Apple-ID-Session-Id":
case "X-Apple-ID-Account-Country":
case "scnt":
case "X-Apple-OAuth-Context":
handelDic.TryAddDic(text4, result.Header[text4]);
if (text4 == "X-Apple-ID-Account-Country")
{
action?.Invoke(base.noticeKey, DisplyType.area, AppleUtlis.GetAreaByCode(result.Header["X-Apple-ID-Account-Country"].ToString()));
}
break;
}
}
return reportaproblem(appleId);
}
if (result.StatusCode == HttpStatusCode.PreconditionFailed)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "阅读协议");
string[] allKeys = result.Header.AllKeys;
foreach (string text5 in allKeys)
{
switch (text5)
{
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(text5, result.Header[text5]);
if (text5 == "X-Apple-ID-Account-Country")
{
action?.Invoke(base.noticeKey, DisplyType.area, AppleUtlis.GetAreaByCode(result.Header["X-Apple-ID-Account-Country"].ToString()));
}
if (text5 == "scnt")
{
handelDic.TryAddDic("idmsa_scnt", result.Header[text5]);
}
break;
}
}
if (repair() && toDMSWebAuthSignin())
{
return reportaproblem(appleId);
}
}
}
else
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "登录异常了");
}
}
return false;
}
private bool toDMSWebAuthSignin()
{
WebHeaderCollection webHeaderCollection = new WebHeaderCollection();
webHeaderCollection.AddWebHander("X-Requested-With", "XMLHttpRequest");
string dicVal = handelDic.getDicVal("login_scnt", "");
string postData = "rememberMe=false&grantCode=&iframeId=" + iframeId + "&requestUri=%2Fsignin&appIdKey=" + Apple_Widget_Key + "&language=CN-ZH&scnt=" + dicVal;
HttpResult httpResult = postWebContent("https://idmsa.apple.com/IDMSWebAuth/signin", postData, "https://idmsa.apple.com/", webHeaderCollection, "text/html,application/xhtml+xml", "application/x-www-form-urlencoded");
if (httpResult.StatusCode != HttpStatusCode.OK && httpResult.StatusCode != HttpStatusCode.Found && httpResult.StatusCode != HttpStatusCode.SeeOther)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "登录失败:网页授权失败..");
return false;
}
return true;
}
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-App-Id", "1879");
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", ""));
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;
}
public bool reportaproblem(string appleId)
{
HttpResult webContent = getWebContent("https://reportaproblem.apple.com", "https://idmsa.apple.com/IDMSWebAuth/signin?appIdKey=6f59402f11d3e2234be5b88bf1c96e1e453a875aec205272add55157582a9f61&language=US-EN");
if (webContent.StatusCode == HttpStatusCode.OK)
{
HttpResult webJsonContent = getWebJsonContent("https://reportaproblem.apple.com/api/login", webContent.ResponseUri);
if (webJsonContent.StatusCode == HttpStatusCode.OK)
{
applyAtion?.Invoke(base.noticeKey, DisplyType.normal, "正常");
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
string.IsNullOrEmpty(base.ProxyIp);
idIsNormal = true;
dataR = new Regex(AppSysConfig.getConfig("Regex_reportaproblem")).Match(webContent.Html).Groups[1].Value;
return true;
}
string token = AppleNetworkBase.GetToken(webJsonContent.Html, "messageBodyLocKey\":\"", "\"");
if (!string.IsNullOrEmpty(token))
{
if (AppSysConfig.getConfig("MessageBodyLocKey").Split(',').Contains(token))
{
applyAtion?.Invoke(base.noticeKey, DisplyType.forbidden, "禁用");
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "失败,错误码:" + token);
return false;
}
applyAtion?.Invoke(base.noticeKey, DisplyType.normal, "正常");
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
if (webContent.StatusCode != HttpStatusCode.Found && webContent.StatusCode != HttpStatusCode.MovedPermanently)
{
action?.Invoke(base.noticeKey, DisplyType.xinxi, "未知异常,请重新检测");
return false;
}
if (webContent.RedirectUrl.ToLower().Contains("accountdisabled"))
{
applyAtion?.Invoke(base.noticeKey, DisplyType.forbidden, "禁用");
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
if (webContent.RedirectUrl.ToLower().Contains("https://reportaproblem.apple.com"))
{
applyAtion?.Invoke(base.noticeKey, DisplyType.normal, "正常");
action?.Invoke(base.noticeKey, DisplyType.xinxi, "查询完成");
return true;
}
action?.Invoke(base.noticeKey, DisplyType.xinxi, "未知异常,请重新检测");
return false;
}
}
}