28
28
import io .netty .channel .socket .SocketChannel ;
29
29
import io .netty .channel .socket .nio .NioSocketChannel ;
30
30
import io .netty .handler .codec .LengthFieldBasedFrameDecoder ;
31
- import io .netty .handler .ssl .SMSslClientContextFactory ;
32
31
import io .netty .handler .ssl .SslContext ;
33
- import io .netty .handler .ssl .SslContextBuilder ;
34
32
import io .netty .handler .ssl .SslHandler ;
35
- import io .netty .handler .ssl .SslProvider ;
36
33
import io .netty .handler .timeout .IdleStateHandler ;
37
34
import io .netty .util .concurrent .Future ;
38
- import java .io .IOException ;
39
35
import java .nio .channels .ClosedChannelException ;
40
- import java .security .Security ;
41
36
import java .util .ArrayList ;
42
37
import java .util .List ;
43
38
import java .util .Map ;
@@ -70,6 +65,9 @@ public class ConnectionManager {
70
65
private EventLoopGroup workerGroup ;
71
66
private Boolean running = false ;
72
67
private Bootstrap bootstrap = new Bootstrap ();
68
+
69
+ private SslContextInitializer sslContextInitializer = new SslContextInitializer ();
70
+
73
71
private List <ChannelFuture > connChannelFuture = new ArrayList <ChannelFuture >();
74
72
private ScheduledExecutorService reconnSchedule = new ScheduledThreadPoolExecutor (1 );
75
73
private int cryptoType ;
@@ -228,85 +226,6 @@ public ChannelHandlerContext getConnectionCtx(String peer) {
228
226
return availableConnections .get (peer );
229
227
}
230
228
231
- private SslContext initSslContext (ConfigOption configOption ) throws NetworkException {
232
- try {
233
- Security .setProperty ("jdk.disabled.namedCurves" , "" );
234
- System .setProperty ("jdk.sunec.disableNative" , "false" );
235
-
236
- // Get file, file existence is already checked when check config file.
237
- // Init SslContext
238
- logger .info (" build ECDSA ssl context with configured certificates " );
239
- SslContext sslCtx =
240
- SslContextBuilder .forClient ()
241
- .trustManager (configOption .getCryptoMaterialConfig ().getCaInputStream ())
242
- .keyManager (
243
- configOption .getCryptoMaterialConfig ().getSdkCertInputStream (),
244
- configOption
245
- .getCryptoMaterialConfig ()
246
- .getSdkPrivateKeyInputStream ())
247
- .sslProvider (SslProvider .OPENSSL )
248
- // .sslProvider(SslProvider.JDK)
249
- .build ();
250
- return sslCtx ;
251
- } catch (IOException e ) {
252
- logger .error (
253
- "initSslContext failed, caCert: {}, sslCert: {}, sslKey: {}, error: {}, e: {}" ,
254
- configOption .getCryptoMaterialConfig ().getCaCertPath (),
255
- configOption .getCryptoMaterialConfig ().getSdkCertPath (),
256
- configOption .getCryptoMaterialConfig ().getSdkPrivateKeyPath (),
257
- e .getMessage (),
258
- e );
259
- throw new NetworkException (
260
- "SSL context init failed, please make sure your cert and key files are properly configured. error info: "
261
- + e .getMessage (),
262
- NetworkException .INIT_CONTEXT_FAILED );
263
- } catch (IllegalArgumentException e ) {
264
- logger .error ("initSslContext failed, error: {}, e: {}" , e .getMessage (), e );
265
- throw new NetworkException (
266
- "SSL context init failed, error info: " + e .getMessage (),
267
- NetworkException .INIT_CONTEXT_FAILED );
268
- }
269
- }
270
-
271
- private SslContext initSMSslContext (ConfigOption configOption ) throws NetworkException {
272
- try {
273
- // Get file, file existence is already checked when check config file.
274
- // Init SslContext
275
- return SMSslClientContextFactory .build (
276
- configOption .getCryptoMaterialConfig ().getCaInputStream (),
277
- configOption .getCryptoMaterialConfig ().getEnSSLCertInputStream (),
278
- configOption .getCryptoMaterialConfig ().getEnSSLPrivateKeyInputStream (),
279
- configOption .getCryptoMaterialConfig ().getSdkCertInputStream (),
280
- configOption .getCryptoMaterialConfig ().getSdkPrivateKeyInputStream ());
281
- } catch (Exception e ) {
282
- if (configOption .getCryptoMaterialConfig ().getCryptoProvider ().equalsIgnoreCase (HSM )) {
283
- logger .error (
284
- "initSMSslContext failed, caCert:{}, sslCert: {}, sslKeyIndex: {}, enCert: {}, enSslKeyIndex: {}, error: {}, e: {}" ,
285
- configOption .getCryptoMaterialConfig ().getCaCertPath (),
286
- configOption .getCryptoMaterialConfig ().getSdkCertPath (),
287
- configOption .getCryptoMaterialConfig ().getSslKeyIndex (),
288
- configOption .getCryptoMaterialConfig ().getEnSSLCertPath (),
289
- configOption .getCryptoMaterialConfig ().getEnSslKeyIndex (),
290
- e .getMessage (),
291
- e );
292
- } else {
293
- logger .error (
294
- "initSMSslContext failed, caCert:{}, sslCert: {}, sslKey: {}, enCert: {}, enSslKey: {}, error: {}, e: {}" ,
295
- configOption .getCryptoMaterialConfig ().getCaCertPath (),
296
- configOption .getCryptoMaterialConfig ().getSdkCertPath (),
297
- configOption .getCryptoMaterialConfig ().getSdkPrivateKeyPath (),
298
- configOption .getCryptoMaterialConfig ().getEnSSLCertPath (),
299
- configOption .getCryptoMaterialConfig ().getEnSSLPrivateKeyPath (),
300
- e .getMessage (),
301
- e );
302
- }
303
- throw new NetworkException (
304
- "SSL context init failed, please make sure your cert and key files are properly configured. error info: "
305
- + e .getMessage (),
306
- e );
307
- }
308
- }
309
-
310
229
private void initNetty (ConfigOption configOption ) throws NetworkException {
311
230
workerGroup = new NioEventLoopGroup ();
312
231
bootstrap .group (workerGroup );
@@ -324,8 +243,8 @@ private void initNetty(ConfigOption configOption) throws NetworkException {
324
243
}
325
244
sslContext =
326
245
(sslCryptoType == CryptoType .ECDSA_TYPE
327
- ? initSslContext (configOption )
328
- : initSMSslContext (configOption ));
246
+ ? sslContextInitializer . initSslContext (configOption )
247
+ : sslContextInitializer . initSMSslContext (configOption ));
329
248
SslContext finalSslContext = sslContext ;
330
249
ChannelInitializer <SocketChannel > initializer =
331
250
new ChannelInitializer <SocketChannel >() {
0 commit comments