88using Microsoft . Extensions . Logging ;
99using Monai . Deploy . Messaging . Common ;
1010using RabbitMQ . Client ;
11+ using System . Net . Security ;
1112
1213namespace Monai . Deploy . Messaging . RabbitMq
1314{
@@ -22,8 +23,10 @@ public interface IRabbitMqConnectionFactory
2223 /// <param name="username">User name</param>
2324 /// <param name="password">Password</param>
2425 /// <param name="virtualHost">Virtual host</param>
26+ /// <param name="useSSL">Encrypt communication</param>
27+ /// <param name="portnumber">Port Number</param>
2528 /// <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 ) ;
2730 }
2831
2932 public class RabbitMqConnectionFactory : IRabbitMqConnectionFactory , IDisposable
@@ -40,19 +43,20 @@ public RabbitMqConnectionFactory(ILogger<RabbitMqConnectionFactory> logger)
4043 _connections = new ConcurrentDictionary < string , Lazy < IConnection > > ( ) ;
4144 }
4245
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 )
4447 {
4548 Guard . Against . NullOrWhiteSpace ( hostName , nameof ( hostName ) ) ;
4649 Guard . Against . NullOrWhiteSpace ( username , nameof ( username ) ) ;
4750 Guard . Against . NullOrWhiteSpace ( password , nameof ( password ) ) ;
4851 Guard . Against . NullOrWhiteSpace ( virtualHost , nameof ( virtualHost ) ) ;
4952
53+
5054 var key = $ "{ hostName } { username } { HashPassword ( password ) } { virtualHost } ";
5155
5256 var connection = _connections . AddOrUpdate ( key ,
5357 x =>
5458 {
55- return CreatConnection ( hostName , username , password , virtualHost , key ) ;
59+ return CreatConnection ( hostName , username , password , virtualHost , key , useSSL , portnumber ) ;
5660 } ,
5761 ( updateKey , updateConnection ) =>
5862 {
@@ -62,21 +66,38 @@ public IModel CreateChannel(string hostName, string username, string password, s
6266 }
6367 else
6468 {
65- return CreatConnection ( hostName , username , password , virtualHost , key ) ;
69+ return CreatConnection ( hostName , username , password , virtualHost , key , useSSL , portnumber ) ;
6670 }
6771 } ) ;
6872
6973 return connection . Value . CreateModel ( ) ;
7074 }
7175
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 )
7377 {
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+
7493 var connectionFactory = _connectionFactoriess . GetOrAdd ( key , y => new Lazy < ConnectionFactory > ( ( ) => new ConnectionFactory ( )
7594 {
7695 HostName = hostName ,
7796 UserName = username ,
7897 Password = password ,
79- VirtualHost = virtualHost
98+ VirtualHost = virtualHost ,
99+ Ssl = sslOptions ,
100+ Port = port
80101 } ) ) ;
81102
82103 return new Lazy < IConnection > ( ( ) => connectionFactory . Value . CreateConnection ( ) ) ;
0 commit comments