|
|
@ -0,0 +1,125 @@ |
|
|
|
const express = require("express"); |
|
|
|
const cp = require('child_process'); |
|
|
|
const router= express.Router(); |
|
|
|
const detect = require('detect-port'); |
|
|
|
const oneport = require('oneport'); |
|
|
|
|
|
|
|
|
|
|
|
let User = require("../DBcollection/user.js"); |
|
|
|
const detectPort = require("detect-port"); |
|
|
|
|
|
|
|
let servers = new Set(); |
|
|
|
|
|
|
|
router.get('/', function(req, res) { |
|
|
|
|
|
|
|
if (req.session.user) { |
|
|
|
// res.render('home', {
|
|
|
|
// user: req.session.user,
|
|
|
|
// title: 'HOME',
|
|
|
|
// });
|
|
|
|
|
|
|
|
oneport.acquire(function (err, port) { |
|
|
|
if (err) return console.error(err); |
|
|
|
console.log('port:', port); |
|
|
|
|
|
|
|
codeserver = cp.execFile('sudo', [ |
|
|
|
'code-server', |
|
|
|
'--user-data-dir=/root/codedata/' + req.session.user.name, |
|
|
|
'--auth=none', |
|
|
|
'--bind-addr=0.0.0.0:' + port, |
|
|
|
'--extensions-dir=/root/extensionsdata/' + req.session.user.name, |
|
|
|
'--home=http://106.75.254.18/logoff' |
|
|
|
], function(error, stdout, stderr) { |
|
|
|
console.log(error + '\n' + stdout + '\n' + stderr); |
|
|
|
}); |
|
|
|
servers.add({ |
|
|
|
user: req.session.user, |
|
|
|
server: codeserver |
|
|
|
}); |
|
|
|
res.redirect('http://106.75.254.18:' + port); |
|
|
|
}); |
|
|
|
} |
|
|
|
else { |
|
|
|
res.render('home-signin', { |
|
|
|
user: req.session.user, |
|
|
|
title: "user Sign in", |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
router.get('/logoff', function(req, res) { |
|
|
|
servers.forEach(function(server) { |
|
|
|
if (server.user == req.session.user) { |
|
|
|
server.server.exit(0); |
|
|
|
servers.delete(server); |
|
|
|
} |
|
|
|
}) |
|
|
|
delete req.session.user; |
|
|
|
res.redirect('/'); |
|
|
|
}) |
|
|
|
|
|
|
|
router.post('/', function(req, res) { |
|
|
|
let query = { |
|
|
|
name: req.body.name, |
|
|
|
password: req.body.password |
|
|
|
}; |
|
|
|
User.findOne(query, function(err, user) { |
|
|
|
if (err) { |
|
|
|
res.end("some thing wrong") |
|
|
|
return console.log(err); |
|
|
|
} |
|
|
|
if (user != undefined) { |
|
|
|
req.session.user = user; |
|
|
|
// return res.render('home', {
|
|
|
|
// title: "HOME",
|
|
|
|
// user: req.session.user
|
|
|
|
// });
|
|
|
|
return res.redirect('/'); |
|
|
|
} |
|
|
|
else { |
|
|
|
return res.render('home-signin', { |
|
|
|
title: "Sorry, no this user", |
|
|
|
user: req.session.user |
|
|
|
}) |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
router.get('/signup', function(req, res) { |
|
|
|
res.render('home-signup', { |
|
|
|
user: req.session.user, |
|
|
|
title: "user Sign up", |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
router.post('/signup', function(req, res) { |
|
|
|
User.find({name: req.body.name}, function(err, find) { |
|
|
|
if (find.length) { |
|
|
|
console.log(find); |
|
|
|
res.render("home-signup", { |
|
|
|
user: req.session.user, |
|
|
|
title: "Same User Name - plz try again", |
|
|
|
}); |
|
|
|
} |
|
|
|
else { |
|
|
|
let user = new User(); |
|
|
|
|
|
|
|
console.log(user); |
|
|
|
user.name = req.body.name; |
|
|
|
user.password = req.body.password; |
|
|
|
|
|
|
|
user.save(function(err) { |
|
|
|
if (err) { |
|
|
|
res.end("some thing wrong"); |
|
|
|
return console.log(err); |
|
|
|
} |
|
|
|
req.session.user = user; |
|
|
|
res.redirect('/'); |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
module.exports = router; |