1 19 20 package com.sslexplorer.install.forms; 21 22 import java.net.Authenticator ; 23 import java.net.InetAddress ; 24 import java.net.PasswordAuthentication ; 25 import java.net.UnknownHostException ; 26 import java.util.Iterator ; 27 28 import javax.servlet.http.HttpServletRequest ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionErrors; 34 import org.apache.struts.action.ActionMapping; 35 import org.apache.struts.action.ActionMessage; 36 37 import com.sslexplorer.boot.ContextKey; 38 import com.sslexplorer.boot.PropertyList; 39 import com.sslexplorer.extensions.store.ExtensionStore; 40 import com.sslexplorer.properties.Property; 41 import com.sslexplorer.wizard.AbstractWizardSequence; 42 import com.sslexplorer.wizard.forms.DefaultWizardForm; 43 44 46 public class ConfigureProxiesForm extends DefaultWizardForm { 47 48 final static Log log = LogFactory.getLog(ConfigureProxiesForm.class); 49 50 53 public final static String ATTR_USE_SOCKS_PROXY = "useSOCKSProxy"; 54 56 public final static String ATTR_USE_HTTP_PROXY = "useHTTPProxy"; 57 59 public final static String ATTR_SOCKS_PROXY_HOSTNAME = "SOCKSProxyHostname"; 60 62 public final static String ATTR_SOCKS_PROXY_PORT = "SOCKSProxyPort"; 63 65 public final static String ATTR_SOCKS_PROXY_USERNAME = "SOCKSProxyUsername"; 66 68 public final static String ATTR_SOCKS_PROXY_PASSWORD = "SOCKSProxyPassword"; 69 71 public final static String ATTR_HTTP_PROXY_HOSTNAME = "HTTPProxyHostname"; 72 74 public final static String ATTR_HTTP_PROXY_PORT = "HTTPProxyPort"; 75 77 public final static String ATTR_HTTP_PROXY_USERNAME = "HTTPProxyUsername"; 78 80 public final static String ATTR_HTTP_PROXY_PASSWORD = "HTTPProxyPassword"; 81 83 public final static String ATTR_EXTENSION_STORE_EXCEPTION = "extensionStoreException"; 84 86 public final static Object ATTR_HTTP_NON_PROXY_HOSTS = "HTTPNonProxyHosts"; 87 88 private boolean useSOCKSProxy; 90 private boolean useHTTPProxy; 91 private String socksProxyHostname; 92 private String socksProxyPort; 93 private String socksProxyUsername; 94 private String socksProxyPassword; 95 private String httpProxyHostname; 96 private String httpProxyPort; 97 private String httpProxyUsername; 98 private String httpProxyPassword; 99 private PropertyList httpNonProxyHosts; 100 101 103 public ConfigureProxiesForm() { 104 super(true, true, "/WEB-INF/jsp/content/install/configureProxies.jspf", 105 "useHTTPProxy", true, false, "configureProxies", "install", "installation.configureProxies", 5); 106 } 107 108 112 public void init(AbstractWizardSequence sequence, HttpServletRequest request) throws Exception { 113 try { 114 if(sequence.getAttribute(ATTR_SOCKS_PROXY_HOSTNAME, null) == null) { 115 socksProxyHostname = Property.getProperty(new ContextKey("proxies.socksProxyHost")); 116 if(socksProxyHostname.equals("")) { 117 socksProxyUsername = ""; 118 socksProxyPort = ""; 119 socksProxyPassword = ""; 120 } 121 else { 122 useSOCKSProxy = true; 123 socksProxyUsername = Property.getProperty(new ContextKey("proxies.socksProxyUser")); 124 socksProxyPassword = Property.getProperty(new ContextKey("proxies.socksProxyPassword")); 125 socksProxyPort = Property.getProperty(new ContextKey("proxies.socksProxyPort")); 126 } 127 } 128 else { 129 useSOCKSProxy = ((String )sequence.getAttribute(ATTR_USE_SOCKS_PROXY, 130 String.valueOf(Property.getProperty(new ContextKey("webServer.port"))))).equals("true"); 131 socksProxyHostname = (String )sequence.getAttribute(ATTR_SOCKS_PROXY_HOSTNAME, ""); 132 socksProxyPort = (String )sequence.getAttribute(ATTR_SOCKS_PROXY_PORT, ""); 133 socksProxyUsername = (String )sequence.getAttribute(ATTR_SOCKS_PROXY_USERNAME, ""); 134 socksProxyPassword = (String )sequence.getAttribute(ATTR_SOCKS_PROXY_PASSWORD, ""); 135 } 136 137 if(sequence.getAttribute(ATTR_HTTP_PROXY_HOSTNAME, null) == null) { 138 httpProxyHostname = Property.getProperty(new ContextKey("proxies.http.proxyHost")); 139 httpNonProxyHosts = Property.getPropertyList(new ContextKey("proxies.http.nonProxyHosts")); 140 if(httpProxyHostname.equals("")) { 141 httpProxyUsername = ""; 142 httpProxyPort = ""; 143 httpProxyPassword = ""; 144 httpNonProxyHosts.clear(); 145 } 146 else { 147 useHTTPProxy = true; 148 httpProxyUsername = Property.getProperty(new ContextKey("proxies.http.proxyUser")); 149 httpProxyPassword = Property.getProperty(new ContextKey("proxies.http.proxyPassword")); 150 httpProxyPort = Property.getProperty(new ContextKey("proxies.http.proxyPort")); 151 } 152 } 153 else { 154 useHTTPProxy = ((String )sequence.getAttribute(ATTR_USE_HTTP_PROXY, 155 String.valueOf(Property.getPropertyBoolean(new ContextKey("webServer.port"))))).equals("true"); 156 httpProxyHostname = (String )sequence.getAttribute(ATTR_HTTP_PROXY_HOSTNAME, ""); 157 httpProxyPort = (String )sequence.getAttribute(ATTR_HTTP_PROXY_PORT, ""); 158 httpProxyUsername = (String )sequence.getAttribute(ATTR_HTTP_PROXY_USERNAME, ""); 159 httpProxyPassword = (String )sequence.getAttribute(ATTR_HTTP_PROXY_PASSWORD, ""); 160 httpNonProxyHosts = (PropertyList)sequence.getAttribute(ATTR_HTTP_NON_PROXY_HOSTS, null); 161 } 162 } 163 catch(Exception e) { 164 log.error("Failed to initialise form."); 165 } 166 } 167 168 172 public void apply(AbstractWizardSequence sequence) throws Exception { 173 sequence.putAttribute(ATTR_USE_SOCKS_PROXY, String.valueOf(useSOCKSProxy)); 174 sequence.putAttribute(ATTR_USE_HTTP_PROXY, String.valueOf(useHTTPProxy)); 175 sequence.putAttribute(ATTR_SOCKS_PROXY_HOSTNAME, socksProxyHostname); 176 sequence.putAttribute(ATTR_SOCKS_PROXY_PORT, socksProxyPort); 177 sequence.putAttribute(ATTR_SOCKS_PROXY_USERNAME, socksProxyUsername); 178 sequence.putAttribute(ATTR_SOCKS_PROXY_PASSWORD, socksProxyPassword); 179 sequence.putAttribute(ATTR_HTTP_PROXY_HOSTNAME, httpProxyHostname); 180 sequence.putAttribute(ATTR_HTTP_PROXY_PORT, httpProxyPort); 181 sequence.putAttribute(ATTR_HTTP_PROXY_USERNAME, httpProxyUsername); 182 sequence.putAttribute(ATTR_HTTP_PROXY_PASSWORD, httpProxyPassword); 183 sequence.putAttribute(ATTR_HTTP_NON_PROXY_HOSTS, httpNonProxyHosts); 184 185 186 String socksUsername = null, socksPassword = null, httpUsername = null, httpPassword = null; 187 188 if(sequence.getAttribute(ConfigureProxiesForm.ATTR_USE_SOCKS_PROXY, "").equals("true")) { 190 if (log.isInfoEnabled()) 191 log.info("Configuring outgoing TCP/IP connections to use a SOCKS proxy server."); 192 System.setProperty("socksProxyHost", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_SOCKS_PROXY_HOSTNAME, "")); 193 System.setProperty("socksProxyPort", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_SOCKS_PROXY_PORT, "1080")); 194 socksUsername = (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_SOCKS_PROXY_USERNAME, ""); 195 socksPassword = (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_SOCKS_PROXY_PASSWORD, ""); 196 } 197 if(sequence.getAttribute(ConfigureProxiesForm.ATTR_USE_HTTP_PROXY, "").equals("true")) { 198 if (log.isInfoEnabled()) 199 log.info("Configuring outgoing web connections to use a HTTP proxy server."); 200 System.setProperty("http.proxyHost", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_HOSTNAME, "")); 201 System.setProperty("com.maverick.ssl.https.HTTPProxyHostname", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_HOSTNAME, "")); 202 System.setProperty("http.proxyPort", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_PORT, "3128")); 203 System.setProperty("com.maverick.ssl.https.HTTPProxyPort", (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_PORT, "3128")); 204 PropertyList list =(PropertyList)sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_NON_PROXY_HOSTS, null); 205 StringBuffer hosts = new StringBuffer (); 206 for (Iterator i = list.iterator(); i.hasNext();) { 207 if (hosts.length() != 0) { 208 hosts.append("|"); 209 } 210 hosts.append(i.next()); 211 } 212 System.setProperty("http.nonProxyHosts", hosts.toString()); 213 System.setProperty("com.maverick.ssl.https.HTTPProxyNonProxyHosts", hosts.toString()); 214 httpUsername = (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_USERNAME, ""); 215 httpPassword = (String )sequence.getAttribute(ConfigureProxiesForm.ATTR_HTTP_PROXY_PASSWORD, ""); 216 System.setProperty("com.maverick.ssl.https.HTTPProxySecure", "false"); 217 } 218 if(httpUsername != null || socksUsername != null) { 219 Authenticator.setDefault(new ProxyAuthenticator(socksUsername, socksPassword, httpUsername, httpPassword)); 220 } 221 222 ExtensionStore.getInstance().resetExtensionStoreUpdate(); 223 try { 224 ExtensionStore.getInstance().getDownloadableExtensionStoreDescriptor(true); 225 sequence.removeAttribute(ATTR_EXTENSION_STORE_EXCEPTION); 226 } 227 catch(Exception e) { 228 log.error("Failed to connect to extension store.", e); 229 sequence.putAttribute(ATTR_EXTENSION_STORE_EXCEPTION, e); 230 } 231 } 232 233 237 public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { 238 ActionErrors errs = new ActionErrors(); 239 if (isCommiting() && (useHTTPProxy || useSOCKSProxy)) { 240 validateHostAndPortValues(errs); 241 validateHostnames(errs); 242 } 243 return errs; 244 } 245 246 private void validateHostAndPortValues(ActionErrors errs) { 247 String hostName = useHTTPProxy ? httpProxyHostname : socksProxyHostname; 248 String portString = useHTTPProxy ? httpProxyPort : socksProxyPort; 249 250 if ("".equals(hostName)) { 251 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.emptyHostName", hostName)); 252 } 253 254 if ("".equals(portString)) { 255 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.emptyPort", hostName)); 256 } else { 257 try { 258 int port = Integer.parseInt(portString); 259 if(!isPortValid(port)) { 260 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.invalidPort")); 261 } 262 } catch (NumberFormatException e) { 263 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.invalidPort")); 264 } 265 } 266 } 267 268 private static boolean isPortValid(int port) { 269 return port >= 1 && port <= 65535; 270 } 271 272 private void validateHostnames(ActionErrors errs) { 273 String hostName = useHTTPProxy ? httpProxyHostname : socksProxyHostname; 274 if (!isValidIpAddress(hostName)) { 275 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.invalidHostName", hostName)); 276 } 277 278 for (String address : httpNonProxyHosts) { 279 if (!isValidIpAddress(address)) { 280 errs.add(Globals.ERROR_KEY, new ActionMessage("installation.configureProxies.error.invalidHttpNonProxyHost", address)); 281 } 282 } 283 } 284 285 private static boolean isValidIpAddress(String ipAddress) { 286 try { 287 InetAddress.getByName(ipAddress); 288 return true; 289 } catch (UnknownHostException e) { 290 return false; 291 } 292 } 293 294 297 public boolean getUseHTTPProxy() { 298 return useHTTPProxy; 299 } 300 301 304 public void setUseHTTPProxy(boolean useHTTPProxy) { 305 this.useHTTPProxy = useHTTPProxy; 306 } 307 308 311 public boolean getUseSOCKSProxy() { 312 return useSOCKSProxy; 313 } 314 315 318 public void setUseSOCKSProxy(boolean useSOCKSProxy) { 319 this.useSOCKSProxy = useSOCKSProxy; 320 } 321 322 325 public String getHttpProxyHostname() { 326 return httpProxyHostname; 327 } 328 329 332 public void setHttpProxyHostname(String httpProxyHostname) { 333 this.httpProxyHostname = httpProxyHostname; 334 } 335 336 339 public String getHttpNonProxyHosts() { 340 return httpNonProxyHosts.getAsTextFieldText(); 341 } 342 343 346 public void setHttpNonProxyHosts(String httpNonProxyHosts) { 347 this.httpNonProxyHosts.setAsTextFieldText(httpNonProxyHosts); 348 } 349 350 353 public String getHttpProxyPassword() { 354 return httpProxyPassword; 355 } 356 357 360 public void setHttpProxyPassword(String httpProxyPassword) { 361 this.httpProxyPassword = httpProxyPassword; 362 } 363 364 367 public String getHttpProxyPort() { 368 return httpProxyPort; 369 } 370 371 374 public void setHttpProxyPort(String httpProxyPort) { 375 this.httpProxyPort = httpProxyPort; 376 } 377 378 381 public String getHttpProxyUsername() { 382 return httpProxyUsername; 383 } 384 385 388 public void setHttpProxyUsername(String httpProxyUsername) { 389 this.httpProxyUsername = httpProxyUsername; 390 } 391 392 395 public String getSocksProxyHostname() { 396 return socksProxyHostname; 397 } 398 399 402 public void setSocksProxyHostname(String socksProxyHostname) { 403 this.socksProxyHostname = socksProxyHostname; 404 } 405 406 409 public String getSocksProxyPassword() { 410 return socksProxyPassword; 411 } 412 413 416 public void setSocksProxyPassword(String socksProxyPassword) { 417 this.socksProxyPassword = socksProxyPassword; 418 } 419 420 423 public String getSocksProxyPort() { 424 return socksProxyPort; 425 } 426 427 430 public void setSocksProxyPort(String socksProxyPort) { 431 this.socksProxyPort = socksProxyPort; 432 } 433 434 437 public String getSocksProxyUsername() { 438 return socksProxyUsername; 439 } 440 441 444 public void setSocksProxyUsername(String socksProxyUsername) { 445 this.socksProxyUsername = socksProxyUsername; 446 } 447 448 class ProxyAuthenticator extends Authenticator { 449 450 String socksUsername, socksPassword, httpUsername, httpPassword; 451 452 ProxyAuthenticator(String socksUsername, String socksPassword, 453 String httpUsername, String httpPassword) { 454 this.socksUsername = socksUsername; 455 this.socksPassword = socksPassword; 456 this.httpUsername = httpUsername; 457 this.httpPassword = httpPassword; 458 } 459 460 public PasswordAuthentication getPasswordAuthentication() { 461 if (log.isInfoEnabled()) 462 log.info("Requesting " + getRequestingProtocol() + " proxy authentication for " + getRequestingSite() + " (" 463 + getRequestingHost() + ":" + getRequestingPort() + "), prompt = " + getRequestingPrompt()); 464 String user = null; 465 String pass = null; 466 try { 467 if (getRequestingProtocol().startsWith("SOCKS")) { 468 user = socksUsername; 469 pass = socksPassword; 470 } else { 471 user = httpUsername; 472 pass = httpPassword; 473 } 474 } catch (Exception e) { 475 log.error("Failed to get proxy authentication details."); 476 return null; 477 } 478 return new PasswordAuthentication (user, pass.toCharArray()); 479 } 480 481 } 482 483 public void reset(ActionMapping mapping, HttpServletRequest request) { 484 super.reset(mapping, request); 485 useSOCKSProxy = false; 486 useHTTPProxy = false; 487 } 488 } | Popular Tags |