Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Commit 7ccbdd9

Browse files
committed
tls improvements
1 parent cd2d97e commit 7ccbdd9

File tree

11 files changed

+761
-474
lines changed

11 files changed

+761
-474
lines changed

‎src/Titanium.Web.Proxy/ExplicitClientHandler.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ private async Task HandleClient(ExplicitProxyEndPoint endPoint, TcpClientConnect
123123
{
124124
connectRequest.IsHttps = true; // todo: move this line to the previous "if"
125125

126-
var sslProtocol = clientHelloInfo.SslProtocol;
127-
if ((sslProtocol&SupportedSslProtocols) == SslProtocols.None)
126+
var sslProtocol = clientHelloInfo.SslProtocol&SupportedSslProtocols;
127+
if (sslProtocol == SslProtocols.None)
128128
{
129129
throw new Exception("Unsupported client SSL version.");
130130
}

‎src/Titanium.Web.Proxy/Extensions/SslExtensions.cs‎

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
using System.Net.Security;
44
using System.Security.Authentication;
55
using System.Security.Cryptography.X509Certificates;
6+
using System.Text;
67
using System.Threading;
78
using System.Threading.Tasks;
89
using Titanium.Web.Proxy.StreamExtended;
10+
using Titanium.Web.Proxy.StreamExtended.Models;
911

1012
namespace Titanium.Web.Proxy.Extensions
1113
{
@@ -31,24 +33,24 @@ internal static class SslExtensions
3133
{
3234
if (clientHelloInfo.Extensions != null && clientHelloInfo.Extensions.TryGetValue("ALPN", out var alpnExtension))
3335
{
34-
var alpn = alpnExtension.Data.Split(',');
35-
if (alpn.Length != 0)
36+
var alpn = alpnExtension.Alpns;
37+
if (alpn.Count != 0)
3638
{
37-
varresult=newList<SslApplicationProtocol>(alpn.Length);
38-
foreach(stringpinalpn)
39-
{
40-
stringprotocol=p.Trim();
41-
if(protocol.Equals("http/1.1"))
42-
{
43-
result.Add(SslApplicationProtocol.Http11);
44-
}
45-
elseif(protocol.Equals("h2"))
46-
{
47-
result.Add(SslApplicationProtocol.Http2);
48-
}
49-
}
50-
51-
return result;
39+
returnalpn;
40+
}
41+
}
42+
43+
returnnull;
44+
}
45+
46+
internalstaticList<string>?GetSslProtocols(thisClientHelloInfoclientHelloInfo)
47+
{
48+
if(clientHelloInfo.Extensions!=null&&clientHelloInfo.Extensions.TryGetValue("supported_versions",outvarversions))
49+
{
50+
varprotocols=versions.Protocols;
51+
if(protocols.Count!=0)
52+
{
53+
return protocols;
5254
}
5355
}
5456

@@ -80,10 +82,54 @@ internal static Task AuthenticateAsServerAsync(this SslStream sslStream, SslServ
8082
#if !NET6_0_OR_GREATER
8183
namespace System.Net.Security
8284
{
83-
internal enum SslApplicationProtocol
85+
internal struct SslApplicationProtocol
8486
{
85-
Http11,
86-
Http2
87+
public static readonly SslApplicationProtocol Http11 = new SslApplicationProtocol(SslExtension.Http11Utf8);
88+
89+
public static readonly SslApplicationProtocol Http2 = new SslApplicationProtocol(SslExtension.Http2Utf8);
90+
91+
public static readonly SslApplicationProtocol Http3 = new SslApplicationProtocol(SslExtension.Http3Utf8);
92+
93+
private readonly byte[] readOnlyProtocol;
94+
95+
public ReadOnlyMemory<byte> Protocol => readOnlyProtocol;
96+
97+
public SslApplicationProtocol(byte[] protocol)
98+
{
99+
readOnlyProtocol = protocol;
100+
}
101+
102+
public bool Equals(SslApplicationProtocol other) => Protocol.Span.SequenceEqual(other.Protocol.Span);
103+
104+
public override bool Equals(object? obj) => obj is SslApplicationProtocol protocol && Equals(protocol);
105+
106+
public override int GetHashCode()
107+
{
108+
var arr = Protocol;
109+
if (arr.Length == 0)
110+
{
111+
return 0;
112+
}
113+
114+
int hash = 0;
115+
for (int i = 0; i < arr.Length; i++)
116+
{
117+
hash = ((hash << 5) + hash) ^ arr.Span[i];
118+
}
119+
120+
return hash;
121+
}
122+
123+
public override string ToString()
124+
{
125+
return Encoding.UTF8.GetString(readOnlyProtocol);
126+
}
127+
128+
public static bool operator ==(SslApplicationProtocol left, SslApplicationProtocol right) =>
129+
left.Equals(right);
130+
131+
public static bool operator !=(SslApplicationProtocol left, SslApplicationProtocol right) =>
132+
!(left == right);
87133
}
88134

89135
[SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:FileMayOnlyContainASingleType", Justification =

‎src/Titanium.Web.Proxy/Extensions/StringExtensions.cs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Buffers.Text;
3+
using System.Buffers;
24
using System.Globalization;
5+
using System.Text;
36

47
namespace Titanium.Web.Proxy.Extensions;
58

@@ -24,4 +27,31 @@ internal static int IndexOfIgnoreCase(this string str, string? value)
2427
{
2528
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(str, value, CompareOptions.IgnoreCase);
2629
}
30+
31+
internal static unsafe string ByteArrayToHexString(this ReadOnlySpan<byte> data)
32+
{
33+
if (data.Length == 0)
34+
{
35+
return string.Empty;
36+
}
37+
38+
int length = data.Length * 3;
39+
Span<byte> buf = stackalloc byte[length];
40+
var buf2 = buf;
41+
foreach (var b in data)
42+
{
43+
Utf8Formatter.TryFormat(b, buf2, out _, new StandardFormat('X', 2));
44+
buf2[2] = 32; // space
45+
buf2 = buf2.Slice(3);
46+
}
47+
48+
#if NET6_0_OR_GREATER
49+
return Encoding.UTF8.GetString(buf.Slice(0, length - 1));
50+
#else
51+
fixed (byte* bp = buf)
52+
{
53+
return Encoding.UTF8.GetString(bp, length -1);
54+
}
55+
#endif
56+
}
2757
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /