mirror of
https://git.oceanpay.cc/danial/kami_itunes_june.git
synced 2025-12-18 22:31:24 +00:00
918 lines
25 KiB
C#
918 lines
25 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Cache;
|
|
using System.Net.Sockets;
|
|
using System.Text.RegularExpressions;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AppleBatch_June.Model;
|
|
using DotNet.Utilities;
|
|
|
|
namespace AppleBatch_June.ExecuteTasks
|
|
{
|
|
public static class ProxyRunService
|
|
{
|
|
public class ThreadProxyReturnData
|
|
{
|
|
public ManualResetEvent mre;
|
|
|
|
public bool result { get; set; }
|
|
|
|
public int timeOut { get; set; } = 5000;
|
|
|
|
|
|
public ThreadProxyReturnData(int _timeOut)
|
|
{
|
|
timeOut = _timeOut;
|
|
}
|
|
|
|
public void ProxyCheck(object ipInfo2)
|
|
{
|
|
ProxyIpModel proxyIpModel = ipInfo2 as ProxyIpModel;
|
|
result = getWriteTestRest(proxyIpModel.prixyIp, timeOut);
|
|
if (mre != null && !mre.SafeWaitHandle.IsClosed)
|
|
{
|
|
mre.Set();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static readonly object lokObj;
|
|
|
|
private static int advanceExpirationSeconds;
|
|
|
|
private static bool isFindRun;
|
|
|
|
private static Regex rx;
|
|
|
|
private static DateTime getIpUpTime;
|
|
|
|
private static bool findSucessProxy;
|
|
|
|
private static readonly object findObj;
|
|
|
|
public static DateTime latestLoadTime;
|
|
|
|
private static int getIpErrCount;
|
|
|
|
private static IpInfoModel Inifo { get; set; }
|
|
|
|
public static List<ProxyIpModel> proxyModels { get; set; }
|
|
|
|
public static List<ProxyIpModel> incProxyExpirationTimeModels { get; set; }
|
|
|
|
private static int nowRouteLine { get; set; }
|
|
|
|
private static int nowWorkConfigIndex { get; set; }
|
|
|
|
private static ProxyIpModel tunnelProxyInfo { get; set; }
|
|
|
|
private static string nowProxyTtpe { get; set; }
|
|
|
|
private static int UserCount { get; set; }
|
|
|
|
private static int ExpirationTimeMaxValue { get; set; }
|
|
|
|
public static int nowFun { get; set; }
|
|
|
|
private static string platformName { get; set; }
|
|
|
|
public static DateTime getApiUrlLostTime { get; set; }
|
|
|
|
static ProxyRunService()
|
|
{
|
|
Inifo = null;
|
|
proxyModels = new List<ProxyIpModel>();
|
|
incProxyExpirationTimeModels = new List<ProxyIpModel>();
|
|
tunnelProxyInfo = null;
|
|
lokObj = new object();
|
|
nowProxyTtpe = "";
|
|
UserCount = 2;
|
|
advanceExpirationSeconds = 20;
|
|
ExpirationTimeMaxValue = 10;
|
|
nowFun = 0;
|
|
isFindRun = false;
|
|
rx = new Regex("((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))");
|
|
getIpUpTime = DateTime.Now.AddMinutes(-1.0);
|
|
findSucessProxy = false;
|
|
findObj = new object();
|
|
platformName = "";
|
|
getApiUrlLostTime = DateTime.Now.AddHours(-1.0);
|
|
latestLoadTime = DateTime.Now.AddMinutes(-2.0);
|
|
getIpErrCount = 0;
|
|
}
|
|
|
|
public static void resetProxyData()
|
|
{
|
|
lock (lokObj)
|
|
{
|
|
proxyModels.Clear();
|
|
getIpErrCount = 0;
|
|
tunnelProxyInfo = null;
|
|
nowProxyTtpe = "";
|
|
}
|
|
}
|
|
|
|
public static string getProxyIp(ITaskRunState taskState, int nowCount, bool fistProxy = false)
|
|
{
|
|
int num = Tools.ToNetWorkInt(ConfigUtlis.getConfigValue("comNetworkConfig"), 0);
|
|
if (num == 0 && num == nowWorkConfigIndex && GetConfigRouteLine() == nowRouteLine && nowProxyTtpe == "tunnel" && tunnelProxyInfo != null && tunnelProxyInfo.tunnelProxy.Split('|').Length == 4 && tunnelProxyInfo.expirationTime > DateTime.Now.AddSeconds(advanceExpirationSeconds))
|
|
{
|
|
tunnelProxyInfo.userCount += 1;
|
|
return tunnelProxyInfo.tunnelProxy.Replace("{guid}", Guid.NewGuid().ToString("n").Substring(16));
|
|
}
|
|
if (num == 0 && nowProxyTtpe == "white" && nowCount >= 15)
|
|
{
|
|
proxyModels.Clear();
|
|
getIpErrCount = Tools.ToInt(AppSysConfig.getConfig("IpGetMaxErrCount"), 10);
|
|
}
|
|
ProxyIpModel proxyIpModel = GetProxy(fistProxy, souci: true);
|
|
if (proxyIpModel == null)
|
|
{
|
|
int num2 = Tools.ToInt(AppSysConfig.getConfig("GetIpMaxTotalSeconds"), 3);
|
|
DateTime now = DateTime.Now;
|
|
if (num != 0)
|
|
{
|
|
num2 = 30;
|
|
}
|
|
while (DateTime.Now.Subtract(now).TotalSeconds <= (double)num2 && proxyIpModel == null && taskState.isRun)
|
|
{
|
|
Thread.Sleep(500);
|
|
proxyIpModel = GetProxy(fistProxy);
|
|
if (proxyIpModel == null || proxyIpModel.type != 0 || !proxyIpModel.expirationTime.HasValue)
|
|
{
|
|
continue;
|
|
}
|
|
if (proxyIpModel.expirationTime.Value.AddSeconds(-advanceExpirationSeconds) > DateTime.Now)
|
|
{
|
|
if (!TestingProxyIp(proxyIpModel))
|
|
{
|
|
removeIp(proxyIpModel.prixyIp);
|
|
proxyIpModel = null;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
removeIp(proxyIpModel.prixyIp);
|
|
proxyIpModel = null;
|
|
}
|
|
}
|
|
}
|
|
else if (!TestingProxyIp(proxyIpModel))
|
|
{
|
|
removeIp(proxyIpModel.prixyIp);
|
|
proxyIpModel = null;
|
|
if (taskState.isRun)
|
|
{
|
|
return getProxyIp(taskState, nowCount);
|
|
}
|
|
}
|
|
if (proxyIpModel != null)
|
|
{
|
|
if (proxyIpModel.type == 0)
|
|
{
|
|
return proxyIpModel.prixyIp;
|
|
}
|
|
return proxyIpModel.tunnelProxy.Replace("{guid}", Guid.NewGuid().ToString("n").Substring(16));
|
|
}
|
|
if (num == 0)
|
|
{
|
|
string config = AppSysConfig.getConfig("DefaultNewTtunnelTeluns");
|
|
if (!string.IsNullOrEmpty(config))
|
|
{
|
|
string text = DESEncrypt.Decrypt(config, "62d610TelunsFd90", "0d6db719d74f10be").Replace('\0', ' ').Trim();
|
|
if (text.Split('|').Length >= 4)
|
|
{
|
|
return text.Replace("{guid}", Guid.NewGuid().ToString("n").Substring(16));
|
|
}
|
|
}
|
|
}
|
|
return "";
|
|
}
|
|
|
|
public static bool CheckIpAlive(string ip, int secondsTimeOut = 15)
|
|
{
|
|
if (ip.Split('|').Length >= 4)
|
|
{
|
|
return true;
|
|
}
|
|
if (!ip.StartsWith("rest_ip,") && ip.Substring(ip.IndexOf('@') + 1).Split(':').Length == 2)
|
|
{
|
|
ProxyIpModel proxyIpModel = null;
|
|
lock (lokObj)
|
|
{
|
|
delInvalidProxy(checkNumberUses: false);
|
|
proxyIpModel = (from c in proxyModels.ToArray()
|
|
where c.prixyIp == ip
|
|
select c).FirstOrDefault();
|
|
if (proxyIpModel == null)
|
|
{
|
|
proxyIpModel = (from c in incProxyExpirationTimeModels.ToArray()
|
|
where c.prixyIp == ip
|
|
select c).FirstOrDefault();
|
|
}
|
|
}
|
|
if (proxyIpModel != null)
|
|
{
|
|
if (proxyIpModel.expirationTime.HasValue && !(proxyIpModel.expirationTime.Value.AddSeconds(-secondsTimeOut) > DateTime.Now))
|
|
{
|
|
removeIp(ip);
|
|
return false;
|
|
}
|
|
if (TestingProxyIp(proxyIpModel))
|
|
{
|
|
return true;
|
|
}
|
|
removeIp(ip);
|
|
}
|
|
return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static bool IsDomainAlive(string prixyIp, int aTimeoutSeconds)
|
|
{
|
|
string[] array = prixyIp.Substring(prixyIp.IndexOf('@') + 1).Split(':');
|
|
string host = array[0].Trim();
|
|
int port = Tools.ToInt(array[1], 80);
|
|
new Stopwatch().Start();
|
|
try
|
|
{
|
|
using TcpClient tcpClient = new TcpClient();
|
|
IAsyncResult asyncResult = tcpClient.BeginConnect(host, port, null, null);
|
|
if (!asyncResult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(aTimeoutSeconds)))
|
|
{
|
|
return false;
|
|
}
|
|
tcpClient.EndConnect(asyncResult);
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static ProxyIpModel GetProxy(bool fistProxy, bool souci = false)
|
|
{
|
|
lock (lokObj)
|
|
{
|
|
delInvalidProxy();
|
|
}
|
|
if (proxyModels.Count < 2)
|
|
{
|
|
startFindProxy(souci);
|
|
}
|
|
if (nowProxyTtpe == "tunnel" && tunnelProxyInfo != null && tunnelProxyInfo.tunnelProxy.Split('|').Length == 4 && tunnelProxyInfo.expirationTime > DateTime.Now.AddSeconds(advanceExpirationSeconds))
|
|
{
|
|
tunnelProxyInfo.userCount += 1;
|
|
return tunnelProxyInfo;
|
|
}
|
|
lock (lokObj)
|
|
{
|
|
ProxyIpModel proxyIpModel = proxyModels.OrderBy((ProxyIpModel c) => c.userCount).FirstOrDefault();
|
|
if (proxyIpModel != null)
|
|
{
|
|
if (fistProxy)
|
|
{
|
|
proxyModels.Remove(proxyIpModel);
|
|
}
|
|
if (proxyIpModel.userCount >= UserCount)
|
|
{
|
|
return null;
|
|
}
|
|
proxyIpModel.userCount++;
|
|
}
|
|
return proxyIpModel;
|
|
}
|
|
}
|
|
|
|
private static void delInvalidProxy(bool checkNumberUses = true)
|
|
{
|
|
List<ProxyIpModel> list = new List<ProxyIpModel>();
|
|
foreach (ProxyIpModel proxyModel in proxyModels)
|
|
{
|
|
if (checkNumberUses && proxyModel.userCount >= UserCount)
|
|
{
|
|
list.Add(proxyModel);
|
|
}
|
|
if (proxyModel.expirationTime.HasValue && !(proxyModel.expirationTime.Value.AddSeconds(-10.0) > DateTime.Now))
|
|
{
|
|
list.Add(proxyModel);
|
|
}
|
|
}
|
|
foreach (ProxyIpModel item in list)
|
|
{
|
|
proxyModels.Remove(item);
|
|
}
|
|
}
|
|
|
|
public static void removeIp(string ip)
|
|
{
|
|
try
|
|
{
|
|
lock (lokObj)
|
|
{
|
|
ProxyIpModel proxyIpModel = proxyModels.Where((ProxyIpModel c) => c.prixyIp == ip).FirstOrDefault();
|
|
if (proxyIpModel != null)
|
|
{
|
|
proxyModels.Remove(proxyIpModel);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
|
|
private static int GetConfigRouteLine()
|
|
{
|
|
int result = 0;
|
|
string configValue = ConfigUtlis.getConfigValue("HttpAgentRouteLine");
|
|
if (!string.IsNullOrEmpty(configValue) && int.TryParse(configValue, out var result2))
|
|
{
|
|
result = result2;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static void ApiJsonParameterPase(string suore, out string[] ipParameter, out string[] proParameter, out string[] expireTimeParameter)
|
|
{
|
|
ipParameter = new string[1] { "ip" };
|
|
proParameter = new string[1] { "port" };
|
|
expireTimeParameter = new string[1] { "expire_time" };
|
|
string[] array = suore.Split('|');
|
|
foreach (string text in array)
|
|
{
|
|
if (text.StartsWith("ip="))
|
|
{
|
|
ipParameter = text.Split('=')[1].Split(',').ToArray();
|
|
}
|
|
if (text.StartsWith("port="))
|
|
{
|
|
proParameter = text.Split('=')[1].Split(',').ToArray();
|
|
}
|
|
if (text.StartsWith("expireTime="))
|
|
{
|
|
expireTimeParameter = text.Split('=')[1].Split(',').ToArray();
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void startFindProxy(bool souci)
|
|
{
|
|
if (isFindRun)
|
|
{
|
|
return;
|
|
}
|
|
lock (findObj)
|
|
{
|
|
if (getIpUpTime > DateTime.Now.AddSeconds(-5.0))
|
|
{
|
|
return;
|
|
}
|
|
isFindRun = true;
|
|
getIpUpTime = DateTime.Now;
|
|
}
|
|
Task.Run(delegate
|
|
{
|
|
Console.WriteLine("开始代理:" + DateTime.Now);
|
|
try
|
|
{
|
|
if (proxyModels.Count < 2)
|
|
{
|
|
string authenUserInfo = "";
|
|
string apiUrl = GetApiUrl(ref authenUserInfo);
|
|
if (!string.IsNullOrEmpty(apiUrl) && !apiUrl.StartsWith("tunnel"))
|
|
{
|
|
string text = apiUrl.Split('|')[0];
|
|
if (!string.IsNullOrEmpty(text))
|
|
{
|
|
List<ProxyIpModel> list = new List<ProxyIpModel>();
|
|
HttpResult http = GetHttp(text, addVerifyToken: true, apiUrl);
|
|
if (http.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
if (!http.Html.Contains("白名单") && !http.Html.Contains("六月账户"))
|
|
{
|
|
string value = new Regex("\\[([\\s\\S]*?)\\]").Match(http.Html).Groups[1].Value;
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
try
|
|
{
|
|
object obj = Tools.Todejosn<object>("[" + value + "]");
|
|
ApiJsonParameterPase(AppSysConfig.getConfig("GetApiJsonParameter"), out var ipParameter, out var proParameter, out var expireTimeParameter);
|
|
foreach (object item2 in (dynamic)obj)
|
|
{
|
|
Dictionary<string, object> dictionary = (Dictionary<string, object>)(dynamic)item2;
|
|
string text2 = "";
|
|
string[] array = ipParameter;
|
|
foreach (string key in array)
|
|
{
|
|
if (dictionary.ContainsKey(key))
|
|
{
|
|
text2 = (dynamic)dictionary[key];
|
|
break;
|
|
}
|
|
}
|
|
int num = 0;
|
|
array = proParameter;
|
|
foreach (string key2 in array)
|
|
{
|
|
if (dictionary.ContainsKey(key2))
|
|
{
|
|
object obj2 = dictionary[key2];
|
|
if (obj2 is int)
|
|
{
|
|
num = (dynamic)obj2;
|
|
}
|
|
else
|
|
{
|
|
_ = (string)(dynamic)obj2;
|
|
int.TryParse((dynamic)obj2, out num);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
string text3 = "";
|
|
array = expireTimeParameter;
|
|
foreach (string key3 in array)
|
|
{
|
|
if (dictionary.ContainsKey(key3))
|
|
{
|
|
text3 = (dynamic)dictionary[key3];
|
|
break;
|
|
}
|
|
}
|
|
if (string.IsNullOrEmpty(text3) && dictionary.ContainsKey("timeout"))
|
|
{
|
|
int? num2 = dictionary["timeout"] as int?;
|
|
if (num2.HasValue)
|
|
{
|
|
text3 = DateTime.Now.AddSeconds(num2.Value).ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
}
|
|
if (text3.Contains("addMinTime".ToLower()))
|
|
{
|
|
int num3 = Tools.ToInt(new Regex("\\(([\\s\\S]*?)\\)", RegexOptions.IgnoreCase).Match(text3).Groups[1].Value, 0);
|
|
if (num3 > 0)
|
|
{
|
|
text3 = DateTime.Now.AddMinutes(num3).ToString("yyyy-MM-dd HH:mm:ss");
|
|
}
|
|
}
|
|
if (num > 0 && !text2.Contains(":"))
|
|
{
|
|
text2 = text2 + ":" + num;
|
|
}
|
|
if (!string.IsNullOrEmpty(authenUserInfo))
|
|
{
|
|
text2 = authenUserInfo + "@" + text2;
|
|
}
|
|
if (text2.Substring(text2.IndexOf('@') + 1).Split(':').Length == 2 && rx.IsMatch(text2.Substring(text2.IndexOf('@') + 1).Split(':')[0]))
|
|
{
|
|
DateTime? expirationTime = null;
|
|
if (DateTime.TryParse(text3, out var result))
|
|
{
|
|
expirationTime = ((!(result > DateTime.Now.AddMinutes(ExpirationTimeMaxValue))) ? new DateTime?(result) : new DateTime?(DateTime.Now.AddMinutes(ExpirationTimeMaxValue)));
|
|
}
|
|
if (!expirationTime.HasValue || expirationTime.Value.AddSeconds(-15.0) > DateTime.Now)
|
|
{
|
|
list.Add(new ProxyIpModel
|
|
{
|
|
expirationTime = expirationTime,
|
|
type = 0,
|
|
userCount = 0,
|
|
tunnelProxy = "",
|
|
prixyIp = text2
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string[] array = http.Html.Split('\n');
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
string text4 = array[i].Replace("\r", "").Replace("\n", "");
|
|
if (!string.IsNullOrEmpty(authenUserInfo))
|
|
{
|
|
text4 = authenUserInfo + "@" + text4;
|
|
}
|
|
if (text4.Substring(text4.IndexOf('@') + 1).Split(':').Length == 2 && rx.IsMatch(text4.Substring(text4.IndexOf('@') + 1).Split(':')[0]))
|
|
{
|
|
list.Add(new ProxyIpModel
|
|
{
|
|
expirationTime = null,
|
|
type = 0,
|
|
userCount = 0,
|
|
tunnelProxy = "",
|
|
prixyIp = text4
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
IpListGetError(apiUrl);
|
|
}
|
|
}
|
|
else if (http.Html.Contains("白名单") || http.Html.Contains("六月账户") || http.StatusCode == HttpStatusCode.Unauthorized)
|
|
{
|
|
IpListGetError(apiUrl);
|
|
}
|
|
if (list.Count > 0)
|
|
{
|
|
int num4 = Tools.ToNetWorkInt(ConfigUtlis.getConfigValue("comNetworkConfig"), 0);
|
|
string config = AppSysConfig.getConfig("CheckUseIPConfig");
|
|
findSucessProxy = false;
|
|
if (num4 == 0 && config == "1")
|
|
{
|
|
CheckUseIP(list);
|
|
}
|
|
List<Task> list2 = new List<Task>();
|
|
DateTime now = DateTime.Now;
|
|
int millisecondsTimeout = 15000;
|
|
Action<ProxyIpModel, int> actCheckIp = CheckIp;
|
|
ThreadPool.GetMinThreads(out var workerThreads, out var _);
|
|
if (workerThreads < list.Count)
|
|
{
|
|
ThreadPool.SetMinThreads(list.Count * 2, list.Count);
|
|
}
|
|
foreach (ProxyIpModel item in list)
|
|
{
|
|
list2.Add(Task.Run(delegate
|
|
{
|
|
actCheckIp(item, millisecondsTimeout);
|
|
}));
|
|
}
|
|
Task.WaitAll(list2.ToArray(), millisecondsTimeout + 3000);
|
|
Console.WriteLine("ttt:" + (DateTime.Now - now).TotalSeconds);
|
|
foreach (Task item3 in list2)
|
|
{
|
|
if (item3.Status == TaskStatus.RanToCompletion)
|
|
{
|
|
item3.Dispose();
|
|
}
|
|
}
|
|
if (findSucessProxy)
|
|
{
|
|
getIpErrCount = 0;
|
|
}
|
|
else
|
|
{
|
|
IpListGetError(apiUrl);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex2)
|
|
{
|
|
APIUtlis.ApiSeriveError(ex2);
|
|
}
|
|
finally
|
|
{
|
|
isFindRun = false;
|
|
}
|
|
});
|
|
}
|
|
|
|
public static void CheckIp(ProxyIpModel item, int millisecondsTimeout)
|
|
{
|
|
if (!TestingProxyIp(item, millisecondsTimeout))
|
|
{
|
|
return;
|
|
}
|
|
findSucessProxy = true;
|
|
lock (lokObj)
|
|
{
|
|
proxyModels.Add(item);
|
|
if (item.expirationTime.HasValue)
|
|
{
|
|
incProxyExpirationTimeModels.Add(new ProxyIpModel
|
|
{
|
|
expirationTime = item.expirationTime,
|
|
prixyIp = item.prixyIp,
|
|
type = item.type
|
|
});
|
|
}
|
|
else
|
|
{
|
|
incProxyExpirationTimeModels.Add(new ProxyIpModel
|
|
{
|
|
expirationTime = DateTime.Now.AddMinutes(5.0),
|
|
prixyIp = item.prixyIp,
|
|
type = item.type
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void CheckUseIP(List<ProxyIpModel> getIPList)
|
|
{
|
|
List<string> ipLists = getIPList.Select((ProxyIpModel c) => c.prixyIp.Substring(c.prixyIp.IndexOf('@') + 1)).ToList();
|
|
HttpResult httpResult = APIUtlis.ApiCheckUseIP(platformName, ipLists);
|
|
if (httpResult.StatusCode != HttpStatusCode.OK)
|
|
{
|
|
return;
|
|
}
|
|
dynamic val = Tools.Todejosn<object>(httpResult.Html);
|
|
if (!((val["Code"] == "0000") ? true : false) || !(val["Data"] is object[] source))
|
|
{
|
|
return;
|
|
}
|
|
List<string> list = source.Select((dynamic c) => c as string).ToList();
|
|
List<ProxyIpModel> list2 = new List<ProxyIpModel>();
|
|
foreach (ProxyIpModel getIP in getIPList)
|
|
{
|
|
string item = getIP.prixyIp.Substring(getIP.prixyIp.IndexOf('@') + 1);
|
|
if (!list.Contains(item))
|
|
{
|
|
list2.Add(getIP);
|
|
}
|
|
}
|
|
foreach (ProxyIpModel item2 in list2)
|
|
{
|
|
getIPList.Remove(item2);
|
|
}
|
|
}
|
|
|
|
private static bool getWriteTestRest(string proxyIp, int timeOut)
|
|
{
|
|
try
|
|
{
|
|
HttpWebRequest httpWebRequest = WebRequest.Create(AppSysConfig.getConfig("TestingProxyIpUrl")) as HttpWebRequest;
|
|
httpWebRequest.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
|
httpWebRequest.Method = "OPTIONS";
|
|
httpWebRequest.KeepAlive = false;
|
|
httpWebRequest.Timeout = timeOut;
|
|
httpWebRequest.ReadWriteTimeout = timeOut;
|
|
httpWebRequest.Proxy = new WebProxy(proxyIp);
|
|
if (httpWebRequest.Proxy is WebProxy)
|
|
{
|
|
WebProxy webProxy = httpWebRequest.Proxy as WebProxy;
|
|
if (!string.IsNullOrEmpty(webProxy.Address.UserInfo))
|
|
{
|
|
Uri address = new Uri(webProxy.Address.Scheme + "://" + webProxy.Address.Authority);
|
|
string[] array = webProxy.Address.UserInfo.Split(':');
|
|
if (array.Length >= 2)
|
|
{
|
|
NetworkCredential credentials = new NetworkCredential(array[0], array[1]);
|
|
httpWebRequest.Proxy = new WebProxy(address, BypassOnLocal: false, null, credentials);
|
|
}
|
|
}
|
|
}
|
|
httpWebRequest.ContentLength = 1L;
|
|
using (Stream stream = httpWebRequest.GetRequestStream())
|
|
{
|
|
stream.Write(new byte[49], 0, 1);
|
|
stream.Close();
|
|
stream.Dispose();
|
|
}
|
|
httpWebRequest.Abort();
|
|
httpWebRequest = null;
|
|
return true;
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
_ = ex.Message;
|
|
if (ex.Response != null)
|
|
{
|
|
ex.Response.Close();
|
|
ex.Response.Dispose();
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception ex2)
|
|
{
|
|
_ = ex2.Message;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static bool TestingProxyIp(ProxyIpModel ipInfo, int timeOut = 5000)
|
|
{
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
stopwatch.Start();
|
|
bool result = false;
|
|
try
|
|
{
|
|
if (ipInfo != null)
|
|
{
|
|
if (timeOut > 5000 && !IsDomainAlive(ipInfo.prixyIp, 5))
|
|
{
|
|
return false;
|
|
}
|
|
using (ManualResetEvent manualResetEvent = new ManualResetEvent(initialState: false))
|
|
{
|
|
ThreadProxyReturnData obj = new ThreadProxyReturnData(timeOut)
|
|
{
|
|
mre = manualResetEvent
|
|
};
|
|
ThreadPool.QueueUserWorkItem(obj.ProxyCheck, ipInfo);
|
|
manualResetEvent.WaitOne(timeOut);
|
|
result = obj.result;
|
|
}
|
|
return result;
|
|
}
|
|
return false;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return false;
|
|
}
|
|
finally
|
|
{
|
|
stopwatch.Stop();
|
|
}
|
|
}
|
|
|
|
public static string GetApiUrl(ref string authenUserInfo)
|
|
{
|
|
getApiUrlLostTime = DateTime.Now;
|
|
int num = Tools.ToNetWorkInt(ConfigUtlis.getConfigValue("comNetworkConfig"), 0);
|
|
string result = "";
|
|
if (num == 0)
|
|
{
|
|
int configRouteLine = GetConfigRouteLine();
|
|
int num2 = Tools.ToInt(AppSysConfig.getConfig("IpGetMaxErrCount"), 10);
|
|
int type = 1;
|
|
if (getIpErrCount >= num2)
|
|
{
|
|
type = 3;
|
|
getIpErrCount = 0;
|
|
}
|
|
HttpResult httpResult = APIUtlis.ApiProxyIp(Inifo, configRouteLine, type);
|
|
if (httpResult.StatusCode == HttpStatusCode.OK)
|
|
{
|
|
dynamic val = Tools.Todejosn<object>(httpResult.Html);
|
|
if (val["Code"] == "0000")
|
|
{
|
|
result = val["Data"];
|
|
string text = val["extend"];
|
|
nowWorkConfigIndex = num;
|
|
nowRouteLine = configRouteLine;
|
|
if (result.StartsWith("tunnelHttp="))
|
|
{
|
|
string tunnelProxy = DESEncrypt.Decrypt(result.Replace("tunnelHttp=", ""), "62d6108d2cfafd90", "0d6db719d74f10be").Replace('\0', ' ').Trim();
|
|
nowProxyTtpe = "tunnel";
|
|
tunnelProxyInfo = new ProxyIpModel
|
|
{
|
|
type = 1,
|
|
prixyIp = "",
|
|
userCount = 0,
|
|
tunnelProxy = tunnelProxy,
|
|
expirationTime = DateTime.Now.AddMinutes(5.0)
|
|
};
|
|
getIpErrCount = 0;
|
|
return "tunnel|" + num + "|" + text;
|
|
}
|
|
nowProxyTtpe = "white";
|
|
tunnelProxyInfo = null;
|
|
if (val is Dictionary<string, object> dictionary)
|
|
{
|
|
if (dictionary.ContainsKey("authenUserInfo"))
|
|
{
|
|
string text2 = val["authenUserInfo"];
|
|
if (!string.IsNullOrEmpty(text2))
|
|
{
|
|
authenUserInfo = DESEncrypt.Decrypt(text2, "62d6108d2cfafd90", "0d6db719d74f10be").Replace('\0', ' ').Trim();
|
|
}
|
|
}
|
|
if (dictionary.ContainsKey("platformName"))
|
|
{
|
|
platformName = val["platformName"];
|
|
}
|
|
}
|
|
result = result + "|" + num + "|" + text;
|
|
}
|
|
}
|
|
}
|
|
if (num == 1)
|
|
{
|
|
nowProxyTtpe = "white";
|
|
result = Tools.ToUrlDecode(ConfigUtlis.getConfigValue("txtNetWordApiURL"));
|
|
result = result + "|" + num + "|0";
|
|
string configValue = ConfigUtlis.getConfigValue("txtApiAccountName");
|
|
string configValue2 = ConfigUtlis.getConfigValue("txtApiAccountPwd");
|
|
if (!string.IsNullOrEmpty(configValue) && !string.IsNullOrEmpty(configValue2))
|
|
{
|
|
authenUserInfo = configValue + ":" + configValue2;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static HttpResult GetHttp(string url, bool addVerifyToken, string ApiUrlInfo)
|
|
{
|
|
string[] array = ApiUrlInfo.Split('|');
|
|
string value = "";
|
|
if (array[1] == "0" && addVerifyToken && array[2].Split(',').Length == 2)
|
|
{
|
|
value = array[2].Split(',')[1];
|
|
}
|
|
HttpItem httpItem = new HttpItem
|
|
{
|
|
URL = url,
|
|
Method = "GET",
|
|
Timeout = 15000,
|
|
ReadWriteTimeout = 15000,
|
|
UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",
|
|
Accept = "text/html, application/xhtml+xml, */*",
|
|
ContentType = "text/html",
|
|
ResultType = ResultType.String
|
|
};
|
|
if (!string.IsNullOrEmpty(value))
|
|
{
|
|
httpItem.Header.Add("cuuIPnow", value);
|
|
}
|
|
if (addVerifyToken)
|
|
{
|
|
httpItem.Header.Add("liyueToken", getVerifyToken("liuyueGetIp"));
|
|
}
|
|
return new HttpHelper().GetHtml(httpItem);
|
|
}
|
|
|
|
public static string getVerifyToken(string key)
|
|
{
|
|
string arg = "";
|
|
if (key == "liuyueGetIp")
|
|
{
|
|
arg = AppSysConfig.userToken;
|
|
}
|
|
string arg2 = Guid.NewGuid().ToString("n");
|
|
return DESEncrypt.Encrypt(string.Format("key=" + key + "|token={0}|guid={1}", arg, arg2), "62d6108d2cfa0000", "0d6db719d7411111");
|
|
}
|
|
|
|
private static void IpListGetError(string getIpListUrl)
|
|
{
|
|
loadIPInfoTask();
|
|
string[] array = getIpListUrl.Split('|');
|
|
if (array.Length == 3)
|
|
{
|
|
string text = array[1];
|
|
string text2 = array[2];
|
|
if (text == "0" && text2.Split(',')[0] == "1")
|
|
{
|
|
getIpErrCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void loadIPInfoTask()
|
|
{
|
|
if (!(DateTime.Now.AddMinutes(-1.0) > latestLoadTime))
|
|
{
|
|
return;
|
|
}
|
|
latestLoadTime = DateTime.Now;
|
|
Task.Run(delegate
|
|
{
|
|
IpInfoModel ipinfo = Tools.GetIpinfo();
|
|
if (ipinfo != null)
|
|
{
|
|
Inifo = ipinfo;
|
|
}
|
|
else
|
|
{
|
|
Inifo = new IpInfoModel
|
|
{
|
|
country = "中国,未知",
|
|
ip = "",
|
|
isp = ""
|
|
};
|
|
}
|
|
});
|
|
}
|
|
|
|
public static IpInfoModel loadIPInfo()
|
|
{
|
|
IpInfoModel ipinfo = Tools.GetIpinfo();
|
|
if (ipinfo != null)
|
|
{
|
|
Inifo = ipinfo;
|
|
}
|
|
else
|
|
{
|
|
Inifo = new IpInfoModel
|
|
{
|
|
country = "中国,未知",
|
|
ip = "",
|
|
isp = ""
|
|
};
|
|
}
|
|
return Inifo;
|
|
}
|
|
}
|
|
}
|