8
8
using Microsoft . Extensions . Logging ;
9
9
using Monai . Deploy . Messaging . Common ;
10
10
using RabbitMQ . Client ;
11
+ using System . Net . Security ;
11
12
12
13
namespace Monai . Deploy . Messaging . RabbitMq
13
14
{
@@ -22,8 +23,10 @@ public interface IRabbitMqConnectionFactory
22
23
/// <param name="username">User name</param>
23
24
/// <param name="password">Password</param>
24
25
/// <param name="virtualHost">Virtual host</param>
26
+ /// <param name="useSSL">Encrypt communication</param>
27
+ /// <param name="portnumber">Port Number</param>
25
28
/// <returns>Instance of <see cref="IModel"/>.</returns>
26
- IModel CreateChannel ( string hostName , string username , string password , string virtualHost ) ;
29
+ IModel CreateChannel ( string hostName , string username , string password , string virtualHost , string useSSL , string portnumber ) ;
27
30
}
28
31
29
32
public class RabbitMqConnectionFactory : IRabbitMqConnectionFactory , IDisposable
@@ -40,19 +43,20 @@ public RabbitMqConnectionFactory(ILogger<RabbitMqConnectionFactory> logger)
40
43
_connections = new ConcurrentDictionary < string , Lazy < IConnection > > ( ) ;
41
44
}
42
45
43
- public IModel CreateChannel ( string hostName , string username , string password , string virtualHost )
46
+ public IModel CreateChannel ( string hostName , string username , string password , string virtualHost , string useSSL , string portnumber )
44
47
{
45
48
Guard . Against . NullOrWhiteSpace ( hostName , nameof ( hostName ) ) ;
46
49
Guard . Against . NullOrWhiteSpace ( username , nameof ( username ) ) ;
47
50
Guard . Against . NullOrWhiteSpace ( password , nameof ( password ) ) ;
48
51
Guard . Against . NullOrWhiteSpace ( virtualHost , nameof ( virtualHost ) ) ;
49
52
53
+
50
54
var key = $ "{ hostName } { username } { HashPassword ( password ) } { virtualHost } ";
51
55
52
56
var connection = _connections . AddOrUpdate ( key ,
53
57
x =>
54
58
{
55
- return CreatConnection ( hostName , username , password , virtualHost , key ) ;
59
+ return CreatConnection ( hostName , username , password , virtualHost , key , useSSL , portnumber ) ;
56
60
} ,
57
61
( updateKey , updateConnection ) =>
58
62
{
@@ -62,21 +66,38 @@ public IModel CreateChannel(string hostName, string username, string password, s
62
66
}
63
67
else
64
68
{
65
- return CreatConnection ( hostName , username , password , virtualHost , key ) ;
69
+ return CreatConnection ( hostName , username , password , virtualHost , key , useSSL , portnumber ) ;
66
70
}
67
71
} ) ;
68
72
69
73
return connection . Value . CreateModel ( ) ;
70
74
}
71
75
72
- private Lazy < IConnection > CreatConnection ( string hostName , string username , string password , string virtualHost , string key )
76
+ private Lazy < IConnection > CreatConnection ( string hostName , string username , string password , string virtualHost , string key , string useSSL , string portnumber )
73
77
{
78
+ int port ;
79
+ Boolean SslEnabled ;
80
+ Boolean . TryParse ( useSSL , out SslEnabled ) ;
81
+ if ( ! Int32 . TryParse ( portnumber , out port ) )
82
+ {
83
+ port = SslEnabled ? 5671 : 5672 ; // 5671 is default port for SSL/TLS , 5672 is default port for PLAIN.
84
+ }
85
+
86
+ SslOption sslOptions = new SslOption
87
+ {
88
+ Enabled = SslEnabled ,
89
+ ServerName = hostName ,
90
+ AcceptablePolicyErrors = SslPolicyErrors . RemoteCertificateNameMismatch | SslPolicyErrors . RemoteCertificateChainErrors | SslPolicyErrors . RemoteCertificateNotAvailable
91
+ } ;
92
+
74
93
var connectionFactory = _connectionFactoriess . GetOrAdd ( key , y => new Lazy < ConnectionFactory > ( ( ) => new ConnectionFactory ( )
75
94
{
76
95
HostName = hostName ,
77
96
UserName = username ,
78
97
Password = password ,
79
- VirtualHost = virtualHost
98
+ VirtualHost = virtualHost ,
99
+ Ssl = sslOptions ,
100
+ Port = port
80
101
} ) ) ;
81
102
82
103
return new Lazy < IConnection > ( ( ) => connectionFactory . Value . CreateConnection ( ) ) ;
0 commit comments