X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=chat.js;h=0b7e68e05e23248eaf87773287b182b41dbce9a5;hb=a9d03214b2768688cd906de1618f45b9a708f678;hp=5b68ad5dd242a1db0c17bf410f3aa7e7404464d0;hpb=aaafbeea9847b0654b35fcf3967087c4ab298e2b;p=webchat%2FWebChat.git diff --git a/chat.js b/chat.js index 5b68ad5..0b7e68e 100644 --- a/chat.js +++ b/chat.js @@ -11,8 +11,8 @@ $pastlogfile_pattern = "logfile%d(_+.*)?\.txt"; //過去ログと判定する正 var resource = require("./resources.js"); var config = require("./configure.js"); -var lazy = require("./lazy.js"); var security = require("./security.js"); +var lazy = require("lazy"); var fs = require("fs"); var async = require("async"); var path = require("path"); @@ -29,8 +29,6 @@ module.exports = function(app,server,express,session){ app.get("/chat", chat_proc); app.all("/log/*",express.basicAuth(auth_proc)); app.get("/log/*",log_proc); - app.all("/admin_chat",express.basicAuth(auth_proc)); - app.get("/admin_chat", adminchat_proc); app.all("/admin",express.basicAuth(auth_proc)); app.get("/admin", admin_proc); app.post("/admin",admin_postproc); @@ -58,7 +56,7 @@ function chat_proc(req, res){ var room_number = 0; if(typeof(req.query.rno) != "undefined") room_number = req.query.rno; - res.render("chat",{rno:room_number,token:info.token}); + res.render("chat",{rno:room_number,token:req.session._csrf}); } function auth_proc(user, pass) { @@ -69,22 +67,7 @@ function log_proc(req, res) { res.sendfile(__dirname + req.url); } -function adminchat_proc(req, res){ - var info = new security.SessionInfomation(true); - req.session.items = info; - - var room_number = 0; - if(typeof(req.query.rno) != "undefined") - room_number = req.query.rno; - res.render("chat",{rno:room_number,token:info.token}); -} - function admin_postproc(req,res){ - if(req.session.items.token != req.body.token) - { - res.send(resource.invaild_token_message); - return; - } if(typeof(req.body.erase) != "undefined") { removeLog(req.body.file,function(){ @@ -99,7 +82,7 @@ function admin_postproc(req,res){ } if(typeof(req.body.updateroom) != "undefined") { - $rooms.Update(req.body.newroomlist,function(){ + $rooms.Update(req.body,function(){ res.redirect("/admin"); }); } @@ -112,12 +95,13 @@ function admin_proc(req,res) var iplist = ipbanlist.GetText(); fs.readdir($log_directory,function(err,list){ + res.setHeader("X-FRAME-OPTIONS","DENY"); res.render("admin", { files: list, log_directory:$log_directory, ipbanlist:iplist, - token:info.token, - roomlist:$rooms.GetString() + token:req.session._csrf, + roomlist:$rooms.GetMessage() }); }); } @@ -144,6 +128,14 @@ function removeLog(files,callback) //RoomInfomationCollecionクラス function RoomInfomationCollection() { + var MySQLPool = new require("./mysql_pool.js"); + var pool = new MySQLPool({ + host : config.db_host, + user : config.db_user, + password : config.db_password, + port : config.db_port, + database : "webchat", + }); var collection = {}; this.Get = function(rno){ return collection[rno]; @@ -151,17 +143,17 @@ function RoomInfomationCollection() this.IsContains = function(rno){ return rno in collection; }; - this.GetString = function(){ - var retval = ""; + this.GetMessage = function(){ + var retval = new Array(); for(var rno in collection) { - if($rooms.Get(rno).IsVolatile()) - continue; - var pass = collection[rno].password; - if(pass == null) - pass = ""; - var hiddenlog = collection[rno].hiddenlog; - retval += rno + ":" + pass + ":" + hiddenlog + "\r\n"; + item={}; + item.applyflag = !$rooms.Get(rno).IsVolatile(); + item.password = collection[rno].password; + if(item.password == null) + item.password = ""; + item.hiddenlog = collection[rno].hiddenlog; + retval.push(item); } return retval; }; @@ -173,67 +165,55 @@ function RoomInfomationCollection() } return retval; } - this.Update = function(text,callfunc){ + this.Update = function(data,callfunc){ + Clear(); async.waterfall([ - function(callback){ - fs.open($room_configure_file_name,"w",callback); - }, - function(fd,callback){ - var buf = new Buffer(text); - fs.write(fd,buf,0,Buffer.byteLength(text),null,function(){ - callback(null,fd); - }); + function(next){ + pool.query("TRUNCATE TABLE rooms",null,next); }, - function(fd,callback){ - fs.close(fd,function(){ - GetRoomList(callfunc); - }); + function(result,next){ + console.log(util.inspect(data)); + var items = new Array(); + var config = data.config; + for(var i = 0; i < config.length; i++) + { + var rno = Number(config[i].applyflag); + if(isNaN(rno)) + continue; + var password,romonly; + if(typeof(config[rno].password)=="undefined") + password = null; + else if(config[rno].password == "") + password = null; + else + password = config[rno].password; + if(typeof(config[rno].hiddenlog)=="undefined") + romonly = false; + else + romonly = config[rno].hiddenlog == "romonly"; + + Add(rno,password,romonly); + items.push(new Array(rno,password,romonly)); + } + pool.query("INSERT INTO rooms VALUES ?",[items],callfunc); } - ]); + ],callfunc); } function GetRoomList(callback){ Clear(); - fs.exists($room_configure_file_name,function(exists){ - if(exists == false) - { - if(typeof(callback) == "function") - callback(); - return; + async.waterfall([ + function(next){ + pool.query("SELECT * FROM rooms",null,next); + }, + function(result,next){ + for(var i = 0; i < result.length; i++) + { + //MySQLではTINYINTが使われている + Add(result[i].number,result[i].password,result[i].hiddenlog != 0); + } + next(null,null); } - var stream = fs.createReadStream($room_configure_file_name); - new lazy(stream) - .lines - .forEach(function(line){ - var token = line.toString().replace(/(\r|\n|\r\n)/gm, "").split(":"); - if(token.length == 1) - { - Add(token[0],null,false); - } - else if(token.length == 2) - { - var rno = token[0]; - var pass = token[1]; - if(pass == "") - pass = null; - Add(rno, pass,false); - } - else if(token.length == 3) - { - var rno = token[0]; - var pass = token[1]; - if(pass == "") - pass = null; - var hiddenlog = false; - if(token[2] == "true") - hiddenlog = true; - Add(rno, pass,hiddenlog); - } - }) - .join(function(){ - if(typeof(callback) == "function") - callback(); - }); - }); + ],callback); } function Clear(){ collection = {}; @@ -365,6 +345,14 @@ function RoomInfomation(pass,hiddenlogflag) //IPBANクラス function IpBanCollecion() { + var MySQLPool = new require("./mysql_pool.js"); + var pool = new MySQLPool({ + host : config.db_host, + user : config.db_user, + password : config.db_password, + port : config.db_port, + database : "webchat", + }); var collection = {}; this.IsBaned = function(ip){ return collection[ip] == "r"; @@ -384,49 +372,42 @@ function IpBanCollecion() return text; } this.Update = function(text,callfunc){ + collection = {}; async.waterfall([ - function(callback){ - fs.open($ip_ban_list_file_name,"w",callback); - }, - function(fd,callback){ - var buf = new Buffer(text); - fs.write(fd,buf,0,Buffer.byteLength(text),null,function(){ - callback(null,fd); - }); + function(next){ + pool.query("TRUNCATE TABLE ipbanlist",null,next); }, - function(fd,callback){ - fs.close(fd,function(){ - GetIpBanList(callfunc); - }); - } - ]); - } - function GetIpBanList(callback) - { - collection = {}; - fs.exists($ip_ban_list_file_name,function(exists){ - if(exists == false) - { - if(typeof(callback) == "function") - callback(); - return; - } - var stream = fs.createReadStream($ip_ban_list_file_name); - new lazy(stream) - .lines - .forEach(function(line){ - var token = line.toString().replace(/(\r|\n|\r\n)/gm, "").split(":"); + function(result,next){ + var items = new Array(); + lines = text.split("\r\n"); + for(var i = 0; i < lines.length; i++) + { + var token = lines[i].split(":"); var ip = token[0]; + if(ip == "") + continue; if(token.length == 1) collection[ip] = ""; else collection[ip] = token[1]; - }) - .join(function(){ - if(typeof(callback) == "function") - callback(); - }); - }); + items.push(new Array(ip,collection[ip])); + } + pool.query("INSERT INTO ipbanlist VALUES ?",[items],next); + }, + ],callfunc); + } + function GetIpBanList(callfunc) + { + async.waterfall([ + function(next){ + pool.query("SELECT * FROM ipbanlist",null,next); + }, + function(result,next){ + for(var i = 0; i < result.length; i++) + collection[result[i].ip] = result[i].type; + next(null,null); + }, + ],callfunc); } GetIpBanList(); } @@ -456,7 +437,6 @@ function ParseConnect(socket) room.AddRom(ip); var roomconfig = room.GetConfig(); - roomconfig.admin = socket.handshake.admin; socket.json.emit("send roominfo",roomconfig); var romcount = room.GetRomCount(); @@ -497,13 +477,10 @@ function ParseAuthorization(handshakeData, callback) result = "failed get from session store"; else if(err) result = err; - else if(handshakeData.query.token != session.items.token) + else if(handshakeData.query.token != session._csrf) result = "invaild token"; if(typeof(session) != "undefined" && result == null) - { - handshakeData.admin = session.items.admin; handshakeData.sessionID = sessionID; - } callback(result,result == null && !err); }); } else { @@ -570,7 +547,7 @@ function ParseJoin(socket,msg) } else { - socket.emit("error",$not_match_password); + socket.emit("error",resource.unmatch_password); return; } } @@ -695,7 +672,7 @@ function ParseGetPastLog(socket,file) return; var path = $log_directory + "/" + file; var log = new ChatLog(path); - log.ToArray(socket.handshake.admin,function(array){ + log.ToArray(config.showip,function(array){ socket.json.emit("req pastlog",array); }); } @@ -710,7 +687,7 @@ function ChatLog(path) var array = new Array(); var stream = fs.createReadStream(path); new lazy(stream) - .spilt(";") + .lines .forEach(function(line){ var msg = CreateMessageFromText(line.toString()); if(hasIp == false) @@ -737,7 +714,7 @@ function ChatLog(path) msg.date + "<>" + ip + "<>" + msg.message + - ";"; + "\n"; return text; }