From 0b1b498b7d25766a33e5148e9157e20bfaa2613c Mon Sep 17 00:00:00 2001 From: N0rdye Date: Tue, 27 Jun 2023 20:29:42 +0500 Subject: [PATCH] reconfigure db --- db.js | 21 ++++++-- func.js | 33 ++++++------ index.js | 127 +++++++++++++++++++++++++++++------------------ public/lib/fn.js | 52 ++++++++++++++++--- views/header.ejs | 35 +++++++++++-- views/main.ejs | 59 +--------------------- 6 files changed, 189 insertions(+), 138 deletions(-) diff --git a/db.js b/db.js index 0ef43c0..19719e9 100644 --- a/db.js +++ b/db.js @@ -32,14 +32,27 @@ module.exports.cv = (table,key,value,callback) => { }) } -// get_from -module.exports.gv = (table,key,value,callback) => { - logcon.query('SELECT * FROM `'+table+'` WHERE `'+key+'` = "'+value+'"', (err, rows, fields) => { +module.exports.dl = (table,key,value,callback) => { + // console.log('SELECT * FROM `'+table+'` WHERE `'+key+'` = '+value); + logcon.query('DELETE FROM `'+table+'` WHERE `'+key+'` = '+value, (err, rows, fields) => { if (err) { console.log("sql err"); throw err; }else{ - callback(rows[0]); + callback(rows); + } + }) +} + +// get_from +module.exports.gv = (table,key,value,callback) => { + // console.log('SELECT * FROM `'+table+'` WHERE `'+key+'` = '+value); + logcon.query('SELECT * FROM `'+table+'` WHERE `'+key+'` = '+value, (err, rows, fields) => { + if (err) { + console.log("sql err"); + throw err; + }else{ + callback(rows); } }) } diff --git a/func.js b/func.js index 508ae9d..db7d936 100644 --- a/func.js +++ b/func.js @@ -26,27 +26,24 @@ module.exports.check_sid = (Cookies, callback) =>{ let uuid = Cookies["uuid"]; let sid = Cookies["sid"]; // console.log(uuid,sid); - db.gv("users","uuid",uuid,(rdata)=>{ - let sids = rdata["sids"]; - if(sids != null){ - if(sids.split(";").includes(sid)){ - // console.log("good boy "); - callback(true,Cookies); + db.gv("users","uuid",`'${uuid}'`,(udata)=>{ udata = udata[0]; + // console.log(udata); + db.gv("sids","uid",udata["id"],(rdata)=>{ + let valid = ""; + rdata.forEach(rec => { + if (rec["sid"] == sid){ + valid = rec["sid"]; + return; + } + }); + if(valid != ""){ + callback(true,udata); } else{ - // console.log("bad boy "); - callback(false,Cookies); + callback(false,udata); } - } - else{ - // console.log("bad boy "); - callback(false,Cookies); - } - }); -} - -module.exports.get_by_sid = (Cookies) =>{ - + }); + }) } diff --git a/index.js b/index.js index 0122744..89225c7 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ app.use(express.urlencoded({ app.use(express.static('public')); app.use(cookieParser()); + app.post('/back_login', (req, res) => { let inp = req.body; let cook = req.cookies; @@ -37,37 +38,44 @@ app.post('/back_login', (req, res) => { res.status(210).send({out:"bad", err:"user"}); } else if (login != null){ - db.gv("users","login",ilogin,(udata)=>{ + db.gv("users","login",`'${ilogin}'`,(udata)=>{udata = udata[0]; if(ipass == udata["pass"]){ console.log(udata["uuid"]+" logged in by login & pass from "+cook["sid"]); res.cookie("uuid",udata["uuid"],{maxAge:1000000,path:"/;SameSite=Strict"}); - db.gv("users","uuid",udata["uuid"],(rdata)=>{ - // console.log(rdata["sids"]); - let sids = rdata["sids"]; - // console.log(sids.split(";")); - if(sids != null){ - if(sids.split(";").includes(inp["sid"])){ - console.log("good boy"); - } - else{ - db.sv("users","sids",sids += inp["sid"]+";","uuid",udata["uuid"],()=>{}); - } - } - else{ - db.sv("users","sids",inp["sid"]+";","uuid",udata["uuid"],()=>{}); - } - }); - // db.sv("users","id",udata["id"],"uuid",udata["uuid"]+"|"+uuid); + // db.sv("users","sids",sids += inp["sid"]+";","uuid",udata["uuid"],()=>{}); + db.nr("sids",'`sid`,`uid`',`'${cook["sid"]}','${udata["id"]}'`); + res.send({out:"goto",url:"/main"}); + + // db.gv("users","uuid",udata["uuid"],(rdata)=>{ + // // console.log(rdata["sids"]); + // let sids = rdata["sids"]; + // // console.log(sids.split(";")); + // if(sids != null){ + // if(sids.split(";").includes(inp["sid"])){ + // console.log("good boy"); + // } + // else{ + // db.sv("users","sids",sids += inp["sid"]+";","uuid",udata["uuid"],()=>{}); + // db.nr("sids",'`sid`,`uid`',`'${cook["sid"]}','${udata["id"]}'`); + // // db.sv("sids","uid",sids += inp["sid"]+";","uuid",udata["uuid"], ()=>{}); + // } + // } + // else{ + // db.sv("users","sids",inp["sid"]+";","uuid",udata["uuid"],()=>{}); + // db.nr("sids",'`sid`,`uid`',`'${cook["sid"]}','${udata["id"]}'`); + // } + // }); + // // db.sv("users","id",udata["id"],"uuid",udata["uuid"]+"|"+uuid); - if(udata["admin"] == 1){ - res.send({out:"goto",url:"/main"}); + // if(udata["admin"] == 1){ + // res.send({out:"goto",url:"/main"}); - } - else{ - res.send({out:"goto",url:"/main"}); - // res.redirect("main"); - } + // } + // else{ + // res.send({out:"goto",url:"/main"}); + // // res.redirect("main"); + // } } else{ @@ -94,7 +102,7 @@ app.post('/reg_user', (req, res) => { console.log("/reg_user same uuid recs = "+udata); if(udata==null){ if(ldata==null){ - good_reg(); + good_reg(udata); console.log("/reg_user good reg"); console.log("/reg_user reged "+login+" uuid = "+uuid); } @@ -109,15 +117,18 @@ app.post('/reg_user', (req, res) => { }) }) } - function good_reg(){ + function good_reg(udata){ db.nr("users",'`login`,`pass`,`uuid`,`admin`',`'${login}','${pass}','${uuid}',${admin}`); + if (admin){ + db.gv("users","uuid",`'${uuid}'`,(res)=>{ res = res[0] + db.nr("admins",'`login`,`uid`',`'${login}',${res["id"]}`); + }) + } res.redirect("/reg"); } }) app.post("/sid_log",(req,res) =>{ - // console.log(req.cookies["sid"]); - // console.log(req.cookies["uuid"]); let inp = req.body; let cook = req.cookies; @@ -133,24 +144,6 @@ app.post("/sid_log",(req,res) =>{ } }) } - // if(req.cookies["uuid"]!= null){ - // let sid = req.cookies["sid"]; - // let uuid = func.decrypt(req.cookies["uuid"],"key"); - // db.gv("users","uuid",uuid,(rdata)=>{ - // let sids = rdata["sids"]; - // if(sids != null){ - // // console.log(sids.split(";")); - // if(sids.split(";").includes(sid)){ - // console.log("good boy "+rdata["login"]+" logged in by sid"); - // res.send({out:"goto",url:"/user",args:{pass:rdata["pass"],uuid:uuid,login:rdata["login"]}}); - // } - // else{ - // res.redirect("/login"); - // } - // } - // }); - // } - }) app.post("/get_sid" , (req,res) =>{ @@ -159,13 +152,51 @@ app.post("/get_sid" , (req,res) =>{ var week = 7 * 24 * 3600 * 1000; res.cookie("sid",sid,{maxAge:(week),path:"/;SameSite=Strict"}); res.send({out:"good"}); +}); + +app.post("/clear_sid" , (req,res) =>{ + let inp = req.body; + let cook = req.cookies; + + if(cook["uuid"] != null && cook["sid"] != null){ + res.send({out:"good"}); + db.dl("sids","sid",`'${cook["sid"]}'`,() =>{ + console.log(cook["uuid"] + "logged out from "+cook["sid"]); + }); + } }); app.post("/get_cr_uuid", (req,res) => { - + let inp = req.body; + if(inp["uuid"] != null && inp["sid"] != null){ + db.gv("users","uuid",`"${inp["uuid"]}"`,(udata)=>{udata = udata[0]; + let re = udata; + delete re["pass"]; + delete re["uuid"]; + // delete re["sids"]; + func.check_sid(inp,(include,id) => { + if(include){ + res.send({out:"good",body:re}); + } + else if (!include){ + res.send({out:"bad",body:"expired"}); + } + else{ + res.send({out:"bad"}); + } + }) + }); + } }) +// app.post("/set_cr_uuid", (req,res) => { +// let inp = req.body; +// if(inp["uuid"] != null && inp["sid"] != null){ +// db.sv("users"); +// } +// }) + // app.post("/get_uuid" , (req,res) =>{ // let inp = req.body; // db.gv("users","login",inp["login"],(udata)=>{ diff --git a/public/lib/fn.js b/public/lib/fn.js index 91003eb..6c98046 100644 --- a/public/lib/fn.js +++ b/public/lib/fn.js @@ -23,13 +23,34 @@ function log(nlogin,npass){ }); } -function get_uuid_name(){ +function get_from_uuid(callback){ const uid = $.cookie("uuid"); const sid = $.cookie("sid"); $.post( "/get_cr_uuid", { uuid:uid,sid:sid }) .done(function( res ) { if (res["out"] == "good"){ - + callback(res["body"]) + } + else if (res["out"] == bad){ + if (res["body"] == "expired"){ + clear_ck(); + } + } + }); +} +function get_from_uuid(callback){ + const uid = $.cookie("uuid"); + const sid = $.cookie("sid"); + $.post( "/get_cr_uuid", { uuid:uid,sid:sid }) + .done(function( res ) { + if (res["out"] == "good"){ + console.log("good"); + callback(res["body"]) + } + else if (res["out"] == bad){ + if (res["body"] == "expired"){ + logout(true); + } } }); } @@ -37,7 +58,7 @@ function get_uuid_name(){ function log_by_sid() { // const uuid = $.cookie("uuid"); // const sid = $.cookie("sid"); - console.log("log"); + // console.log("log"); if($.cookie('sid') == null){ // get_sid(location.hostname); clear_ck(false); @@ -55,6 +76,7 @@ function log_by_sid() { } function clear_ck(redirect = true){ + console.log("sid"); $.cookie("uuid",null); $.cookie("sid",null); get_sid(location.hostname); @@ -76,10 +98,26 @@ function check_sid(){ } } -function logout() { - let dialog = confirm("logout?"); - if(dialog){ - clear_ck(); +function logout(quet = false) { + if (!quet) + { + let dialog = confirm("logout?"); + if(dialog){ + $.post( "/clear_sid") + .done(function( res ) { + if(res["out"] == "good"){ + clear_ck(); + } + }) + } + } + else{ + $.post( "/clear_sid") + .done(function( res ) { + if(res["out"] == "good"){ + clear_ck(); + } + }) } } diff --git a/views/header.ejs b/views/header.ejs index c285cf3..c9cd21f 100644 --- a/views/header.ejs +++ b/views/header.ejs @@ -2,10 +2,39 @@ header{ display: flex; /* width: 90%; */ - justify-content: space-evenly; + justify-content: space-between; + width: 90%; + text-align: center; + margin: auto; } + #top_panel_right{ + float: right; + } + #top_panel_left{ + float: left; + } + /* button{ + /* margin-inline: 5px; */ + /* } */ -
+
+ + +
puzzle
-
+
+
\ No newline at end of file diff --git a/views/main.ejs b/views/main.ejs index 54f7953..003ceb1 100644 --- a/views/main.ejs +++ b/views/main.ejs @@ -44,12 +44,6 @@ <%- include('./header.ejs') %> - -

none

-

hello

- - -
@@ -60,55 +54,4 @@ -<%- include('./static/end.ejs') %> - - - - - \ No newline at end of file +<%- include('./static/end.ejs') %> \ No newline at end of file