⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vpntcptunnel.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  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.base;

import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

import com.sslexplorer.vpn.util.*;

public class VPNTCPTunnel implements VPNTunnel {
    Socket tunnel;
    Socket client;
    String destinationHost;
    int destinationPort;
    IOStreamConnector tx;
    IOStreamConnector rx;
    IOStreamListener listener = new IOStreamListener();
    Vector listeners;
    IOStreamConnectorListener txListener;
    IOStreamConnectorListener rxListener;
    VPNConnectionListener vpnConnectionListener;

    /* DEBUG */org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AbstractVPNClient.class);

    VPNTCPTunnel(VPNConnectionListener vpnConnectionListener, Socket tunnel, Socket client, String destinationHost,
                    int destinationPort, IOStreamConnectorListener txListener, IOStreamConnectorListener rxListener) {
        this.tunnel = tunnel;
        this.client = client;
        this.destinationHost = destinationHost;
        this.destinationPort = destinationPort;
        this.txListener = txListener;
        this.rxListener = rxListener;
        this.vpnConnectionListener = vpnConnectionListener;
        this.listeners = new Vector();
    }

    public String getDestinationHost() {
        return destinationHost;
    }

    public int getDestinationPort() {
        return destinationPort;
    }

    public String getClientHost() {
        return client.getInetAddress().getHostName();
    }

    public void addListener(VPNTunnelListener listener) {
        if (listener != null)
            listeners.addElement(listener);
    }

    public VPNConnectionListener getVPNConnectionListener() {
        return vpnConnectionListener;
    }

    public void start() throws IOException {
        try {
            rx = new IOStreamConnector();
            rx.addListener(listener);
            if (rxListener != null)
                rx.addListener(rxListener);
            rx.setCloseInput(false);
            rx.connect(tunnel.getInputStream(), client.getOutputStream());

            tx = new IOStreamConnector();
            tx.addListener(listener);
            if (txListener != null)
                tx.addListener(txListener);
            tx.setCloseOutput(false);
            tx.connect(client.getInputStream(), tunnel.getOutputStream());

            for (int i = 0; i < listeners.size(); i++)
                ((VPNTunnelListener) listeners.elementAt(i)).started(this);
        } catch (Throwable ex) {
            /* DEBUG */log.error(ex);
            try {
                client.close();
            } catch (Throwable e) {
            }

            try {
                tunnel.close();
            } catch (Throwable e) {
            }

            throw new IOException("The tunnel failed to start: " + ex.getMessage());
        }
    }

    /**
     * Stop's the tunnel from transfering data, closing the channel and the
     * attached socket.
     */
    public void stop() {
        if (!rx.isClosed()) {
            rx.close();
        }

        if (!tx.isClosed()) {
            tx.close();
        }

        for (int i = 0; i < listeners.size(); i++)
            ((VPNTunnelListener) listeners.elementAt(i)).stopped(this);
    }

    class IOStreamListener implements IOStreamConnectorListener {

        boolean hasStopped = false;

        public synchronized void connectorClosed(IOStreamConnector connector) {

            try {
                client.close();
            } catch (IOException ex) {
            }

            try {
                tunnel.close();
            } catch (Exception ex1) {
            }

            if (!hasStopped) {
                stop();
                hasStopped = true;
            }

        }

        public void dataTransfered(byte[] buffer, int count) {
            for (int i = listeners.size() - 1; i >= 0; i--)
                ((VPNTunnelListener) listeners.elementAt(i)).dataTransferred(VPNTCPTunnel.this, buffer, count);
        }
    }

}

⌨️ 快捷键说明

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