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