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,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Sockets; using System.Net.Sockets;
@@ -18,12 +19,22 @@ namespace Cryville.Common.Network {
readonly int origPort; readonly int origPort;
protected string Version = "HTTP/1.1"; protected string Version = "HTTP/1.1";
protected TcpClient TcpClient; protected TcpClient TcpClient { get; private set; }
protected Stream RawTcpStream {
get {
return TcpClient.GetStream();
}
}
protected virtual Stream Stream { protected virtual Stream Stream {
get { get {
return TcpClient.GetStream(); return TcpClient.GetStream();
} }
} }
protected virtual string WindowsProxyProtocolName {
get {
return "http";
}
}
private readonly bool _proxied = false; private readonly bool _proxied = false;
@@ -42,7 +53,7 @@ namespace Cryville.Common.Network {
public virtual void Connect() { public virtual void Connect() {
if (_proxied) { if (_proxied) {
Request("CONNECT", _baseUri.Host + ":" + origPort.ToString()); Request(RawTcpStream, "CONNECT", string.Format(CultureInfo.InvariantCulture, "{0}:{1}", _baseUri.Host, origPort));
} }
} }
@@ -52,17 +63,13 @@ namespace Cryville.Common.Network {
public HttpResponse Request(string method, Uri uri, string body = null, Encoding encoding = null) { public HttpResponse Request(string method, Uri uri, string body = null, Encoding encoding = null) {
string struri = GetUri(uri).PathAndQuery; string struri = GetUri(uri).PathAndQuery;
// if (_proxied) struri = GetUri(uri).AbsoluteUri; return Request(Stream, method, struri, body, encoding);
return Request(method, struri, body, encoding);
} }
public HttpResponse Request(string method, string uri, string body = null, Encoding encoding = null) { public HttpResponse Request(Stream stream, string method, string uri, string body = null, Encoding encoding = null) {
var headers = new Dictionary<string, string>(); var headers = new Dictionary<string, string>();
// if (Stream.CanTimeout) Stream.ReadTimeout = Stream.WriteTimeout = 5000;
foreach (var h in Headers) foreach (var h in Headers)
headers.Add(h.Key, h.Value); headers.Add(h.Key, h.Value);
// headers["Accept"] = "text/plain, */*";
// headers["Connection"] = "close";
headers["Host"] = _baseUri.Host; headers["Host"] = _baseUri.Host;
byte[] payload = new byte[0]; byte[] payload = new byte[0];
if (body != null) { if (body != null) {
@@ -85,14 +92,14 @@ namespace Cryville.Common.Network {
Array.Copy(buffer0, buffer1, buffer0.Length); Array.Copy(buffer0, buffer1, buffer0.Length);
Array.Copy(payload, 0, buffer1, buffer0.Length, payload.Length); Array.Copy(payload, 0, buffer1, buffer0.Length, payload.Length);
Logger.Log("main", 0, "Network", Encoding.UTF8.GetString(buffer1)); Logger.Log("main", 0, "Network", Encoding.UTF8.GetString(buffer1));
Stream.Write(buffer1, 0, buffer1.Length); stream.Write(buffer1, 0, buffer1.Length);
Stream.Flush(); stream.Flush();
var response = new HttpResponse(Stream); var response = new HttpResponse(stream);
Logger.Log("main", 0, "Network", "{0}", response); Logger.Log("main", 0, "Network", "{0}", response);
return response; return response;
} }
protected virtual bool GetProxy(ref string host, ref int port) { protected bool GetProxy(ref string host, ref int port) {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) { if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings"); var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
var proxyEnable = (int)reg.GetValue("ProxyEnable"); var proxyEnable = (int)reg.GetValue("ProxyEnable");
@@ -101,7 +108,13 @@ namespace Cryville.Common.Network {
if (!string.IsNullOrEmpty(proxyStr)) { if (!string.IsNullOrEmpty(proxyStr)) {
string[] proxies = proxyStr.Split(';'); string[] proxies = proxyStr.Split(';');
foreach (var p in proxies) { foreach (var p in proxies) {
if (p.StartsWith("http=")) { if (!p.Contains('=')) {
string[] s = p.Split(':');
host = s[0];
port = int.Parse(s[1]);
return true;
}
else if (p.StartsWith(WindowsProxyProtocolName + "=")) {
string[] s = p.Split('=', ':'); string[] s = p.Split('=', ':');
host = s[1]; host = s[1];
port = int.Parse(s[2]); port = int.Parse(s[2]);

View File

@@ -1,7 +1,6 @@
using Microsoft.Win32; using Microsoft.Win32;
using System; using System;
using System.IO; using System.IO;
using System.Net.Sockets;
namespace Cryville.Common.Network { namespace Cryville.Common.Network {
public class HttpsClient : HttpClient { public class HttpsClient : HttpClient {
@@ -12,40 +11,24 @@ namespace Cryville.Common.Network {
return _tlsTcpClient.Stream; return _tlsTcpClient.Stream;
} }
} }
protected override string WindowsProxyProtocolName {
get {
return "https";
}
}
public HttpsClient(Uri baseUri) : base(baseUri, 443) { public HttpsClient(Uri baseUri) : base(baseUri, 443) {
_tlsTcpClient = new TlsTcpClient(DirectHost, DirectPort); _tlsTcpClient = new TlsTcpClient(TcpClient, baseUri.Host);
} }
public override void Connect() { public override void Connect() {
_tlsTcpClient.Connect();
base.Connect(); base.Connect();
_tlsTcpClient.Connect();
} }
public override void Close() { public override void Close() {
base.Close();
_tlsTcpClient.Close(); _tlsTcpClient.Close();
} base.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;
} }
} }
} }

View File

@@ -10,13 +10,11 @@ using System.Text;
namespace Cryville.Common.Network { namespace Cryville.Common.Network {
public class TlsTcpClient { public class TlsTcpClient {
readonly TcpClient _tcpClient;
readonly TlsClientProtocol _protocol; readonly TlsClientProtocol _protocol;
readonly TlsClient _tlsClient; readonly TlsClient _tlsClient;
public Stream Stream { get; private set; } public Stream Stream { get; private set; }
public TlsTcpClient(string hostname, int port) { public TlsTcpClient(TcpClient tcpClient, string hostname) {
_tcpClient = new TcpClient(hostname, port); _protocol = new TlsClientProtocol(tcpClient.GetStream());
_protocol = new TlsClientProtocol(_tcpClient.GetStream());
_tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom())); _tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom()));
} }