Abstract TLS client for more generic-purposed use.

This commit is contained in:
2022-10-28 00:33:02 +08:00
parent 92e5ed1f9c
commit d9d4eb236e
5 changed files with 12 additions and 12 deletions

View File

@@ -100,6 +100,7 @@ namespace Cryville.Common.Network {
} }
protected bool GetProxy(ref string host, ref int port) { protected bool GetProxy(ref string host, ref int port) {
// TODO use winhttp.dll
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");

View File

@@ -32,7 +32,7 @@ namespace Cryville.Common.Network {
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase); return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
} }
internal static bool ParseHeader(BinaryReader reader, Dictionary<string,string> headers) { internal static bool ParseHeader(BinaryReader reader, Dictionary<string, string> headers) {
// TODO Multiline header // TODO Multiline header
var header = ReadLine(reader); var header = ReadLine(reader);
if (header == "") return false; if (header == "") return false;

View File

@@ -1,14 +1,13 @@
using Microsoft.Win32;
using System; using System;
using System.IO; using System.IO;
namespace Cryville.Common.Network { namespace Cryville.Common.Network {
public class HttpsClient : HttpClient { public class HttpsClient : HttpClient {
readonly TlsTcpClient _tlsTcpClient; readonly TlsClient _tlsClient;
protected override Stream Stream { protected override Stream Stream {
get { get {
return _tlsTcpClient.Stream; return _tlsClient.Stream;
} }
} }
protected override string WindowsProxyProtocolName { protected override string WindowsProxyProtocolName {
@@ -18,16 +17,16 @@ namespace Cryville.Common.Network {
} }
public HttpsClient(Uri baseUri) : base(baseUri, 443) { public HttpsClient(Uri baseUri) : base(baseUri, 443) {
_tlsTcpClient = new TlsTcpClient(TcpClient, baseUri.Host); _tlsClient = new TlsClient(RawTcpStream, baseUri.Host);
} }
public override void Connect() { public override void Connect() {
base.Connect(); base.Connect();
_tlsTcpClient.Connect(); _tlsClient.Connect();
} }
public override void Close() { public override void Close() {
_tlsTcpClient.Close(); _tlsClient.Close();
base.Close(); base.Close();
} }
} }

View File

@@ -5,16 +5,16 @@ using Org.BouncyCastle.Tls.Crypto.Impl.BC;
using System.Collections; using System.Collections;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net.Sockets;
using System.Text; using System.Text;
using BcTlsClient = Org.BouncyCastle.Tls.TlsClient;
namespace Cryville.Common.Network { namespace Cryville.Common.Network {
public class TlsTcpClient { public class TlsClient {
readonly TlsClientProtocol _protocol; readonly TlsClientProtocol _protocol;
readonly TlsClient _tlsClient; readonly BcTlsClient _tlsClient;
public Stream Stream { get; private set; } public Stream Stream { get; private set; }
public TlsTcpClient(TcpClient tcpClient, string hostname) { public TlsClient(Stream baseStream, string hostname) {
_protocol = new TlsClientProtocol(tcpClient.GetStream()); _protocol = new TlsClientProtocol(baseStream);
_tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom())); _tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom()));
} }