index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const http = require('http');
  2. const pages = require('./oauth.js');
  3. const dashboard = require('./guilds.js');
  4. const {db, settingsData} = require('./util.js');
  5. const fs = require('fs');
  6. const path = require('path');
  7. const files = new Map(fs.readdirSync( './dashboard/src' ).map( file => {
  8. let contentType = 'text/html';
  9. switch ( path.extname(file) ) {
  10. case '.css':
  11. contentType = 'text/css';
  12. break;
  13. case '.js':
  14. contentType = 'text/javascript';
  15. break;
  16. case '.json':
  17. contentType = 'application/json';
  18. break;
  19. case '.svg':
  20. contentType = 'image/svg+xml';
  21. break;
  22. case '.png':
  23. contentType = 'image/png';
  24. break;
  25. case '.jpg':
  26. contentType = 'image/jpg';
  27. break;
  28. }
  29. return [`/src/${file}`, {
  30. name: file, contentType,
  31. path: `./dashboard/src/${file}`
  32. }];
  33. } ));
  34. const server = http.createServer((req, res) => {
  35. if ( req.method !== 'GET' ) {
  36. let body = '<img width="400" src="https://http.cat/418"><br><strong>' + http.STATUS_CODES[418] + '</strong>';
  37. res.writeHead(418, {'Content-Length': body.length});
  38. res.write( body );
  39. return res.end();
  40. }
  41. var reqURL = new URL(req.url, process.env.dashboard);
  42. if ( reqURL.pathname === '/favicon.ico' ) {
  43. res.writeHead(302, {Location: 'https://cdn.discordapp.com/avatars/461189216198590464/f69cdc197791aed829882b64f9760dbb.png?size=64'});
  44. return res.end();
  45. }
  46. if ( files.has(reqURL.pathname) ) {
  47. let file = files.get(reqURL.pathname);
  48. res.writeHead(200, {'Content-Type': file.contentType});
  49. return fs.createReadStream(file.path).pipe(res);
  50. }
  51. res.setHeader('Content-Type', 'text/html');
  52. res.setHeader('Content-Language', ['en']);
  53. var lastGuild = req.headers?.cookie?.split('; ')?.filter( cookie => {
  54. return cookie.split('=')[0] === 'guild';
  55. } )?.map( cookie => cookie.replace( /^guild="(\w+)"$/, '$1' ) )?.join();
  56. if ( lastGuild ) res.setHeader('Set-Cookie', [`guild="${lastGuild}"; Max-Age=0; HttpOnly; Path=/`]);
  57. var state = req.headers.cookie?.split('; ')?.filter( cookie => {
  58. return cookie.split('=')[0] === 'wikibot';
  59. } )?.map( cookie => cookie.replace( /^wikibot="(\w+(?:-\d+)?)"$/, '$1' ) )?.join();
  60. if ( reqURL.pathname === '/login' ) {
  61. return pages.login(res, state, reqURL.searchParams.get('action'));
  62. }
  63. if ( reqURL.pathname === '/logout' ) {
  64. settingsData.delete(state);
  65. res.writeHead(302, {
  66. Location: '/login?action=logout',
  67. 'Set-Cookie': [`wikibot="${state}"; Max-Age=0; HttpOnly`]
  68. });
  69. return res.end();
  70. }
  71. if ( !state ) {
  72. res.writeHead(302, {
  73. Location: ( reqURL.pathname === '/' ? '/login' : '/login?action=unauthorized' )
  74. });
  75. return res.end();
  76. }
  77. if ( reqURL.pathname === '/oauth' ) {
  78. return pages.oauth(res, state, reqURL.searchParams, lastGuild);
  79. }
  80. if ( !settingsData.has(state) ) {
  81. res.writeHead(302, {
  82. Location: ( reqURL.pathname === '/' ? '/login' : '/login?action=unauthorized' )
  83. });
  84. return res.end();
  85. }
  86. if ( reqURL.pathname === '/refresh' ) {
  87. return pages.refresh(res, state, reqURL.searchParams.get('return'));
  88. }
  89. if ( reqURL.pathname === '/' || reqURL.pathname.startsWith( '/guild/' ) ) {
  90. return dashboard(res, state, reqURL);
  91. }
  92. res.writeHead(302, {Location: '/'});
  93. return res.end();
  94. });
  95. server.listen(8080, 'localhost', () => {
  96. console.log( '- Dashboard: Server running at http://localhost:8080/' );
  97. });
  98. /**
  99. * End the process gracefully.
  100. * @param {NodeJS.Signals} signal - The signal received.
  101. */
  102. function graceful(signal) {
  103. console.log( '- Dashboard: ' + signal + ': Closing the dashboard...' );
  104. server.close( () => {
  105. console.log( '- Dashboard: ' + signal + ': Closed the dashboard server.' );
  106. } );
  107. db.close( dberror => {
  108. if ( dberror ) {
  109. console.log( '- Dashboard: ' + signal + ': Error while closing the database connection: ' + dberror );
  110. return dberror;
  111. }
  112. console.log( '- Dashboard: ' + signal + ': Closed the database connection.' );
  113. process.exit(0);
  114. } );
  115. }
  116. process.once( 'SIGINT', graceful );
  117. process.once( 'SIGTERM', graceful );