index.js 5.0 KB

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