/
UnixDomainTest.java
272 lines (243 loc) · 10.3 KB
/
UnixDomainTest.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
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.unixdomain.server;
import java.net.SocketAddress;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpProxy;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.dynamic.HttpClientTransportDynamic;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.io.ClientConnector;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.ProxyConnectionFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.util.component.LifeCycle;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledForJreRange;
import org.junit.jupiter.api.condition.JRE;
import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V1;
import static org.eclipse.jetty.client.ProxyProtocolClientConnectionFactory.V2;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledForJreRange(min = JRE.JAVA_16)
public class UnixDomainTest
{
private static final Class<?> unixDomainSocketAddressClass = probe();
private static Class<?> probe()
{
try
{
return ClassLoader.getPlatformClassLoader().loadClass("java.net.UnixDomainSocketAddress");
}
catch (Throwable x)
{
return null;
}
}
private ConnectionFactory[] factories = new ConnectionFactory[]{new HttpConnectionFactory()};
private Server server;
private Path unixDomainPath;
@BeforeEach
public void prepare()
{
Assumptions.assumeTrue(unixDomainSocketAddressClass != null);
}
private void start(Handler handler) throws Exception
{
server = new Server();
UnixDomainServerConnector connector = new UnixDomainServerConnector(server, factories);
String dir = System.getProperty("jetty.unixdomain.dir");
assertNotNull(dir);
unixDomainPath = Files.createTempFile(Path.of(dir), "unix_", ".sock");
assertTrue(unixDomainPath.toAbsolutePath().toString().length() < 108, "Unix-Domain path too long");
Files.delete(unixDomainPath);
connector.setUnixDomainPath(unixDomainPath);
server.addConnector(connector);
server.setHandler(handler);
server.start();
}
@AfterEach
public void dispose()
{
LifeCycle.stop(server);
}
@Test
public void testHTTPOverUnixDomain() throws Exception
{
String uri = "http://localhost:1234/path";
start(new AbstractHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
jettyRequest.setHandled(true);
// Verify the URI is preserved.
assertEquals(uri, request.getRequestURL().toString());
EndPoint endPoint = jettyRequest.getHttpChannel().getEndPoint();
// Verify the SocketAddresses.
SocketAddress local = endPoint.getLocalSocketAddress();
assertThat(local, Matchers.instanceOf(unixDomainSocketAddressClass));
SocketAddress remote = endPoint.getRemoteSocketAddress();
assertThat(remote, Matchers.instanceOf(unixDomainSocketAddressClass));
// Verify that other address methods don't throw.
local = assertDoesNotThrow(endPoint::getLocalAddress);
assertNull(local);
remote = assertDoesNotThrow(endPoint::getRemoteAddress);
assertNull(remote);
assertDoesNotThrow(endPoint::toString);
}
});
ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
httpClient.start();
try
{
ContentResponse response = httpClient.newRequest(uri)
.timeout(5, TimeUnit.SECONDS)
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
}
finally
{
httpClient.stop();
}
}
@Test
public void testHTTPOverUnixDomainWithHTTPProxy() throws Exception
{
int fakeProxyPort = 4567;
int fakeServerPort = 5678;
start(new AbstractHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
jettyRequest.setHandled(true);
// Proxied requests must have an absolute URI.
HttpURI uri = jettyRequest.getMetaData().getURI();
assertNotNull(uri.getScheme());
assertEquals(fakeServerPort, uri.getPort());
}
});
ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
httpClient.getProxyConfiguration().getProxies().add(new HttpProxy("localhost", fakeProxyPort));
httpClient.start();
try
{
ContentResponse response = httpClient.newRequest("localhost", fakeServerPort)
.timeout(5, TimeUnit.SECONDS)
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());
}
finally
{
httpClient.stop();
}
}
@Test
public void testHTTPOverUnixDomainWithProxyProtocol() throws Exception
{
String srcAddr = "/proxySrcAddr";
String dstAddr = "/proxyDstAddr";
factories = new ConnectionFactory[]{new ProxyConnectionFactory(), new HttpConnectionFactory()};
start(new AbstractHandler()
{
@Override
public void handle(String target, Request jettyRequest, HttpServletRequest request, HttpServletResponse response)
{
jettyRequest.setHandled(true);
EndPoint endPoint = jettyRequest.getHttpChannel().getEndPoint();
assertThat(endPoint, Matchers.instanceOf(ProxyConnectionFactory.ProxyEndPoint.class));
assertThat(endPoint.getLocalSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass));
assertThat(endPoint.getRemoteSocketAddress(), Matchers.instanceOf(unixDomainSocketAddressClass));
if ("/v1".equals(target))
{
// As PROXYv1 does not support UNIX, the wrapped EndPoint data is used.
Path localPath = toUnixDomainPath(endPoint.getLocalSocketAddress());
assertThat(localPath, Matchers.equalTo(unixDomainPath));
}
else if ("/v2".equals(target))
{
assertThat(toUnixDomainPath(endPoint.getLocalSocketAddress()).toString(), Matchers.equalTo(FS.separators(dstAddr)));
assertThat(toUnixDomainPath(endPoint.getRemoteSocketAddress()).toString(), Matchers.equalTo(FS.separators(srcAddr)));
}
else
{
Assertions.fail("Invalid PROXY protocol version " + target);
}
}
});
// Java 11+ portable way to implement SocketChannelWithAddress.Factory.
ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
httpClient.start();
try
{
// Try PROXYv1 with the PROXY information retrieved from the EndPoint.
// PROXYv1 does not support the UNIX family.
ContentResponse response1 = httpClient.newRequest("localhost", 0)
.path("/v1")
.tag(new V1.Tag())
.timeout(5, TimeUnit.SECONDS)
.send();
assertEquals(HttpStatus.OK_200, response1.getStatus());
// Try PROXYv2 with explicit PROXY information.
var tag = new V2.Tag(V2.Tag.Command.PROXY, V2.Tag.Family.UNIX, V2.Tag.Protocol.STREAM, srcAddr, 0, dstAddr, 0, null);
ContentResponse response2 = httpClient.newRequest("localhost", 0)
.path("/v2")
.tag(tag)
.timeout(5, TimeUnit.SECONDS)
.send();
assertEquals(HttpStatus.OK_200, response2.getStatus());
}
finally
{
httpClient.stop();
}
}
private static Path toUnixDomainPath(SocketAddress address)
{
try
{
Assertions.assertNotNull(unixDomainSocketAddressClass);
return (Path)unixDomainSocketAddressClass.getMethod("getPath").invoke(address);
}
catch (Throwable x)
{
Assertions.fail(x);
throw new AssertionError();
}
}
}