<%@ page import = "com.cyscape.browserhawk.*" %>

<%
	// This sample is provided under the terms of the BrowserHawk End User License
  // Agreement.  Copyright (c) 2003-2011 cyScape, Inc. All rights reserved.

  // This script demonstrates how you can check for the presence of a firewall
  // that is blocking ports (typically with the exception of 80 and 443)
  // from outbound traffic.

  // Note how we define the term "Firewall" for our purposes.  If BrowserHawk
  // detects that it cannot freely send data across ports because they are
  // blocked, the Firewall property is set to True. Likewise if it can
  // transmit outbound traffic across ports in general, the Firewall property
  // is set to False - even if you actually have a firewall installed.  Again,
  // we use the property name "firewall" to refer to a firewall configured to
  // block ports, not to refer to whether a firewall of any kind is installed.

  // We also demonstrate how you can check to see whether specific ports
  // are blocked or open.  For example, if you plan to stream media 
  // using Real Networks or MediaPlayer's advanced options, the visitor
  // will need ports 554 and 1755 open, respectively.

  // Lastly we also check to see if the port 443 is open to confirm that the
  // browser can securely access pages via HTTPS.  If this port is blocked,
  // then any attempt to try and send the visitor to a page with an https://
  // link will result in an error - not exactly what you want when sending
  // folks to your order page!  Therefore you can use BH to proactively detect
  // this and ensure they can successfully access content over HTTPS before
  // redirecting them.  Note that the SSLEnabled property also makes this
  // check, in addition to also making sure the visitor has not disabled SSL
  // in the browser.

	// First we create an instance of the ExtendedOptions class so we can tell
	// BrowserHawk extact what extended properties we want to test - in this
	// case the properties are OpenPorts and Firewall.  ExtendedOptions also
	// allows us to specify additional information, such as what ports to check,
	// and various options such as the message to display in the
	// browser while we perform the check, such as "Please wait...".

  ExtendedOptions options = new ExtendedOptions();
  
  // Set the message to be displayed in the browser window and title bar 
  // while the check is performed.
  // Note you can use HTML in this message if you wish.
  options.setPageMessage("Checking firewall and several ports. Please wait...");
  options.setPageTitle("Please wait...");
  
  // Now we indicate exactly what ports we want it to check.  Note that we also
  // tell it to check port 80.  This may seem odd a first, considering that
  // if port 80 was blocked the user could not have accessed this test page
  // in the first place.  However, there is a specific reason for this...
  options.setPortsToCheck("554, 1755, 443, 80");

	// When detecting ports it is necessary to issue background transparent
	// requests to another web site, called a Port Check Server, which can exist
	// on the same or different server as the script you are performing the port
	// check from.

	// cyScape provides a Port Check Server you can use for testing and
	// development only (the default settings will use the cyScape Port Check
	// Server).  For use on production servers you must set up your own Port
	// Check Server. For details on how to make these configuration changes and
	// set up your own Port Check Server see the "Port Check Server
	// Configuration" topic in the BrowserHawk documentation.

	// As mentioned above, by default the cyScape Port Check Server is used for
	// this test.  After configuring your own Port Check Server you must tell
	// BrowserHawk where it is located.  Uncomment the following line to have
	// BrowserHawk use your Port Check Server instead of the cyScape server once
	// you set it up.  This can be set to either a fully
	// qualified domain name (such as ports.company.com or an IP address).

  // options.setPortCheckURL("http://something.company.com");
  // or an IP...
  // options.setPortCheckURL("http://x.y.z.a");

  // Note: The PortCheckURL value you set must begin with "http://"

  
	// You may have noticed in the PortsToCheck setting above that we are
	// testing port 80.  By testing port 80 explictly we can then ensure that
	// the results we get back are valid by making sure BH says that port 80 is
	// open.  If not, then we know the results of the port check can not be
	// counted on, because of an error reaching the test server or the timeout
	// value specified for the test was too short.  More on this later.

	// Currently the cyScape Port Check Server support tests of the following
	// ports: 80, 443, 554, 1755, 8080, 8081, 9090

	// If you are using your own server for port check services, you can
	// configure this on your own - see the documentation for details.

	// Let's set the timeout period required for the port checks.  The default
	// value of 3000 miliseconds (3 seconds) should be sufficient for most
	// purposes.

  options.setFirewallTimeout(3000);

	// No need to explictly set this since 3000 is the default, but we list it
	// above for demonstration purposes.

	// Note that for IE, the maximum time it will take to check for blocked
	// ports is defined as the FirewallTimeout value * (number of ports you are
	// checking plus 1 if you are also checking for the firewall property and
	// plus 1 one if you are also checking for the SSLEnabled property) / 1000 =
	// # seconds required to check.  For example, if all ports are blocked this
	// script will take 15 seconds to run the full check (4 ports checked + 1
	// for Firewall check) = 5 * 3000 = 15000 / 1000 = 15 seconds.  For non-IE
	// browsers the maximum time to execute this script if all ports are blocked
	// is only ExtPropFWTimeout / 1000, or 3 seconds in this case.

	// The moral of the story is that for best results and fastest test times we
	// recommend you test only the minimum number of ports that are mandatory to
	// know.  Testing too many ports at once (say over 6 or so) can begin to
	// cause long test times and possibily less accurate results.

	// Now that we've told BH what ports we want it to check, we now tell BH to
	// that we want it to test the ports specified in the PortsToCheck property.
	// Since we also want to check for the firewall status, we list that as
	// well.  Note that we could add any other extended property checks here if
	// we wanted, sush as checking for plugins, SSL key sizes and more.

	// Note that we also check for disabled images, because the results of the
	// port and firewall test are undeterminable if images are disabled.  Since
	// none of these tests will execute if JavaScript is disabled, we need to
	// know that as well.

  options.addProperties("OpenPorts, Firewall, ImagesEnabled, JavaScriptEnabled");

  // To avoid an exception, we check that the system executing this
  // example has an Enterprise Edition license as required for this test.
  // Note: This is NOT needed in your pages, only for this sample.
  if (BrowserHawk.getVersion().indexOf("Ent") == -1) {
    out.println("Enterprise License Required.  ");
    out.println("You are using " + BrowserHawk.getVersion() + ".  ");
    out.println("See <a href='http://www.cyscape.com/order/'>http://www.cyscape.com/order/</a> for upgrade information.");
    return;
  }

	// Tell BH to now execute our tests and get the results for us.  Notice that
	// we pass in our options object so that BH knows exactly what extended
	// browser checks we want performed and their associated settings.
	ExtendedBrowserInfo extBrowser =
    BrowserHawk.getExtendedBrowserInfo(request, response, options);

  // A null return value means we're done with the first pass
  if (extBrowser == null) return;

	// It is interesting to note that in this sample we are not interested in
	// any basic properties, such as browser type, version, platform, etc (no
	// non-extended properties are used).  Therefore we do not call the
	// getBrowserInfo() method.

   // We're back from the test at this point - let's display the results!
%>

<html>
<head>
  <title>BrowserHawk Sample - Check blocked ports and firewall (JSP)</title>
</head>
<body>
<font face="arial"><blockquote>

The purpose of this sample is to demonstrate BrowserHawk's ability to detect
ports which are blocked from passing outbound traffic.  This detection is
invaluable if your site or application relies on streaming data through
specific ports other than the default HTTP port of 80.<P>

<% 
   if (extBrowser.getJavaScriptEnabled() == false) {
%>
      Port and firewall status not determinable with JavaScript disabled.
<%
   } else if (extBrowser.getImagesEnabled() == false) {
%>
      Port and firewall status not determinable with images disabled.
<%
   } else {
%>
      The following ports were checked: <%= options.getPortsToCheck() %><br>

<%
      if (extBrowser.getOpenPorts() == null || extBrowser.getOpenPorts().equals("")) {
         // At a minimum this list should contain "80".  If it does not, 
         // then it means that the test timed out or the port check server 
         // was not reachable.  We use an error message below geared to 
         // developer - you would likely want to handle this more gracefully.
%>
         Note to site developer: Port test timed out - this likely means that
         the port check server is unavailable or you are checking too many 
         ports at once. If you have verified the port server is available 
         try increasing the FirewallTimeout value.
<%
      } else {
%>
         Out of the above ports, the following are open and permitting 
         outbound traffic: <%= extBrowser.getOpenPorts() %>
<%
      }
%>

      <P>Firewall status: 
<%
      if (extBrowser.getOpenPorts() == null || extBrowser.getOpenPorts().equals("")) {
%>
         Not determinable because the test timed out.
<%
      } else if (extBrowser.getFirewall()) {
%>
         PRESENT
<%
      } else {
%>
         NOT PRESENT (you may have a firewall, but it is not configured to 
         block ports from outbound traffic)
<%
      }
   }
%>

<HR>

<P>
<b><a href="portcheck.jsp">Retry this test</a></b><P>
<b><a href="index.html">Return to list of other JSP samples</a></b><P>
<b><a href="http://www.cyscape.com/order/">BrowserHawk Pricing and Ordering</a></b><P>
<b><a href="http://www.cyscape.com/">cyScape home page</a></b><P>
</font></blockquote>
</body>
</html>
