1 19 20 package com.sslexplorer.agent.client.launcher; 21 22 import java.applet.Applet ; 23 import java.awt.BorderLayout ; 24 import java.awt.Button ; 25 import java.awt.Color ; 26 import java.awt.Label ; 27 import java.awt.Panel ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.InputStreamReader ; 33 import java.io.OutputStream ; 34 import java.lang.reflect.Method ; 35 import java.net.MalformedURLException ; 36 import java.net.URL ; 37 import java.net.URLConnection ; 38 import java.net.URLEncoder ; 39 import java.text.MessageFormat ; 40 import java.util.Hashtable ; 41 import java.util.Locale ; 42 import java.util.StringTokenizer ; 43 44 import com.sshtools.ui.awt.ImageCanvas; 45 import com.sshtools.ui.awt.UIUtil; 46 import com.sshtools.ui.awt.options.OptionDialog; 47 import com.sslexplorer.agent.client.util.AbstractApplicationLauncher; 48 import com.sslexplorer.agent.client.util.ApplicationLauncherEvents; 49 import com.sslexplorer.agent.client.util.ProcessMonitor; 50 import com.sslexplorer.agent.client.util.TunnelConfiguration; 51 import com.sslexplorer.agent.client.util.Utils; 52 import com.sslexplorer.agent.client.util.XMLElement; 53 54 55 61 public class AgentLauncher extends Applet implements ApplicationLauncherEvents { 62 private boolean isStandalone = false; 63 String ticket; 64 ProgressBar progress; 65 String cmdline; 66 String name; 67 boolean monitor = false; 68 boolean debug = false; 69 boolean cleanOnExit = false; 70 Button launch; 71 String localProxyURL; 72 ProcessMonitor processMonitor; 73 long totalNumBytes; 74 String userAgent; 75 Locale locale; 76 Panel mainPanel; 77 int timeout; 78 URL debugCodebase; 79 80 String appName = null; 82 String extensionId = "sslexplorer-agent"; boolean isAgent = true; 84 String launcherImage = "/images/launcher-agent.gif"; String localeName = "en"; 87 93 public String getParameter(String key, String def) { 94 return isStandalone ? System.getProperty(key, def) : (getParameter(key) != null ? getParameter(key) : def); 95 } 96 97 100 public void init() { 101 loadResourceBundles(); 103 104 106 appName = Messages.getString("VPNLauncher.applicationName"); System.out.println(Messages.getString("VPNLauncher.sysout.initialising")); 109 116 125 129 System.out.println(Messages.getString("VPNLauncher.sysout.gettingLauncherType")); 131 timeout = Integer.parseInt(getParameter("timeout", "60000")) + 10000; 133 134 appName = getParameter("appName", appName); extensionId = getParameter("extensionId", extensionId); launcherImage = getParameter("launcherImage", launcherImage); isAgent = Boolean.valueOf(getParameter("isAgent", String.valueOf(isAgent))).booleanValue(); 139 System.out.println(Messages.getString("VPNLauncher.sysout.retrievingDebugStatus")); debug = Boolean.valueOf(getParameter("debug", "false")).booleanValue(); if(debug) { 142 String debugCodebaseLocation = getParameter("debugCodebase", ""); 143 if(!debugCodebaseLocation.equals("")) { 144 try { 145 debugCodebase = new URL (debugCodebaseLocation); 146 } 147 catch(MalformedURLException murle) { 148 } 149 } 150 } 151 cleanOnExit = Boolean.valueOf(getParameter("cleanOnExit", "false")).booleanValue(); 153 if (!isAgent) { 154 try { 155 URL cb = getActualCodebase(); 158 UIUtil.setCodeBase(new URL (cb.getProtocol(), cb.getHost(), 159 cb.getPort() == -1 ? 443 : cb.getPort(), "")); } catch (MalformedURLException ex) { 161 } 162 } 163 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.launchingApplication"), new Object [] { appName, extensionId, launcherImage, new Boolean (isAgent) })); 165 try { 166 jbInit(); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 } 171 172 175 public void start() { 176 if ("true".equals(getParameter("autoStart", "false"))) { System.out.println(Messages.getString("VPNLauncher.sysout.autostartLaunching")); launch(); 179 } else { 180 System.out.println(Messages.getString("VPNLauncher.sysout.nonAutostartLaunching")); } 182 } 183 184 187 public void stop() { 188 System.out.println(Messages.getString("VPNLauncher.sysout.stopping")); } 190 191 196 public URL getActualCodebase() { 197 if(debugCodebase != null) { 198 return debugCodebase; 199 } 200 return getCodeBase(); 201 } 202 203 206 public void launch() { 207 try { 208 209 212 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.launching"), new Object [] { appName } )); 214 ticket = this.getParameter("ticket", null); 217 if (ticket == null && isAgent) { 219 System.out.println(Messages.getString("VPNLauncher.sysout.nullTicket")); return; 222 } 223 224 234 System.out.println(Messages.getString("VPNLauncher.sysout.retrievingMonitorStatus")); monitor = Boolean.valueOf(getParameter("monitor", "false")).booleanValue(); 237 System.out.println(Messages.getString("VPNLauncher.sysout.checkingBrowserProxySettings")); 240 userAgent = getParameter("userAgent", "Unknown"); System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.userAgent"), new Object [] { userAgent } )); 243 localProxyURL = getParameter("proxyURL", ""); String pluginProxyURL = ""; 246 if (localProxyURL.toLowerCase().startsWith("browser://")) { System.out.println(Messages.getString("VPNLauncher.sysout.checkingJavaProxyProperties")); 249 253 String browserProxy = System.getProperty("javaplugin.proxy.config.list"); if (browserProxy != null && !browserProxy.equals("")) { StringTokenizer t = new StringTokenizer (browserProxy, ","); while (t.hasMoreTokens()) { 257 String type = t.nextToken(); 258 int idx = type.indexOf('='); 259 if (idx != -1) { 260 String value = type.substring(idx + 1); 261 type = type.substring(0, idx); 262 if (type.equals("http")) { pluginProxyURL = "http://" + value; break; 265 } 266 } else { 267 } 269 } 270 } 271 272 } else if (localProxyURL != null && !localProxyURL.equals("")) { if (!localProxyURL.startsWith("http://") && !localProxyURL.startsWith("https://") && !localProxyURL.startsWith("browser://")) localProxyURL = "http://" + localProxyURL; } 277 278 final Hashtable params = new Hashtable (); 280 params.put("id", extensionId); params.put("ticket", ticket); params.put("cleanOnExit", String.valueOf(cleanOnExit)); params.put("userAgent", userAgent); params.put("pluginProxyURL", pluginProxyURL); 286 if (isAgent) { 287 288 setIfNotEmpty("java.version", params); setIfNotEmpty("java.vendor", params); setIfNotEmpty("sun.os.patch.level", params); setIfNotEmpty("os.name", params); setIfNotEmpty("os.version", params); setIfNotEmpty("os.arch", params); } 295 Thread thread = new Thread () { 296 297 public void run() { 298 299 317 System.out.println(Messages.getString("VPNLauncher.sysout.startingApplicationLauncher")); 320 System.out.println(System.getProperties().toString()); 321 try { 322 startingLaunch(appName); 323 URL codebase = getActualCodebase(); 324 AbstractApplicationLauncher launcher = new AgentLauncherApplicationLauncher(new File (Utils.getHomeDirectory(), ".sslexplorer"), "https", null, codebase .getHost().equals("") ? "localhost" : codebase.getHost(), codebase.getPort() == -1 ? 443 : codebase.getPort(), params, AgentLauncher.this); 327 328 launcher.setDebug(debug); 329 330 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.settingLocalProxy"), new Object [] { localProxyURL } ) ); launcher.setLocalProxyURL(localProxyURL); 332 launcher.prepare(); 333 System.out.println(Messages.getString("VPNLauncher.sysout.preparedLauncher")); 335 340 if(isAgent) { 341 SynchronizationThread sync = new SynchronizationThread(launcher); 342 System.out.println(Messages.getString("VPNLauncher.sysout.startingSyncThread")); sync.start(); 344 try { 346 Thread.sleep(1000); 347 } catch (InterruptedException ie) { 348 ie.printStackTrace(); 349 } 350 } 351 352 launcher.start(); 353 354 359 if(!isAgent) { 360 String returnTo = getParameter("returnTo", ""); URL returnToUrl = returnTo.equals("") ? getDocumentBase() : new URL (getDocumentBase(), returnTo); returnToUrl = new URL (addRedirectParameter(returnToUrl.toExternalForm(), "vpnMessage", appName + " launched.")); System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.redirecting"), new Object [] { returnToUrl.toExternalForm() })); setMessage(Messages.getString("VPNLauncher.complete")); getAppletContext().showDocument(returnToUrl); 366 } 367 else { 368 369 372 373 if (monitor) { 374 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.monitoring"), new Object [] { appName })); processMonitor = launcher.getApplicationType().getProcessMonitor(); 376 if (processMonitor != null) { 377 MonitorOutputStream out = new MonitorOutputStream(); 378 finishedLaunch(); 379 int exitcode = processMonitor.watch(out, out); 380 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.finished"), new Object [] { appName, new Integer (exitcode) })); } else { 382 System.out.println(Messages.getString("VPNLauncher.sysout.noProgressMonitor")); } 384 } 385 } 386 387 } catch (Exception ex) { 388 setMessage(ex.getMessage()); 390 ex.printStackTrace(); 391 } finally { 392 finishedLaunch(); 393 } 394 } 395 }; 396 397 thread.start(); 398 } catch (Exception e) { 399 e.printStackTrace(); 400 } 404 405 } 406 407 protected static void setIfNotEmpty(String name, Hashtable p) { 408 String v = System.getProperty(name); 409 if (v != null && !v.equals("")) { p.put(name, v); 411 } 412 } 413 414 public String getAppletInfo() { 416 return Messages.getString("VPNLauncher.appletInfo"); } 418 419 public String [][] getParameterInfo() { 421 String [][] pinfo = { { "ticket", "String", "" }, }; return pinfo; 423 } 424 425 public void startDownload(long totalNumBytes) { 426 progress.setMessage(Messages.getString("VPNLauncher.downloadingFiles")); this.totalNumBytes = totalNumBytes; 428 progress.updateValue(1L); 429 progress.updateValue(20); 430 } 431 432 public void progressedDownload(long bytesSoFar) { 433 int percent = (int) (((float) bytesSoFar / (float) totalNumBytes) * 70f) + 20; 434 progress.updateValue(percent); 435 } 436 437 public void completedDownload() { 438 progress.setMessage(Messages.getString("VPNLauncher.downloadComplete")); progress.updateValue(90); 440 } 441 442 public void executingApplication(String name, String cmdline) { 443 progress.setMessage(MessageFormat.format(Messages.getString("VPNLauncher.executingApplication"), new Object [] { name } ) ); progress.updateValue(95); 445 } 446 447 public TunnelConfiguration createTunnel(String name, String hostToConnect, int portToConnect, boolean usePreferredPort, 448 boolean singleConnection, String sourceInterface) { 449 return null; 451 } 452 453 public void debug(String msg) { 454 System.out.println(msg); 455 } 456 457 public void startingLaunch(String application) { 458 progress = new ProgressBar(this, 459 MessageFormat.format(Messages.getString("VPNLauncher.startingLaunch"), new Object [] { application } ), "SSL-Explorer", 100L, false); progress.updateValue(7L); 461 } 462 463 public void processingDescriptor() { 464 progress.updateValue(14L); 465 progress.setMessage(Messages.getString("VPNLauncher.processingApplicationDescriptor")); } 467 468 public void finishedLaunch() { 469 if (progress.getCurrentValue() != 100) { 470 progress.setMessage(Messages.getString("VPNLauncher.launchedAgent")); progress.updateValue(100); 472 Thread t = new Thread () { 473 public void run() { 474 try { 475 Thread.sleep(2000); 476 } catch (InterruptedException ex) { 477 } 478 progress.dispose(); 479 } 480 }; 481 482 t.start(); 483 } 484 } 485 486 491 public void destroy() { 492 System.out.println(Messages.getString("VPNLauncher.destroyingApplet")); super.destroy(); 494 } 495 496 class SynchronizationThread extends Thread { 497 498 AbstractApplicationLauncher launcher; 499 500 SynchronizationThread(AbstractApplicationLauncher launcher) { 501 this.launcher = launcher; 502 } 503 504 public void run() { 505 506 try { 507 508 String returnTo = getParameter("returnTo", ""); setMessage(Messages.getString("VPNLauncher.synchronizing")); 511 518 URL codebase = getActualCodebase(); 519 URL url = new URL ("https", codebase.getHost().equals("") ? "localhost" : codebase.getHost(), codebase.getPort() == -1 ? 443 521 : codebase.getPort(), 522 "/registerClientSynchronization.do?ticket=" + ticket); System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.sendingSync"), new Object [] { url })); 525 URLConnection con = url.openConnection(); 526 setReadTimeout(con, timeout); 527 528 con.connect(); 529 System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.connectedTo"), new Object [] { url } )); 531 XMLElement result = new XMLElement(); 532 result.parseFromReader(new InputStreamReader (con.getInputStream())); 533 534 if (result.getName().equalsIgnoreCase("success")) { System.out.println(Messages.getString("VPNLauncher.sysout.syncOk")); 537 543 URL returnToUrl = returnTo.equals("") ? getDocumentBase() : new URL (getDocumentBase(), returnTo); if (returnToUrl.getProtocol().equals("http") && returnToUrl.getHost().equals("localhost") && returnToUrl.getPort() == -1) { 546 returnToUrl = new URL (returnToUrl.getProtocol(), returnToUrl.getHost(), Integer.parseInt(result 547 .getAttribute("clientPort").toString()), returnToUrl.getFile()); } 549 returnToUrl = new URL (addRedirectParameter(returnToUrl.toExternalForm(), 550 "vpnMessage", appName + " launched.")); System.out.println(MessageFormat.format(Messages.getString("VPNLauncher.sysout.redirecting"), new Object [] { returnToUrl.toExternalForm() })); getAppletContext().showDocument(returnToUrl); 553 setMessage(Messages.getString("VPNLauncher.complete")); 555 } else { 556 System.out.println(Messages.getString("VPNLauncher.sysout.failedSync")); System.out.println(result.getContent()); 559 ProcessMonitor monitor = launcher.getApplicationType().getProcessMonitor(); 560 if (monitor != null) { 561 setMessage(Messages.getString("VPNLauncher.failedSync")); } 566 } 567 } catch (Exception ex) { 568 ex.printStackTrace(); 569 addLaunchButton(); } finally { 573 progress.dispose(); 574 } 575 576 } 577 } 578 579 private String addRedirectParameter(String redirect, String name, String value) { 580 StringBuffer buf = new StringBuffer (redirect); 581 int idx = redirect.indexOf('?'); 582 if (idx == -1) { 583 buf.append("?"); } else { 585 buf.append("&"); } 587 buf.append(name); 588 buf.append('='); 589 buf.append(URLEncoder.encode(value)); 590 return buf.toString(); 591 } 592 593 private void loadResourceBundles() { 594 localeName = getParameter("locale", localeName); locale = Utils.createLocale(localeName); 596 URL codebase = getActualCodebase(); 597 ClassLoader cl = getClass().getClassLoader(); 599 try { 600 URL url = new URL ("https", codebase.getHost().equals("") ? "localhost" : codebase.getHost(), codebase.getPort() == -1 ? 443 601 : codebase.getPort(), "/loadMessageResources/"); 602 Messages.setBundle(Utils.getBundle( 605 "com.sslexplorer.agent.client.launcher.ApplicationResources", locale, 607 cl, url)); 608 com.sslexplorer.agent.client.util.Messages.setBundle(Utils.getBundle( 609 "com.sslexplorer.agent.client.util.ApplicationResources", locale, 611 cl, url)); 612 com.sslexplorer.agent.client.util.types.Messages.setBundle(Utils.getBundle( 613 "com.sslexplorer.agent.client.util.types.ApplicationResources", locale, 615 cl, url)); 616 com.sshtools.ui.awt.Messages.setBundle(Utils.getBundle( 617 "com.sshtools.ui.awt.ApplicationResources", locale, 619 cl, url)); 620 } 621 catch(MalformedURLException murle) { 622 murle.printStackTrace(); 623 } 624 } 625 626 private static void setReadTimeout(URLConnection conx, int timeout) { 627 try { 628 Method m = conx.getClass().getMethod("setReadTimeout", new Class [] { int.class }); 629 m.invoke(conx, new Object [] { new Integer (timeout) } ); 630 } 631 catch(Throwable t) { 632 } 634 } 635 636 private void jbInit() throws Exception { 637 String background = getParameter("background", "#e6e6e6"); setBackground(Color.decode(background)); 639 String foreground = getParameter("foreground", "#000000"); setForeground(Color.decode(foreground)); 641 setLayout(new BorderLayout ()); 642 ImageCanvas ic = new ImageCanvas(UIUtil.waitFor(UIUtil.loadImage(getClass(), launcherImage), this)); 643 ic.setValign(ImageCanvas.CENTER_ALIGNMENT); 644 add(ic, BorderLayout.WEST); 645 mainPanel = new Panel (new BorderLayout ()); 646 add(mainPanel, BorderLayout.CENTER); 647 648 if ("true".equals(getParameter("autoStart", "false"))) { mainPanel.add(new Label (Messages.getString("VPNLauncher.launching")), BorderLayout.CENTER); } 653 } 654 655 private void setMessage(String text) { 656 mainPanel.invalidate(); 657 mainPanel.removeAll(); 658 mainPanel.add(new Label (text), BorderLayout.CENTER); 659 mainPanel.validate(); 660 661 } 662 663 private void addLaunchButton() { 664 665 mainPanel.invalidate(); 666 mainPanel.removeAll(); 667 launch = new Button (Messages.getString("VPNLauncher.launcher")); launch.addActionListener(new ActionListener () { 669 public void actionPerformed(ActionEvent e) { 670 launch(); 671 } 672 }); 673 mainPanel.add(launch, BorderLayout.SOUTH); 674 mainPanel.validate(); 675 } 676 677 public void error(String msg) { 678 System.out.println(msg); 679 OptionDialog.error(mainPanel, Messages.getString("VPNLauncher.error"), msg); } 681 682 class MonitorOutputStream extends OutputStream { 683 public void write(int b) throws IOException { 684 System.out.write(b); 685 } 686 687 public void write(byte[] buf, int off, int len) throws IOException { 688 System.out.write(buf, off, len); 689 } 690 } 691 } | Popular Tags |