-
Notifications
You must be signed in to change notification settings - Fork 972
/
AlpnOpenListener.java
346 lines (305 loc) · 13 KB
/
AlpnOpenListener.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
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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
*
* http://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 io.undertow.server.protocol.http;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLEngine;
import org.xnio.ChannelListener;
import org.xnio.IoUtils;
import org.xnio.OptionMap;
import org.xnio.Pool;
import org.xnio.StreamConnection;
import org.xnio.channels.StreamSourceChannel;
import org.xnio.ssl.SslConnection;
import io.undertow.UndertowLogger;
import io.undertow.UndertowMessages;
import io.undertow.UndertowOptions;
import io.undertow.connector.ByteBufferPool;
import io.undertow.connector.PooledByteBuffer;
import io.undertow.protocols.alpn.ALPNManager;
import io.undertow.protocols.alpn.ALPNProvider;
import io.undertow.protocols.ssl.SslConduit;
import io.undertow.protocols.ssl.UndertowXnioSsl;
import io.undertow.server.AggregateConnectorStatistics;
import io.undertow.server.ConnectorStatistics;
import io.undertow.server.DelegateOpenListener;
import io.undertow.server.HttpHandler;
import io.undertow.server.OpenListener;
import io.undertow.server.XnioByteBufferPool;
/**
* Open listener adaptor for ALPN connections
* <p>
* Not a proper open listener as such, but more a mechanism for selecting between them.
*
* @author Stuart Douglas
*/
public class AlpnOpenListener implements ChannelListener<StreamConnection>, OpenListener {
/**
* HTTP/2 required cipher. Not strictly part of ALPN but it can live here for now till we have a better solution.
*/
public static final String REQUIRED_CIPHER = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
public static final String REQUIRED_PROTOCOL = "TLSv1.2";
private final ALPNManager alpnManager = ALPNManager.INSTANCE; //todo: configurable
private final ByteBufferPool bufferPool;
private final Map<String, ListenerEntry> listeners = new HashMap<>();
private String[] protocols;
private final String fallbackProtocol;
private volatile HttpHandler rootHandler;
private volatile OptionMap undertowOptions;
private volatile boolean statisticsEnabled;
public AlpnOpenListener(Pool<ByteBuffer> bufferPool, OptionMap undertowOptions, DelegateOpenListener httpListener) {
this(bufferPool, undertowOptions, "http/1.1", httpListener);
}
public AlpnOpenListener(Pool<ByteBuffer> bufferPool, OptionMap undertowOptions) {
this(bufferPool, undertowOptions, null, null);
}
public AlpnOpenListener(Pool<ByteBuffer> bufferPool, OptionMap undertowOptions, String fallbackProtocol, DelegateOpenListener fallbackListener) {
this(new XnioByteBufferPool(bufferPool), undertowOptions, fallbackProtocol, fallbackListener);
}
public AlpnOpenListener(ByteBufferPool bufferPool, OptionMap undertowOptions, DelegateOpenListener httpListener) {
this(bufferPool, undertowOptions, "http/1.1", httpListener);
}
public AlpnOpenListener(ByteBufferPool bufferPool) {
this(bufferPool, OptionMap.EMPTY, null, null);
}
public AlpnOpenListener(ByteBufferPool bufferPool, OptionMap undertowOptions) {
this(bufferPool, undertowOptions, null, null);
}
public AlpnOpenListener(ByteBufferPool bufferPool, OptionMap undertowOptions, String fallbackProtocol, DelegateOpenListener fallbackListener) {
this.bufferPool = bufferPool;
this.undertowOptions = undertowOptions;
this.fallbackProtocol = fallbackProtocol;
statisticsEnabled = undertowOptions.get(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false);
if (fallbackProtocol != null && fallbackListener != null) {
addProtocol(fallbackProtocol, fallbackListener, 0);
}
}
@Override
public HttpHandler getRootHandler() {
return rootHandler;
}
@Override
public void setRootHandler(HttpHandler rootHandler) {
this.rootHandler = rootHandler;
for (Map.Entry<String, ListenerEntry> delegate : listeners.entrySet()) {
delegate.getValue().listener.setRootHandler(rootHandler);
}
}
@Override
public OptionMap getUndertowOptions() {
return undertowOptions;
}
@Override
public void setUndertowOptions(OptionMap undertowOptions) {
if (undertowOptions == null) {
throw UndertowMessages.MESSAGES.argumentCannotBeNull("undertowOptions");
}
this.undertowOptions = undertowOptions;
for (Map.Entry<String, ListenerEntry> delegate : listeners.entrySet()) {
delegate.getValue().listener.setRootHandler(rootHandler);
}
statisticsEnabled = undertowOptions.get(UndertowOptions.ENABLE_CONNECTOR_STATISTICS, false);
}
@Override
public ByteBufferPool getBufferPool() {
return bufferPool;
}
@Override
public ConnectorStatistics getConnectorStatistics() {
if (statisticsEnabled) {
List<ConnectorStatistics> stats = new ArrayList<>();
for (Map.Entry<String, ListenerEntry> l : listeners.entrySet()) {
ConnectorStatistics c = l.getValue().listener.getConnectorStatistics();
if (c != null) {
stats.add(c);
}
}
return new AggregateConnectorStatistics(stats.toArray(new ConnectorStatistics[stats.size()]));
}
return null;
}
private static class ListenerEntry implements Comparable<ListenerEntry> {
final DelegateOpenListener listener;
final int weight;
final String protocol;
ListenerEntry(DelegateOpenListener listener, int weight, String protocol) {
this.listener = listener;
this.weight = weight;
this.protocol = protocol;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ListenerEntry)) return false;
ListenerEntry that = (ListenerEntry) o;
if (weight != that.weight) return false;
if (!listener.equals(that.listener)) return false;
return protocol.equals(that.protocol);
}
@Override
public int hashCode() {
int result = listener.hashCode();
result = 31 * result + weight;
result = 31 * result + protocol.hashCode();
return result;
}
@Override
public int compareTo(ListenerEntry o) {
return -Integer.compare(this.weight, o.weight);
}
}
public AlpnOpenListener addProtocol(String name, DelegateOpenListener listener, int weight) {
listeners.put(name, new ListenerEntry(listener, weight, name));
List<ListenerEntry> list = new ArrayList<>(listeners.values());
Collections.sort(list);
protocols = new String[list.size()];
for (int i = 0; i < list.size(); ++i) {
protocols[i] = list.get(i).protocol;
}
return this;
}
public void handleEvent(final StreamConnection channel) {
if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) {
UndertowLogger.REQUEST_LOGGER.tracef("Opened connection with %s", channel.getPeerAddress());
}
final SslConduit sslConduit = UndertowXnioSsl.getSslConduit((SslConnection) channel);
final SSLEngine sslEngine = sslConduit.getSSLEngine();
if (!engineSupportsHTTP2(sslEngine)) {
UndertowLogger.REQUEST_LOGGER.debugf("ALPN has been configured however %s is not present, falling back to default protocol", REQUIRED_CIPHER);
if (fallbackProtocol != null) {
ListenerEntry listener = listeners.get(fallbackProtocol);
if (listener != null) {
listener.listener.handleEvent(channel);
return;
}
}
}
ALPNProvider provider = alpnManager.getProvider(sslEngine);
if (provider == null) {
if (fallbackProtocol != null) {
ListenerEntry listener = listeners.get(fallbackProtocol);
if (listener != null) {
listener.listener.handleEvent(channel);
return;
}
}
UndertowLogger.REQUEST_LOGGER.debugf("No ALPN provider available and no fallback defined");
IoUtils.safeClose(channel);
return;
}
SSLEngine newEngine = provider.setProtocols(sslEngine, protocols);
if (newEngine != sslEngine) {
sslConduit.setSslEngine(newEngine);
}
final AlpnConnectionListener potentialConnection = new AlpnConnectionListener(channel, newEngine, provider);
channel.getSourceChannel().setReadListener(potentialConnection);
potentialConnection.handleEvent(channel.getSourceChannel());
}
public static boolean engineSupportsHTTP2(SSLEngine engine) {
//check to make sure the engine meets the minimum requirements for HTTP/2
//if not then ALPN will not be attempted
String[] protcols = engine.getEnabledProtocols();
boolean found = false;
for(String proto : protcols) {
if(proto.equals(REQUIRED_PROTOCOL)) {
found = true;
break;
}
}
if(!found) {
return false;
}
String[] ciphers = engine.getEnabledCipherSuites();
for (String i : ciphers) {
if (i.equals(REQUIRED_CIPHER)) {
return true;
}
}
return false;
}
private class AlpnConnectionListener implements ChannelListener<StreamSourceChannel> {
private final StreamConnection channel;
private final SSLEngine engine;
private final ALPNProvider provider;
private AlpnConnectionListener(StreamConnection channel, SSLEngine engine, ALPNProvider provider) {
this.channel = channel;
this.engine = engine;
this.provider = provider;
}
@Override
public void handleEvent(StreamSourceChannel source) {
PooledByteBuffer buffer = bufferPool.allocate();
boolean free = true;
try {
while (true) {
int res = channel.getSourceChannel().read(buffer.getBuffer());
if (res == -1) {
IoUtils.safeClose(channel);
return;
}
buffer.getBuffer().flip();
final String selected = provider.getSelectedProtocol(engine);
if (selected != null) {
DelegateOpenListener listener;
if (selected.isEmpty()) {
//alpn not in use
if (fallbackProtocol == null) {
UndertowLogger.REQUEST_IO_LOGGER.noALPNFallback(channel.getPeerAddress());
IoUtils.safeClose(channel);
return;
}
listener = listeners.get(fallbackProtocol).listener;
} else {
listener = listeners.get(selected).listener;
}
source.getReadSetter().set(null);
listener.handleEvent(channel, buffer);
free = false;
return;
} else if (res > 0) {
if (fallbackProtocol == null) {
UndertowLogger.REQUEST_IO_LOGGER.noALPNFallback(channel.getPeerAddress());
IoUtils.safeClose(channel);
return;
}
DelegateOpenListener listener = listeners.get(fallbackProtocol).listener;
source.getReadSetter().set(null);
listener.handleEvent(channel, buffer);
free = false;
return;
} else if (res == 0) {
channel.getSourceChannel().resumeReads();
return;
}
}
} catch (IOException e) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
IoUtils.safeClose(channel);
} finally {
if (free) {
buffer.close();
}
}
}
}
}