-
Notifications
You must be signed in to change notification settings - Fork 630
/
SslProvider.java
669 lines (596 loc) · 21.9 KB
/
SslProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*
* Copyright (c) 2017-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty5.tcp;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import javax.net.ssl.SNIServerName;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLParameters;
import io.netty5.channel.Channel;
import io.netty5.channel.ChannelHandlerAdapter;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.channel.ChannelPipeline;
import io.netty5.handler.logging.LogLevel;
import io.netty5.handler.logging.LoggingHandler;
import io.netty5.handler.ssl.SslContext;
import io.netty5.handler.ssl.SslContextBuilder;
import io.netty5.handler.ssl.SslHandler;
import io.netty5.handler.ssl.SslHandshakeCompletionEvent;
import io.netty5.util.AsyncMapping;
import reactor.core.Exceptions;
import reactor.netty5.NettyPipeline;
import reactor.netty5.ReactorNetty;
import reactor.netty5.transport.logging.AdvancedBufferFormat;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.annotation.Nullable;
import static reactor.netty5.ReactorNetty.format;
/**
* SSL Provider
*
* @author Violeta Georgieva
*/
public final class SslProvider {
/**
* Creates a builder for {@link SslProvider SslProvider}
*
* @return a new SslProvider builder
*/
public static SslProvider.SslContextSpec builder() {
return new SslProvider.Build();
}
/**
* Creates a new {@link SslProvider SslProvider} with a prepending handler
* configurator callback to inject default settings to an existing provider
* configuration.
*
* @return a new SslProvider
*/
public static SslProvider addHandlerConfigurator(
SslProvider provider, Consumer<? super SslHandler> handlerConfigurator) {
Objects.requireNonNull(provider, "provider");
Objects.requireNonNull(handlerConfigurator, "handlerConfigurator");
return new SslProvider(provider, handlerConfigurator);
}
/**
* Return the default client ssl provider
*
* @return default client ssl provider
*/
public static SslProvider defaultClientProvider() {
return TcpClientSecure.DEFAULT_SSL_PROVIDER;
}
public interface Builder {
/**
* Set a configurator callback to mutate any property from the provided
* {@link SslHandler}
*
* @param handlerConfigurator A callback given the generated {@link SslHandler}
*
* @return {@literal this}
*/
Builder handlerConfigurator(Consumer<? super SslHandler> handlerConfigurator);
/**
* Set the options to use for configuring SSL handshake timeout. Default to 10000 ms.
*
* @param handshakeTimeout The timeout {@link Duration}
*
* @return {@literal this}
*/
Builder handshakeTimeout(Duration handshakeTimeout);
/**
* Set the options to use for configuring SSL handshake timeout. Default to 10000 ms.
*
* @param handshakeTimeoutMillis The timeout in milliseconds
*
* @return {@literal this}
*/
Builder handshakeTimeoutMillis(long handshakeTimeoutMillis);
/**
* Set the options to use for configuring SSL close_notify flush timeout. Default to 3000 ms.
*
* @param closeNotifyFlushTimeout The timeout {@link Duration}
*
* @return {@literal this}
*/
Builder closeNotifyFlushTimeout(Duration closeNotifyFlushTimeout);
/**
* Set the options to use for configuring SSL close_notify flush timeout. Default to 3000 ms.
*
* @param closeNotifyFlushTimeoutMillis The timeout in milliseconds
*
* @return {@literal this}
*/
Builder closeNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis);
/**
* Set the options to use for configuring SSL close_notify read timeout. Default to 0 ms.
*
* @param closeNotifyReadTimeout The timeout {@link Duration}
*
* @return {@literal this}
*/
Builder closeNotifyReadTimeout(Duration closeNotifyReadTimeout);
/**
* Set the options to use for configuring SSL close_notify read timeout. Default to 0 ms.
*
* @param closeNotifyReadTimeoutMillis The timeout in milliseconds
*
* @return {@literal this}
*/
Builder closeNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis);
/**
* Adds a mapping for the given domain name to an {@link SslProvider} builder.
* If a mapping already exists, it will be overridden.
* <p><strong>Note:</strong> This method is a sync alternative of {@link #setSniAsyncMappings(AsyncMapping)},
* which removes the async mappings.
* <p><strong>Note:</strong> This configuration is applicable only when configuring the server.
*
* @param domainName the domain name, it may contain wildcard
* @param sslProviderBuilder an {@link SslProvider} builder for building the {@link SslProvider}
* @return {@literal this}
*/
Builder addSniMapping(String domainName, Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder);
/**
* Adds the provided mappings of domain names to {@link SslProvider} builders to the existing mappings.
* If a mapping already exists, it will be overridden.
* <p><strong>Note:</strong> This method is a sync alternative of {@link #setSniAsyncMappings(AsyncMapping)},
* which removes the async mappings.
* <p><strong>Note:</strong> This configuration is applicable only when configuring the server.
*
* @param confPerDomainName mappings of domain names to {@link SslProvider} builders
* @return {@literal this}
*/
Builder addSniMappings(Map<String, Consumer<? super SslContextSpec>> confPerDomainName);
/**
* Sets the provided mappings of domain names to {@link SslProvider} builders.
* The existing mappings will be removed.
* <p><strong>Note:</strong> This method is a sync alternative of {@link #setSniAsyncMappings(AsyncMapping)},
* which removes the async mappings.
* <p><strong>Note:</strong> This configuration is applicable only when configuring the server.
*
* @param confPerDomainName mappings of domain names to {@link SslProvider} builders
* @return {@literal this}
*/
Builder setSniMappings(Map<String, Consumer<? super SslProvider.SslContextSpec>> confPerDomainName);
/**
* Sets the provided mappings of domain names to {@link SslProvider}.
* <p><strong>Note:</strong> This method is an alternative of {@link #addSniMapping(String, Consumer)},
* {@link #addSniMappings(Map)} and {@link #setSniMappings(Map)}.
* <p><strong>Note:</strong> This configuration is applicable only when configuring the server.
*
* @param mappings mappings of domain names to {@link SslProvider}
* @return {@literal this}
* @since 1.0.19
*/
Builder setSniAsyncMappings(AsyncMapping<String, SslProvider> mappings);
/**
* Sets the desired {@link SNIServerName}s.
* Note: This configuration is applicable only when configuring the client.
*
* @param serverNames the desired {@link SNIServerName}s
* @return {@literal this}
*/
Builder serverNames(SNIServerName... serverNames);
/**
* Builds new SslProvider
*
* @return builds new SslProvider
*/
SslProvider build();
}
public interface SslContextSpec {
/**
* SslContext builder that provides, specific for the protocol, default configuration
* e.g. {@link DefaultSslContextSpec}, {@link TcpSslContextSpec} etc.
* The default configuration is applied before any other custom configuration.
*
* @param spec SslContext builder that provides, specific for the protocol, default configuration
* @return {@literal this}
* @since 1.0.6
*/
Builder sslContext(ProtocolSslContextSpec spec);
/**
* The SslContext to set when configuring SSL
*
* @param sslContext The context to set when configuring SSL
*
* @return {@literal this}
*/
Builder sslContext(SslContext sslContext);
}
/**
* SslContext builder that provides, specific for the protocol, default configuration.
* The default configuration is applied prior any other custom configuration.
*
* @since 1.0.6
*/
public interface ProtocolSslContextSpec {
/**
* Configures the underlying {@link SslContextBuilder}.
*
* @param sslCtxBuilder a callback for configuring the underlying {@link SslContextBuilder}
* @return {@code this}
*/
ProtocolSslContextSpec configure(Consumer<SslContextBuilder> sslCtxBuilder);
/**
* Create a new {@link SslContext} instance with the configured settings.
*
* @return a new {@link SslContext} instance
* @throws SSLException thrown when {@link SslContext} instance cannot be created
*/
SslContext sslContext() throws SSLException;
}
final SslContext sslContext;
final SslContextBuilder sslContextBuilder;
final long handshakeTimeoutMillis;
final long closeNotifyFlushTimeoutMillis;
final long closeNotifyReadTimeoutMillis;
final Consumer<? super SslHandler> handlerConfigurator;
final int builderHashCode;
final SniProvider sniProvider;
final Map<String, SslProvider> confPerDomainName;
final AsyncMapping<String, SslProvider> sniMappings;
SslProvider(SslProvider.Build builder) {
this.sslContextBuilder = builder.sslCtxBuilder;
if (builder.sslContext == null) {
if (sslContextBuilder != null) {
try {
this.sslContext = sslContextBuilder.build();
}
catch (SSLException e) {
throw Exceptions.propagate(e);
}
}
else if (builder.protocolSslContextSpec != null) {
try {
this.sslContext = builder.protocolSslContextSpec.sslContext();
}
catch (SSLException e) {
throw Exceptions.propagate(e);
}
}
else {
throw new IllegalArgumentException("Neither SslContextBuilder nor SslContext is specified");
}
}
else {
this.sslContext = builder.sslContext;
}
if (builder.serverNames != null) {
Consumer<SslHandler> configurator =
h -> {
SSLEngine engine = h.engine();
SSLParameters sslParameters = engine.getSSLParameters();
sslParameters.setServerNames(builder.serverNames);
engine.setSSLParameters(sslParameters);
};
this.handlerConfigurator = builder.handlerConfigurator == null ? configurator :
configurator.andThen(builder.handlerConfigurator);
}
else {
this.handlerConfigurator = builder.handlerConfigurator;
}
this.handshakeTimeoutMillis = builder.handshakeTimeoutMillis;
this.closeNotifyFlushTimeoutMillis = builder.closeNotifyFlushTimeoutMillis;
this.closeNotifyReadTimeoutMillis = builder.closeNotifyReadTimeoutMillis;
this.builderHashCode = builder.hashCode();
this.confPerDomainName = builder.confPerDomainName;
this.sniMappings = builder.sniMappings;
if (!confPerDomainName.isEmpty()) {
this.sniProvider = new SniProvider(confPerDomainName, this);
}
else if (sniMappings != null) {
this.sniProvider = new SniProvider(sniMappings);
}
else {
this.sniProvider = null;
}
}
SslProvider(SslProvider from, Consumer<? super SslHandler> handlerConfigurator) {
this.sslContext = from.sslContext;
this.sslContextBuilder = from.sslContextBuilder;
if (from.handlerConfigurator == null) {
this.handlerConfigurator = handlerConfigurator;
}
else {
this.handlerConfigurator = h -> {
handlerConfigurator.accept(h);
from.handlerConfigurator.accept(h);
};
}
this.handshakeTimeoutMillis = from.handshakeTimeoutMillis;
this.closeNotifyFlushTimeoutMillis = from.closeNotifyFlushTimeoutMillis;
this.closeNotifyReadTimeoutMillis = from.closeNotifyReadTimeoutMillis;
this.builderHashCode = from.builderHashCode;
this.confPerDomainName = from.confPerDomainName;
this.sniMappings = from.sniMappings;
this.sniProvider = from.sniProvider;
}
/**
* Returns {@code SslContext} instance with configured settings.
*
* @return {@code SslContext} instance with configured settings.
*/
public SslContext getSslContext() {
return this.sslContext;
}
public void configure(SslHandler sslHandler) {
Objects.requireNonNull(sslHandler, "sslHandler");
sslHandler.setHandshakeTimeoutMillis(handshakeTimeoutMillis);
sslHandler.setCloseNotifyFlushTimeoutMillis(closeNotifyFlushTimeoutMillis);
sslHandler.setCloseNotifyReadTimeoutMillis(closeNotifyReadTimeoutMillis);
if (handlerConfigurator != null) {
handlerConfigurator.accept(sslHandler);
}
}
public void addSslHandler(Channel channel, @Nullable SocketAddress remoteAddress, boolean sslDebug) {
Objects.requireNonNull(channel, "channel");
if (sniProvider != null) {
sniProvider.addSniHandler(channel, sslDebug);
return;
}
SslHandler sslHandler;
if (remoteAddress instanceof InetSocketAddress sniInfo) {
sslHandler = getSslContext()
.newHandler(channel.bufferAllocator(), sniInfo.getHostString(), sniInfo.getPort());
if (log.isDebugEnabled()) {
log.debug(format(channel, "SSL enabled using engine {} and SNI {}"), sslHandler.engine(), sniInfo);
}
}
else {
sslHandler = getSslContext().newHandler(channel.bufferAllocator());
if (log.isDebugEnabled()) {
log.debug(format(channel, "SSL enabled using engine {}"), sslHandler.engine());
}
}
configure(sslHandler);
ChannelPipeline pipeline = channel.pipeline();
if (pipeline.get(NettyPipeline.ProxyHandler) != null) {
pipeline.addAfter(NettyPipeline.ProxyHandler, NettyPipeline.SslHandler, sslHandler);
}
else if (pipeline.get(NettyPipeline.NonSslRedirectDetector) != null) {
pipeline.addAfter(NettyPipeline.NonSslRedirectDetector, NettyPipeline.SslHandler, sslHandler);
}
else {
pipeline.addFirst(NettyPipeline.SslHandler, sslHandler);
}
addSslReadHandler(pipeline, sslDebug);
}
@Override
public String toString() {
return "SslProvider {" +
", handshakeTimeoutMillis=" + handshakeTimeoutMillis +
", closeNotifyFlushTimeoutMillis=" + closeNotifyFlushTimeoutMillis +
", closeNotifyReadTimeoutMillis=" + closeNotifyReadTimeoutMillis +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
SslProvider that = (SslProvider) o;
return builderHashCode == that.builderHashCode;
}
@Override
public int hashCode() {
return Objects.hash(builderHashCode);
}
static void addSslReadHandler(ChannelPipeline pipeline, boolean sslDebug) {
if (pipeline.get(NettyPipeline.LoggingHandler) != null) {
pipeline.addAfter(NettyPipeline.LoggingHandler, NettyPipeline.SslReader, new SslReadHandler());
if (sslDebug) {
pipeline.addBefore(NettyPipeline.SslHandler, NettyPipeline.SslLoggingHandler, LOGGING_HANDLER);
}
}
else {
pipeline.addAfter(NettyPipeline.SslHandler, NettyPipeline.SslReader, new SslReadHandler());
}
}
static final class Build implements SslContextSpec, Builder {
/**
* Default SSL handshake timeout (milliseconds), fallback to 10 seconds
*/
static final long DEFAULT_SSL_HANDSHAKE_TIMEOUT =
Long.parseLong(System.getProperty(
ReactorNetty.SSL_HANDSHAKE_TIMEOUT,
"10000"));
SslContextBuilder sslCtxBuilder;
ProtocolSslContextSpec protocolSslContextSpec;
SslContext sslContext;
Consumer<? super SslHandler> handlerConfigurator;
long handshakeTimeoutMillis = DEFAULT_SSL_HANDSHAKE_TIMEOUT;
long closeNotifyFlushTimeoutMillis = 3000L;
long closeNotifyReadTimeoutMillis;
List<SNIServerName> serverNames;
final Map<String, SslProvider> confPerDomainName = new HashMap<>();
AsyncMapping<String, SslProvider> sniMappings;
// SslContextSpec
@Override
public Builder sslContext(ProtocolSslContextSpec protocolSslContextSpec) {
this.protocolSslContextSpec = protocolSslContextSpec;
return this;
}
@Override
public final Builder sslContext(SslContext sslContext) {
this.sslContext = Objects.requireNonNull(sslContext, "sslContext");
return this;
}
// Builder
@Override
public final Builder handshakeTimeout(Duration handshakeTimeout) {
Objects.requireNonNull(handshakeTimeout, "handshakeTimeout");
return handshakeTimeoutMillis(handshakeTimeout.toMillis());
}
@Override
public final Builder handlerConfigurator(Consumer<? super SslHandler> handlerConfigurator) {
Objects.requireNonNull(handlerConfigurator, "handshakeTimeout");
this.handlerConfigurator = handlerConfigurator;
return this;
}
@Override
public final Builder handshakeTimeoutMillis(long handshakeTimeoutMillis) {
if (handshakeTimeoutMillis < 0L) {
throw new IllegalArgumentException("ssl handshake timeout must be positive"
+ " was: " + handshakeTimeoutMillis);
}
this.handshakeTimeoutMillis = handshakeTimeoutMillis;
return this;
}
@Override
public final Builder closeNotifyFlushTimeout(Duration closeNotifyFlushTimeout) {
Objects.requireNonNull(closeNotifyFlushTimeout, "closeNotifyFlushTimeout");
return closeNotifyFlushTimeoutMillis(closeNotifyFlushTimeout.toMillis());
}
@Override
public final Builder closeNotifyFlushTimeoutMillis(long closeNotifyFlushTimeoutMillis) {
if (closeNotifyFlushTimeoutMillis < 0L) {
throw new IllegalArgumentException("ssl close_notify flush timeout must be positive,"
+ " was: " + closeNotifyFlushTimeoutMillis);
}
this.closeNotifyFlushTimeoutMillis = closeNotifyFlushTimeoutMillis;
return this;
}
@Override
public final Builder closeNotifyReadTimeout(Duration closeNotifyReadTimeout) {
Objects.requireNonNull(closeNotifyReadTimeout, "closeNotifyReadTimeout");
return closeNotifyReadTimeoutMillis(closeNotifyReadTimeout.toMillis());
}
@Override
public final Builder closeNotifyReadTimeoutMillis(long closeNotifyReadTimeoutMillis) {
if (closeNotifyReadTimeoutMillis < 0L) {
throw new IllegalArgumentException("ssl close_notify read timeout must be positive,"
+ " was: " + closeNotifyReadTimeoutMillis);
}
this.closeNotifyReadTimeoutMillis = closeNotifyReadTimeoutMillis;
return this;
}
@Override
public Builder addSniMapping(String domainName, Consumer<? super SslContextSpec> sslProviderBuilder) {
addInternal(domainName, sslProviderBuilder);
this.sniMappings = null;
return this;
}
@Override
public Builder addSniMappings(Map<String, Consumer<? super SslContextSpec>> confPerDomainName) {
Objects.requireNonNull(confPerDomainName);
confPerDomainName.forEach(this::addInternal);
this.sniMappings = null;
return this;
}
@Override
public Builder setSniMappings(Map<String, Consumer<? super SslContextSpec>> confPerDomainName) {
Objects.requireNonNull(confPerDomainName);
this.confPerDomainName.clear();
confPerDomainName.forEach(this::addInternal);
this.sniMappings = null;
return this;
}
@Override
public Builder setSniAsyncMappings(AsyncMapping<String, SslProvider> mappings) {
this.sniMappings = Objects.requireNonNull(mappings);
this.confPerDomainName.clear();
return this;
}
@Override
public Builder serverNames(SNIServerName... serverNames) {
Objects.requireNonNull(serverNames);
this.serverNames = Arrays.asList(serverNames);
return this;
}
@Override
public SslProvider build() {
return new SslProvider(this);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Build build)) {
return false;
}
return handshakeTimeoutMillis == build.handshakeTimeoutMillis &&
closeNotifyFlushTimeoutMillis == build.closeNotifyFlushTimeoutMillis &&
closeNotifyReadTimeoutMillis == build.closeNotifyReadTimeoutMillis &&
Objects.equals(sslCtxBuilder, build.sslCtxBuilder) &&
Objects.equals(sslContext, build.sslContext) &&
Objects.equals(handlerConfigurator, build.handlerConfigurator) &&
Objects.equals(serverNames, build.serverNames) &&
confPerDomainName.equals(build.confPerDomainName) &&
Objects.equals(protocolSslContextSpec, build.protocolSslContextSpec);
}
@Override
public int hashCode() {
return Objects.hash(sslCtxBuilder, sslContext, handlerConfigurator,
handshakeTimeoutMillis, closeNotifyFlushTimeoutMillis, closeNotifyReadTimeoutMillis,
serverNames, confPerDomainName, protocolSslContextSpec);
}
void addInternal(String domainName, Consumer<? super SslProvider.SslContextSpec> sslProviderBuilder) {
Objects.requireNonNull(domainName, "domainName");
Objects.requireNonNull(sslProviderBuilder, "sslProviderBuilder");
SslProvider.SslContextSpec builder = SslProvider.builder();
sslProviderBuilder.accept(builder);
confPerDomainName.put(domainName, ((SslProvider.Builder) builder).build());
}
}
static final class SslReadHandler extends ChannelHandlerAdapter {
boolean handshakeDone;
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.read(); //consume handshake
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
if (!handshakeDone) {
ctx.read(); /* continue consuming. */
}
ctx.fireChannelReadComplete();
}
@Override
public void channelInboundEvent(ChannelHandlerContext ctx, Object evt) {
if (evt instanceof SslHandshakeCompletionEvent handshake) {
handshakeDone = true;
if (handshake.isSuccess()) {
ctx.fireChannelActive();
}
else {
ctx.fireChannelExceptionCaught(handshake.cause());
}
}
ctx.fireChannelInboundEvent(evt);
if (handshakeDone && ctx.pipeline().context(this) != null) {
ctx.pipeline().remove(this);
}
}
}
static final Logger log = Loggers.getLogger(SslProvider.class);
static final LoggingHandler LOGGING_HANDLER =
AdvancedBufferFormat.HEX_DUMP
.toLoggingHandler("reactor.netty5.tcp.ssl", LogLevel.DEBUG, Charset.defaultCharset());
}