软件升级相关代码
This commit is contained in:
1 parent
93e5e36dd3
commit
892d7e1f6c
20 files changed
+1697
-3
No files matched your search
@@ -0,0 +1,144 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Cache;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Text;
|
||||
|
||||
namespace Update
|
||||
{
|
||||
public class WebClientUtils : WebClient
|
||||
{/// <summary>
|
||||
/// 默认超时多少秒
|
||||
/// </summary>
|
||||
public int Timeout = 100;
|
||||
/// <summary>
|
||||
/// 允许自动重定向
|
||||
/// </summary>
|
||||
public bool AllowAutoRedirect = true;
|
||||
public bool IsDelete = false;
|
||||
|
||||
private string OutIP = "";
|
||||
/// <summary>
|
||||
/// 证书列表
|
||||
/// </summary>
|
||||
public static List<Certificate> X509List = new List<Certificate>();
|
||||
/// <summary>
|
||||
/// 绑定IP
|
||||
/// </summary>
|
||||
/// <param name="outIp"></param>
|
||||
public WebClientUtils(string outIp)
|
||||
{
|
||||
OutIP = outIp;
|
||||
CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
|
||||
}
|
||||
|
||||
public WebClientUtils()
|
||||
{
|
||||
}
|
||||
/// <summary>
|
||||
/// 重写
|
||||
/// </summary>
|
||||
/// <param name="address"></param>
|
||||
/// <returns></returns>
|
||||
protected override WebRequest GetWebRequest(Uri address)
|
||||
{
|
||||
string s = address.ToString();
|
||||
if (string.IsNullOrEmpty(s) || s.Length < 7 || !(s.Substring(0, 7).ToLower() == "http://" || s.Substring(0, 8).ToLower() == "https://"))
|
||||
return null;
|
||||
var request = (HttpWebRequest)base.GetWebRequest(address);
|
||||
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
|
||||
request.Timeout = 1000 * Timeout;
|
||||
request.ReadWriteTimeout = 500 * Timeout;
|
||||
request.AllowAutoRedirect = AllowAutoRedirect;
|
||||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate);
|
||||
if (IsDelete)
|
||||
request.Method = "DELETE";
|
||||
if (!string.IsNullOrEmpty(OutIP))
|
||||
{
|
||||
request.ServicePoint.BindIPEndPointDelegate = (servicePoint, remoteEndPoint, retryCount) =>
|
||||
{
|
||||
servicePoint.MaxIdleTime = 20;
|
||||
return new IPEndPoint(IPAddress.Parse(OutIP), 0);
|
||||
};
|
||||
}
|
||||
if (request.RequestUri.Scheme == "https") //挂接验证服务端证书的回调
|
||||
{
|
||||
X509Certificate2 x509c = null;
|
||||
for (int i = 0; i < X509List.Count; i++)
|
||||
{
|
||||
if (X509List[i].Domain.ToLower() == address.Host.ToLower())
|
||||
{
|
||||
x509c = X509List[i].X509;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (x509c == null)
|
||||
{
|
||||
ServicePointManager.ServerCertificateValidationCallback =
|
||||
new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);
|
||||
try
|
||||
{
|
||||
X509Store _store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
|
||||
_store.Open(OpenFlags.ReadWrite);
|
||||
string findValue = Dns.GetHostName(); //获取本地主机名称作为证书查找的参数
|
||||
X509Certificate2Collection _certsCollection =
|
||||
_store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false);
|
||||
if (_certsCollection.Count > 0)
|
||||
{
|
||||
x509c = _certsCollection[0];
|
||||
request.ClientCertificates.Add(x509c);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
}
|
||||
}
|
||||
return request;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取证书
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="certificate"></param>
|
||||
/// <param name="chain"></param>
|
||||
/// <param name="sslPolicyErrors"></param>
|
||||
/// <returns></returns>
|
||||
public bool RemoteCertificateValidationCallback(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
|
||||
{
|
||||
//if (sslPolicyErrors == SslPolicyErrors.None) {
|
||||
// return true;
|
||||
//}
|
||||
|
||||
//Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
|
||||
|
||||
//// Do not allow this client to communicate with unauthenticated servers.
|
||||
//return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 网站证书
|
||||
/// </summary>
|
||||
public class Certificate
|
||||
{
|
||||
/// <summary>
|
||||
/// 域名
|
||||
/// </summary>
|
||||
public string Domain { get; set; }
|
||||
/// <summary>
|
||||
/// 证书
|
||||
/// </summary>
|
||||
public X509Certificate2 X509 { get; set; }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user