Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Commit 21a2788

Browse files
committed
rewrote the code to iterate html elements with a specific class
1 parent 31b730d commit 21a2788

6 files changed

+96
-52
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.project
2+
.settings

README

-1
This file was deleted.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Port scanning with Javascript
2+
3+
The index.html shows how it can be used.
4+
Further notes about the implementation can be
5+
found in portscanner.js (use the source, Luke).
6+
7+
The portscanning function is taken from
8+
http://www.gnucitizen.org/blog/javascript-port-scanner/
9+
written by Petko Petkov.

index.html

+33-21
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,36 @@
11
<html>
2-
<head>
3-
<script src="jsportscanner.js" type="text/javascript"></script>
4-
<script type="text/javascript">
5-
<!--
6-
7-
function check() {
8-
var host = document.getElementById('host').value;
9-
var port = document.getElementById('port').value;
10-
AttackAPI.PortScanner.scanTarget(function(target,port,result) {
11-
alert(target + ":" + port + " " + result);
12-
}, host, [port], 1000);
13-
}
14-
-->
15-
</script>
16-
</head>
17-
<body>
18-
<form>
19-
Host: <input type="text" id="host" name="host" length="20" />
20-
Port: <input type="text" id="port" name="port" length="6" />
21-
<input type="submit" value="Check" onclick="check();return false" />
22-
</body>
2+
<head>
3+
<script
4+
src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"
5+
type="text/javascript"></script>
6+
<script
7+
src="portscanner.js"
8+
type="text/javascript"></script>
9+
<script>
10+
$(document).ready(function() {
11+
portscanner.init();
12+
});
13+
</script>
14+
</head>
15+
<body>
16+
<table>
17+
<tr>
18+
<th>host:port</th>
19+
<th>status</th>
20+
</tr>
21+
<tr>
22+
<td>github.com:443</td>
23+
<td><span class="portscanner">github.com:443</span></td>
24+
</tr>
25+
<tr>
26+
<td>github.com:80</td>
27+
<td><span class="portscanner">github.com:80</span></td>
28+
</tr>
29+
<tr>
30+
<td>github.com:8080</td>
31+
<td><span class="portscanner">github.com:8080</span></td>
32+
</tr>
33+
</table>
34+
</body>
2335

2436
</html>

jsportscanner.js

-30
This file was deleted.

portscanner.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function PortScanner() {
2+
/* intentionally left blank */
3+
}
4+
5+
PortScanner.prototype.init = function() {
6+
7+
var self = this;
8+
9+
/* iterate over all elements with class "portscanner" */
10+
$(".portscanner").each(function(index) {
11+
12+
/* split the content of the element into host and port */
13+
var target = $(this).text();
14+
var parts = target.split(":");
15+
var host = parts[0];
16+
var port = parts[1];
17+
18+
/* perform the check for the host and port */
19+
var element = $(this);
20+
self.check(function(target,port,status) {
21+
22+
/* put the status into the element */
23+
element.text(status);
24+
}, host, port, 1000);
25+
});
26+
};
27+
28+
/**
29+
* Taken from http://www.gnucitizen.org/blog/javascript-port-scanner/
30+
*/
31+
PortScanner.prototype.check = function (callback, target, port, timeout) {
32+
33+
var timeout = (timeout == null)?100:timeout;
34+
var img = new Image();
35+
36+
img.onerror = function () {
37+
if (!img) return;
38+
img = undefined;
39+
callback(target, port, 'open');
40+
};
41+
42+
img.onload = img.onerror;
43+
img.src = 'http://' + target + ':' + port;
44+
45+
setTimeout(function () {
46+
if (!img) return;
47+
img = undefined;
48+
callback(target, port, 'closed');
49+
}, timeout);
50+
};
51+
52+
var portscanner = new PortScanner();

0 commit comments

Comments
 (0)