这最终通过jQuery AJAX(和JSONP)的“ timeout”属性解决。查看我自己的答案!
请参阅更新的部分,我也尝试过使用applet。如果您可以通过applet实现提供解决方案,也将毫不犹豫地接受您的回答。
我正在使用基于Java的Web应用程序。我的要求是检查特定端口(例如1935)是否在客户端打开或阻塞。我实现了一个“ jsonp”(为什么使用“ jsonp”?我发现通过AJAX发出的“ http”请求不适用于浏览器“相同来源策略”的corssdomain)对包含特定端口的服务器之一的AJAX调用。并且如果服务器返回xhr.status == 200
,则说明端口已打开。这是一个缺点,我无法让执行流程等待(同步),直到调用完成。这是我正在使用的JavaScript函数。
也欢迎使用任何其他解决方案(必须是客户端的东西必须与我的应用程序平行,请不要建议使用python / php /其他语言。谢谢您的时间。
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
}
------------------------------------------------ -----------更新-------------------------------------- --------------------------
我尝试使用Java Applet(由于Y Martin的建议)。这在appletviewer中工作正常。但是,当我在HTML页面中添加applet时,它会产生脆弱的结果。从某种意义上讲,当我更改标签或调整浏览器大小时,它很容易受到攻击,在打印的消息中portAvailable
的值正在更改。
Applet代码:
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);
}
}
这是我的HTML页面(我将其托管在运行9090端口的另一台Tomcat 6所在的同一台计算机上。我可以使用url ---> http://101.220.25.76:9090/test/
查看此页面:]
http://101.220.25.76:9090/test/
以及我如何对1935端口进行阻止和打开?
我已经为端口1935的入站和出站创建了防火墙规则。我通过禁用/启用这两个规则来检查1935端口的打开/阻止情况。
这是我的<html>
<body>
<applet code="ConnectionTestApplet" width=300 height=50>
</applet>
</body>
</html>
。现在请帮助我:)
Gotcha !!!我已经解决了JSONP和jQuery AJAX调用的问题。我发现了jQuery AJAX的S.S.C.C.E属性,并且当端口被阻塞或打开时,我的代码可以流畅地执行。这是未来访客的解决方案。感谢所有答复者的贡献。
timeout
我认为您不了解JSONP的用例,因此无法使用它测试开放端口。 <!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>
如果您需要客户端解决方案,则可以通过websockets来实现,但这仅在chrome或ff之类的新浏览器中可用。否则,请请求执行ping的服务器端脚本。例如-使用curl脚本:http://en.wikipedia.org/wiki/JSONP
以下是作为Applet的Java代码,用于测试服务器/端口的连接:
curl and ping - how to check whether a website is either up or down?
您仍然必须从applet中获取信息才能对结果进行其他处理。最简单的方法是通过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);
}
}
可以使用Flash组件代替applet。使用ActionCcript中可用的Socket类,可以打开从闪存到服务器端口的tcp连接,以检查其是否打开。但是基于Flash Player版本,需要在打开套接字的服务器上放置一个策略文件。
检查此:
getAppletContext().showDocument(url)
使用JS-Recon,您可以使用javascript进行端口扫描。您只需将其指向您的本地IP地址即可。我相信它可以通过将Web套接字/ CORS连接到任意目标IP /套接字并测量超时来工作。这不是一个完美的方法,但这最终可能是javascript的限制。
如果您可以在Java Applet / Flash应用程序中完成此操作,最终可能会更好,因为它们具有较低级别的访问权限。
您无法在JavaScript中执行此操作,因为它没有真正的套接字支持,使用JavaScript您只能测试HTTP套接字的存在。您可以使用Java(JavaScript不是Java)并编写适当的Java Applet来实现。
您还应该阅读此http://blog.andlabs.org/2010/12/port-scanning-with-html5-and-js-recon.html
在JavaScript中,您必须解决异步问题。这是一个建议:
基于使用checkURL
的following document,这是XMLHttpRequest
的代码示例:
checkURL
此代码需要1秒钟的时间才能从基本资源上的HTTP GET获得200响应。
在您的本地测试中,您得到了立即的答案,因为如果端口关闭但防火墙没有回答,则系统会回答连接重置。
即使可以同时使用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
方法,我还是建议使用计时器,因为代码很可能等待TCP超时并重试(3 x 1分钟?),因为防火墙通常会掉线封闭端口上的数据包,可能会拒绝ICMP数据包,这要感谢ping阻止您测试可用性。而且我想这样的支票不会等待这么长时间。
我偶尔是前端/ javascript / jQuery家伙,所以这可能不是100%专业,但是它足够好,它解决了我类似的问题:
open
由于视图已缓存,因此我无法在服务器端检查端口是否已打开(桌面应用程序正在运行),因此我需要在用户单击浏览器之前检查本地主机/端口
欢迎翻译成JS的编辑