2
|
1 |
/*
|
|
2 |
* Copyright 2002 Sun Microsystems, Inc. All Rights Reserved.
|
|
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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
*
|
|
26 |
* This class includes a proxy server that processes HTTP CONNECT requests,
|
|
27 |
* and tunnels the data from the client to the server, once the CONNECT
|
|
28 |
* request is accepted.
|
|
29 |
* It is used by the TunnelThroughProxy test.
|
|
30 |
*/
|
|
31 |
|
|
32 |
import java.io.*;
|
|
33 |
import java.net.*;
|
|
34 |
import javax.net.ssl.*;
|
|
35 |
import javax.net.ServerSocketFactory;
|
|
36 |
import sun.net.www.*;
|
|
37 |
|
|
38 |
public class ProxyTunnelServer extends Thread {
|
|
39 |
|
|
40 |
private static ServerSocket ss = null;
|
|
41 |
/*
|
|
42 |
* holds the registered user's username and password
|
|
43 |
* only one such entry is maintained
|
|
44 |
*/
|
|
45 |
private String userPlusPass;
|
|
46 |
|
|
47 |
// client requesting for a tunnel
|
|
48 |
private Socket clientSocket = null;
|
|
49 |
|
|
50 |
/*
|
|
51 |
* Origin server's address and port that the client
|
|
52 |
* wants to establish the tunnel for communication.
|
|
53 |
*/
|
|
54 |
private InetAddress serverInetAddr;
|
|
55 |
private int serverPort;
|
|
56 |
|
|
57 |
/*
|
|
58 |
* denote whether the proxy needs to authorize
|
|
59 |
* CONNECT requests.
|
|
60 |
*/
|
|
61 |
static boolean needAuth = false;
|
|
62 |
|
|
63 |
public ProxyTunnelServer() throws IOException {
|
|
64 |
if (ss == null) {
|
|
65 |
ss = (ServerSocket) ServerSocketFactory.getDefault().
|
|
66 |
createServerSocket(0);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
|
|
70 |
public void needUserAuth(boolean auth) {
|
|
71 |
needAuth = auth;
|
|
72 |
}
|
|
73 |
|
|
74 |
/*
|
|
75 |
* register users with the proxy, by providing username and
|
|
76 |
* password. The username and password are used for authorizing the
|
|
77 |
* user when a CONNECT request is made and needAuth is set to true.
|
|
78 |
*/
|
|
79 |
public void setUserAuth(String uname, String passwd) {
|
|
80 |
userPlusPass = uname + ":" + passwd;
|
|
81 |
}
|
|
82 |
|
|
83 |
public void run() {
|
|
84 |
try {
|
|
85 |
clientSocket = ss.accept();
|
|
86 |
processRequests();
|
|
87 |
} catch (Exception e) {
|
|
88 |
System.out.println("Proxy Failed: " + e);
|
|
89 |
e.printStackTrace();
|
|
90 |
try {
|
|
91 |
ss.close();
|
|
92 |
}
|
|
93 |
catch (IOException excep) {
|
|
94 |
System.out.println("ProxyServer close error: " + excep);
|
|
95 |
excep.printStackTrace();
|
|
96 |
}
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
/*
|
|
101 |
* Processes the CONNECT requests, if needAuth is set to true, then
|
|
102 |
* the name and password are extracted from the Proxy-Authorization header
|
|
103 |
* of the request. They are checked against the one that is registered,
|
|
104 |
* if there is a match, connection is set in tunneling mode. If
|
|
105 |
* needAuth is set to false, Proxy-Authorization checks are not made
|
|
106 |
*/
|
|
107 |
private void processRequests() throws Exception {
|
|
108 |
|
|
109 |
InputStream in = clientSocket.getInputStream();
|
|
110 |
MessageHeader mheader = new MessageHeader(in);
|
|
111 |
String statusLine = mheader.getValue(0);
|
|
112 |
|
|
113 |
if (statusLine.startsWith("CONNECT")) {
|
|
114 |
// retrieve the host and port info from the status-line
|
|
115 |
// retrieveConnectInfo(statusLine);
|
|
116 |
if (needAuth) {
|
|
117 |
String authInfo;
|
|
118 |
if ((authInfo = mheader.findValue("Proxy-Authorization"))
|
|
119 |
!= null) {
|
|
120 |
if (authenticate(authInfo)) {
|
|
121 |
needAuth = false;
|
|
122 |
System.out.println(
|
|
123 |
"Proxy: client authentication successful");
|
|
124 |
}
|
|
125 |
}
|
|
126 |
}
|
|
127 |
respondForConnect(needAuth);
|
|
128 |
|
|
129 |
// connection set to the tunneling mode
|
|
130 |
if (!needAuth) {
|
|
131 |
// doTunnel();
|
|
132 |
/*
|
|
133 |
* done with tunneling, we process only one successful
|
|
134 |
* tunneling request
|
|
135 |
*/
|
|
136 |
ss.close();
|
|
137 |
} else {
|
|
138 |
// we may get another request with Proxy-Authorization set
|
|
139 |
in.close();
|
|
140 |
clientSocket.close();
|
|
141 |
restart();
|
|
142 |
}
|
|
143 |
} else {
|
|
144 |
System.out.println("proxy server: processes only "
|
|
145 |
+ "CONNECT method requests, recieved: "
|
|
146 |
+ statusLine);
|
|
147 |
}
|
|
148 |
}
|
|
149 |
|
|
150 |
private void respondForConnect(boolean needAuth) throws Exception {
|
|
151 |
|
|
152 |
OutputStream out = clientSocket.getOutputStream();
|
|
153 |
PrintWriter pout = new PrintWriter(out);
|
|
154 |
|
|
155 |
if (needAuth) {
|
|
156 |
pout.println("HTTP/1.1 407 Proxy Auth Required");
|
|
157 |
pout.println("Proxy-Authenticate: Basic realm=\"WallyWorld\"");
|
|
158 |
pout.println();
|
|
159 |
pout.flush();
|
|
160 |
out.close();
|
|
161 |
} else {
|
|
162 |
pout.println("HTTP/1.1 500 Server Error");
|
|
163 |
pout.println();
|
|
164 |
pout.flush();
|
|
165 |
out.close();
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
private void restart() throws IOException {
|
|
170 |
(new Thread(this)).start();
|
|
171 |
}
|
|
172 |
|
|
173 |
/*sc
|
|
174 |
* note: Tunneling has to be provided in both directions, i.e
|
|
175 |
* from client->server and server->client, even if the application
|
|
176 |
* data may be unidirectional, SSL handshaking data flows in either
|
|
177 |
* direction.
|
|
178 |
*/
|
|
179 |
private void doTunnel() throws Exception {
|
|
180 |
|
|
181 |
Socket serverSocket = new Socket(serverInetAddr, serverPort);
|
|
182 |
ProxyTunnel clientToServer = new ProxyTunnel(
|
|
183 |
clientSocket, serverSocket);
|
|
184 |
ProxyTunnel serverToClient = new ProxyTunnel(
|
|
185 |
serverSocket, clientSocket);
|
|
186 |
clientToServer.start();
|
|
187 |
serverToClient.start();
|
|
188 |
System.out.println("Proxy: Started tunneling.......");
|
|
189 |
|
|
190 |
clientToServer.join();
|
|
191 |
serverToClient.join();
|
|
192 |
System.out.println("Proxy: Finished tunneling........");
|
|
193 |
|
|
194 |
clientToServer.close();
|
|
195 |
serverToClient.close();
|
|
196 |
}
|
|
197 |
|
|
198 |
/*
|
|
199 |
* This inner class provides unidirectional data flow through the sockets
|
|
200 |
* by continuously copying bytes from the input socket onto the output
|
|
201 |
* socket, until both sockets are open and EOF has not been received.
|
|
202 |
*/
|
|
203 |
class ProxyTunnel extends Thread {
|
|
204 |
Socket sockIn;
|
|
205 |
Socket sockOut;
|
|
206 |
InputStream input;
|
|
207 |
OutputStream output;
|
|
208 |
|
|
209 |
public ProxyTunnel(Socket sockIn, Socket sockOut)
|
|
210 |
throws Exception {
|
|
211 |
this.sockIn = sockIn;
|
|
212 |
this.sockOut = sockOut;
|
|
213 |
input = sockIn.getInputStream();
|
|
214 |
output = sockOut.getOutputStream();
|
|
215 |
}
|
|
216 |
|
|
217 |
public void run() {
|
|
218 |
int BUFFER_SIZE = 400;
|
|
219 |
byte[] buf = new byte[BUFFER_SIZE];
|
|
220 |
int bytesRead = 0;
|
|
221 |
int count = 0; // keep track of the amount of data transfer
|
|
222 |
|
|
223 |
try {
|
|
224 |
while ((bytesRead = input.read(buf)) >= 0) {
|
|
225 |
output.write(buf, 0, bytesRead);
|
|
226 |
output.flush();
|
|
227 |
count += bytesRead;
|
|
228 |
}
|
|
229 |
} catch (IOException e) {
|
|
230 |
/*
|
|
231 |
* The peer end has closed the connection
|
|
232 |
* we will close the tunnel
|
|
233 |
*/
|
|
234 |
close();
|
|
235 |
}
|
|
236 |
}
|
|
237 |
|
|
238 |
public void close() {
|
|
239 |
try {
|
|
240 |
if (!sockIn.isClosed())
|
|
241 |
sockIn.close();
|
|
242 |
if (!sockOut.isClosed())
|
|
243 |
sockOut.close();
|
|
244 |
} catch (IOException ignored) { }
|
|
245 |
}
|
|
246 |
}
|
|
247 |
|
|
248 |
/*
|
|
249 |
***************************************************************
|
|
250 |
* helper methods follow
|
|
251 |
***************************************************************
|
|
252 |
*/
|
|
253 |
|
|
254 |
/*
|
|
255 |
* This method retrieves the hostname and port of the destination
|
|
256 |
* that the connect request wants to establish a tunnel for
|
|
257 |
* communication.
|
|
258 |
* The input, connectStr is of the form:
|
|
259 |
* CONNECT server-name:server-port HTTP/1.x
|
|
260 |
*/
|
|
261 |
private void retrieveConnectInfo(String connectStr) throws Exception {
|
|
262 |
int starti;
|
|
263 |
int endi;
|
|
264 |
String connectInfo;
|
|
265 |
String serverName = null;
|
|
266 |
try {
|
|
267 |
starti = connectStr.indexOf(' ');
|
|
268 |
endi = connectStr.lastIndexOf(' ');
|
|
269 |
connectInfo = connectStr.substring(starti+1, endi).trim();
|
|
270 |
// retrieve server name and port
|
|
271 |
endi = connectInfo.indexOf(':');
|
|
272 |
serverName = connectInfo.substring(0, endi);
|
|
273 |
serverPort = Integer.parseInt(connectInfo.substring(endi+1));
|
|
274 |
} catch (Exception e) {
|
|
275 |
throw new IOException("Proxy recieved a request: "
|
|
276 |
+ connectStr);
|
|
277 |
}
|
|
278 |
serverInetAddr = InetAddress.getByName(serverName);
|
|
279 |
}
|
|
280 |
|
|
281 |
public int getPort() {
|
|
282 |
return ss.getLocalPort();
|
|
283 |
}
|
|
284 |
|
|
285 |
/*
|
|
286 |
* do "basic" authentication, authInfo is of the form:
|
|
287 |
* Basic <encoded username":"password>
|
|
288 |
* reference RFC 2617
|
|
289 |
*/
|
|
290 |
private boolean authenticate(String authInfo) throws IOException {
|
|
291 |
boolean matched = false;
|
|
292 |
try {
|
|
293 |
authInfo.trim();
|
|
294 |
int ind = authInfo.indexOf(' ');
|
|
295 |
String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
|
|
296 |
// extract encoded (username:passwd
|
|
297 |
if (userPlusPass.equals(
|
|
298 |
new String(
|
|
299 |
(new sun.misc.BASE64Decoder()).
|
|
300 |
decodeBuffer(recvdUserPlusPass)
|
|
301 |
))) {
|
|
302 |
matched = true;
|
|
303 |
}
|
|
304 |
} catch (Exception e) {
|
|
305 |
throw new IOException(
|
|
306 |
"Proxy received invalid Proxy-Authorization value: "
|
|
307 |
+ authInfo);
|
|
308 |
}
|
|
309 |
return matched;
|
|
310 |
}
|
|
311 |
}
|