author | chegar |
Fri, 27 Sep 2019 09:55:35 +0100 | |
changeset 58365 | 73950479184b |
parent 53551 | ab949b3e39bc |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
36131 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
import java.net.*; |
|
25 |
import java.io.*; |
|
26 |
import java.util.*; |
|
27 |
import java.security.*; |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
28 |
import java.util.concurrent.CopyOnWriteArrayList; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
29 |
import static java.nio.charset.StandardCharsets.UTF_8; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
30 |
import static java.util.Arrays.asList; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
31 |
import static java.util.stream.Collectors.toList; |
36131 | 32 |
|
33 |
/** |
|
34 |
* A minimal proxy server that supports CONNECT tunneling. It does not do |
|
35 |
* any header transformations. In future this could be added. |
|
36 |
* Two threads are created per client connection. So, it's not |
|
37 |
* intended for large numbers of parallel connections. |
|
38 |
*/ |
|
39 |
public class ProxyServer extends Thread implements Closeable { |
|
40 |
||
41 |
ServerSocket listener; |
|
42 |
int port; |
|
43 |
volatile boolean debug; |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
44 |
private final Credentials credentials; // may be null |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
45 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
46 |
private static class Credentials { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
47 |
private final String name; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
48 |
private final String password; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
49 |
private Credentials(String name, String password) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
50 |
this.name = name; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
51 |
this.password = password; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
52 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
53 |
public String name() { return name; } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
54 |
public String password() { return password; } |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
55 |
} |
36131 | 56 |
|
57 |
/** |
|
58 |
* Create proxy on port (zero means don't care). Call getPort() |
|
59 |
* to get the assigned port. |
|
60 |
*/ |
|
61 |
public ProxyServer(Integer port) throws IOException { |
|
62 |
this(port, false); |
|
63 |
} |
|
64 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
65 |
public ProxyServer(Integer port, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
66 |
Boolean debug, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
67 |
String username, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
68 |
String password) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
69 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
70 |
{ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
71 |
this(port, debug, new Credentials(username, password)); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
72 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
73 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
74 |
public ProxyServer(Integer port, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
75 |
Boolean debug) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
76 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
77 |
{ |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
78 |
this(port, debug, null); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
79 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
80 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
81 |
public ProxyServer(Integer port, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
82 |
Boolean debug, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
83 |
Credentials credentials) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
84 |
throws IOException |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
85 |
{ |
36131 | 86 |
this.debug = debug; |
49765 | 87 |
listener = new ServerSocket(); |
88 |
listener.setReuseAddress(false); |
|
89 |
listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), port)); |
|
36131 | 90 |
this.port = listener.getLocalPort(); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
91 |
this.credentials = credentials; |
36131 | 92 |
setName("ProxyListener"); |
93 |
setDaemon(true); |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
94 |
connections = new CopyOnWriteArrayList<Connection>(); |
36131 | 95 |
start(); |
96 |
} |
|
97 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
98 |
public ProxyServer(String s) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
99 |
credentials = null; |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
100 |
connections = new CopyOnWriteArrayList<Connection>(); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
101 |
} |
36131 | 102 |
|
103 |
/** |
|
104 |
* Returns the port number this proxy is listening on |
|
105 |
*/ |
|
106 |
public int getPort() { |
|
107 |
return port; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Shuts down the proxy, probably aborting any connections |
|
112 |
* currently open |
|
113 |
*/ |
|
114 |
public void close() throws IOException { |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
115 |
if (debug) System.out.println("Proxy: closing server"); |
49944 | 116 |
done = true; |
36131 | 117 |
listener.close(); |
118 |
for (Connection c : connections) { |
|
49944 | 119 |
c.close(); |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
120 |
c.awaitCompletion(); |
36131 | 121 |
} |
122 |
} |
|
123 |
||
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
124 |
final CopyOnWriteArrayList<Connection> connections; |
36131 | 125 |
|
126 |
volatile boolean done; |
|
127 |
||
128 |
public void run() { |
|
129 |
if (System.getSecurityManager() == null) { |
|
130 |
execute(); |
|
131 |
} else { |
|
132 |
// so calling domain does not need to have socket permission |
|
133 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
134 |
public Void run() { |
|
135 |
execute(); |
|
136 |
return null; |
|
137 |
} |
|
138 |
}); |
|
139 |
} |
|
140 |
} |
|
141 |
||
142 |
public void execute() { |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
143 |
int id = 0; |
36131 | 144 |
try { |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
145 |
while (!done) { |
36131 | 146 |
Socket s = listener.accept(); |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
147 |
id++; |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
148 |
Connection c = new Connection(s, id); |
36131 | 149 |
if (debug) |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
150 |
System.out.println("Proxy: accepted new connection: " + s); |
36131 | 151 |
connections.add(c); |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
152 |
c.init(); |
36131 | 153 |
} |
154 |
} catch(Throwable e) { |
|
155 |
if (debug && !done) { |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
156 |
System.out.println("Proxy: Fatal error, listener got " + e); |
36131 | 157 |
e.printStackTrace(); |
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
/** |
|
163 |
* Transparently forward everything, once we know what the destination is |
|
164 |
*/ |
|
165 |
class Connection { |
|
166 |
||
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
167 |
private final int id; |
36131 | 168 |
Socket clientSocket, serverSocket; |
169 |
Thread out, in; |
|
170 |
volatile InputStream clientIn, serverIn; |
|
171 |
volatile OutputStream clientOut, serverOut; |
|
172 |
||
173 |
final static int CR = 13; |
|
174 |
final static int LF = 10; |
|
175 |
||
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
176 |
Connection(Socket s, int id) throws IOException { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
177 |
this.id = id; |
36131 | 178 |
this.clientSocket= s; |
179 |
this.clientIn = new BufferedInputStream(s.getInputStream()); |
|
180 |
this.clientOut = s.getOutputStream(); |
|
181 |
} |
|
182 |
||
183 |
byte[] readHeaders(InputStream is) throws IOException { |
|
184 |
byte[] outbuffer = new byte[8000]; |
|
185 |
int crlfcount = 0; |
|
186 |
int bytecount = 0; |
|
187 |
int c; |
|
188 |
while ((c=is.read()) != -1 && bytecount < outbuffer.length) { |
|
189 |
outbuffer[bytecount++] = (byte)c; |
|
190 |
if (debug) System.out.write(c); |
|
191 |
// were looking for CRLFCRLF sequence |
|
192 |
if (c == CR || c == LF) { |
|
193 |
switch(crlfcount) { |
|
194 |
case 0: |
|
195 |
if (c == CR) crlfcount ++; |
|
196 |
break; |
|
197 |
case 1: |
|
198 |
if (c == LF) crlfcount ++; |
|
199 |
break; |
|
200 |
case 2: |
|
201 |
if (c == CR) crlfcount ++; |
|
202 |
break; |
|
203 |
case 3: |
|
204 |
if (c == LF) crlfcount ++; |
|
205 |
break; |
|
206 |
} |
|
207 |
} else { |
|
208 |
crlfcount = 0; |
|
209 |
} |
|
210 |
if (crlfcount == 4) { |
|
211 |
break; |
|
212 |
} |
|
213 |
} |
|
214 |
byte[] ret = new byte[bytecount]; |
|
215 |
System.arraycopy(outbuffer, 0, ret, 0, bytecount); |
|
216 |
return ret; |
|
217 |
} |
|
218 |
||
219 |
boolean running() { |
|
220 |
return out.isAlive() || in.isAlive(); |
|
221 |
} |
|
222 |
||
49944 | 223 |
private volatile boolean closing; |
224 |
public synchronized void close() throws IOException { |
|
225 |
closing = true; |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
226 |
if (debug) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
227 |
System.out.println("Proxy: closing connection {" + this + "}"); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
228 |
if (serverSocket != null) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
229 |
serverSocket.close(); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
230 |
if (clientSocket != null) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
231 |
clientSocket.close(); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
232 |
} |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
233 |
|
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
234 |
public void awaitCompletion() { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
235 |
try { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
236 |
if (in != null) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
237 |
in.join(); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
238 |
if (out!= null) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
239 |
out.join(); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
240 |
} catch (InterruptedException e) { } |
36131 | 241 |
} |
242 |
||
243 |
int findCRLF(byte[] b) { |
|
244 |
for (int i=0; i<b.length-1; i++) { |
|
245 |
if (b[i] == CR && b[i+1] == LF) { |
|
246 |
return i; |
|
247 |
} |
|
248 |
} |
|
249 |
return -1; |
|
250 |
} |
|
251 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
252 |
// Checks credentials in the request against those allowable by the proxy. |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
253 |
private boolean authorized(Credentials credentials, |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
254 |
List<String> requestHeaders) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
255 |
List<String> authorization = requestHeaders.stream() |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
256 |
.filter(n -> n.toLowerCase(Locale.US).startsWith("proxy-authorization")) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
257 |
.collect(toList()); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
258 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
259 |
if (authorization.isEmpty()) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
260 |
return false; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
261 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
262 |
if (authorization.size() != 1) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
263 |
throw new IllegalStateException("Authorization unexpected count:" + authorization); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
264 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
265 |
String value = authorization.get(0).substring("proxy-authorization".length()).trim(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
266 |
if (!value.startsWith(":")) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
267 |
throw new IllegalStateException("Authorization malformed: " + value); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
268 |
value = value.substring(1).trim(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
269 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
270 |
if (!value.startsWith("Basic ")) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
271 |
throw new IllegalStateException("Authorization not Basic: " + value); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
272 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
273 |
value = value.substring("Basic ".length()); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
274 |
String values = new String(Base64.getDecoder().decode(value), UTF_8); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
275 |
int sep = values.indexOf(':'); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
276 |
if (sep < 1) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
277 |
throw new IllegalStateException("Authorization no colon: " + values); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
278 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
279 |
String name = values.substring(0, sep); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
280 |
String password = values.substring(sep + 1); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
281 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
282 |
if (name.equals(credentials.name()) && password.equals(credentials.password())) |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
283 |
return true; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
284 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
285 |
return false; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
286 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
287 |
|
36131 | 288 |
public void init() { |
289 |
try { |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
290 |
byte[] buf; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
291 |
while (true) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
292 |
buf = readHeaders(clientIn); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
293 |
if (findCRLF(buf) == -1) { |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
294 |
if (debug) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
295 |
System.out.println("Proxy: no CRLF closing, buf contains:[" |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
296 |
+ new String(buf, UTF_8) + "]" ); |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
297 |
close(); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
298 |
return; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
299 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
300 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
301 |
List<String> headers = asList(new String(buf, UTF_8).split("\r\n")); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
302 |
// check authorization credentials, if required by the server |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
303 |
if (credentials != null && !authorized(credentials, headers)) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
304 |
String resp = "HTTP/1.1 407 Proxy Authentication Required\r\n" + |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
305 |
"Content-Length: 0\r\n" + |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
306 |
"Proxy-Authenticate: Basic realm=\"proxy realm\"\r\n\r\n"; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
307 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
308 |
clientOut.write(resp.getBytes(UTF_8)); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
309 |
} else { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
310 |
break; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
311 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
312 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
313 |
|
36131 | 314 |
int p = findCRLF(buf); |
315 |
String cmd = new String(buf, 0, p, "US-ASCII"); |
|
316 |
String[] params = cmd.split(" "); |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
49944
diff
changeset
|
317 |
|
36131 | 318 |
if (params[0].equals("CONNECT")) { |
319 |
doTunnel(params[1]); |
|
320 |
} else { |
|
321 |
doProxy(params[1], buf, p, cmd); |
|
322 |
} |
|
48083 | 323 |
} catch (Throwable e) { |
36131 | 324 |
if (debug) { |
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
325 |
System.out.println("Proxy: " + e); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
326 |
e.printStackTrace(); |
36131 | 327 |
} |
328 |
try {close(); } catch (IOException e1) {} |
|
329 |
} |
|
330 |
} |
|
331 |
||
332 |
void doProxy(String dest, byte[] buf, int p, String cmdLine) |
|
333 |
throws IOException |
|
334 |
{ |
|
335 |
try { |
|
336 |
URI uri = new URI(dest); |
|
337 |
if (!uri.isAbsolute()) { |
|
338 |
throw new IOException("request URI not absolute"); |
|
339 |
} |
|
340 |
dest = uri.getAuthority(); |
|
341 |
// now extract the path from the URI and recreate the cmd line |
|
342 |
int sp = cmdLine.indexOf(' '); |
|
343 |
String method = cmdLine.substring(0, sp); |
|
344 |
cmdLine = method + " " + uri.getPath() + " HTTP/1.1"; |
|
345 |
int x = cmdLine.length() - 1; |
|
346 |
int i = p; |
|
347 |
while (x >=0) { |
|
348 |
buf[i--] = (byte)cmdLine.charAt(x--); |
|
349 |
} |
|
350 |
i++; |
|
351 |
||
352 |
commonInit(dest, 80); |
|
49944 | 353 |
OutputStream sout; |
354 |
synchronized (this) { |
|
355 |
if (closing) return; |
|
356 |
sout = serverOut; |
|
357 |
} |
|
358 |
// might fail if we're closing but we don't care. |
|
359 |
sout.write(buf, i, buf.length-i); |
|
36131 | 360 |
proxyCommon(); |
361 |
||
362 |
} catch (URISyntaxException e) { |
|
363 |
throw new IOException(e); |
|
364 |
} |
|
365 |
} |
|
366 |
||
49944 | 367 |
synchronized void commonInit(String dest, int defaultPort) throws IOException { |
368 |
if (closing) return; |
|
36131 | 369 |
int port; |
370 |
String[] hostport = dest.split(":"); |
|
371 |
if (hostport.length == 1) { |
|
372 |
port = defaultPort; |
|
373 |
} else { |
|
374 |
port = Integer.parseInt(hostport[1]); |
|
375 |
} |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
376 |
if (debug) |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
377 |
System.out.printf("Proxy: connecting to (%s/%d)\n", hostport[0], port); |
36131 | 378 |
serverSocket = new Socket(hostport[0], port); |
379 |
serverOut = serverSocket.getOutputStream(); |
|
380 |
||
381 |
serverIn = new BufferedInputStream(serverSocket.getInputStream()); |
|
382 |
} |
|
383 |
||
49944 | 384 |
synchronized void proxyCommon() throws IOException { |
385 |
if (closing) return; |
|
36131 | 386 |
out = new Thread(() -> { |
387 |
try { |
|
388 |
byte[] bb = new byte[8000]; |
|
389 |
int n; |
|
390 |
while ((n = clientIn.read(bb)) != -1) { |
|
391 |
serverOut.write(bb, 0, n); |
|
392 |
} |
|
49944 | 393 |
closing = true; |
36131 | 394 |
serverSocket.close(); |
395 |
clientSocket.close(); |
|
396 |
} catch (IOException e) { |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
397 |
if (!closing && debug) { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
398 |
System.out.println("Proxy: " + e); |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
399 |
e.printStackTrace(); |
36131 | 400 |
} |
401 |
} |
|
402 |
}); |
|
403 |
in = new Thread(() -> { |
|
404 |
try { |
|
405 |
byte[] bb = new byte[8000]; |
|
406 |
int n; |
|
407 |
while ((n = serverIn.read(bb)) != -1) { |
|
408 |
clientOut.write(bb, 0, n); |
|
409 |
} |
|
49944 | 410 |
closing = true; |
36131 | 411 |
serverSocket.close(); |
412 |
clientSocket.close(); |
|
413 |
} catch (IOException e) { |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
414 |
if (!closing && debug) { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
415 |
System.out.println("Proxy: " + e); |
36131 | 416 |
e.printStackTrace(); |
417 |
} |
|
418 |
} |
|
419 |
}); |
|
420 |
out.setName("Proxy-outbound"); |
|
421 |
out.setDaemon(true); |
|
422 |
in.setDaemon(true); |
|
423 |
in.setName("Proxy-inbound"); |
|
424 |
out.start(); |
|
425 |
in.start(); |
|
426 |
} |
|
427 |
||
428 |
void doTunnel(String dest) throws IOException { |
|
49944 | 429 |
if (closing) return; // no need to go further. |
36131 | 430 |
commonInit(dest, 443); |
49944 | 431 |
// might fail if we're closing, but we don't care. |
36131 | 432 |
clientOut.write("HTTP/1.1 200 OK\r\n\r\n".getBytes()); |
433 |
proxyCommon(); |
|
434 |
} |
|
53551
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
435 |
|
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
436 |
@Override |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
437 |
public String toString() { |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
438 |
return "Proxy connection " + id + ", client sock:" + clientSocket; |
ab949b3e39bc
8217976: test/jdk/java/net/httpclient/websocket/WebSocketProxyTest.java fails intermittently
chegar
parents:
53521
diff
changeset
|
439 |
} |
36131 | 440 |
} |
441 |
||
442 |
public static void main(String[] args) throws Exception { |
|
443 |
int port = Integer.parseInt(args[0]); |
|
444 |
boolean debug = args.length > 1 && args[1].equals("-debug"); |
|
445 |
System.out.println("Debugging : " + debug); |
|
446 |
ProxyServer ps = new ProxyServer(port, debug); |
|
447 |
System.out.println("Proxy server listening on port " + ps.getPort()); |
|
448 |
while (true) { |
|
449 |
Thread.sleep(5000); |
|
450 |
} |
|
451 |
} |
|
452 |
} |