1 19 20 package com.sslexplorer.agent.client.applications; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.Enumeration ; 26 import java.util.Hashtable ; 27 import java.util.Vector ; 28 29 import com.maverick.multiplex.ChannelOpenException; 30 import com.sslexplorer.agent.client.Agent; 31 import com.sslexplorer.agent.client.WinRegistry; 32 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher; 33 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents; 34 import com.sslexplorer.agent.client.util.Messages; 35 import com.sslexplorer.agent.client.util.TunnelConfiguration; 36 import com.sslexplorer.agent.client.util.XMLElement; 37 38 44 public class AgentApplicationLauncher extends AbstractApplicationLauncher { 45 46 static org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory.getLog(AgentApplicationLauncher.class); 48 50 private Agent agent; 51 private String name; 52 private String descriptor; 53 private Vector tunnels = new Vector (); 54 55 62 public AgentApplicationLauncher(Agent vpn, String name, Hashtable parameters, String descriptor, ApplicationLauncherEvents events) { 63 super(vpn.getConfiguration().getCacheDir(), "https", vpn.getUsername(), vpn.getSSLExplorerHost(), vpn.getSSLExplorerPort(), parameters, events); 64 this.agent = vpn; 65 this.name = name; 66 this.descriptor = descriptor; 67 } 68 69 public Agent getAgent() { 70 return agent; 71 } 72 73 protected void createTunnel(XMLElement e) throws IOException { 74 75 String hostname = (String ) e.getAttribute("hostname"); String name = (String ) e.getAttribute("name"); int port = Integer.parseInt((String ) e.getAttribute("port")); boolean usePreferredPort = ("true".equals(e.getAttribute("usePreferredPort"))); boolean singleConnection = !("false".equals(e.getAttribute("singleConnection"))); boolean localhostWorkaround = "true".equals(e.getAttribute("localhostWorkaround")); if (events != null) { 82 TunnelConfiguration listeningSocketConfiguration = events.createTunnel(name, 83 hostname, 84 port, 85 usePreferredPort, 86 singleConnection, 87 localhostWorkaround ? "127.0.0.2" : agent.getConfiguration().getLocalhostAddress()); 88 tunnels.addElement(listeningSocketConfiguration); 89 } else 90 throw new IOException (Messages.getString("ApplicationLauncher.tunnelRequiredButNoEventHandler")); 92 } 93 94 97 public String replaceTokens(String str) { 98 str = super.replaceTokens(str); 99 for (Enumeration e = tunnels.elements(); e.hasMoreElements();) { 100 TunnelConfiguration listeningSocketConfiguration = (TunnelConfiguration) e.nextElement(); 101 String paramHost = "${tunnel:" + listeningSocketConfiguration.getName() + ".hostname}"; String paramPort = "${tunnel:" + listeningSocketConfiguration.getName() + ".port}"; 104 str = replaceAllTokens(str, paramHost, listeningSocketConfiguration.getSourceInterface()); str = replaceAllTokens(str, paramPort, String.valueOf(listeningSocketConfiguration.getSourcePort())); 106 } 107 return str; 108 } 109 110 116 public Vector getTunnels() { 117 return tunnels; 118 } 119 120 125 protected boolean processLauncherElement(XMLElement element) { 126 127 if (element.getName().equalsIgnoreCase("registry")) { 128 Enumeration e = element.enumerateChildren(); 129 130 while (e.hasMoreElements()) { 131 XMLElement el = (XMLElement) e.nextElement(); 132 133 processRegistryElements(el); 134 } 135 return true; 136 } else 137 return false; 138 } 139 140 private void processRegistryElements(XMLElement el) { 141 if (el.getName().equalsIgnoreCase("get")) { 142 String scope = el.getStringAttribute("scope"); 143 String key = replaceTokens(el.getStringAttribute("key")); 144 String value = replaceTokens(el.getStringAttribute("value")); 145 String param = replaceTokens(el.getStringAttribute("parameter")); 146 String defaultValue = replaceTokens(el.getStringAttribute("default")); 147 148 addParameter(param, WinRegistry.getRegistryValue(scope, key, value, defaultValue == null ? "" : defaultValue)); 149 } else if (el.getName().equalsIgnoreCase("set")) { 150 String scope = el.getStringAttribute("scope"); 151 String key = replaceTokens(el.getStringAttribute("key")); 152 String value = replaceTokens(el.getStringAttribute("value")); 153 String arg = replaceTokens(el.getStringAttribute("arg")); 154 155 WinRegistry.setRegistryValue(scope, key, value, arg); 156 157 } else if (el.getName().equalsIgnoreCase("if")) { 158 159 String scope = el.getStringAttribute("scope"); 160 String key = replaceTokens(el.getStringAttribute("key")); 161 String value = replaceTokens(el.getStringAttribute("value")); 162 String notAttr = el.getStringAttribute("not"); 163 String existsAttr = el.getStringAttribute("exists"); 164 String equalsAttr = el.getStringAttribute("equals"); 165 166 if (existsAttr != null) { 167 boolean exists = Boolean.getBoolean(existsAttr); 168 String v = WinRegistry.getRegistryValue(scope, key, value, "DEFAULT_VALUE"); 169 if (v.equals("DEFAULT_VALUE") && !exists) { 170 processRegistryElements(el); 171 } else if (!v.equals("DEFAULT_VALUE") && exists) { 172 processRegistryElements(el); 173 } 174 } else if (notAttr != null) { 175 boolean not = Boolean.getBoolean(notAttr == null ? "false" : notAttr); 176 String v = WinRegistry.getRegistryValue(scope, key, value, ""); 177 178 if (equalsAttr.equals(v) && !not) { 179 processRegistryElements(el); 180 } else if (!equalsAttr.equals(v) && not) { 181 processRegistryElements(el); 182 } 183 } 184 185 } 186 } 187 188 191 public InputStream getApplicationDescriptor() throws IOException { 192 193 if (events != null) 194 events.debug("Getting application descriptor using Maverick HTTP client"); 195 196 return new ByteArrayInputStream (descriptor.getBytes()); 197 } 198 199 208 public InputStream getDownloadFile(String name, String ticket, String filename) throws IOException { 209 210 if (events != null) 211 events.debug("Downloading application file " + filename); 212 213 try { 214 ApplicationFileChannel channel = new ApplicationFileChannel(name, ticket, filename); 215 agent.getConnection().openChannel(channel); 216 return channel.getInputStream(); 217 } catch (ChannelOpenException e) { 218 throw new IOException (e.getMessage()); 219 } 220 221 } 222 } 223 | Popular Tags |