You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. const express = require("express");
  2. const cp = require('child_process');
  3. const router= express.Router();
  4. const detect = require('detect-port');
  5. const oneport = require('oneport');
  6. const fs = require('fs');
  7. let User = require("../DBcollection/user.js");
  8. const detectPort = require("detect-port");
  9. let servers = new Set();
  10. router.get('/', function(req, res) {
  11. if (req.session.user) {
  12. // res.render('home', {
  13. // user: req.session.user,
  14. // title: 'HOME',
  15. // });
  16. oneport.acquire(function (err, port) {
  17. if (err) return console.error(err);
  18. console.log('port:', port);
  19. codeserver = cp.execFile('sudo', [
  20. 'PASSWORD=' + req.session.user.password,
  21. 'code-server',
  22. '--auth=password',
  23. '--bind-addr=0.0.0.0:' + port,
  24. '--user-data-dir=/root/codeserver/userdata/' + req.session.user.name + '/',
  25. '--extensions-dir=/root/codeserver/extensions/' + req.session.user.name + '/',
  26. '--config=/root/codeserver/config/' + req.session.user.name,
  27. '-e',
  28. '--home=http://106.75.254.18/logoff'
  29. ], function(error, stdout, stderr) {
  30. console.log(error + '\n' + stdout + '\n' + stderr);
  31. });
  32. servers.add({
  33. user: req.session.user,
  34. server: codeserver
  35. });
  36. dirpath = '/root/codeserver/user/' + req.session.user.name + '/';
  37. fs.mkdirSync(dirpath, { recursive: true });
  38. res.redirect('http://106.75.254.18:' + port + '/?folder=' + dirpath);
  39. });
  40. }
  41. else {
  42. res.render('home-signin', {
  43. user: req.session.user,
  44. title: "user Sign in",
  45. });
  46. }
  47. });
  48. router.get('/logoff', function(req, res) {
  49. servers.forEach(function(server) {
  50. if (server.user == req.session.user) {
  51. server.server.exit(0);
  52. servers.delete(server);
  53. }
  54. })
  55. delete req.session.user;
  56. res.redirect('/');
  57. })
  58. router.post('/', function(req, res) {
  59. let query = {
  60. name: req.body.name,
  61. password: req.body.password
  62. };
  63. User.findOne(query, function(err, user) {
  64. if (err) {
  65. res.end("some thing wrong")
  66. return console.log(err);
  67. }
  68. if (user != undefined) {
  69. req.session.user = user;
  70. // return res.render('home', {
  71. // title: "HOME",
  72. // user: req.session.user
  73. // });
  74. return res.redirect('/');
  75. }
  76. else {
  77. return res.render('home-signin', {
  78. title: "Sorry, no this user",
  79. user: req.session.user
  80. })
  81. }
  82. });
  83. });
  84. router.get('/signup', function(req, res) {
  85. res.render('home-signup', {
  86. user: req.session.user,
  87. title: "user Sign up",
  88. });
  89. });
  90. router.post('/signup', function(req, res) {
  91. User.find({name: req.body.name}, function(err, find) {
  92. if (find.length) {
  93. console.log(find);
  94. res.render("home-signup", {
  95. user: req.session.user,
  96. title: "Same User Name - plz try again",
  97. });
  98. }
  99. else {
  100. let user = new User();
  101. console.log(user);
  102. user.name = req.body.name;
  103. user.password = req.body.password;
  104. user.save(function(err) {
  105. if (err) {
  106. res.end("some thing wrong");
  107. return console.log(err);
  108. }
  109. req.session.user = user;
  110. res.redirect('/');
  111. });
  112. }
  113. });
  114. });
  115. module.exports = router;