1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.ssl;
17
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelHandlerContext;
20 import io.netty.channel.ChannelInboundHandlerAdapter;
21 import io.netty.channel.ChannelInitializer;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.socket.ChannelInputShutdownEvent;
24 import io.netty.handler.codec.DecoderException;
25 import io.netty.util.internal.ObjectUtil;
26 import io.netty.util.internal.RecyclableArrayList;
27 import io.netty.util.internal.logging.InternalLogger;
28 import io.netty.util.internal.logging.InternalLoggerFactory;
29
30 import java.nio.channels.ClosedChannelException;
31 import javax.net.ssl.SSLException;
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 public abstract class ApplicationProtocolNegotiationHandler extends ChannelInboundHandlerAdapter {
69
70 private static final InternalLogger logger =
71 InternalLoggerFactory.getInstance(ApplicationProtocolNegotiationHandler.class);
72
73 private final String fallbackProtocol;
74 private final RecyclableArrayList bufferedMessages = RecyclableArrayList.newInstance();
75 private ChannelHandlerContext ctx;
76 private boolean sslHandlerChecked;
77 private boolean handshakeFailed;
78
79
80
81
82
83
84
85 protected ApplicationProtocolNegotiationHandler(String fallbackProtocol) {
86 this.fallbackProtocol = ObjectUtil.checkNotNull(fallbackProtocol, "fallbackProtocol");
87 }
88
89 @Override
90 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
91 this.ctx = ctx;
92 super.handlerAdded(ctx);
93 }
94
95 @Override
96 public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
97 fireBufferedMessages();
98 bufferedMessages.recycle();
99 super.handlerRemoved(ctx);
100 }
101
102 @Override
103 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
104
105 bufferedMessages.add(msg);
106 if (!sslHandlerChecked) {
107 sslHandlerChecked = true;
108 if (ctx.pipeline().get(SslHandler.class) == null) {
109
110
111 removeSelfIfPresent(ctx);
112 }
113 }
114 }
115
116
117
118
119 private void fireBufferedMessages() {
120 if (!bufferedMessages.isEmpty()) {
121 for (int i = 0; i < bufferedMessages.size(); i++) {
122 ctx.fireChannelRead(bufferedMessages.get(i));
123 }
124 ctx.fireChannelReadComplete();
125 bufferedMessages.clear();
126 }
127 }
128
129 @Override
130 public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
131 if (evt instanceof SslHandshakeCompletionEvent) {
132 SslHandshakeCompletionEvent handshakeEvent = (SslHandshakeCompletionEvent) evt;
133 try {
134 if (handshakeEvent.isSuccess()) {
135 SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
136 if (sslHandler == null) {
137 throw new IllegalStateException("cannot find an SslHandler in the pipeline (required for "
138 + "application-level protocol negotiation)");
139 }
140 String protocol = sslHandler.applicationProtocol();
141 configurePipeline(ctx, protocol != null ? protocol : fallbackProtocol);
142 } else {
143
144
145 Throwable cause = handshakeEvent.cause();
146 if (cause instanceof SslHandshakeTimeoutException || cause instanceof ClosedChannelException) {
147 try {
148 super.userEventTriggered(ctx, evt);
149 } finally {
150 notifyHandshakeFailure(ctx, cause);
151 }
152
153 return;
154 }
155 }
156 } catch (Throwable cause) {
157 exceptionCaught(ctx, cause);
158 } finally {
159
160
161 if (handshakeEvent.isSuccess()) {
162 removeSelfIfPresent(ctx);
163 }
164 }
165 }
166
167 if (evt instanceof ChannelInputShutdownEvent) {
168 fireBufferedMessages();
169 }
170
171 ctx.fireUserEventTriggered(evt);
172 }
173
174 @Override
175 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
176 fireBufferedMessages();
177 super.channelInactive(ctx);
178 }
179
180 private void removeSelfIfPresent(ChannelHandlerContext ctx) {
181 ChannelPipeline pipeline = ctx.pipeline();
182 if (!ctx.isRemoved()) {
183 pipeline.remove(this);
184 }
185 }
186
187
188
189
190
191
192
193
194
195 protected abstract void configurePipeline(ChannelHandlerContext ctx, String protocol) throws Exception;
196
197
198
199
200 protected void handshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
201 logger.warn("{} TLS handshake failed:", ctx.channel(), cause);
202 ctx.close();
203 }
204
205 private void notifyHandshakeFailure(ChannelHandlerContext ctx, Throwable cause) throws Exception {
206 if (!handshakeFailed) {
207 handshakeFailed = true;
208
209 try {
210 handshakeFailure(ctx, cause);
211 } catch (Throwable innerCause) {
212 exceptionCaught(ctx, innerCause);
213 }
214 }
215 }
216
217 @Override
218 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
219 Throwable wrapped;
220 if (cause instanceof DecoderException && ((wrapped = cause.getCause()) instanceof SSLException)) {
221 try {
222 notifyHandshakeFailure(ctx, wrapped);
223 return;
224 } finally {
225 removeSelfIfPresent(ctx);
226 }
227 }
228 logger.warn("{} Failed to select the application-level protocol:", ctx.channel(), cause);
229 ctx.fireExceptionCaught(cause);
230 ctx.close();
231 }
232 }