1 19 20 package com.sslexplorer.tunnels.agent; 21 22 import java.io.IOException ; 23 import java.net.InetAddress ; 24 import java.net.ServerSocket ; 25 import java.net.Socket ; 26 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import com.maverick.multiplex.Channel; 31 import com.maverick.multiplex.ChannelOpenException; 32 import com.maverick.multiplex.Request; 33 import com.maverick.util.ByteArrayWriter; 34 import com.sslexplorer.agent.AgentTunnel; 35 import com.sslexplorer.boot.CustomServerSocketFactory; 36 import com.sslexplorer.boot.Util; 37 import com.sslexplorer.core.stringreplacement.VariableReplacement; 38 import com.sslexplorer.policyframework.LaunchSession; 39 import com.sslexplorer.policyframework.LaunchSessionFactory; 40 import com.sslexplorer.tunnels.Tunnel; 41 import com.sslexplorer.tunnels.TunnelingService; 42 43 51 public class RemoteTunnel extends Thread { 52 53 final static Log log = LogFactory.getLog(RemoteTunnel.class); 54 55 private AgentTunnel agent; 56 private Tunnel tunnel; 57 private ServerSocket listeningSocket; 58 private boolean running; 59 private RemoteTunnelManager tunnelManager; 60 private LaunchSession launchSession; 61 62 71 public RemoteTunnel(Tunnel tunnel, AgentTunnel agent, RemoteTunnelManager tunnelManager, LaunchSession launchSession) throws IOException { 72 this.agent = agent; 73 this.tunnel = tunnel; 74 this.launchSession = launchSession; 75 this.tunnelManager = tunnelManager; 76 listeningSocket = CustomServerSocketFactory.getDefault().createServerSocket( 77 tunnel.getSourcePort(), 78 50, 79 Util.isNullOrTrimmedBlank(tunnel.getSourceInterface()) ? null : InetAddress.getByName(tunnel 80 .getSourceInterface())); 81 82 } 83 84 89 public AgentTunnel getAgent() { 90 return agent; 91 } 92 93 98 public Tunnel getTunnel() { 99 return tunnel; 100 } 101 102 107 public boolean isRunning() { 108 return running; 109 } 110 111 114 public void stopListener() { 115 if (running) { 116 if (log.isInfoEnabled()) 117 log.info("Stopping remote listener on " + tunnel.getSourcePort()); 118 running = false; 119 try { 120 listeningSocket.close(); 121 } catch (IOException ioe) { 122 log.error("Failed to stop listening socket for remote tunnel.", ioe); 123 } 124 if (log.isInfoEnabled()) 125 log.info("Stopped remote listener on " + tunnel.getSourcePort()); 126 127 } 128 } 129 130 135 public void run() { 136 VariableReplacement r = new VariableReplacement(); 138 r.setLaunchSession(launchSession); 139 String destHost = r.replace(tunnel.getDestination().getHost()); 140 141 ByteArrayWriter msg = new ByteArrayWriter(); 142 143 try { 144 145 msg.writeInt(tunnel.getResourceId()); 146 msg.writeString(tunnel.getSourceInterface()); 147 msg.writeString(launchSession.getId()); 148 msg.writeString(tunnel.getSourceInterface()); 149 msg.writeInt(tunnel.getSourcePort()); 150 msg.writeString(destHost); 151 msg.writeInt(tunnel.getDestination().getPort()); 152 153 154 Request request = new Request(TunnelingService.START_REMOTE_TUNNEL, msg.toByteArray()); 155 agent.sendRequest(request, false); 156 157 } catch(IOException ex) { 158 ex.printStackTrace(); 159 } 160 161 running = true; 162 if (log.isInfoEnabled()) 163 log.info("Starting remote listener on " + tunnel.getSourcePort()); 164 try { 165 while (running) { 166 try { 167 Socket s = listeningSocket.accept(); 168 if (log.isInfoEnabled()) 169 log.info("Received new connection on " + tunnel.getSourcePort() + " from " + s.getInetAddress()); 170 RemoteForwardingChannel channel = new RemoteForwardingChannel(this, s, tunnel, launchSession); 171 try { 172 agent.openChannel(channel); 173 } catch (ChannelOpenException e) { 174 log.error("Error opening channel. Remote tunnel remaining open but closing connection.", e); 175 try { 176 s.close(); 177 } catch (IOException ioe) { 178 } 179 } 180 } catch (IOException e) { 181 if (running) { 182 log.error("IO error waiting for connection, stopping remote tunnel.", e); 183 } 184 } 185 } 186 } finally { 187 188 Request request = new Request(TunnelingService.STOP_REMOTE_TUNNEL, msg.toByteArray()); 189 try { 190 agent.sendRequest(request, false); 191 } catch (IOException e) { 192 } 193 194 Channel[] c = agent.getActiveChannels(); 195 if (c != null) { 196 for (int i = 0; i < c.length; i++) { 197 if (c[i] instanceof RemoteForwardingChannel) { 198 RemoteForwardingChannel rfc = (RemoteForwardingChannel) c[i]; 199 if (rfc.getRemoteTunnel() == this && rfc.getConnection() != null) { 200 try { 201 rfc.close(); 202 } catch (Throwable t) { 203 log.error("Failed to close channel.", t); 205 } 206 } 207 } 208 } 209 } 210 tunnelManager.removeRemoteTunnel(this); 211 LaunchSessionFactory.getInstance().removeLaunchSession(launchSession); 212 running = false; 213 } 214 } 215 216 } | Popular Tags |