How to check if a port is open on a client network / firewall?

This is finally solved with the jQuery AJAX (and JSONP) timeout attribute. See my own answer!

Please view the updated part, I also tried the applet. And feel free to accept your answer if you can give a solution with an applet implementation.

I am working with a Java based web application. My requirement is to check if a specific port (for example, 1935) is open or blocked on the client side. I implemented "jsonp" (why "jsonp"? I found that the http request through AJAX cannot work for corssdomain for the same browser origin policy). AJAX call to one of my servers containing a specific port. And if the server returns xhr.status == 200 , the port is open. Here is a drawback that I cannot make wait for execution (synchronously) until the call ends. Here is the JavaScript function I'm using.

Any alternative solution (should be on the client side should be in parallel with my application, please do not offer python / php / other languages). Thank you for your time.

 function checkURL() { var url = "http://10.0.5.255:1935/contextname" ; var isAccessible = false; $.ajax({ url: url, type: "get", cache: false, dataType: 'jsonp', crossDomain : true, asynchronous : false, jsonpCallback: 'deadCode', complete : function(xhr, responseText, thrownError) { if(xhr.status == "200") { isAccessible = true; alert("Request complete, isAccessible==> " + isAccessible); // this alert does not come when port is blocked } } }); alert("returning isAccessible=> "+ isAccessible); //this alert comes 2 times before and after the AJAX call when port is open return isAccessible; } function deadCode() { alert("Inside Deadcode"); // this does not execute in any cases } 

-------------------------------------------- ------ ------- UPDATE ------------------------------------ ------ ----------------------

I tried using a Java applet (thanks to a suggestion by Y Martin). This works great in appletviewer. But when I add the applet to the HTML page, it gives vulnerable results. Vulnerable in the sense that when I change the tab or resize the browser, the portAvailable value changes in the printed message.

Applet code:

 import java.applet.Applet; import java.awt.Graphics; import java.net.InetSocketAddress; import java.net.Socket; public class ConnectionTestApplet extends Applet { private static boolean portAvailable; public void start() { int delay = 1000; // 1 s try { Socket socket = new Socket(); /*****This is my tomcat5.5 which running on port 1935*************/ /***I can view it with url--> http://101.220.25.76:1935/**********/ socket.connect(new InetSocketAddress("101.220.25.76", 1935), delay); portAvailable = socket.isConnected(); socket.close(); System.out.println("init() giving---> " + portAvailable); } catch (Exception e) { portAvailable = false; System.out.println("init() giving---> " + portAvailable); System.out.println("Threw error---> " + e.getMessage()); } } public void paint(Graphics g) { System.out.println("Connection possible---> " + portAvailable); String msg = "Connection possible---> " + portAvailable; g.drawString(msg, 10, 30); } } 

And this is my HTML page (I host it on the same computer as another Tomcat 6, which runs on port 9090. I can view this page from url ---> http://101.220.25.76:9090/test/ ):

 <html> <body> <applet code="ConnectionTestApplet" width=300 height=50> </applet> </body> </html> 

And how do I lock and open port 1935?

I created a firewall rule for inbound and outbound for port 1935. I am checking the script for opening / blocking port 1935 by disabling / enabling both rules.

This is my SSCCE . Now please help me :)

+5
source share
7 answers

Gocha !!! I solved the problem with JSONP and jQuery AJAX call. I found the jQuery AJAX timeout attribute, and my code ran smoothly when the port was blocked or open. Here is a solution for future visitors. Thanks to all the defendants for their input.

 <!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() { var url = "http://101.212.33.60:1935/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, esecute 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> 
+4
source

I don’t think you understand the use cases of JSONP, and it is not possible to check open ports with it. http://en.wikipedia.org/wiki/JSONP

If you want a client solution to be possible with the help of web sockets, but this is only available in new browsers such as chrome or ff. Otherwise, request the server side of the script that pings. For example, with curl script: curl and ping - how to check if a site is up or down?

+2
source

Here is the Java code as an applet to test the connection to the server / port:

 import java.io.*; import java.net.*; public class ConnectionTestApplet extends Applet { public void start() { boolean portAvailable = false; int delay = 1000; // 1 s try { Socket socket = new Socket(); socket.connect(new InetSocketAddress("server.domain.com", 1935), delay); portAvailable = socket.isConnected(); socket.close(); } catch (UnknownHostException uhe) { uhe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } System.out.println("Connection possible: " + portAvailable); } } 

You still need to get information from the applet to do something else with this result. The easiest way is to redirect the browser using getAppletContext().showDocument(url)

+2
source

You cannot do this in JavaScript because it does not support true socket support, and with JavaScript you can only check for an HTTP socket. You can use Java (JavaScript is not Java) and write the appropriate Java applet for this.

You should also read this Q & A How to Ping in java

Try using isReachable

+1
source

Instead of an applet, a flash component can be used. Using the Socket class available in ActionCcript, you can open a tcp connection from flash to a port on the server to check if it is open. But based on the version of the flash player, the policy file must be located on the server to which the socket is open.

+1
source

Check this:

http://blog.andlabs.org/2010/12/port-scanning-with-html5-and-js-recon.html

With JS-Recon, you can perform port scans with javascript. You can simply point it to the local IP address. I believe this works by creating a connection to / cors web sockets with an arbitrary ip / socket descriptor and measuring timeouts. This is not an ideal approach, but it may ultimately be the limit of javascript.

If you can do this in a java applet / flash application, it might be better, since they have access at a lower level.

+1
source

In JavaScript, you have to get around the asynchronous issue. Here is a suggestion:

  • An HTML page displays an animated image as a progress bar.
  • You call checkURL
  • After receiving a callback or a specific timeout, you change the display of the error message or complete the task

Based on the following document using XMLHttpRequest , here is a sample code for checkURL :

 var myrequest = new ajaxRequest(); var isAccessible = false; myrequest._timeout = setTimeout(function() { myrequest.abort(); displayErrorMessage(); }, 1000 ) //end setTimeout myrequest.onreadystatechange = function() { if (myrequest.readyState == 4) { //if request has completed if (myrequest.status == 200) { isAccessible = false; goOnWithTheJob(); } else { displayErrorMessage(); } } myrequest.open("GET", url, true); myrequest.send(null); //send GET request // do nothing - wait for either timeout or readystate callback 

This code allows 1 second to receive a 200 response from an HTTP GET on a base resource.

In your local test, you will get an immediate response because the system responds to the reset connection if the port is closed, but the firewall simply does not respond.

Even if the open method can be used synchronously, I recommend using a timer because the code can wait for TCP timeouts and retry (3 x 1 minute?) As a firewall, it usually just drops packets on closed ports and can reject ICMP packets, which interferes with availability check thanks to ping. And I suppose that such a long wait is not expected for such a check.

0
source

Source: https://habr.com/ru/post/911727/


All Articles