1 19 20 package com.sslexplorer.tunnels.agent; 21 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Collection ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 import com.maverick.multiplex.MultiplexedConnectionListener; 33 import com.sslexplorer.agent.AgentTunnel; 34 import com.sslexplorer.policyframework.LaunchSession; 35 import com.sslexplorer.security.SessionInfo; 36 import com.sslexplorer.tunnels.Tunnel; 37 import com.sslexplorer.tunnels.TunnelException; 38 39 44 public class DefaultRemoteTunnelManager implements RemoteTunnelManager { 45 46 final static Log log = LogFactory.getLog(DefaultRemoteTunnelManager.class); 47 48 private Map <String , RemoteTunnel> tunnels = new HashMap <String , RemoteTunnel>(); 49 private Map <Integer , List <RemoteTunnel>> tunnelsBySession = new HashMap <Integer , List <RemoteTunnel>>(); 50 51 56 public void init() { 57 } 58 59 62 public RemoteTunnel createRemoteTunnel(Tunnel tunnel, AgentTunnel agent, LaunchSession launchSession) throws TunnelException { 63 RemoteTunnel remoteTunnel = null; 64 synchronized (tunnels) { 65 String tunnelId = tunnel.getSourcePort() 66 + "-" 67 + (tunnel.getSourceInterface() == null || tunnel.getSourceInterface().equals("") ? "" : tunnel 68 .getSourceInterface()); 69 remoteTunnel = tunnels.get(tunnelId); 70 if (remoteTunnel != null) { 71 if (remoteTunnel.getAgent() == agent) { 72 throw new TunnelException(TunnelException.REMOTE_TUNNEL_IN_USE, (Throwable )null, String.valueOf(tunnel.getSourcePort())); 73 } 74 throw new TunnelException(TunnelException.REMOTE_TUNNEL_LOCKED, (Throwable )null, String.valueOf(tunnel.getSourcePort())); 75 } 76 try { 77 remoteTunnel = new RemoteTunnel(tunnel, agent, this, launchSession); 78 agent.addListener(new RemoteTunnelListener(remoteTunnel)); 79 if (log.isInfoEnabled()) 80 log.info("Adding remote tunnel with id of " + tunnelId); 81 List <RemoteTunnel> sessionTunnels = tunnelsBySession.get(agent.getSession().getId()); 82 if (sessionTunnels == null) { 83 sessionTunnels = new ArrayList <RemoteTunnel>(); 84 tunnelsBySession.put(agent.getSession().getId(), sessionTunnels); 85 } 86 sessionTunnels.add(remoteTunnel); 87 tunnels.put(tunnelId, remoteTunnel); 88 } catch (IOException e) { 89 log.error("Failed to create new remote tunnel.", e); 90 throw new TunnelException(TunnelException.PORT_ALREADY_IN_USE, (Throwable )null, String.valueOf(tunnel.getSourcePort())); 91 } 92 } 93 return remoteTunnel; 94 } 95 96 101 public Collection <RemoteTunnel> getRemoteTunnels() { 102 return new ArrayList <RemoteTunnel>(tunnels.values()); 103 } 104 105 110 public void removeRemoteTunnel(RemoteTunnel tunnel) { 111 synchronized (tunnels) { 112 String tunnelId = tunnel.getTunnel().getSourcePort() 113 + "-" 114 + (tunnel.getTunnel().getSourceInterface() == null 115 || tunnel.getTunnel().getSourceInterface().equals("") ? "" : tunnel.getTunnel() 116 .getSourceInterface()); 117 if (log.isInfoEnabled()) 118 log.info("Removing remote tunnel with id of " + tunnelId); 119 tunnels.remove(tunnelId); 120 List <RemoteTunnel> sessionTunnels = tunnelsBySession.get(tunnel.getAgent().getSession().getId()); 121 sessionTunnels.remove(tunnel); 122 if (sessionTunnels.size() == 0) { 123 tunnelsBySession.remove(tunnel.getAgent().getSession().getId()); 124 } 125 } 126 127 } 128 129 134 public Collection <RemoteTunnel> getRemoteTunnels(SessionInfo session) { 135 return tunnelsBySession.get(session.getId()); 136 } 137 138 class RemoteTunnelListener implements MultiplexedConnectionListener { 139 RemoteTunnel remoteTunnel; 140 141 RemoteTunnelListener(RemoteTunnel remoteTunnel) { 142 this.remoteTunnel = remoteTunnel; 143 } 144 145 public void onConnectionClose() { 146 remoteTunnel.stopListener(); 147 } 148 149 public void onConnectionOpen() { 150 } 151 152 } 153 154 public RemoteTunnel getRemoteTunnel(int resourceId) { 155 synchronized (tunnels) { 156 for (RemoteTunnel remoteTunnel : tunnels.values()) { 157 if (remoteTunnel.getTunnel().getResourceId() == resourceId) { 158 return remoteTunnel; 159 } 160 } 161 } 162 return null; 163 } 164 165 } 166 | Popular Tags |