Last active
August 29, 2015 14:13
-
-
Save formula1/7f0fd8f1d11ec1bc092a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @license almond 0.3.0 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved. | |
* Available via the MIT or new BSD license. | |
* see: http://github.com/jrburke/almond for details | |
*/ | |
/* | |
* Copyright 2014, Gregg Tavares. | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are | |
* met: | |
* | |
* * Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* * Redistributions in binary form must reproduce the above | |
* copyright notice, this list of conditions and the following disclaimer | |
* in the documentation and/or other materials provided with the | |
* distribution. | |
* * Neither the name of Gregg Tavares. nor the names of its | |
* contributors may be used to endorse or promote products derived from | |
* this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
(function() { | |
var e, t, n; | |
(function(r) { | |
function v(e, t) { | |
return h.call(e, t) | |
} | |
function m(e, t) { | |
var n, r, i, s, o, u, a, f, c, h, p, v = t && t.split("/"), | |
m = l.map, | |
g = m && m["*"] || {}; | |
if (e && e.charAt(0) === ".") | |
if (t) { | |
v = v.slice(0, v.length - 1), e = e.split("/"), o = e.length - 1, l.nodeIdCompat && d.test(e[o]) && (e[o] = e[o].replace(d, "")), e = v.concat(e); | |
for (c = 0; c < e.length; c += 1) { | |
p = e[c]; | |
if (p === ".") e.splice(c, 1), c -= 1; | |
else if (p === "..") { | |
if (c === 1 && (e[2] === ".." || e[0] === "..")) break; | |
c > 0 && (e.splice(c - 1, 2), c -= 2) | |
} | |
} | |
e = e.join("/") | |
} else e.indexOf("./") === 0 && (e = e.substring(2)); | |
if ((v || g) && m) { | |
n = e.split("/"); | |
for (c = n.length; c > 0; c -= 1) { | |
r = n.slice(0, c).join("/"); | |
if (v) | |
for (h = v.length; h > 0; h -= 1) { | |
i = m[v.slice(0, h).join("/")]; | |
if (i) { | |
i = i[r]; | |
if (i) { | |
s = i, u = c; | |
break | |
} | |
} | |
} | |
if (s) break; | |
!a && g && g[r] && (a = g[r], f = c) | |
}!s && a && (s = a, u = f), s && (n.splice(0, u, s), e = n.join("/")) | |
} | |
return e | |
} | |
function g(e, t) { | |
return function() { | |
var n = p.call(arguments, 0); | |
return typeof n[0] != "string" && n.length === 1 && n.push(null), s.apply(r, n.concat([e, t])) | |
} | |
} | |
function y(e) { | |
return function(t) { | |
return m(t, e) | |
} | |
} | |
function b(e) { | |
return function(t) { | |
a[e] = t | |
} | |
} | |
function w(e) { | |
if (v(f, e)) { | |
var t = f[e]; | |
delete f[e], c[e] = !0, i.apply(r, t) | |
} | |
if (!v(a, e) && !v(c, e)) throw new Error("No " + e); | |
return a[e] | |
} | |
function E(e) { | |
var t, n = e ? e.indexOf("!") : -1; | |
return n > -1 && (t = e.substring(0, n), e = e.substring(n + 1, e.length)), [t, e] | |
} | |
function S(e) { | |
return function() { | |
return l && l.config && l.config[e] || {} | |
} | |
} | |
var i, s, o, u, a = {}, | |
f = {}, | |
l = {}, | |
c = {}, | |
h = Object.prototype.hasOwnProperty, | |
p = [].slice, | |
d = /\.js$/; | |
o = function(e, t) { | |
var n, r = E(e), | |
i = r[0]; | |
return e = r[1], i && (i = m(i, t), n = w(i)), i ? n && n.normalize ? e = n.normalize(e, y(t)) : e = m(e, t) : (e = m(e, t), r = E(e), i = r[0], e = r[1], i && (n = w(i))), { | |
f: i ? i + "!" + e : e, | |
n: e, | |
pr: i, | |
p: n | |
} | |
}, u = { | |
require: function(e) { | |
return g(e) | |
}, | |
exports: function(e) { | |
var t = a[e]; | |
return typeof t != "undefined" ? t : a[e] = {} | |
}, | |
module: function(e) { | |
return { | |
id: e, | |
uri: "", | |
exports: a[e], | |
config: S(e) | |
} | |
} | |
}, i = function(e, t, n, i) { | |
var s, l, h, p, d, m = [], | |
y = typeof n, | |
E; | |
i = i || e; | |
if (y === "undefined" || y === "function") { | |
t = !t.length && n.length ? ["require", "exports", "module"] : t; | |
for (d = 0; d < t.length; d += 1) { | |
p = o(t[d], i), l = p.f; | |
if (l === "require") m[d] = u.require(e); | |
else if (l === "exports") m[d] = u.exports(e), E = !0; | |
else if (l === "module") s = m[d] = u.module(e); | |
else if (v(a, l) || v(f, l) || v(c, l)) m[d] = w(l); | |
else { | |
if (!p.p) throw new Error(e + " missing " + l); | |
p.p.load(p.n, g(i, !0), b(l), {}), m[d] = a[l] | |
} | |
} | |
h = n ? n.apply(a[e], m) : undefined; | |
if (e) | |
if (s && s.exports !== r && s.exports !== a[e]) a[e] = s.exports; | |
else if (h !== r || !E) a[e] = h | |
} else e && (a[e] = n) | |
}, e = t = s = function(e, t, n, a, f) { | |
if (typeof e == "string") return u[e] ? u[e](t) : w(o(e, t).f); | |
if (!e.splice) { | |
l = e, l.deps && s(l.deps, l.callback); | |
if (!t) return; | |
t.splice ? (e = t, t = n, n = null) : e = r | |
} | |
return t = t || function() {}, typeof n == "function" && (n = a, a = f), a ? i(r, e, t, n) : setTimeout(function() { | |
i(r, e, t, n) | |
}, 4), s | |
}, s.config = function(e) { | |
return s(e) | |
}, e._defined = a, n = function(e, t, n) { | |
t.splice || (n = t, t = []), !v(a, e) && !v(f, e) && (f[e] = [e, t, n]) | |
}, n.amd = { | |
jQuery: !0 | |
} | |
})(), n("almond.js", function() {}), n("cookies", [], function() { | |
var e = function(e, t) { | |
var n = t || "/"; | |
this.set = function(t, r) { | |
var i = ""; | |
if (r !== undefined) { | |
var s = new Date; | |
s.setTime(Date.now() + r * 24 * 60 * 60 * 1e3), i = "; expires=" + s.toGMTString() | |
} | |
var o = encodeURIComponent(e) + "=" + encodeURIComponent(t) + i + "; path=" + n; | |
document.cookie = o | |
}, this.get = function() { | |
var t = encodeURIComponent(e) + "=", | |
n = document.cookie.split(";"); | |
for (var r = 0; r < n.length; ++r) { | |
var i = n[r]; | |
while (i.charAt(0) == " ") i = i.substring(1, i.length); | |
if (i.indexOf(t) == 0) return decodeURIComponent(i.substring(t.length, i.length)) | |
} | |
}, this.erase = function() { | |
document.cookie = this.set(" ", -1) | |
} | |
}; | |
return e | |
}), n("io", [], function() { | |
var e = function() {}, | |
t = function(t, n, r, i) { | |
i = i || {}; | |
var s = 'sendJSON failed to load url "' + t + '"', | |
o = new XMLHttpRequest; | |
o.overrideMimeType && o.overrideMimeType("text/plain"); | |
var u = i.timeout || 0; | |
o.timeout = u, e("set timeout to: " + o.timeout), o.open("POST", t, !0); | |
var a = JSON.stringify(n), | |
f = function(t, n) { | |
r && (e("calling-callback:" + (t ? " has error" : "success")), r(t, n), r = undefined) | |
}, | |
l = function(n) { | |
e("--abort--"), f("error (abort) sending json to " + t) | |
}, | |
c = function(n) { | |
e("--error--"), f("error sending json to " + t) | |
}, | |
h = function(n) { | |
e("--timeout--"), f("timeout sending json to " + t) | |
}, | |
p = function(n) { | |
r && (e("--forced timeout--"), o.abort(), f("forced timeout sending json to " + t)) | |
}, | |
d = function() { | |
e("--finish--"); | |
var n = undefined, | |
r = o.status == 200 || o.status == 0; | |
if (r) try { | |
n = JSON.parse(o.responseText) | |
} catch (i) { | |
r = !1 | |
} | |
f(r ? null : "could not load: " + t, n) | |
}; | |
try { | |
u && setTimeout(p, u + 50), o.addEventListener("load", d, !1), o.addEventListener("timeout", h, !1), o.addEventListener("error", c, !1), o.setRequestHeader("Content-type", "application/json"), o.send(a), e("--sent: " + t) | |
} catch (v) { | |
e("--exception--"), setTimeout(function() { | |
f("could not load: " + t, null) | |
}, 0) | |
} | |
}; | |
return { | |
sendJSON: t | |
} | |
}), n("iputils", [], function() { | |
var e = window.webkitRTCPeerConnection || window.mozRTCPeerConnection, | |
t = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/, | |
n = function(e) { | |
var n = e.match(t); | |
return n[0] | |
}, | |
r = { | |
iceServers: [] | |
}, | |
i = [], | |
s = !1, | |
o = [], | |
u = 2, | |
a = ["0.0.0.0", "127.0.0.1"], | |
f = function(e) { | |
if (a.indexOf(e) >= 0) return; | |
if (i.indexOf(e) >= 0) return; | |
i.push(e) | |
}, | |
l = function() { | |
if (u <= 0) { | |
s = !0; | |
for (var e = 0; e < o.length; ++e) o[e](i); | |
o = [] | |
} | |
}, | |
c = function() { | |
--u, l() | |
}; | |
if (e) { | |
var h = new e(r); | |
window.mozRTCPeerConnection && h.createDataChannel("", { | |
reliable: !1 | |
}), h.onicecandidate = function(e) { | |
e.candidate && p(e.candidate.candidate), h.iceGatheringState == "complete" && c() | |
}, h.createOffer(function(e) { | |
p(e.sdp), h.setLocalDescription(e), c() | |
}, function(e) { | |
c() | |
}) | |
} else u = 0, l(); | |
var p = function(e) { | |
var t = []; | |
e.split("\r\n").forEach(function(e) { | |
if (~e.indexOf("a=candidate")) { | |
var t = e.split(" "), | |
n = t[4], | |
r = t[7]; | |
r === "host" && f(n) | |
} else if (~e.indexOf("c=")) { | |
var t = e.split(" "), | |
n = t[2]; | |
f(n) | |
} | |
}), l() | |
}, | |
d = function(e) { | |
s ? setTimeout(function() { | |
e(i) | |
}, 0) : o.push(e) | |
}; | |
return { | |
getLocalIpAddresses: d | |
} | |
}), n("progress", [], function() { | |
var e = function(e) { | |
var t = document.createElement("div"); | |
e.appendChild(t), this.set = function(e) { | |
t.style.width = (Math.max(0, Math.min(1, e)) * 100 | 0) + "%" | |
}, this.set(0) | |
}; | |
return e | |
}), n("strings", [], function() { | |
var e = function() { | |
var e = {}; | |
return function(t, n) { | |
var r = e[t]; | |
if (!r || r.length < n) r = (new Array(n + 1)).join(t), e[t] = r; | |
return r | |
} | |
}(), | |
t = function(e) { | |
return typeof e == "string" ? e : e.toString() | |
}, | |
n = function(n, r, i) { | |
n = t(n); | |
if (n.length >= r) return n; | |
var s = e(i, r); | |
return n + s.substr(n.length - r) | |
}, | |
r = function(n, r, i) { | |
n = t(n); | |
if (n.length >= r) return n; | |
var s = e(i, r); | |
return s.substr(n.length - r) + n | |
}, | |
i = function() { | |
var e = /%\(([^\)]+)\)s/g; | |
return function(t, n) { | |
return n.length || (n = [n]), t.replace(e, function(e, t) { | |
var r = t.split("."); | |
for (var i = 0; i < n.length; ++i) { | |
var s = n[i]; | |
for (var o = 0; o < r.length; ++o) { | |
var t = r[o], | |
s = s[t]; | |
if (s === undefined) break | |
} | |
if (s !== undefined) return s | |
} | |
return console.error("unknown key: " + t), "%(" + t + ")s" | |
}) | |
} | |
}(), | |
s = function(e, t) { | |
return e.length >= t.length && e.substr(0, t.length) == t | |
}, | |
o = function(e, t) { | |
return e.length >= t.length && e.substring(e.length - t.length) == t | |
}, | |
u = String.fromCodePoint ? String.fromCodePoint : function() { | |
var e = String.fromCharCode, | |
t = Math.floor, | |
n = function() { | |
var n = 16384, | |
r = [], | |
i, s, o = -1, | |
u = arguments.length; | |
if (!u) return ""; | |
var a = ""; | |
while (++o < u) { | |
var f = Number(arguments[o]); | |
if (!isFinite(f) || f < 0 || f > 1114111 || t(f) != f) throw RangeError("Invalid code point: " + f); | |
f <= 65535 ? r.push(f) : (f -= 65536, i = (f >> 10) + 55296, s = f % 1024 + 56320, r.push(i, s)); | |
if (o + 1 == u || r.length > n) a += e.apply(null, r), r.length = 0 | |
} | |
return a | |
}; | |
return n | |
}(), | |
a = { | |
endsWith: o, | |
fromCodePoint: u, | |
padLeft: r, | |
padRight: n, | |
replaceParams: i, | |
startsWith: s | |
}; | |
return a | |
}), e(["./cookies", "./io", "./iputils", "./progress", "./strings"], function(e, t, n, r, i) { | |
var s = function(e) { | |
return document.getElementById(e) | |
}, | |
o = function(e, t) { | |
var n = t || {}; | |
try { | |
var r = e.indexOf("?"), | |
i = e.indexOf("#"); | |
i < 0 && (i = e.length); | |
var s = e.substring(r + 1, i), | |
o = s.split("&"); | |
for (var u = 0; u < o.length; ++u) { | |
var a = o[u].split("="), | |
f = a[0], | |
l = decodeURIComponent(a[1]); | |
n[f] = l | |
} | |
} catch (i) { | |
console.error(i) | |
} | |
return n | |
}, | |
u = function(e) { | |
return o(window.location.href, e) | |
}, | |
a = u(), | |
f = a.debug || a.verbose ? console.log.bind(console) : function() {}, | |
l = "http://happyfuntimes.net/api/getgames"; | |
a.debug && (l = "http://localhost:1337/api/getgames"), a.local && (l = "http://local.happyfuntimes.net/api/getgames"); | |
var c = new e("name"), | |
h = 1, | |
p = a.debug ? 5 : 254, | |
d = 0, | |
v = 4, | |
m = 2e3, | |
g = 18679, | |
y = !1, | |
b = [], | |
w = [], | |
E = 0, | |
S = 0, | |
x = new r(s("scan-progress")), | |
T = function() { | |
Array.prototype.forEach.call(document.querySelectorAll(".hidden"), function(e) { | |
e.style.display = "block" | |
}) | |
}, | |
N = function() { | |
++S, --d, x.set(S / E), !y && S >= E && T() | |
}, | |
C = function(e, t) { | |
f("hft response: " + JSON.stringify(t)); | |
if (e) { | |
console.error(e), k(); | |
return | |
} | |
A(t) | |
}, | |
k = function() { | |
a.scan ? n.getLocalIpAddresses(M) : T() | |
}, | |
L = function(e) { | |
return "http://" + e.ipAddress | |
}, | |
A = function(e) { | |
var t = [], | |
n = 0, | |
r = function() { | |
if (n == e.length) { | |
t.length == 0 ? k() : t.length == 1 ? H(L(t[0])) : O(t); | |
return | |
} | |
var i = e[n++]; | |
I(i, function(e) { | |
return f("ping hft: " + e), | |
function(n, i) { | |
n || t.push({ | |
ipAddress: e, | |
data: i | |
}), r() | |
} | |
}(i)) | |
}; | |
r() | |
}, | |
O = function(e) { | |
f("ask player to choose hft: " + JSON.stringify(e, undefined, " ")), s("looking").style.display = "none"; | |
var t = s("hft-button-template").text, | |
n = e.map(function(e, n) { | |
return i.replaceParams(t, { | |
id: "game-" + n, | |
url: L(e), | |
name: e.data.serverName || "*unknown*" | |
}) | |
}); | |
s("multiple-hfts").style.display = "block", s("systems").innerHTML = n.join("\n") | |
}; | |
f("checking: " + l), t.sendJSON(l, {}, C, { | |
timeout: 5e3 | |
}); | |
var M = function(e) { | |
e && P(e), D() | |
}, | |
_ = ["192.168.0.0", "192.168.1.0", "192.168.2.0", "192.168.10.0", "192.168.11.0", "10.0.0.0", "10.0.1.0", "192.168.20.0", "192.168.30.0", "192.168.50.0", "192.168.62.0", "192.168.100.0", "192.168.101.0", "192.168.4.0", "192.168.8.0", "192.168.123.0", "192.168.254.0"]; | |
a.debug && (_ = ["192.168.0.0", "10.0.0.0", "192.168.123.0"]); | |
var D = function() { | |
var e = [1, 2, 3, 10, 11, 12, 20, 21, 22, 50, 51, 52, 100, 101, 102, 150, 151, 152, 200, 201, 202]; | |
_.forEach(function(t) { | |
e.forEach(function(e) { | |
b.push(t.replace(/\d+$/, e)), ++E | |
}) | |
}), R() | |
}, | |
P = function(e) { | |
f("addFullScan: " + e), e.forEach(function(e) { | |
for (var t = h; t <= p; ++t) w.push(e.replace(/\d+$/, t)), ++E | |
}), R() | |
}, | |
H = function(e) { | |
y = !0; | |
var t = c.get() || "", | |
n = e + "/enter-name.html?fromHFTNet=true&name=" + encodeURIComponent(t); | |
f("**GOTO** url: " + n), !a.debug && a.go !== !1 && a.go !== "false" && (window.location.href = n) | |
}, | |
B = function(e, t) { | |
H(e) | |
}, | |
j = function(e, t) { | |
var n = Date.now(); | |
return function(r, i) { | |
N(); | |
if (y) return; | |
if (r) { | |
var s = Date.now(), | |
o = s - n; | |
if (o < m * .8) { | |
f("fastScan: " + t + " got fast response"); | |
var u = t.replace(/\.\d+$/, "."); | |
b = b.filter(function(e) { | |
var t = e.substring(0, u.length) != u; | |
return t || N(), t | |
}), P([t]) | |
} else R() | |
} else B(e, i) | |
} | |
}, | |
F = function(e, t) { | |
return function(n, r) { | |
N(); | |
if (y) return; | |
n ? (f("fullScan: " + t + " failed"), R()) : B(e, r) | |
} | |
}, | |
I = function(e, n) { | |
var r = "http://" + e; | |
t.sendJSON(r, { | |
cmd: "happyFunTimesPing" | |
}, function(e, t) { | |
e || (t.version != "0.0.0" ? e = "bad api version: " + t.version : t.id != "HappyFunTimes" && (e = "bad id: " + t.id)), n(e, t) | |
}, { | |
timeout: m | |
}) | |
}, | |
q = function(e, t) { | |
++d; | |
var n = e + ":" + g; | |
I(n, t("http://" + n, e)) | |
}, | |
R = function() { | |
w.length ? (f("fullScan: " + w[0]), q(w.shift(), F)) : b.length && (f("fastScan: " + b[0]), q(b.shift(), j)), d < v && (b.length || w.length) && R() | |
} | |
}), n("scanforserver.js", function() {}), t(["scanforserver.js"]) | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check the most common home class C ip addresses. | |
var commonIpAddresses = [ | |
"192.168.0.0", // D-Link, Linksys, Netgear, Senao, Trendtech, | |
"192.168.1.0", // 3com, Asus, Dell, D-Link, Linksys, MSI, Speedtouch, Trendtech, US Robotics, Zytel, | |
"192.168.2.0", // Belkin, Microsoft, Trendtech, US Robotics, Zyxel, | |
"192.168.10.0", // Motorola, Trendtech, Zyxel | |
"192.168.11.0", // Buffalo | |
"10.0.0.0", // Speedtouch, Zyxel, | |
"10.0.1.0", // Apple, Belkin, D-Link | |
"192.168.20.0", // Motorola | |
"192.168.30.0", // Motorola | |
"192.168.50.0", // Motorola | |
"192.168.62.0", // Motorola | |
"192.168.100.0", // Motorola | |
"192.168.101.0", // Motorola | |
"192.168.4.0", // Zyxel | |
"192.168.8.0", // Zyxel | |
"192.168.123.0", // US Robotics | |
"192.168.254.0", // Flowpoint | |
]; | |
if (g.debug) { | |
commonIpAddresses = [ | |
"192.168.0.0", | |
"10.0.0.0", | |
"192.168.123.0", | |
]; | |
} | |
var addFastScans = function() { | |
// Check these addresses first | |
var commonCClassParts = [1, 2, 3, 10, 11, 12, 20, 21, 22, 50, 51, 52, 100, 101, 102, 150, 151, 152, 200, 201, 202]; | |
commonIpAddresses.forEach(function(ipAddress) { | |
commonCClassParts.forEach(function(cClassPart) { | |
fastScanAddresses.push(ipAddress.replace(/\d+$/, cClassPart)); | |
++totalThingsToDo; | |
}); | |
}); | |
doNextThing(); | |
}; | |
var addFullScans = function(ipAddresses) { | |
log("addFullScan: " + ipAddresses); | |
ipAddresses.forEach(function(ipAddress) { | |
for (var ii = startingAddress; ii <= endingAddress; ++ii) { | |
fullScanAddresses.push(ipAddress.replace(/\d+$/, ii)); | |
++totalThingsToDo; | |
} | |
}); | |
doNextThing(); | |
}; | |
var goToUrl = function(baseUrl) { | |
found = true; | |
var name = nameCookie.get() || ""; | |
var url = baseUrl + "/enter-name.html?fromHFTNet=true&name=" + encodeURIComponent(name); | |
log("**GOTO** url: " + url); | |
if (!g.debug && g.go !== false && g.go !== "false") { | |
window.location.href = url; | |
} | |
}; | |
var checkGoodResponse = function(url, obj) { | |
goToUrl(url); | |
}; | |
var fastScanCheckAddress = function(url, ipAddress) { | |
var timeSent = Date.now(); | |
return function(err, obj) { | |
updateProgress(); | |
if (found) { | |
return; | |
} | |
if (err) { | |
// it wasn't the correct place BUT did we timeout? | |
var now = Date.now(); | |
var elapsedTime = now - timeSent; | |
if (elapsedTime < timeout * 0.8) { | |
log("fastScan: " + ipAddress + " got fast response"); | |
// We didn't timeout which means we probably got a rejected from some machine | |
// So do a fullscan of this network | |
// Remove all pending fastScans for this ip | |
var prefix = ipAddress.replace(/\.\d+$/, '.'); | |
fastScanAddresses = fastScanAddresses.filter(function(address) { | |
var keep = address.substring(0, prefix.length) != prefix; | |
if (!keep) { | |
updateProgress(); | |
} | |
return keep; | |
}); | |
addFullScans([ipAddress]); | |
} else { | |
doNextThing(); | |
} | |
} else { | |
checkGoodResponse(url, obj); | |
} | |
}; | |
}; | |
var fullScanCheckAddress = function(url, ipAddress) { | |
return function(err, obj) { | |
updateProgress(); | |
if (found) { | |
return; | |
} | |
if (err) { | |
log("fullScan: " + ipAddress + " failed"); | |
doNextThing(); | |
} else { | |
checkGoodResponse(url, obj); | |
} | |
}; | |
}; | |
var makeHFTPingRequest = function(ipAndPort, fn) { | |
var url = "http://" + ipAndPort; | |
IO.sendJSON(url, {cmd: 'happyFunTimesPing'}, function(err, obj) { | |
if (!err) { | |
if (obj.version != "0.0.0") { | |
err = "bad api version: " + obj.version; | |
} else if (obj.id != "HappyFunTimes") { | |
err = "bad id: " + obj.id; | |
} | |
} | |
fn(err, obj); | |
}, { timeout: timeout }); | |
}; | |
var startScan = function(ipAddress, fn) { | |
++numRequestsInProgress; | |
var ipAndPort = ipAddress + ":" + port; | |
makeHFTPingRequest(ipAndPort, fn("http://" + ipAndPort, ipAddress)); | |
}; | |
var doNextThing = function() { | |
// If there are fullScan things do those | |
if (fullScanAddresses.length) { | |
log("fullScan: " + fullScanAddresses[0]); | |
startScan(fullScanAddresses.shift(), fullScanCheckAddress); | |
} else if (fastScanAddresses.length) { | |
// If there are fastScan things do those | |
log("fastScan: " + fastScanAddresses[0]); | |
startScan(fastScanAddresses.shift(), fastScanCheckAddress); | |
} | |
if (numRequestsInProgress < maxSimultaneousRequests && | |
(fastScanAddresses.length || fullScanAddresses.length)) { | |
doNextThing(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var lookforLocal = registerArguments( | |
true, | |
{ | |
port:3000, | |
start:1, | |
end:254, | |
} | |
,function(port,start,end,next){ | |
var ips = []; | |
lookforLocal.commonIpAddresses.forEach(function(ipAddress) { | |
lookforLocal.commonCClassParts.forEach(function(cClassPart) { | |
ips.push(ipAddress.replace(/\d+$/, cClassPart)); | |
}); | |
}); | |
async.detect(ips, | |
function(ipAddress,next){ | |
$.ajax({ | |
url:"http://"+ipAddress, | |
timeout:timeout | |
}).done(function(obj){ | |
next(true); | |
}).error(function(x, t, m) { | |
next(t==="timeout") | |
}) | |
}, | |
function(result){ | |
if(!result) return next(void(0),[]); | |
ips = []; | |
for (var i = config.start; i <= config.end; i++) { | |
ips.push(result.replace(/\d+$/, i)); | |
} | |
async.concat(ips, | |
function(ip,next){ | |
$.ajax({ | |
url:"http://"+ip+"/silk-info.json", | |
timeout:timeout | |
}).done(function(obj){ | |
next(void(0),[{ | |
ipAddress: ip, | |
info: obj, | |
}]); | |
}).error(function(x, t, m) { | |
next(void(0),[]); | |
}); | |
},function(err,results){ | |
next(void(0),results); | |
}) | |
}); | |
} | |
lookforLocal.commonIpAddresses = [ | |
"192.168.0.0", // D-Link, Linksys, Netgear, Senao, Trendtech, | |
"192.168.1.0", // 3com, Asus, Dell, D-Link, Linksys, MSI, Speedtouch, Trendtech, US Robotics, Zytel, | |
"192.168.2.0", // Belkin, Microsoft, Trendtech, US Robotics, Zyxel, | |
"192.168.10.0", // Motorola, Trendtech, Zyxel | |
"192.168.11.0", // Buffalo | |
"10.0.0.0", // Speedtouch, Zyxel, | |
"10.0.1.0", // Apple, Belkin, D-Link | |
"192.168.20.0", // Motorola | |
"192.168.30.0", // Motorola | |
"192.168.50.0", // Motorola | |
"192.168.62.0", // Motorola | |
"192.168.100.0", // Motorola | |
"192.168.101.0", // Motorola | |
"192.168.4.0", // Zyxel | |
"192.168.8.0", // Zyxel | |
"192.168.123.0", // US Robotics | |
"192.168.254.0", // Flowpoint | |
]; | |
lookforLocal.commonCClassParts = [1, 2, 3, 10, 11, 12, 20, 21, 22, 50, 51, 52, 100, 101, 102, 150, 151, 152, 200, 201, 202]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment