📄 localrequesthandlertunnel.java
字号:
// ========================================================================
// $Id: LocalRequestHandlerTunnel.java,v 1.6 2006/07/25 12:46:13 seb Exp $
// Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ========================================================================
package com.sslexplorer.forwarding;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.OutputStream;
import java.net.Socket;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.LabelValueBean;
import com.sslexplorer.boot.RequestHandlerTunnel;
import com.sslexplorer.boot.Util;
import com.sslexplorer.core.CoreAttributeConstants;
import com.sslexplorer.core.CoreEvent;
import com.sslexplorer.core.CoreEventConstants;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.policyframework.ResourceChangeEvent;
import com.sslexplorer.tunnels.TransportType;
/* ------------------------------------------------------------ */
/** HTTP Tunnel.
* A HTTP Tunnel can be used to take over a HTTP connection in order to
* tunnel another protocol over it. The prime example is the CONNECT method
* handled by the ProxyHandler to setup a SSL tunnel between the client and
* the real server.
*
* Modified for use in SSL-Explorer.
*
* @see HttpConnection
* @version $Revision: 1.6 $
* @author Greg Wilkins (gregw)
*/
public class LocalRequestHandlerTunnel implements RequestHandlerTunnel, ForwardingTunnel
{
private static Log log= LogFactory.getLog(LocalRequestHandlerTunnel.class);
private Socket _socket;
private Thread _thread;
private int _timeoutMs;
private InputStream _in;
private OutputStream _out;
protected VPNListeningSocket _vpnTunnel;
/* ------------------------------------------------------------ */
/** Constructor.
*/
protected LocalRequestHandlerTunnel()
{
}
/* ------------------------------------------------------------ */
/** Constructor.
* @param socket The tunnel socket.
* @param timeoutMs The maximum time to wait for a read on the tunnel. Note that
* sotimer exceptions are ignored by the tunnel.
*/
public LocalRequestHandlerTunnel(VPNListeningSocket vpnTunnel, Socket socket, int timeoutMs)
{
_vpnTunnel = vpnTunnel;
_socket= socket;
_timeoutMs=timeoutMs;
}
/* ------------------------------------------------------------ */
/** handle method.
* This method is called by the HttpConnection.handleNext() method if
* this HttpTunnel has been set on that connection.
* The default implementation of this method copies between the HTTP
* socket and the socket passed in the constructor.
* @param in
* @param out
*/
public void tunnel(InputStream in, OutputStream out)
{
_vpnTunnel.addActiveTunnel(this);
Copy copy= new Copy();
_in= in;
_out= out;
try
{
_thread= Thread.currentThread();
copy.start();
copydata(_socket.getInputStream(), _out);
}
catch (Exception e)
{
}
finally
{
try
{
_in.close();
_socket.shutdownOutput();
_socket.close();
}
catch (Exception e)
{
if (log.isDebugEnabled())
log.debug("Failed to close tunnel.", e);
}
copy.interrupt();
_vpnTunnel.removeActiveTunnel(this);
CoreServlet.getServlet().fireCoreEvent(new ResourceChangeEvent(this,
CoreEventConstants.TUNNEL_CLOSED, _vpnTunnel.getTunnel(), null, ResourceChangeEvent.STATE_SUCCESSFUL)
.addAttribute(CoreAttributeConstants.EVENT_ATTR_TUNNEL_TRANSPORT, _vpnTunnel.tunnel.getTransport())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_TUNNEL_DESTINATION, _vpnTunnel.tunnel.getDestination().toString())
.addAttribute(CoreAttributeConstants.EVENT_ATTR_TUNNEL_SOURCE_PORT, String.valueOf(_vpnTunnel.tunnel.getSourcePort()))
//.addAttribute(CoreAttributeConstants.EVENT_ATTR_TUNNEL_TYPE, String.valueOf(_vpnTunnel.tunnel.getType())));
.addAttribute(CoreAttributeConstants.EVENT_ATTR_TUNNEL_TYPE, ((LabelValueBean)TransportType.TYPES.get(_vpnTunnel.tunnel.getType())).getLabel()));
}
}
/* ------------------------------------------------------------ */
private void copydata(InputStream in, OutputStream out) throws java.io.IOException
{
long timestamp= 0;
while (true)
{
try
{
Util.copy(in, out);
timestamp= 0;
return;
}
catch (InterruptedIOException e)
{
if (timestamp == 0)
timestamp= System.currentTimeMillis();
else if (_timeoutMs > 0 && (System.currentTimeMillis() - timestamp) > _timeoutMs)
throw e;
}
}
}
/* ------------------------------------------------------------ */
/* ------------------------------------------------------------ */
/** Copy thread.
* Helper thread to copy from the HTTP input to the sockets output
*/
private class Copy extends Thread
{
public void run()
{
try
{
copydata(_in, _socket.getOutputStream());
}
catch (Exception e)
{
if (log.isDebugEnabled())
log.debug("Failed to copy data." , e);
}
finally
{
try
{
_out.close();
_socket.shutdownInput();
_socket.close();
}
catch (Exception e)
{
}
_thread.interrupt();
}
}
}
public void close() {
try {
_socket.close();
}
catch(IOException ioe) {
log.error("Failed to close tunnel. ", ioe);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -