您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

137 行
3.7 KiB

const express = require("express");
const cp = require('child_process');
const router= express.Router();
const detect = require('detect-port');
const oneport = require('oneport');
const fs = require('fs');
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', [
'PASSWORD=' + req.session.user.password,
'code-server',
'--auth=password',
'--bind-addr=0.0.0.0:' + port,
'--user-data-dir=/root/codeserver/userdata/' + req.session.user.name + '/',
'--extensions-dir=/root/codeserver/extensions/' + req.session.user.name + '/',
'--config=/root/codeserver/config/' + req.session.user.name,
'-e',
'--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
});
dirpath = '/root/codeserver/user/' + req.session.user.name + '/';
fs.mkdirSync(dirpath, { recursive: true });
res.redirect('http://106.75.254.18:' + port + '/?folder=' + dirpath);
});
}
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;