Fix proxy connection for Cryville.Common.Network.

This commit is contained in:
2022-10-27 11:28:10 +08:00
parent 6b6b7d9a48
commit 92e5ed1f9c
3 changed files with 36 additions and 42 deletions

View File

@@ -1,7 +1,6 @@
using Microsoft.Win32;
using System;
using System.IO;
using System.Net.Sockets;
namespace Cryville.Common.Network {
public class HttpsClient : HttpClient {
@@ -12,40 +11,24 @@ namespace Cryville.Common.Network {
return _tlsTcpClient.Stream;
}
}
protected override string WindowsProxyProtocolName {
get {
return "https";
}
}
public HttpsClient(Uri baseUri) : base(baseUri, 443) {
_tlsTcpClient = new TlsTcpClient(DirectHost, DirectPort);
_tlsTcpClient = new TlsTcpClient(TcpClient, baseUri.Host);
}
public override void Connect() {
_tlsTcpClient.Connect();
base.Connect();
_tlsTcpClient.Connect();
}
public override void Close() {
base.Close();
_tlsTcpClient.Close();
}
protected override bool GetProxy(ref string host, ref int port) {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
var proxyEnable = (int)reg.GetValue("ProxyEnable");
if (proxyEnable == 0) return false;
var proxyStr = (string)reg.GetValue("ProxyServer");
if (!string.IsNullOrEmpty(proxyStr)) {
string[] proxies = proxyStr.Split(';');
foreach (var p in proxies) {
if (p.StartsWith("https=")) {
string[] s = p.Split('=', ':');
host = s[1];
port = int.Parse(s[2]);
return true;
}
}
}
}
return false;
base.Close();
}
}
}