I'm trying to implement a proxy ban list (obv a big list then) and it's behaving inconsistently. So, I isolated the actual list part to test separately in a web browser: Code (text): function ProxyList() { this.works = true; this.list; } ProxyList.prototype.isBanned = function(ip) { if (!this.works) return false; //binary search var high = this.list.length - 1, low = 0, mid, val; while (low <= high) { mid = (low + high) / 2 | 0; val = this.list[mid]; if (val < ip) { low = mid + 1; } else if (val > ip) { high = mid - 1; } else { return true; } } return false; } ipbans = new ProxyList(); ipbans.list = ["0.2.2.37", "0.2.2.38", "0.2.2.39", "0.2.3.12", "0.2.3.13", "0.2.3.14", "0.2.3.18", "0.2.3.19", "0.2.3.20"]; document.write("" + ipbans.isBanned("1.0.0.1") + ipbans.isBanned("0.2.3.20") + ipbans.isBanned("0.2.3.18")); This works exactly as intended and changing the values of the IPs works flawlessly. So I put that function back in to the script to use on the actual list, where everything stops working. Code (text): function ProxyList() { this.works = true; try { this.list = sys.getFileContent(proxyfile).split(/\n/); // a txt file with one IP Address per line } catch (e) { print("Failed to load proxies."); sys.sendAll("Failed to load proxies.", watch); this.works = false; } } The function isBanned is exactly the same. The proxyFile was tested with a list of only 20 IPs instead of the full 709k, at which time it functioned flawlessly, proof that grabbing it into the list from the file is functioning fine. The following input is with the full list: Show there was no catch: /eval sys.sendMessage(source, ipbans.works, chan) (17:50:58) true Grab one off the list: /eval sys.sendMessage(source, ipbans.list[0], chan); (18:37:15) 0.2.2.35 Neither indexOf nor my search function think it's in there: /eval sys.sendMessage(source, ipbans.isBanned("0.2.2.35"), chan); (18:40:20) false /eval sys.sendMessage(source, ipbans.list.indexOf("0.2.2.35"), chan); (18:40:59) -1 So it has this one-way deal, right? So when I pull off this trick /eval sys.sendMessage(source, ipbans.list.indexOf(ipbans.list[0]), chan); (18:42:50) 0 it suddenly knows where to find it? So why can't I find it? Now then there's this: /eval sys.sendMessage(source, ipbans.isBanned(ipbans.list[0])); (18:55:05) true and here's where I pull the hair out of my head in frustration. It behaves the exact same for any index, not just 0. I don't understand why it can't find it like this: ipbans.list[0] -> 0.2.2.35 ipbans.list.indexOf(0.2.2.35) -> -1 ipbans.isBanned(0.2.2.35) -> false but doing it all in one step: ipbans.list.indexOf(ipbans.list[0]) -> 0 ipbans.isBanned(ipbans.list[0]) -> true The effect of this not working like it should is that the test always fails, so proxy ips are not blocked.
/eval "0.2.2.35" === ipbans.list[0] will probably return false for you. Check if there's any gibberish after the IPs in your list (like a space), and remove that from the file. Also, your list might use Windows-style line terminators (\r\n), either split using \r\n or remove all carriage returns from your file.
This is why computer science classes are evil.... JavaScript has a built in hashmap, use that instead of a binary search, it's faster and simpler... (18:03:59) [#Watch] server: /eval for (var i = 0; i < 10000; i++) script.f("127.0.0.9") // script.f contains your function Exec Time: 35 miliseconds Serializer Time: <1 milisecond Result: <Bool> false (18:04:12) [#Watch] server: /eval for (var i = 0; i < 10000; i++) "127.0.0.9" in script.modules.antiproxy.PROXYBANS Exec Time: 6 miliseconds Serializer Time: <1 milisecond Result: <Bool> false Make an object with keys of the values of the array.