-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathindex.html
226 lines (223 loc) · 10.6 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!DOCTYPE html>
<html>
<head>
<style>
.TreeNode, .Container {
background-position: left top;
background-size: 1em;
background-repeat: no-repeat;
}
.TreeNode .TreeNode {
padding-left: 1em;
background-image: url(child.svg);
}
.TreeNode .TreeNode .Container {
padding-left: 1em;
margin-left: -1em;
background-image: url(extend-child.svg);
background-repeat: repeat-y;
}
.TreeNode .TreeNode:last-child {
background-image: url(last-child.svg);
}
.TreeNode .TreeNode:last-child .Container {
background-image: none;
}
</style>
<script src="fGetIPAddresses.js"></script>
<script src="fXHRScanIPAddressPorts.js"></script>
<script src="fxIPAddress.js"></script>
<script>
onload = function fWindow_load_EventHandler() {
var uStartTime = new Date().valueOf();
function fsGetTime() {
var uTimeSeconds = Math.round((new Date().valueOf() - uStartTime) / 1000),
uTimeMinutes = Math.floor(uTimeSeconds / 60) % 60,
uTimeHours = Math.floor(uTimeSeconds / 60 / 60);
return (
(uTimeHours ? uTimeHours + " hours, " : "") +
(uTimeMinutes ? uTimeMinutes + " minutes, " : "") +
(uTimeSeconds % 60) + " seconds"
);
};
var oInterval = setInterval(function () {
document.title = "Scanning (" + fsGetTime() + ")...";
}, 1000);
fGetIPAddresses(
oIFrame,
function fGetIPAddressSuccessCallback(asIPAddresses) {
if (asIPAddresses.length == 0) {
document.body.appendChild(new cTreeNode("Your local IP address could not be determined.", "error.svg").oRootElement);
return;
}
fScanNetworksForIPAddresses(asIPAddresses, function() {
document.title = "Done.";
clearInterval(oInterval);
document.body.appendChild(new cTreeNode("Scanning took " + fsGetTime() + ".", "info.svg").oRootElement);
// done scanning.
});
},
function fGetIPAddressErrorCallback(sErrorMessage) {
document.body.appendChild(new cTreeNode(sErrorMessage, "error.svg").oRootElement);
}
);
};
function fScanNetworksForIPAddresses(asIPAddresses, fCallback) {
(function fScanNetworksForIPAddressesLoop() {
if (asIPAddresses.length != 0) {
return fScanNetwork(asIPAddresses.shift(), fScanNetworksForIPAddressesLoop);
};
fCallback();
})();
};
function fScanNetwork(sIPAddress, fCallback) {
var oNetworkTreeNode = new cTreeNode("Determining subnet for " + sIPAddress + "...", "scanning.svg"),
uIPAddress = fuIPAddress(sIPAddress);
document.body.appendChild(oNetworkTreeNode.oRootElement);
fGetNetworkSubnetPrefixLength(uIPAddress, oNetworkTreeNode, function (uPrefixLength) {
oNetworkTreeNode.setName("Scanning network " + sIPAddress + "/" + uPrefixLength + "...")
var uBitMask = - (1 << (32 - uPrefixLength)),
uAllOnes = (1 << (32 - uPrefixLength)) - 1,
uStartIPAddress = uIPAddress & uBitMask,
uEndIPAddress = uIPAddress | uAllOnes,
uCurrentIPAddress = uStartIPAddress + 1,
uScansStarted = 0,
uScansFinished = 0;
function fScanIPAddressThread() {
uScansStarted++;
if (uCurrentIPAddress != uEndIPAddress) {
var sCurrentIPAddress = fsIPAddress(uCurrentIPAddress);
if (uCurrentIPAddress == uIPAddress) {
oTreeNode = oNetworkTreeNode.appendChild(new cTreeNode(sCurrentIPAddress + " (you)", "machine.svg"));
uCurrentIPAddress++;
fScanIPAddressThread(); // no need to scan self.
} else {
oTreeNode = oNetworkTreeNode.appendChild(new cTreeNode(sCurrentIPAddress, "scanning.svg"));
fScanIPAddress(uCurrentIPAddress++, oTreeNode, fScanIPAddressThread);
};
return;
};
oNetworkTreeNode.setName("Network " + sIPAddress + "/" + uPrefixLength)
oNetworkTreeNode.setIcon("network.svg")
uScansFinished++;
if (uScansStarted == uScansFinished) {
fCallback();
};
};
for (var uThreads = 64; uThreads--;) setTimeout(fScanIPAddressThread);
});
};
function fGetNetworkSubnetPrefixLength(uIPAddress, oNetworkTreeNode, fCallback) {
// Attempting to make an XHR to the broadcast address will result in an immediate error. Attempting to make an
// XHR to an unused IP address will result in a time-out. We'll start with a large prefix and try increasingly
// smaller ones to look for potential broadcast addresses using this timing difference. An IP address can also
// be in-use by a *nix machine, which will also result in an immediate error. In an attempt to distinguish
// between these two, try the next smaller prefix as well: if that fails, assume the former prefix is right and
// return. Obviously this is not perfect, but it seems to work well enough.
var uPrefixLength = 26,
bBroadcastAddressMayHaveBeenFound = false,
oTreeNode = oNetworkTreeNode.appendChild(new cTreeNode("", "scanning.svg"));
(function fTestPrefixLength() {
if (uPrefixLength >= 16) {
var uAllOnes = (1 << (32 - uPrefixLength)) - 1,
sBroadcastIPAddress = fsIPAddress(uIPAddress | uAllOnes);
oTreeNode.setName("Testing potential broadcast address " + sBroadcastIPAddress + " (/" + uPrefixLength + ")...");
fXHRScanIPAddressPorts(sBroadcastIPAddress, [2], function(auDetectedPortNumbers) {
if (auDetectedPortNumbers.length > 0) {
// This IP address results in an immediate error. It may be the broadcast address.
if (uPrefixLength == 16) {
// We won't try to scan larger networks: use this.
oTreeNode.remove();
return fCallback(uPrefixLength);
};
bBroadcastAddressMayHaveBeenFound = true;
// Try the next: in most setups this should fail if we just found the broadcast address.
uPrefixLength--;
return fTestPrefixLength();
};
if (!bBroadcastAddressMayHaveBeenFound) {
// This IP address is not used, nor was the previously tested one: try the next.
uPrefixLength--;
return fTestPrefixLength();
};
// This IP address is not used, so the previous one is probably the broadcast address.
oTreeNode.remove();
fCallback(uPrefixLength + 1);
});
return;
};
oTreeNode.setName("Could not determine subnet, assuming /24...");
oTreeNode.setIcon("error.svg");
fCallback(8);
})();
};
function fScanIPAddress(uIPAddress, oMachineTreeNode, fCallback) {
var sIPAddress = fsIPAddress(uIPAddress);
oMachineTreeNode.setName(sIPAddress);
oMachineTreeNode.setIcon("scanning.svg");
// check if machine responds on the SMB/RDP ports, which both Windows and *nix machines might.
fXHRScanIPAddressPorts(sIPAddress, [80, 443, 445, 3389], function(auDetectedPortNumbers) {
// no response on this port: assume IP address not in use.
if (auDetectedPortNumbers.length == 0) {
oMachineTreeNode.remove();
return fCallback();
};
// check if machine responds to other ports that are very unlikely to be in use:
fXHRScanIPAddressPorts(sIPAddress, [2], function(auDetectedPortNumbers) {
if (auDetectedPortNumbers.length > 0) {
// machine responds to ports that are very unlikely to be in use: probably *nix.
oMachineTreeNode.setIcon("machine.svg");
return fCallback();
};
// check again as this is somewhat unreliable:
fXHRScanIPAddressPorts(sIPAddress, [3], function(auDetectedPortNumbers) {
if (auDetectedPortNumbers.length > 0) {
// machine responds to ports that are very unlikely to be in use: probably *nix.
oMachineTreeNode.setIcon("machine.svg");
} else {
// machine does not appear to respond to ports that are not in use: probably Windows.
oMachineTreeNode.setIcon("windows.svg");
};
fCallback();
});
});
});
};
function cTreeNode(sNodeName, sIconURL) {
this.oRootElement = document.createElement("div");
this.oRootElement.className = "TreeNode";
oNodeElement = this.oRootElement.appendChild(document.createElement("div"))
this.oNodeIconImageNode = oNodeElement.appendChild(document.createElement("img"));
this.oNodeIconImageNode.style.setProperty("vertical-align", "top");
this.oNodeIconImageNode.style.setProperty("width", "1em");
if (sIconURL) {
this.oNodeIconImageNode.src = sIconURL;
} else {
this.oNodeIconImageNode.style.setProperty("display", "none");
};
oNodeNameTextElement = oNodeElement.appendChild(document.createElement("span"))
this.oNodeNameTextNode = oNodeNameTextElement.appendChild(document.createTextNode(sNodeName));
oNodeNameTextElement.style.setProperty("margin-left", "0.25em");
this.oContainerElement = this.oRootElement.appendChild(document.createElement("div"));
this.oContainerElement.className = "Container";
};
cTreeNode.prototype.setName = function cTreeNode_setName(sNewName) {
this.oNodeNameTextNode.nodeValue = sNewName;
};
cTreeNode.prototype.setIcon = function cTreeNode_setName(sIconURL) {
this.oNodeIconImageNode.style.setProperty("display", "inline-block");
this.oNodeIconImageNode.src = sIconURL;
};
cTreeNode.prototype.appendChild = function cTreeNode_appendChild(oChildTreeNode) {
this.oContainerElement.appendChild(oChildTreeNode.oRootElement);
return oChildTreeNode;
};
cTreeNode.prototype.remove = function cTreeNode_remove() {
this.oRootElement.parentNode.removeChild(this.oRootElement);
};
</script>
</head>
<body>
<iframe id="oIFrame" sandbox="allow-same-origin" style="display: none"></iframe>
</body>
</html>