embeddedvpnclient.java

来自「这是linux下ssl vpn的实现程序」· Java 代码 · 共 455 行 · 第 1/2 页

JAVA
455
字号
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.vpn.embed;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.maverick.http.AuthenticationCancelledException;
import com.maverick.http.AuthenticationPrompt;
import com.maverick.http.GetMethod;
import com.maverick.http.HttpClient;
import com.maverick.http.HttpException;
import com.maverick.http.HttpResponse;
import com.maverick.http.PostMethod;
import com.maverick.http.UnsupportedAuthenticationException;
import com.sslexplorer.vpn.base.AbstractVPNClient;
import com.sslexplorer.vpn.util.IOStreamConnectorListener;
import com.sslexplorer.vpn.util.Tunnel;
import com.sslexplorer.vpn.util.URI;
import com.sslexplorer.vpn.util.XMLElement;

/**
 * <p>
 * This class implements an embedded instance of the SSL Explorer VPN Client.
 * Typically this will be used within an application to provide support for the
 * tunnelling of application data through the SSL Explorer gateway.
 * </p>
 *
 * <p>
 * An embedded client may be created in one of two ways. Either using a
 * pre-existing <b>Pending Authorization Ticket</b> that is passed to this
 * client from SSL-Explorer, or by having the embedded client authenticate
 * with SSL-Explorer using a username and password.
 * </p>
 * <h3>Pending Authorization Ticket</h3>
 *
 * <p>
 * Take the following steps to connect to SSL-Explorer using this method :-</p>
 *  <ul>
 *      <li>Create a new instance of this object using the four argument
 *          constructor ({@link EmbeddedVPNClient#EmbeddedVPNClient(String, int, String, String)}.
 *          make sure you pass all four arguments. The ticket is the last argument
 *          and must be a valid pending authorization ticket that SSL-Explorer has
 *          created.</li>
 *      <li>Call {@link EmbeddedVPNClient#registerClient()}.</li>
 *  </ul>
 * </p>
 *
 * <h3>Direct Authentication</h3>
 *
 * <p>
 * Take the following steps to connect to SSL-Explorer using this method :-</p>
 *  <ul>
 *      <li>Create a new instance of this object using the 3 argument
 *          constructor ({@link EmbeddedVPNClient#EmbeddedVPNClient(String, int, LogonCredentialsPrompt)}.
 *          make sure you pass all arguments. You may alternatively use
 *          {@link EmbeddedVPNClient#EmbeddedVPNClient(String, int, String, String)} which
 *          will create a default LogonCredentialsPrompt using the supplied username
 *          and password.</li>
 *      <li>Call {@link EmbeddedVPNClient#connect()}.</li>
 *  </ul>
 * </p>
 *
 * @author Lee David Painter
 * @version $Revision: 1.21 $
 */
public class EmbeddedVPNClient extends AbstractVPNClient {

    static Hashtable connectionsByProxy = new Hashtable();

    private LogonCredentialsPrompt prompt;

    /* DEBUG */Log log = LogFactory.getLog(EmbeddedVPNClient.class);

    /**
     * Constructor to use for direct authentication.
     *
     * @param hostname hostname / address of the SSL-Explorer server
     * @param port port on which SSL-Explorer is running
     * @param prompt callback prompt object to supply username / password
     */
    public EmbeddedVPNClient(String hostname, int port, LogonCredentialsPrompt prompt) {
        init(hostname, port, null, null);
        this.prompt = prompt;
    }

    /**
     * Constructor to use for direct authentication. This is actually a convenience
     * constructor, it just creates a {@link LogonCredentialsPrompt} object
     * and passes it to the {@link EmbeddedVPNClient#EmbeddedVPNClient(String, int, LogonCredentialsPrompt)}
     * constructor.
     *
     * @param hostname hostname / address of the SSL-Explorer server
     * @param port port on which SSL-Explorer is running
     * @param username username to authenticate as
     * @param pasword password to use.
     */
    public EmbeddedVPNClient(String hostname, int port, final  String username, final  String password) {
        this(hostname, port, new LogonCredentialsPrompt() {

            public boolean promptForCredentials() {
                return true;
            }

            public String getUsername() {
                return username;
            }

            public String getPassword() {
                return password;
            }

        });
    }

    /**
     *
     * Constructor to use for <b>Pending Authorization Ticket</b> authentication.
     *
     * @param username username
     * @param ticket pending authorization ticket
     * @param hostname hostname / address of the SSL-Explorer server
     * @param port port on which SSL-Explorer is running
     */
    public EmbeddedVPNClient(String username, String ticket, String hostname, int port) {
        init(hostname, port, username, ticket);
    }


    /**
       * Create a connection to an SSL-Explorer server.
       *
       * @param hostname String
       * @param port int
       * @param username String
       * @param password String
       * @return EmbeddedVPNClient
       * @throws IOException
       * @throws CancelledException
       */
      public static EmbeddedVPNClient getCachedConnection(String hostname,
                                              int port,
                                              String username,
                                              String password,
                                              String proxyURL,
                                              AuthenticationPrompt proxyPrompt)
        throws IOException, CancelledException {

      // Check to see if we have a cached connection
      String key = hostname + ":" + port + ":" + username;

        try {
          if (connectionsByProxy.containsKey(key)) {
            return (EmbeddedVPNClient) connectionsByProxy.get(key);
          }

          EmbeddedVPNClient client = new EmbeddedVPNClient(hostname,
              port,
              username,
              password);

          if (proxyURL != null) {
            client.setLocalProxyURL(proxyURL);
            client.configureProxy();
            if (proxyPrompt != null) {
              client.setDefaultProxyAuthenticationPrompt(proxyPrompt);
            }
          }

          client.connect();

          // Cache the client
          connectionsByProxy.put(key, client);

          return client;
        }
        catch (AuthenticationCancelledException ex) {
          throw new IOException(ex.getMessage());
        }
        catch (UnsupportedAuthenticationException ex) {
          throw new IOException(ex.getMessage());
        }
        catch (HttpException ex) {
          throw new IOException(ex.getMessage());
        }

    }

    public static EmbeddedVPNClient getCachedTicketConnection(String hostname,
                                                  int port,
                                                  String username,
                                                  String ticket,
                                                  String proxyURL,
                                                  AuthenticationPrompt proxyPrompt)
            throws IOException, CancelledException {

          // Check to see if we have a cached connection
          String key = hostname + ":" + port + ":" + username + ":" + ticket;

            try {
              if (connectionsByProxy.containsKey(key)) {
                return (EmbeddedVPNClient) connectionsByProxy.get(key);
              }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?