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 ;
54using System . Net ;
6- using System . Net . Sockets ;
75using System . Runtime . InteropServices ;
8- using System . Text ;
9- using Microsoft . AspNetCore . Http ;
106
117namespace 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
0 commit comments