var $ = require('cheerio');
var Datastore = require('nedb'),
    db = new Datastore({
        filename: 'bans.db',
        autoload: true
    });
module.exports = {
    checkAdmin: function(socket) {
        if (this.isAdmin(socket)) {
            console.log(socket.handshake.address + " is admin");
            socket.join('admin');
            // console.log(socket.username + " is admin")
            var b = '<input type="button" id="my-button" class="uk-button" value="Admin" onClick="UIkit.offcanvas.show(\'#my-id\');socket.emit(\'update userlist\');"></input>';
            var oc = '<div id="my-id" class="uk-offcanvas"><div class="uk-offcanvas-bar"><div id="ulist"></div></div>';
            socket.emit('inject', b);
            socket.emit('inject', oc);
        } else {
            console.log(socket.handshake.address + " is not admin");
        }
    },
    isAdmin: function(socket) {
        if (socket.handshake.address == "::1" || socket.handshake.address == "::ffff:69.23.147.201") return true;
        return false;
    },
    users: function(io) {
        var users = $("<ul>");
        var ns = io.of("/");
        for (var id in ns.connected) {
            var li = $("<li>");
            var kick = $("<a>");
            kick.attr('onClick', 'socket.emit(\'kick\', \'' + ns.connected[id].id + '\');');
            kick.text("Kick")
            var ban = $("<a>");
            ban.attr('onClick', 'socket.emit(\'ban\', \'' + ns.connected[id].id + '\');');
            ban.text("Ban")
            li.text(ns.connected[id].username + " ");
            li.append(kick + " ")
            li.append(ban)
            users.append(li);
        }
        return users.html();
    },
    checkban: function(socket) {
        // socket.handshake.address
        db.findOne({
            ip: socket.handshake.address
        }, function(err, doc) {
            // doc is the document Mars
            // If no document is found, doc is null
            if (!err && doc) {
                socket.banned = true;
                socket.emit('banned');
                socket.disconnect();
            }
            if (!err && !doc) socket.emit('auth');
        })
    },
    checkproxy: function(req) {
        var ipAddr = req.headers["x-forwarded-for"];
        console.log("x-for: " + ipAddr);
        if (ipAddr) {
            var list = ipAddr.split(",");
            ipAddr = list[list.length - 1];
        } else {
            ipAddr = req.connection.remoteAddress;
            console.log("noprox?: " + ipAddr);
        }
    },
    banip: function(ip) {
        db.insert({
            ip: ip
        });
    }
}
@@ -2220,21 +2220,12 @@
     
-var ipAddr = 
+if (
 req.
@@ -2250,25 +2250,17 @@
 ed-for%22%5D
-;%0A       
+)
  console
@@ -2280,249 +2280,105 @@
 %22 + 
-ipAddr);%0A        if (ipAddr) %7B%0A            var list = ipAddr.split(%22,%22);%0A            ipAddr = list%5Blist.length - 1%5D;%0A        %7D else %7B%0A            ipAddr = req.connection.remoteAddress;%0A            console.log(%22noprox?: %22 + ipAddr);%0A        %7D
+req.headers%5B%22x-forwarded-for%22%5D);%0A        console.log(%22noprox?: %22 + req.connection.remoteAddress);
 %0A   

check proxies