1 19 20 package com.sslexplorer.agent.client.tunneling; 21 22 import java.io.IOException ; 23 import java.net.Socket ; 24 import java.text.MessageFormat ; 25 import java.util.Vector ; 26 27 import com.maverick.multiplex.Channel; 28 import com.sslexplorer.agent.client.util.IOStreamConnector; 29 import com.sslexplorer.agent.client.util.IOStreamConnectorListener; 30 import com.sslexplorer.agent.client.util.TunnelConfiguration; 31 32 38 public class LocalTunnelConnection { 39 40 private Channel tunnel; 42 private Socket client; 43 private IOStreamConnector tx; 44 private IOStreamConnector rx; 45 private IOStreamListener rxStreamListener = new IOStreamListener(false); 46 private IOStreamListener txStreamListener = new IOStreamListener(false); 47 private Vector listeners; 48 private IOStreamConnectorListener txListener; 49 private IOStreamConnectorListener rxListener; 50 private LocalTunnelServer vpnConnectionListener; 51 private boolean hasStopped = false; 52 private boolean stopping; 53 org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(LocalTunnelConnection.class); 55 57 67 public LocalTunnelConnection(LocalTunnelServer vpnConnectionListener, Channel tunnel, Socket client, TunnelConfiguration configuration, IOStreamConnectorListener txListener, IOStreamConnectorListener rxListener) { 68 this.tunnel = tunnel; 69 this.client = client; 70 this.txListener = txListener; 71 this.rxListener = rxListener; 72 this.vpnConnectionListener = vpnConnectionListener; 73 this.listeners = new Vector (); 74 } 75 76 79 public String getClientHost() { 80 return client.getInetAddress().getHostName(); 81 } 82 83 86 public LocalTunnelServer getVPNConnectionListener() { 87 return vpnConnectionListener; 88 } 89 90 93 public void start() throws IOException { 94 try { 95 rx = new IOStreamConnector(); 96 rx.addListener(rxListener); 97 rx.addListener(rxStreamListener); 98 rx.connect(tunnel.getInputStream(), client.getOutputStream()); 99 100 tx = new IOStreamConnector(); 101 tx.addListener(txListener); 102 tx.addListener(txStreamListener); 103 tx.connect(client.getInputStream(), tunnel.getOutputStream()); 104 105 for (int i = 0; i < listeners.size(); i++) 106 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionStarted(this); 107 108 } catch (Throwable ex) { 109 log.error("ActiveTunnel threads failed to start", ex); 111 113 try { 114 client.close(); 115 } catch (Throwable e) { 116 } 117 118 try { 119 tunnel.close(); 120 } catch (Throwable e) { 121 } 122 123 throw new IOException (MessageFormat.format(Messages.getString("LocalTCPTunnel.tunnelFailedToStart"), new Object [] { ex.getMessage() })); } 125 } 126 127 130 public synchronized void stop() { 131 132 if(!hasStopped) { 133 stopping = true; 134 if (!rx.isClosed()) { 135 rx.close(); 136 } 137 if (!tx.isClosed()) { 138 tx.close(); 139 } 140 for (int i = 0; i < listeners.size(); i++) 141 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionStopped(this); 142 } 143 144 try { 145 client.close(); 146 } catch (IOException e) { 147 } 148 149 tunnel.close(); 150 151 stopping = false; 152 hasStopped = true; 153 } 154 155 protected void addListener(LocalTunnelConnectionEventListener listener) { 156 if (listener != null) 157 listeners.addElement(listener); 158 } 159 160 class IOStreamListener implements IOStreamConnectorListener { 161 162 private boolean send; 163 164 public IOStreamListener(boolean send) { 165 this.send = send; 166 } 167 168 public void connectorClosed(IOStreamConnector connector) { 169 try { 170 client.close(); 171 } catch (IOException ex) { 172 } 173 try { 174 tunnel.close(); 175 } catch (Exception ex1) { 176 } 177 if (connector != rx) { 178 rx.close(); 179 } 180 if (connector != tx) { 181 tx.close(); 182 } 183 if(!stopping) { 184 stop(); 185 } 186 } 187 188 public void dataTransfered(byte[] buffer, int count) { 189 for (int i = listeners.size() - 1; i >= 0; i--) 190 ((LocalTunnelConnectionEventListener) listeners.elementAt(i)).localTunnelConnectionDataTransferred(LocalTunnelConnection.this, buffer, count, send); 191 } 192 } 193 194 } 195 | Popular Tags |