1
+ using Microsoft . Extensions . Logging ;
1
2
using System ;
2
3
using System . Collections . Generic ;
3
4
using System . Diagnostics ;
6
7
using System . Net ;
7
8
using System . Net . Security ;
8
9
using System . Net . Sockets ;
9
- using System . Text ;
10
10
using System . Threading ;
11
11
using System . Threading . Tasks ;
12
- using Microsoft . Extensions . Logging ;
13
12
14
13
namespace Enyim . Caching . Memcached
15
14
{
@@ -27,13 +26,24 @@ public partial class PooledSocket : IDisposable
27
26
28
27
private NetworkStream _inputStream ;
29
28
private SslStream _sslStream ;
29
+ private readonly SslClientAuthenticationOptions _sslClientAuthOptions ;
30
30
31
- public PooledSocket ( EndPoint endpoint , TimeSpan connectionTimeout , TimeSpan receiveTimeout , ILogger logger , bool useSslStream , bool useIPv6 )
31
+ public PooledSocket ( EndPoint endpoint , TimeSpan connectionTimeout , TimeSpan receiveTimeout , ILogger logger , bool useSslStream , bool useIPv6 , SslClientAuthenticationOptions sslClientAuthOptions )
32
32
{
33
33
_logger = logger ;
34
34
_isAlive = true ;
35
35
_useSslStream = useSslStream ;
36
36
_useIPv6 = useIPv6 ;
37
+ _sslClientAuthOptions = sslClientAuthOptions ;
38
+
39
+ if ( _useSslStream && _sslClientAuthOptions == null )
40
+ {
41
+ // When not provided, create a default instance with target host set to the endpoint's host
42
+ _sslClientAuthOptions = new SslClientAuthenticationOptions
43
+ {
44
+ TargetHost = ( ( DnsEndPoint ) _endpoint ) . Host ,
45
+ } ;
46
+ }
37
47
38
48
var socket = new Socket ( useIPv6 ? AddressFamily . InterNetworkV6 : AddressFamily . InterNetwork , SocketType . Stream , ProtocolType . Tcp ) ;
39
49
socket . SetSocketOption ( SocketOptionLevel . Socket , SocketOptionName . KeepAlive , true ) ;
@@ -99,7 +109,7 @@ void Cancel()
99
109
if ( _useSslStream )
100
110
{
101
111
_sslStream = new SslStream ( new NetworkStream ( _socket ) ) ;
102
- _sslStream . AuthenticateAsClient ( ( ( DnsEndPoint ) _endpoint ) . Host ) ;
112
+ _sslStream . AuthenticateAsClient ( _sslClientAuthOptions ) ;
103
113
}
104
114
else
105
115
{
@@ -158,7 +168,7 @@ public async Task ConnectAsync()
158
168
if ( _useSslStream )
159
169
{
160
170
_sslStream = new SslStream ( new NetworkStream ( _socket ) ) ;
161
- await _sslStream . AuthenticateAsClientAsync ( ( ( DnsEndPoint ) _endpoint ) . Host ) ;
171
+ await _sslStream . AuthenticateAsClientAsync ( _sslClientAuthOptions ) ;
162
172
}
163
173
else
164
174
{
0 commit comments