From d9d4eb236e2a94b5f3642d73bb21b938acacd888 Mon Sep 17 00:00:00 2001 From: PopSlime Date: Fri, 28 Oct 2022 00:33:02 +0800 Subject: [PATCH] Abstract TLS client for more generic-purposed use. --- Assets/Cryville/Common/Network/HttpClient.cs | 1 + Assets/Cryville/Common/Network/HttpResponse.cs | 2 +- Assets/Cryville/Common/Network/HttpsClient.cs | 11 +++++------ .../Common/Network/{TlsTcpClient.cs => TlsClient.cs} | 10 +++++----- .../{TlsTcpClient.cs.meta => TlsClient.cs.meta} | 0 5 files changed, 12 insertions(+), 12 deletions(-) rename Assets/Cryville/Common/Network/{TlsTcpClient.cs => TlsClient.cs} (93%) rename Assets/Cryville/Common/Network/{TlsTcpClient.cs.meta => TlsClient.cs.meta} (100%) diff --git a/Assets/Cryville/Common/Network/HttpClient.cs b/Assets/Cryville/Common/Network/HttpClient.cs index ea63170..92cdbfb 100644 --- a/Assets/Cryville/Common/Network/HttpClient.cs +++ b/Assets/Cryville/Common/Network/HttpClient.cs @@ -100,6 +100,7 @@ namespace Cryville.Common.Network { } protected bool GetProxy(ref string host, ref int port) { + // TODO use winhttp.dll if (Environment.OSVersion.Platform == PlatformID.Win32NT) { var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings"); var proxyEnable = (int)reg.GetValue("ProxyEnable"); diff --git a/Assets/Cryville/Common/Network/HttpResponse.cs b/Assets/Cryville/Common/Network/HttpResponse.cs index 9d3e4db..b9fb8e5 100644 --- a/Assets/Cryville/Common/Network/HttpResponse.cs +++ b/Assets/Cryville/Common/Network/HttpResponse.cs @@ -32,7 +32,7 @@ namespace Cryville.Common.Network { return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase); } - internal static bool ParseHeader(BinaryReader reader, Dictionary headers) { + internal static bool ParseHeader(BinaryReader reader, Dictionary headers) { // TODO Multiline header var header = ReadLine(reader); if (header == "") return false; diff --git a/Assets/Cryville/Common/Network/HttpsClient.cs b/Assets/Cryville/Common/Network/HttpsClient.cs index 30945c2..35f6010 100644 --- a/Assets/Cryville/Common/Network/HttpsClient.cs +++ b/Assets/Cryville/Common/Network/HttpsClient.cs @@ -1,14 +1,13 @@ -using Microsoft.Win32; using System; using System.IO; namespace Cryville.Common.Network { public class HttpsClient : HttpClient { - readonly TlsTcpClient _tlsTcpClient; + readonly TlsClient _tlsClient; protected override Stream Stream { get { - return _tlsTcpClient.Stream; + return _tlsClient.Stream; } } protected override string WindowsProxyProtocolName { @@ -18,16 +17,16 @@ namespace Cryville.Common.Network { } public HttpsClient(Uri baseUri) : base(baseUri, 443) { - _tlsTcpClient = new TlsTcpClient(TcpClient, baseUri.Host); + _tlsClient = new TlsClient(RawTcpStream, baseUri.Host); } public override void Connect() { base.Connect(); - _tlsTcpClient.Connect(); + _tlsClient.Connect(); } public override void Close() { - _tlsTcpClient.Close(); + _tlsClient.Close(); base.Close(); } } diff --git a/Assets/Cryville/Common/Network/TlsTcpClient.cs b/Assets/Cryville/Common/Network/TlsClient.cs similarity index 93% rename from Assets/Cryville/Common/Network/TlsTcpClient.cs rename to Assets/Cryville/Common/Network/TlsClient.cs index 61f7150..a6b1fca 100644 --- a/Assets/Cryville/Common/Network/TlsTcpClient.cs +++ b/Assets/Cryville/Common/Network/TlsClient.cs @@ -5,16 +5,16 @@ using Org.BouncyCastle.Tls.Crypto.Impl.BC; using System.Collections; using System.IO; using System.Linq; -using System.Net.Sockets; using System.Text; +using BcTlsClient = Org.BouncyCastle.Tls.TlsClient; namespace Cryville.Common.Network { - public class TlsTcpClient { + public class TlsClient { readonly TlsClientProtocol _protocol; - readonly TlsClient _tlsClient; + readonly BcTlsClient _tlsClient; public Stream Stream { get; private set; } - public TlsTcpClient(TcpClient tcpClient, string hostname) { - _protocol = new TlsClientProtocol(tcpClient.GetStream()); + public TlsClient(Stream baseStream, string hostname) { + _protocol = new TlsClientProtocol(baseStream); _tlsClient = new InternalTlsClient(hostname, new BcTlsCrypto(new SecureRandom())); } diff --git a/Assets/Cryville/Common/Network/TlsTcpClient.cs.meta b/Assets/Cryville/Common/Network/TlsClient.cs.meta similarity index 100% rename from Assets/Cryville/Common/Network/TlsTcpClient.cs.meta rename to Assets/Cryville/Common/Network/TlsClient.cs.meta