10

Is it possible to detect in JavaScript (in the browser) if a port is disabled by the firewall or router?

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
Danny Fox
  • 38,659
  • 28
  • 68
  • 94

3 Answers3

5

No, with pure javascript this is not possible (aside of making http requests to the specific ports, but those results mean little), what you can do however is check from the outside (in other words your server) whether the specific port is open. Another option would be to use a java applet or browser plugin which could do this for you if you really need it, in which case there are various open source tools which you could probably port if you have the necessary experience with those. Do note however that this isn't exactly user friendly. (Either way, it would be useful if you would describe the exact scenario where you need this, as there might be an altogether different solution.)

David Mulder
  • 26,123
  • 9
  • 51
  • 114
  • I have a flash policy server, but if the port 843 isn't available, then I don't want to load the flash applet. – Danny Fox Mar 27 '12 at 07:56
  • In that case I would advise you to load the flash applet, and do a callback to your page if it can't function and next serve the feedback to the user (in which case it's about flash functionality rather than javascript where this isn't possible) (and I don't know what is and isn't possible in flash exactly.). – David Mulder Mar 27 '12 at 08:13
4

You can only see if the expected response is there or not.

One has to stay in the boundaries of HTTP when using javascript.

Of course you can send an Ajax request on whatever port of server and see if you get an error. If you want to check port for current machine then probably sending a request on "localhost:843" could help.

But the error could be of some other reasons and not necessarily firewall issue.

We need more information to help you out.

  • I have a flash policy server, but if the port 843 isn't available, then I don't want to load the flash applet. – Danny Fox Mar 27 '12 at 07:34
3

If you are flexible enough to use jQuery, then see this Answer by me. This will not only check the availability of port, but also whether a success response code 200 is coming from the remote (or any , I meant it supports cross-domain also) server. Also giving the solution here. I will be checking here for port 843.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <script type="text/javascript" src="jquery-1.7.2-min.js"></script>
    </head>
    <body>
        <script type"text/javascript">
            var isAccessible = null;
            function checkConnection() {
  /*make sure you host a helloWorld HTML page in the following URL, so that requests are succeeded with 200 status code*/
                var url = "http://yourserverIP:843/test/hello.html" ; 
                $.ajax({
                    url: url,
                    type: "get",
                    cache: false,
                    dataType: 'jsonp', // it is for supporting crossdomain
                    crossDomain : true,
                    asynchronous : false,
                    jsonpCallback: 'deadCode',
                    timeout : 1500, // set a timeout in milliseconds
                    complete : function(xhr, responseText, thrownError) {
                        if(xhr.status == "200") {
                           isAccessible = true;
                           success(); // yes response came, execute success()
                        }
                        else {
                           isAccessible = false;
                           failure(); // this will be executed after the request gets timed out due to blockage of ports/connections/IPs
                        }
                    }
               });
            }
            $(document).ready( function() {
                checkConnection(); // here I invoke the checking function
            });
        </script>
    </body>
</html>
Community
  • 1
  • 1
tusar
  • 3,364
  • 6
  • 37
  • 60
  • This prompted me to see if this method would apply to a problem I had where we were attempting to ping a port that may not have a listener on the other side running. The problem was that when there was not a listener Chrome always logged an error even when wrapping the xhr request in a try/catch .. not a killer but still ugly. By removing the success and error blocks and handling the response in the complete block, as you did here, we avoided the console message! I was able to still do this with an async call too. Thanks for the code. – likestoski Jan 30 '14 at 15:34