Files
kami_itunes_june/AppleBatch_June.ExecuteTasks/AppleNetworkBase.cs
danial cb905409f8 Refactor AppleBatch_June project:
- Removed DotRas library dependency in RasTools.cs, providing empty implementations for Connect and Disconnect methods.
- Updated context menu implementation in ReddemHelp.cs to use ToolStripMenuItem and ContextMenuStrip.
- Replaced caching mechanism in SiteHelper.cs with a custom dictionary-based implementation, removing reliance on HttpRuntime.Cache.
- Switched from JavaScriptSerializer to Newtonsoft.Json for JSON serialization/deserialization in multiple files (Tools.cs, addMaterial.cs).
- Added WebHeaderCollection property to HttpItem.cs for better header management.
- Deleted obsolete AssemblyInfo.cs file.
- Introduced apple_balance_query.py for querying Apple ID balance via Privacy Center, implementing authentication and balance retrieval logic.
2025-11-10 17:38:18 +08:00

690 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using AppleBatch_June.Model;
using AppleBatch_June.Utils;
using DotNet.Utilities;
namespace AppleBatch_June.ExecuteTasks
{
public class AppleNetworkBase
{
protected ITaskRunState taskState;
public bool openVpn;
protected Action<string, DisplyType, string> action;
private string _appleId;
public string ProxyIp { get; set; }
public string appleId
{
get
{
return _appleId;
}
set
{
_appleId = value;
}
}
public string notyKey { get; set; }
public AppleNetworkBase(ITaskRunState _taskState, string _notyKey, Action<string, DisplyType, string> _action = null, bool _OpenVpn = false)
{
taskState = _taskState ?? throw new Exception("任务运行状态不能是空的");
notyKey = _notyKey;
openVpn = _OpenVpn;
action = _action;
}
public bool getIsLogin(int type, Action<string, DisplyType, string> _action)
{
_action?.Invoke(notyKey, DisplyType.xinxi, "正在处理");
HttpResult isLogin = APIUtlis.getIsLogin(type);
if (isLogin.StatusCode == HttpStatusCode.OK)
{
dynamic val = Tools.Todejosn<object>(isLogin.Html);
if (val["Code"] == "0000")
{
if (val["Data"]["integral"] >= AppSysConfig.getTypeById(type).consNum)
{
return true;
}
_action?.Invoke(notyKey, DisplyType.xinxi, "点数不足,请联系客服充值");
}
else
{
_action?.Invoke(notyKey, DisplyType.xinxi, val["Message"]);
}
}
else
{
_action?.Invoke(notyKey, DisplyType.xinxi, "网络请求失败");
}
return false;
}
public string GetNoticeActKey(HttpItem item)
{
string result = notyKey;
if (item != null && item.Header != null)
{
string text = "noticeActKey";
if (item.Header.AllKeys.Contains(text))
{
result = item.Header[text];
lock (item)
{
item.Header.Remove(text);
return result;
}
}
}
return result;
}
public HttpResult GetHttpReuslt(HttpItem item, bool isContityNeet = true)
{
if (!taskState.isRun)
{
throw new Exception("停止执行");
}
using HttpHelper httpHelper = new HttpHelper();
string noticeActKey = GetNoticeActKey(item);
HttpResult httpResult = null;
int num = Tools.ToNetWorkInt(ConfigUtlis.getConfigValue("comNetworkConfig"), 0);
if (openVpn && num < 3)
{
bool flag = false;
int num2 = 1;
List<ProxyAccountModel> proxyAccountModel = ProxyAccountCache.GetProxyAccountModel(ProxyIp);
do
{
flag = false;
if (num == 0 || num == 1)
{
if (!string.IsNullOrEmpty(ProxyIp))
{
int secondsTimeOut = 15;
if (item.URL.Contains("redeemCode"))
{
secondsTimeOut = item.Timeout / 1000;
}
if (!ProxyRunService.CheckIpAlive(ProxyIp, secondsTimeOut))
{
ProxyIp = "";
}
}
if (string.IsNullOrEmpty(ProxyIp))
{
action?.Invoke(noticeActKey, DisplyType.xinxi, "正在获取代理IP..." + num2);
ProxyIp = ProxyRunService.getProxyIp(taskState, num2);
if (string.IsNullOrEmpty(ProxyIp))
{
if (taskState.isRun)
{
break;
}
throw new Exception("停止执行");
}
foreach (ProxyAccountModel item2 in proxyAccountModel)
{
item2.ProxyIp = ProxyIp;
}
action?.Invoke(noticeActKey, DisplyType.xinxi, "代理IP获取成功.." + num2);
}
if (ProxyIp.Split('|').Length == 4)
{
string[] array = ProxyIp.Split('|');
WebProxy webProxy = new WebProxy();
webProxy.Address = new Uri($"http://{array[0].Trim()}:{array[1].Trim()}");
webProxy.Credentials = new NetworkCredential(array[2].Trim(), array[3].Trim());
item.WebProxy = webProxy;
item.KeepAlive = false;
}
else if (!string.IsNullOrEmpty(ProxyIp))
{
item.WebProxy = new WebProxy(ProxyIp);
}
}
else if (num == 2)
{
WebProxy webProxy2 = new WebProxy();
webProxy2.Address = new Uri(string.Format("http://{0}:{1}", ConfigUtlis.getConfigValue("txtTpsHost"), ConfigUtlis.getConfigValue("txtTpsPort")));
webProxy2.Credentials = new NetworkCredential(ConfigUtlis.getConfigValue("txtTpsUserName"), ConfigUtlis.getConfigValue("txtTpsPwd"));
item.WebProxy = webProxy2;
}
if (item.Timeout > 35000 && isContityNeet)
{
item.Timeout = 35000;
}
if (item.ReadWriteTimeout > 35000)
{
item.ReadWriteTimeout = 35000;
}
if (num2 >= 2)
{
item.KeepAlive = false;
}
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
httpResult = httpHelper.GetHtml(item);
stopwatch.Stop();
if (httpResult.StatusCode != 0 && httpResult.StatusCode != (HttpStatusCode)441 && httpResult.StatusCode != HttpStatusCode.RequestTimeout && httpResult.StatusCode != HttpStatusCode.BadGateway && httpResult.StatusCode != HttpStatusCode.ServiceUnavailable && !(httpResult.Html == "操作已超时。"))
{
if (httpResult.StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
{
ProxyRunService.removeIp(ProxyIp);
flag = true;
ProxyIp = string.Empty;
}
if (httpResult.Html.Contains("407 Proxy Authentication Required"))
{
ProxyRunService.removeIp(ProxyIp);
flag = true;
ProxyIp = string.Empty;
}
if (httpResult.Html.Contains("远程主机强迫关闭了一个现有的连接"))
{
ProxyRunService.removeIp(ProxyIp);
flag = true;
ProxyIp = string.Empty;
}
if ((item.Method.ToUpper() == "POST" || item.Method.ToUpper() == "PUT") && (httpResult.Html.Contains("发生未知错误") || httpResult.Html.Contains("BuyContentDisallowedForAccountFromFraudScoringTxn")))
{
ProxyRunService.removeIp(ProxyIp);
flag = true;
ProxyIp = string.Empty;
}
if (proxyAccountModel.Count == 0 && !string.IsNullOrEmpty(ProxyIp))
{
ProxyAccountCache.RestAllot(ProxyIp);
}
}
else
{
ProxyRunService.removeIp(ProxyIp);
flag = true;
ProxyIp = string.Empty;
}
num2++;
if (num2 > 50)
{
httpResult = null;
break;
}
}
while (flag && taskState.isRun && isContityNeet);
}
else
{
item.WebProxy = WebRequest.DefaultWebProxy;
httpResult = httpHelper.GetHtml(item);
}
if (httpResult == null)
{
item.Timeout = 30000;
item.ReadWriteTimeout = 30000;
item.WebProxy = WebRequest.DefaultWebProxy;
httpResult = httpHelper.GetHtml(item);
}
if (!string.IsNullOrEmpty(httpResult.Html))
{
APIUtlis.AppRequestHtml(httpResult.Html);
APIUtlis.requestUrl = item.URL;
}
return httpResult;
}
public HttpResult getItunesWebContext(string url, string referer, AppleItunesLogin itunelogin, Dictionary<string, string> hander, bool addToken, ResultType resultType = ResultType.String)
{
string text = "";
KeyValuePair<string, string>[] array = itunelogin.cookis.ToArray();
for (int i = 0; i < array.Length; i++)
{
KeyValuePair<string, string> keyValuePair = array[i];
text = text + keyValuePair.Key + "=" + keyValuePair.Value + ";";
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "get",
Timeout = 180000,
ReadWriteTimeout = 60000,
UserAgent = itunelogin.Headers["User-Agent"].ToString(),
Cookie = text,
ContentType = "text/html",
Accept = "*/*",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
PostDataType = PostDataType.Byte,
ResultType = resultType,
WebProxy = WebRequest.DefaultWebProxy
};
if (hander != null)
{
array = hander.ToArray();
for (int i = 0; i < array.Length; i++)
{
KeyValuePair<string, string> keyValuePair2 = array[i];
httpItem.Header.Add(keyValuePair2.Key, keyValuePair2.Value);
}
}
if (addToken)
{
httpItem.Header.Add("X-Dsid", itunelogin.dsis);
httpItem.Header.Add("X-Token", itunelogin.xtoken);
}
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
addCookle(itunelogin, httpReuslt.Cookie);
return httpReuslt;
}
protected void addHander(HttpItem item)
{
item.Header.Add("Accept-Encoding", "gzip, deflate");
item.Header.Add("Accept-Language", "zh-CN,zh;q=0.9,en;q=0");
}
protected void addCookle(AppleItunesLogin itunesLogin, string cookies)
{
if (cookies == null || string.IsNullOrEmpty(cookies.Trim()))
{
return;
}
foreach (CookieItem cookie in HttpCookieHelper.GetCookieList(cookies))
{
if (cookie.Key.ToLower() != "path" && cookie.Key.ToLower() != "domain" && cookie.Key.ToLower() != "expires" && cookie.Key.ToLower() != "max-age")
{
if (!itunesLogin.cookis.ContainsKey(cookie.Key))
{
itunesLogin.cookis.Add(cookie.Key, cookie.Value);
}
else if (!string.IsNullOrEmpty(cookie.Value.Trim()))
{
itunesLogin.cookis[cookie.Key] = cookie.Value;
}
}
}
}
public HttpResult postJsonItunesWebContext(string url, string postData, string referer, AppleItunesLogin itunelogin, Dictionary<string, string> hander, bool addToken)
{
string text = "";
KeyValuePair<string, string>[] array = itunelogin.cookis.ToArray();
for (int i = 0; i < array.Length; i++)
{
KeyValuePair<string, string> keyValuePair = array[i];
text = text + keyValuePair.Key + "=" + keyValuePair.Value + ";";
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "post",
Timeout = 180000,
ReadWriteTimeout = 60000,
UserAgent = itunelogin.Headers["User-Agent"].ToString(),
Cookie = text,
ContentType = "application/json; charset=UTF-8",
Accept = "application/json, text/javascript, */*; q=0.01",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
PostEncoding = Encoding.UTF8,
Postdata = postData,
ResultType = ResultType.String,
WebProxy = WebRequest.DefaultWebProxy
};
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
if (addToken)
{
httpItem.Header.Add("X-Dsid", itunelogin.dsis);
httpItem.Header.Add("X-Token", itunelogin.xtoken);
}
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
addCookle(itunelogin, httpReuslt.Cookie);
return httpReuslt;
}
public HttpResult getJsonItunesWebContext(string url, string referer, AppleItunesLogin itunelogin, Dictionary<string, string> hander, bool addToken)
{
string text = "";
KeyValuePair<string, string>[] array = itunelogin.cookis.ToArray();
for (int i = 0; i < array.Length; i++)
{
KeyValuePair<string, string> keyValuePair = array[i];
text = text + keyValuePair.Key + "=" + keyValuePair.Value + ";";
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "get",
Timeout = 180000,
ReadWriteTimeout = 60000,
UserAgent = itunelogin.Headers["User-Agent"].ToString(),
Cookie = text,
ContentType = "application/json; charset=UTF-8",
Accept = "application/json, text/javascript, */*; q=0.01",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
PostDataType = PostDataType.Byte,
ResultType = ResultType.String,
WebProxy = WebRequest.DefaultWebProxy
};
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
if (addToken)
{
httpItem.Header.Add("X-Dsid", itunelogin.dsis);
httpItem.Header.Add("X-Token", itunelogin.xtoken);
}
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
addCookle(itunelogin, httpReuslt.Cookie);
return httpReuslt;
}
protected HttpResult postLoginWebContext(string url, byte[] postData, string referer, string userAgent, Dictionary<string, string> cookis, string index, Dictionary<string, string> hander)
{
string cookie = "mzf_in=072381; " + ((index != "0") ? ("itspod=" + index + ";") : "") + " s_vi=; itsMetricsR=Genre-CN-Mobile%20Software%20Applications-29099@@Mobile%20Software%20Applications-main@@@@; " + ((index != "0") ? ("pod=" + index + ";") : "") + " s_vnum_n2_us=0%7C1";
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "post",
Timeout = 13000,
ReadWriteTimeout = 13000,
UserAgent = userAgent,
Cookie = cookie,
ContentType = "application/x-apple-plist; Charset=UTF-8",
Accept = "*/*",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
PostdataByte = postData,
PostDataType = PostDataType.Byte,
ResultType = ResultType.String,
WebProxy = WebRequest.DefaultWebProxy
};
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
addHander(httpItem);
httpItem.Header.Add("X-Apple-Partner", "origin.0");
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
httpItem.Header.Add("X-Apple-Client-Versions", "GameCenter/2.0");
httpItem.Header.Add("X-Token-T", "M");
httpItem.Header.Add("X-Apple-Tz", "28800");
HttpResult httpReuslt = GetHttpReuslt(httpItem);
Array.Clear(postData, 0, postData.Length);
return httpReuslt;
}
public string GetKeyValue(string itemKey, string valueKey1, string valueKey2, string plist)
{
string text = "";
bool num = plist.Contains(itemKey);
int num2 = 0;
if (num)
{
try
{
num2 = plist.IndexOf(itemKey, StringComparison.InvariantCulture);
text = plist.Substring(num2 + itemKey.Length);
num2 = text.IndexOf(valueKey1, StringComparison.InvariantCulture);
text = text.Substring(num2 + valueKey1.Length);
num2 = text.IndexOf(valueKey2, StringComparison.InvariantCulture);
text = text.Substring(0, num2);
}
catch (ArgumentNullException)
{
}
}
return text.Trim();
}
protected Dictionary<string, string> ConveCookle(string cookies)
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
if (cookies != null && !string.IsNullOrEmpty(cookies.Trim()))
{
foreach (CookieItem cookie in HttpCookieHelper.GetCookieList(cookies))
{
if (cookie.Key.ToLower() != "path" && cookie.Key.ToLower() != "domain" && cookie.Key.ToLower() != "expires" && cookie.Key.ToLower() != "max-age")
{
if (!dictionary.ContainsKey(cookie.Key))
{
dictionary.Add(cookie.Key, cookie.Value);
}
else if (!string.IsNullOrEmpty(cookie.Value.Trim()))
{
dictionary[cookie.Key] = cookie.Value;
}
}
}
return dictionary;
}
return dictionary;
}
public static string GetToken(string source, string string_1, string string_2)
{
int num = source.IndexOf(string_1);
if (num > -1)
{
if (string.IsNullOrEmpty(string_2))
{
return source.Substring(num + string_1.Length, source.Length - num - string_1.Length);
}
int num2 = source.IndexOf(string_2, num + string_1.Length);
if (num2 > -1)
{
return source.Substring(num + string_1.Length, num2 - num - string_1.Length);
}
}
return string.Empty;
}
public string strplist(string string_0)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<plist version=\"1.0\">\n\t<dict>\n");
string[] array = string_0.Split('&');
foreach (string source in array)
{
string token = GetToken(source, "", "=");
string token2 = GetToken(source, "=", "");
stringBuilder.Append("\t\t<key>");
stringBuilder.Append(token);
stringBuilder.Append("</key>\n");
if (token != "kbsync")
{
if (token == "supportsGpuContentProtection")
{
stringBuilder.Append("\t\t<" + token2 + "/>\n");
continue;
}
stringBuilder.Append("\t\t<string>");
stringBuilder.Append(token2);
stringBuilder.Append("</string>\n");
}
else
{
stringBuilder.Append("\t\t<data>");
stringBuilder.Append(token2);
stringBuilder.Append("</data>\n");
}
}
stringBuilder.Append("\t</dict>\n</plist>");
return stringBuilder.ToString();
}
public HttpResult postJsoniCloudWebContext(string url, string postData, string referer, AppleiCloundLogin cloundLogin, Dictionary<string, string> hander)
{
string cookie = "";
if (cloundLogin == null)
{
throw new Exception("没有登录状态");
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "post",
Timeout = 30000,
ReadWriteTimeout = 30000,
UserAgent = cloundLogin.userAgent,
Cookie = cookie,
ContentType = "application/json; charset=UTF-8",
Accept = "*/*",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
ResultType = ResultType.String,
Postdata = postData,
WebProxy = WebRequest.DefaultWebProxy
};
hander.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(cloundLogin.dsid + ":" + cloundLogin.mmeAuthToken)));
hander.Add("X-Mme-Client-Info", cloundLogin.clientInfo);
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
addHander(httpItem);
return GetHttpReuslt(httpItem);
}
public HttpResult postItunesWebContext(string url, string postData, string referer, AppleItunesLogin itunelogin, Dictionary<string, string> hander, bool isContityNeet = true)
{
string text = "";
foreach (KeyValuePair<string, string> cooki in itunelogin.cookis)
{
if (!string.IsNullOrEmpty(cooki.Value) && cooki.Key != "wosid")
{
text = text + cooki.Key + "=" + cooki.Value + ";";
}
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "post",
Timeout = 90000,
ReadWriteTimeout = 60000,
UserAgent = itunelogin.Headers["User-Agent"].ToString(),
Cookie = text,
ContentType = "application/x-apple-plist; Charset=UTF-8",
Accept = "*/*",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
Postdata = strplist(postData),
PostDataType = PostDataType.String,
ResultType = ResultType.String,
WebProxy = WebRequest.DefaultWebProxy,
Encoding = Encoding.UTF8
};
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
httpItem.Header.Add("X-Dsid", itunelogin.dsis);
httpItem.Header.Add("X-Token", itunelogin.xtoken);
httpItem.Header.Add("X-Apple-Software-Guid", itunelogin.Guid);
httpItem.Header.Add("X-Apple-Partner", "origin.0");
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
httpItem.Header.Add("X-Apple-Client-Versions", "GameCenter/2.0");
httpItem.Header.Add("X-Token-T", "M");
httpItem.Header.Add("X-Apple-Tz", "28800");
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem, isContityNeet);
addCookle(itunelogin, httpReuslt.Cookie);
return httpReuslt;
}
public HttpResult postUrlencodedItunesWebContext(string url, string postData, string referer, AppleItunesLogin itunelogin, Dictionary<string, string> hander)
{
string text = "";
foreach (KeyValuePair<string, string> cooki in itunelogin.cookis)
{
text = text + cooki.Key + "=" + cooki.Value + ";";
}
HttpItem httpItem = new HttpItem
{
URL = url,
Method = "post",
Timeout = 180000,
ReadWriteTimeout = 60000,
UserAgent = itunelogin.Headers["User-Agent"].ToString(),
Cookie = text,
ContentType = "application/x-www-form-urlencoded; charset=UTF-8",
Accept = "*/*",
Referer = referer,
ProtocolVersion = HttpVersion.Version11,
KeepAlive = true,
Postdata = postData,
PostDataType = PostDataType.String,
ResultType = ResultType.String,
WebProxy = WebRequest.DefaultWebProxy
};
if (hander != null)
{
foreach (KeyValuePair<string, string> item in hander)
{
httpItem.Header.Add(item.Key, item.Value);
}
}
httpItem.Header.Add("X-Dsid", itunelogin.dsis);
httpItem.Header.Add("X-Token", itunelogin.xtoken);
httpItem.Header.Add("X-Apple-Software-Guid", itunelogin.Guid);
httpItem.Header.Add("X-Apple-Partner", "origin.0");
httpItem.Header.Add("X-Apple-Client-Application", "Software");
httpItem.Header.Add("X-Apple-Connection-Type", "WiFi");
httpItem.Header.Add("X-Apple-Client-Versions", "GameCenter/2.0");
httpItem.Header.Add("X-Token-T", "M");
httpItem.Header.Add("X-Apple-Tz", "28800");
addHander(httpItem);
HttpResult httpReuslt = GetHttpReuslt(httpItem);
addCookle(itunelogin, httpReuslt.Cookie);
return httpReuslt;
}
}
}