var express = require('express'); const http = require('http').Server(router); var router = express.Router(); var bodyParser = require('body-parser'); var path = require("path"); const nodemailer = require('nodemailer'); const fs = require('fs'); const multer = require('multer') const Database = require('sqlite-async'); const { name } = require('ejs'); var execSync = require('child_process').execSync; var jsonParser = bodyParser.json(); var urlencodedParser = bodyParser.urlencoded({ extended: false }) //桌面latest版 function execute(cmd){ execSync(cmd,{cwd:'C:\\Users\\17217\\Desktop\\english\\python'},function(error, stdout, stderr){ if (error){ console.log("error!") console.error(error); } else{ console.log("运行成功!") } }) } //邮箱发送验证码 function sendMails(mailId, VerificationCode){ //设置邮箱配置、资源链接池 var transporter = nodemailer.createTransport({ //host:'smtp.qq.com', //邮箱服务的主机,smtp.qq.com service: 'qq', port: '465', //对应的端口号QQ邮箱的端口号是465 secure: false, //开启安全连接 auth: { //用户信息 user: '1721777650@qq.com', //用来发邮件的邮箱账户 pass: 'woaibabamama' //这里的密码是qq的smtp授权码,可以去qq邮箱后台开通查看 } }); //设置收件人信息、邮件内容 var mailOptions = { from: '1721777650@qq.com', //发送者 to: '947213307@qq.com', //接收者 subject: '验证码邮件', //主题 text: '您的注册验证码为' + VerificationCode, //文本内容 }; transporter.sendMail(mailOptions, (error) => { if (error) { return console.log(error); } console.log('邮件发送成功,发送至:', mailId); }); } //验证码生成 function getVerificationCode(){ //传入需要的字符串长度 // 准备一个用来抽取码的字符串,或者字典 var verification_code_str = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; //数字和字母 // 获取某个范围的随机整数,封装的函数,在上面抽取字典的时候进行了调用 function getRandom(min, max) { //获取min-max数字之间的某个随机数,直接调用即可 return Math.round(Math.random() * (max - min) + min); } let newStr = ''; //创建一个空字符串,用来拼接四位随机码 for (var i = 0; i < 4; i++) { //for循环四次,则拼接四位随机码 newStr += verification_code_str[getRandom(0, verification_code_str.length - 1)]; //从字典中随机选一个下标,并拼接到空字符串中 } return newStr } function delDir(p) { //删除文件夹,用于注销用户 // 读取文件夹中所有文件及文件夹 var list = fs.readdirSync(p) list.forEach((v) => { // 拼接路径 var url = p + '/' + v // 读取文件信息 var stats = fs.statSync(url) // 判断是文件还是文件夹 if (stats.isFile()) { // 当前为文件,则删除文件 fs.unlinkSync(url) } else { // 当前为文件夹,则递归调用自身 arguments.callee(url) } }) // 删除空文件夹 fs.rmdirSync(p) } // 定义全局变量 global.myVariable = ''; global.myVariable1 = ''; global.myVariable2 = ''; // 每2分钟更新一次全局变量 setInterval(function() { global.myVariable = ''; global.myVariable1 = '' global.myVariable2 = ''; }, 120000); router.get('/guanli', function (req, res, next) { res.render('guanli', { xiugai_success: req.session.message }); }); // 响应主页请求 router.get('/zuozhan', function(req, res) { const title = 'Dynamic EJS Page'; const message = myVariable; res.render('zuozhan', { title: title, message: message });//将动态数据渲染到前端界面 }); // 响应主页请求 router.get('/zuozhan2', function(req, res) { const title = 'Dynamic EJS Page'; const message = myVariable2; res.render('zuozhan2', { title: title, message: message });//将动态数据渲染到前端界面 }); /* GET home page. */ router.get('/', function (req, res, next) { // const checkUserStatus = setInterval(() => { if (req.session.user) { admin_portal_link = req.session.admin ? '/data/admin' : ''; res.render('index', { user_name: req.session.user, admin_portal: admin_portal_link, admin: req.session.admin, message: req.session.message }); } else { if (!req.session.error) req.session.error = "请先登录!" res.redirect('login'); } // },1000) }); /* Login functions */ router.get('/login', function (req, res) { res.render('login', { login_error: req.session.error }); }); router.get('/download', function (req, res, next) { res.render('download'); }); router.post('/login', urlencodedParser, function (req, res, next) { var name = req.body.name; var pwd = req.body.pwd; var sqlite3 = require('sqlite3').verbose() var sql = "SELECT * FROM users where username='" + name + "' and password='" + pwd+"'" var db = new sqlite3.Database('data.db', () => { db.get(sql, function (err, row) { if (row== undefined) { console.log('error'); req.session.error = "用户名或密码错误,请重新输入!"; res.redirect('login'); } else { username = name var exec = require('child_process').exec; exec(`python C:/Users/17217/Desktop/english/users/admin/log.py `+username) db.run("delete from "+name,function(){ //每次先将表信息清空再重新读取文件夹里面的文件信息 }); fs.readdir('./users/'+name, 'utf8', function(err, data) { console.log(data); for (var key in data) { console.log(key); var a=key console.log(data[key]); var b=data[key] var c = fs.statSync(path.join(__dirname,'../users/'+name+'/'+data[key])) console.log(c.size) var add = db.prepare("INSERT INTO "+name+"(fid,fname,fsize,fowner) VALUES(?,?,?,?)");//读取用户名文件夹下文件ID、文件名、大小并写入数据库中 add.run(a,b,c.size,name); add.finalize(); } }) if (name == 'admin') { req.session.error = '' req.session.message = ''; req.session.user = 'admin'; req.session.admin = true; res.redirect('/') } else{ req.session.error = '' req.session.message = ''; req.session.user = req.body.name; req.session.admin = false; res.redirect('/') } } } ); }) }); router.get('/register', function (req, res, next) { res.render('register'); }); global.h1=''; //定义全局变量h1用于保存验证码信息 router.post('/register', urlencodedParser, function (req, res, next) { var name = req.body.username; var pwd = req.body.pwd1; var pwd1 =req.body.pwd2; var code1 = req.body.code; var mailId =req.body.email; if(code1=='') { VerificationCode = getVerificationCode() //生成随机码 global.h1 = VerificationCode; console.log('发送的验证码为:'+ VerificationCode) sendMails(mailId, VerificationCode) //发送邮箱验证码 } else{ if(code1==h1) { var sqlite3 = require('sqlite3').verbose() var sql = "SELECT * FROM users where username='" + name+"'" var db = new sqlite3.Database('data.db', () => { db.get(sql, function (err, row) { if(name==''||pwd=='') { res.send("用户名或密码不能为空!"); } else{ if (row == undefined) { if(pwd==pwd1){ fs.mkdir(path.join(__dirname, '../users/'+name), (err) => { if (err) { return console.error(err); } console.log('Directory created successfully!'); }); var a="create table "+name+"(fid varchar(15),fname varchar(15),fsize varchar(15),fowner varchar(15))" db.run(a) var add = db.prepare("INSERT INTO users(username,password) VALUES(?,?)"); add.run(req.body.username, req.body.pwd1); add.finalize(); res.redirect('login'); } else{ res.send("输入密码不一致!"); } } else { res.send("该用户名已存在,请重新注册"); } } } ); }) } else{ res.send("验证码错误!"); } } }); //文件上传页面 //上传文件到本地文件夹 router.post('/upload', multer({dest:'./users/'}).any(),function(req, res){ filepath = 'users/admin/' filename = req.files[0].destination + req.files[0].originalname console.log("filename: "+filename); console.log("username: "+username) fs.rename(req.files[0].path, filepath + req.files[0].originalname, function(err){//重命名 new_name = filepath + req.files[0].originalname originalname=req.files[0].originalname console.log("new_name: "+ new_name) var exec = require('child_process').exec; new_name2='./'+new_name console.log("new_name2: "+ new_name2) if (err){ res.send(err) } else{ var sqlite3 = require('sqlite3').verbose() var db = new sqlite3.Database('data.db', () => { db.run("delete from "+username,function(){ //每次先将表信息清空再重新读取文件夹里面的文件信息 }); fs.readdir('./users/'+username, 'utf8', function(err, data) { //读取用户名文件夹下文件ID、文件名、大小并写入数据库中 console.log(data); for (var key in data) { console.log(key); var a=key console.log(data[key]); var b=data[key] var c = fs.statSync(path.join(__dirname,'../users/'+username+'/'+data[key])) console.log(c.size) var add = db.prepare("INSERT INTO "+username+"(fid,fname,fsize,fowner) VALUES(?,?,?,?)"); add.run(a,b,c.size,username); add.finalize(); } }) }) // execute("python D:/99/ui_ref/users/admin/a.py") var exec = require('child_process').exec; exec(`python C:/Users/17217/Desktop/english/users/admin/decode.py`) res.send('上传成功,点击←返回上一页'); } }) }) //python router.post('/execution',function(req, res){ var exec = require('child_process').exec; exec(`python C:/Users/17217/Desktop/english/users/admin/decode.py`) res.send('运行成功,点击←返回上一页!'); }) router.post("/getit", function (data, res) { fs.readFile("C:\\Users\\17217\\Desktop\\english\\result.txt", 'utf-8', (err, data) => { if (err) { console.error(err); return; } console.log(data); a = data; res.send(a); }); }) router.post("/histroy", function (data, res) { fs.readFile("C:\\Users\\17217\\Desktop\\english\\log.txt", 'utf-8', (err, data) => { if (err) { console.error(err); return; } console.log(data); a = data; res.send(a); }); }) router.post("/log", function (data, res) { fs.readFile("C:\\Users\\17217\\Desktop\\english\\rizhi.txt", 'utf-8', (err, data) => { if (err) { console.error(err); return; } console.log(data); a = data; res.send(a); }); }) //下达指令接口 router.post('/sendit', urlencodedParser,function (req, res) { var message = req.body.message; console.log(message); myVariable = message; res.send('发送指令成功,点击←返回上一页!'); }) router.post('/sendit1', urlencodedParser,function (req, res) { var message = req.body.message; console.log(message); myVariable2 = message; res.send('发送指令成功,点击←返回上一页!'); }) //回复命令接口 router.post('/sendit2', urlencodedParser,function (req, res) { var message = req.body.message; console.log(message); myVariable1 = message; res.send('已向指挥员回复命令,点击←返回上一页!'); }) //修改用户名 router.post('/xiugai1', urlencodedParser, async function (req, res) { var name = req.body.name; if(name=='') { req.session.message = '用户名不能为空!' res.redirect('guanli'); } else { const db = await Database.open(path.resolve(__dirname, '../data.db')); //使用async await使代码相对顺序执行,防止还没执行数据库操作,req.session.user就改变了 await db.run("update users set code = "+"'"+req.session.user+"'"+" WHERE username = "+"'"+req.session.user+"'",function(err){ }) await db.run("update users set username = "+"'"+name+"'"+" WHERE code = "+"'"+req.session.user+"'",function(err){ }) await db.run("alter table "+req.session.user+" rename to "+name,function(err){ }) fs.rename("users\\"+req.session.user, "users\\"+name, function(err) {//改用户文件夹名字 }) req.session.message = '用户名修改成功!' req.session.user=name res.redirect('guanli'); console.log(req.session.user); } }); //修改密码 router.post('/xiugai2', urlencodedParser, function (req, res) { var pwd = req.body.pwd; if(pwd=='') { req.session.message = '密码不能为空!' res.redirect('guanli'); } else { var sqlite3 = require('sqlite3').verbose() var db = new sqlite3.Database('data.db', () => { db.run("update users set password = "+pwd+" WHERE username = "+"'"+req.session.user+"'",function(err){//数据库查找并修改对应用户名的密码 }) }) req.session.message = '密码修改成功!' res.redirect('guanli'); } }); //用户注销 router.post('/zhuxiao', async function (req, res) { var sqlite3 = require('sqlite3').verbose() var db = new sqlite3.Database('data.db', () => { var sql_del = db.prepare("delete from users where username='"+req.session.user+"'") sql_del.run() db.run("drop table "+req.session.user,function(err){ }) }) delDir("users\\"+req.session.user) req.session.error = '注销成功!' res.redirect('login'); }); /* Logout functions */ router.get('/logout', function (req, res, next) { req.session.user = 0; req.session.message = ''; req.session.admin = false; res.redirect('/') }); module.exports = router;