-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathSocksProxyTest.java
More file actions
217 lines (195 loc) · 9.04 KB
/
SocksProxyTest.java
File metadata and controls
217 lines (195 loc) · 9.04 KB
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
/*
* Copyright (c) 2024-2026 AsyncHttpClient Project. 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
*
* 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 org.asynchttpclient.proxy;
import io.github.artsok.RepeatedIfExceptionsTest;
import org.asynchttpclient.AbstractBasicTest;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.Response;
import org.asynchttpclient.testserver.SocksProxy;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.time.Duration;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
/**
* Tests for SOCKS proxy support with both HTTP and HTTPS.
* Validates fix for GitHub issue #2139 (SOCKS proxy support broken).
*/
public class SocksProxyTest extends AbstractBasicTest {
@Override
public AbstractHandler configureHandler() throws Exception {
return new ProxyTest.ProxyHandler();
}
/**
* Returns a port that is not in use by binding to port 0 and then closing the socket.
*/
private static int findFreePort() throws IOException {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
}
}
@RepeatedIfExceptionsTest(repeats = 5)
public void testSocks4ProxyWithHttp() throws Exception {
// Start SOCKS proxy in background thread
Thread socksProxyThread = new Thread(() -> {
try {
new SocksProxy(60000);
} catch (Exception e) {
logger.error("Failed to establish SocksProxy", e);
}
});
socksProxyThread.start();
// Give the proxy time to start
Thread.sleep(1000);
try (AsyncHttpClient client = asyncHttpClient()) {
String target = "http://localhost:" + port1 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("localhost", 8000).setProxyType(ProxyType.SOCKS_V4))
.execute();
Response response = f.get(60, TimeUnit.SECONDS);
assertNotNull(response);
assertEquals(200, response.getStatusCode());
}
}
/**
* Validates that when a SOCKS5 proxy is configured at an address where no
* SOCKS server is running, the HTTP request FAILS instead of silently
* bypassing the proxy and using normal routing.
* This is the core regression test for GitHub issue #2139.
*/
@Test
public void testSocks5ProxyNotRunningMustFailHttp() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "http://localhost:" + port1 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V5))
.execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Request should fail when SOCKS5 proxy is not running, not bypass proxy");
}
}
/**
* Validates that when a SOCKS4 proxy is configured at an address where no
* SOCKS server is running, the HTTP request FAILS instead of silently
* bypassing the proxy and using normal routing.
*/
@Test
public void testSocks4ProxyNotRunningMustFailHttp() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "http://localhost:" + port1 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V4))
.execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Request should fail when SOCKS4 proxy is not running, not bypass proxy");
}
}
/**
* Validates that when a SOCKS5 proxy is configured at an address where no
* SOCKS server is running, an HTTPS request FAILS instead of silently
* bypassing the proxy and using normal routing.
*/
@Test
public void testSocks5ProxyNotRunningMustFailHttps() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "https://localhost:" + port2 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V5))
.execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Request should fail when SOCKS5 proxy is not running, not bypass proxy");
}
}
/**
* Validates that when a SOCKS4 proxy is configured at an address where no
* SOCKS server is running, an HTTPS request FAILS instead of silently
* bypassing the proxy and using normal routing.
*/
@Test
public void testSocks4ProxyNotRunningMustFailHttps() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "https://localhost:" + port2 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V4))
.execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Request should fail when SOCKS4 proxy is not running, not bypass proxy");
}
}
/**
* Validates that per-request SOCKS5 proxy config with a non-existent proxy
* also correctly fails the request.
*/
@Test
public void testPerRequestSocks5ProxyNotRunningMustFail() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "http://localhost:" + port1 + '/';
Future<Response> f = client.prepareGet(target)
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V5))
.execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Per-request SOCKS5 proxy config should not be silently ignored");
}
}
/**
* Validates that client-level SOCKS5 proxy config with a non-existent proxy
* also correctly fails the request.
*/
@Test
public void testClientLevelSocks5ProxyNotRunningMustFail() throws Exception {
int freePort = findFreePort();
try (AsyncHttpClient client = asyncHttpClient(config()
.setProxyServer(new ProxyServer.Builder("127.0.0.1", freePort)
.setProxyType(ProxyType.SOCKS_V5))
.setConnectTimeout(Duration.ofMillis(5000))
.setRequestTimeout(Duration.ofMillis(10000)))) {
String target = "http://localhost:" + port1 + '/';
Future<Response> f = client.prepareGet(target).execute();
assertThrows(ExecutionException.class, () -> f.get(10, TimeUnit.SECONDS),
"Client-level SOCKS5 proxy config should not be silently ignored");
}
}
}