Skip to content

Commit 9eeaee3

Browse files
committed
fb
1 parent 54a9c75 commit 9eeaee3

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/Servers/HttpSys/samples/TlsFeaturesObserve/HttpSys/HttpSysConfigurator.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
54
using System.Net;
6-
using System.Net.Sockets;
75
using System.Runtime.InteropServices;
8-
using System.Text;
9-
using Microsoft.AspNetCore.Http;
106

117
namespace TlsFeaturesObserve.HttpSys;
128

@@ -19,21 +15,22 @@ internal static class HttpSysConfigurator
1915

2016
internal static void ConfigureCacheTlsClientHello()
2117
{
22-
IPEndPoint ipPort = new IPEndPoint(new IPAddress([0, 0, 0, 0]), 6000);
23-
string certThumbprint = "" /* your cert thumbprint here */;
24-
Guid appId = Guid.NewGuid();
25-
string sslCertStoreName = "My";
18+
// Arbitrarily chosen port, but must match the port used in the web server. Via UrlPrefixes or launchsettings.
19+
var ipPort = new IPEndPoint(new IPAddress([0, 0, 0, 0]), 6000);
20+
var certThumbprint = "" /* your cert thumbprint here */;
21+
var appId = Guid.NewGuid();
22+
var sslCertStoreName = "My";
2623

2724
CallHttpApi(() => SetConfiguration(ipPort, certThumbprint, appId, sslCertStoreName));
2825
}
2926

3027
static void SetConfiguration(IPEndPoint ipPort, string certThumbprint, Guid appId, string sslCertStoreName)
3128
{
32-
GCHandle sockAddrHandle = CreateSockaddrStructure(ipPort);
29+
var sockAddrHandle = CreateSockaddrStructure(ipPort);
3330
var pIpPort = sockAddrHandle.AddrOfPinnedObject();
3431
var httpServiceConfigSslKey = new HTTP_SERVICE_CONFIG_SSL_KEY(pIpPort);
3532

36-
byte[] hash = GetHash(certThumbprint);
33+
var hash = GetHash(certThumbprint);
3734
var handleHash = GCHandle.Alloc(hash, GCHandleType.Pinned);
3835
var configSslParam = new HTTP_SERVICE_CONFIG_SSL_PARAM
3936
{
@@ -58,15 +55,15 @@ static void SetConfiguration(IPEndPoint ipPort, string certThumbprint, Guid appI
5855
Marshal.SizeOf(typeof(HTTP_SERVICE_CONFIG_SSL_SET)));
5956
Marshal.StructureToPtr(configSslSet, pInputConfigInfo, false);
6057

61-
uint status = HttpSetServiceConfiguration(nint.Zero,
58+
var status = HttpSetServiceConfiguration(nint.Zero,
6259
HTTP_SERVICE_CONFIG_ID.HttpServiceConfigSSLCertInfo,
6360
pInputConfigInfo,
6461
Marshal.SizeOf(configSslSet),
6562
nint.Zero);
6663

6764
if (status == ERROR_ALREADY_EXISTS || status == 0) // already present or success
6865
{
69-
Console.WriteLine("HttpServiceConfiguration is correct");
66+
Console.WriteLine($"HttpServiceConfiguration is correct");
7067
}
7168
else
7269
{
@@ -76,9 +73,9 @@ static void SetConfiguration(IPEndPoint ipPort, string certThumbprint, Guid appI
7673

7774
static byte[] GetHash(string thumbprint)
7875
{
79-
int length = thumbprint.Length;
80-
byte[] bytes = new byte[length / 2];
81-
for (int i = 0; i < length; i += 2)
76+
var length = thumbprint.Length;
77+
var bytes = new byte[length / 2];
78+
for (var i = 0; i < length; i += 2)
8279
{
8380
bytes[i / 2] = Convert.ToByte(thumbprint.Substring(i, 2), 16);
8481
}
@@ -88,12 +85,12 @@ static byte[] GetHash(string thumbprint)
8885

8986
static GCHandle CreateSockaddrStructure(IPEndPoint ipEndPoint)
9087
{
91-
SocketAddress socketAddress = ipEndPoint.Serialize();
88+
var socketAddress = ipEndPoint.Serialize();
9289

9390
// use an array of bytes instead of the sockaddr structure
94-
byte[] sockAddrStructureBytes = new byte[socketAddress.Size];
95-
GCHandle sockAddrHandle = GCHandle.Alloc(sockAddrStructureBytes, GCHandleType.Pinned);
96-
for (int i = 0; i < socketAddress.Size; ++i)
91+
var sockAddrStructureBytes = new byte[socketAddress.Size];
92+
var sockAddrHandle = GCHandle.Alloc(sockAddrStructureBytes, GCHandleType.Pinned);
93+
for (var i = 0; i < socketAddress.Size; ++i)
9794
{
9895
sockAddrStructureBytes[i] = socketAddress[i];
9996
}
@@ -103,7 +100,7 @@ static GCHandle CreateSockaddrStructure(IPEndPoint ipEndPoint)
103100
static void CallHttpApi(Action body)
104101
{
105102
const uint flags = HTTP_INITIALIZE_CONFIG;
106-
uint retVal = HttpInitialize(HttpApiVersion, flags, IntPtr.Zero);
103+
var retVal = HttpInitialize(HttpApiVersion, flags, IntPtr.Zero);
107104
body();
108105
}
109106

src/Servers/HttpSys/samples/TlsFeaturesObserve/HttpSys/Native.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace TlsFeaturesObserve.HttpSys;
1010

11+
// Http.Sys types from https://learn.microsoft.com/windows/win32/api/http/
12+
1113
[StructLayout(LayoutKind.Sequential, Pack = 2)]
1214
public struct HTTPAPI_VERSION
1315
{

0 commit comments

Comments
 (0)