123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- global.isDebug = ( process.argv[2] === 'debug' );
- const http = require('http');
- const pages = require('./oauth.js');
- const dashboard = require('./guilds.js');
- const {db, sessionData, settingsData} = require('./util.js');
- const Lang = require('./i18n.js');
- const allLangs = Lang.allLangs();
- const posts = {
- user: require('./user.js').post,
- settings: require('./settings.js').post,
- verification: require('./verification.js').post,
- rcscript: require('./rcscript.js').post,
- slash: require('./slash.js').post
- };
- const fs = require('fs');
- const path = require('path');
- const files = new Map([
- ...fs.readdirSync( './dashboard/src' ).map( file => {
- return [`/src/${file}`, `./dashboard/src/${file}`];
- } ),
- ...fs.readdirSync( './i18n/widgets' ).map( file => {
- return [`/src/widgets/${file}`, `./i18n/widgets/${file}`];
- } ),
- ...( fs.existsSync('./RcGcDb/start.py') ? fs.readdirSync( './RcGcDb/locale/widgets' ).map( file => {
- return [`/src/widgets/RcGcDb/${file}`, `./RcGcDb/locale/widgets/${file}`];
- } ) : [] )
- ].map( ([file, filepath]) => {
- let contentType = 'text/html';
- switch ( path.extname(file) ) {
- case '.css':
- contentType = 'text/css';
- break;
- case '.js':
- contentType = 'text/javascript';
- break;
- case '.json':
- contentType = 'application/json';
- break;
- case '.svg':
- contentType = 'image/svg+xml';
- break;
- case '.png':
- contentType = 'image/png';
- break;
- case '.jpg':
- contentType = 'image/jpg';
- break;
- }
- return [file, {path: filepath, contentType}];
- } ));
- const server = http.createServer( (req, res) => {
- res.setHeader('Referrer-Policy', 'strict-origin-when-cross-origin');
- if ( req.method === 'POST' && req.headers['content-type'] === 'application/x-www-form-urlencoded' && ( req.url.startsWith( '/guild/' ) || req.url === '/user' ) ) {
- let args = req.url.split('/');
- let state = req.headers.cookie?.split('; ')?.filter( cookie => {
- return cookie.split('=')[0] === 'wikibot' && /^"([\da-f]+(?:-\d+)*)"$/.test(( cookie.split('=')[1] || '' ));
- } )?.map( cookie => cookie.replace( /^wikibot="([\da-f]+(?:-\d+)*)"$/, '$1' ) )?.join();
- if ( state && sessionData.has(state) && settingsData.has(sessionData.get(state).user_id) &&
- ( ( args.length === 5 && ['settings', 'verification', 'rcscript', 'slash'].includes( args[3] ) && /^(?:default|new|notice|\d+)$/.test(args[4])
- && settingsData.get(sessionData.get(state).user_id).guilds.isMember.has(args[2]) ) || req.url === '/user' ) ) {
- if ( process.env.READONLY ) return save_response(`${req.url}?save=failed`);
- let body = [];
- req.on( 'data', chunk => {
- body.push(chunk);
- } );
- req.on( 'error', () => {
- console.log( '- Dashboard: ' + error );
- res.end('error');
- } );
- return req.on( 'end', () => {
- var settings = {};
- Buffer.concat(body).toString().split('&').forEach( arg => {
- if ( arg ) {
- let setting = decodeURIComponent(arg.replace( /\+/g, ' ' )).split('=');
- if ( setting[0] && setting.slice(1).join('=').trim() ) {
- if ( settings[setting[0]] ) {
- settings[setting[0]] += '|' + setting.slice(1).join('=').trim();
- }
- else settings[setting[0]] = setting.slice(1).join('=').trim();
- }
- }
- } );
- if ( isDebug ) console.log( '- Dashboard:', req.url, settings, sessionData.get(state).user_id );
- if ( req.url === '/user' ) {
- let setting = Object.keys(settings);
- if ( setting.length === 1 && setting[0].startsWith( 'oauth_' ) && setting[0].split('_').length >= 3 ) {
- setting = setting[0].split('_');
- return posts.user(save_response, sessionData.get(state).user_id, setting[1], setting.slice(2).join('_'));
- }
- }
- else return posts[args[3]](save_response, settingsData.get(sessionData.get(state).user_id), args[2], args[4], settings);
- } );
- /**
- * @param {String} [resURL]
- * @param {String} [action]
- * @param {String[]} [actionArgs]
- */
- function save_response(resURL = '/', action, ...actionArgs) {
- if ( action === 'REDIRECT' && resURL.startsWith( 'https://' ) ) {
- res.writeHead(303, {Location: resURL});
- return res.end();
- }
- var themeCookie = ( req.headers?.cookie?.split('; ')?.find( cookie => {
- return cookie.split('=')[0] === 'theme' && /^"(?:light|dark)"$/.test(( cookie.split('=')[1] || '' ));
- } ) || 'dark' ).replace( /^theme="(light|dark)"$/, '$1' );
- var langCookie = ( req.headers?.cookie?.split('; ')?.filter( cookie => {
- return cookie.split('=')[0] === 'language' && /^"[a-z\-]+"$/.test(( cookie.split('=')[1] || '' ));
- } )?.map( cookie => cookie.replace( /^language="([a-z\-]+)"$/, '$1' ) ) || [] );
- var dashboardLang = new Lang(...langCookie, ...( req.headers?.['accept-language']?.split(',')?.map( lang => {
- lang = lang.split(';')[0].toLowerCase();
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- lang = lang.replace( /-\w+$/, '' );
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- lang = lang.replace( /-\w+$/, '' );
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- return '';
- } ) || [] ));
- dashboardLang.fromCookie = langCookie;
- return dashboard(res, dashboardLang, themeCookie, sessionData.get(state), new URL(resURL, process.env.dashboard), action, actionArgs);
- }
- }
- }
- var reqURL = new URL(req.url, process.env.dashboard);
- if ( req.method === 'HEAD' && files.has(reqURL.pathname) ) {
- let file = files.get(reqURL.pathname);
- res.writeHead(200, {'Content-Type': file.contentType});
- return res.end();
- }
- if ( req.method !== 'GET' ) {
- let body = '<img width="400" src="https://http.cat/418"><br><strong>' + http.STATUS_CODES[418] + '</strong>';
- res.writeHead(418, {
- 'Content-Type': 'text/html',
- 'Content-Length': Buffer.byteLength(body)
- });
- res.write( body );
- return res.end();
- }
- if ( reqURL.pathname === '/favicon.ico' ) reqURL.pathname = '/src/icon.png';
- if ( files.has(reqURL.pathname) ) {
- let file = files.get(reqURL.pathname);
- res.writeHead(200, {'Content-Type': file.contentType});
- return fs.createReadStream(file.path).pipe(res);
- }
- res.setHeader('Content-Type', 'text/html');
- var themeCookie = ( req.headers?.cookie?.split('; ')?.find( cookie => {
- return cookie.split('=')[0] === 'theme' && /^"(?:light|dark)"$/.test(( cookie.split('=')[1] || '' ));
- } ) || 'dark' ).replace( /^theme="(light|dark)"$/, '$1' );
- var langCookie = ( req.headers?.cookie?.split('; ')?.filter( cookie => {
- return cookie.split('=')[0] === 'language' && /^"[a-z\-]+"$/.test(( cookie.split('=')[1] || '' ));
- } )?.map( cookie => cookie.replace( /^language="([a-z\-]+)"$/, '$1' ) ) || [] );
- var dashboardLang = new Lang(...langCookie, ...( req.headers?.['accept-language']?.split(',')?.map( lang => {
- lang = lang.split(';')[0].toLowerCase();
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- lang = lang.replace( /-\w+$/, '' );
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- lang = lang.replace( /-\w+$/, '' );
- if ( allLangs.map.hasOwnProperty(lang) ) return lang;
- return '';
- } ) || [] ));
- dashboardLang.fromCookie = langCookie;
- res.setHeader('Content-Language', [dashboardLang.lang]);
- var lastGuild = req.headers?.cookie?.split('; ')?.filter( cookie => {
- return cookie.split('=')[0] === 'guild' && /^"(?:user|\d+\/(?:settings|verification|rcscript|slash)(?:\/(?:\d+|new|notice))?)"$/.test(( cookie.split('=')[1] || '' ));
- } )?.map( cookie => cookie.replace( /^guild="(user|\d+\/(?:settings|verification|rcscript|slash)(?:\/(?:\d+|new|notice))?)"$/, '$1' ) )?.join();
- if ( lastGuild ) res.setHeader('Set-Cookie', ['guild=""; SameSite=Lax; Path=/; Max-Age=0']);
- var state = req.headers.cookie?.split('; ')?.filter( cookie => {
- return cookie.split('=')[0] === 'wikibot' && /^"([\da-f]+(?:-\d+)*)"$/.test(( cookie.split('=')[1] || '' ));
- } )?.map( cookie => cookie.replace( /^wikibot="([\da-f]+(?:-\d+)*)"$/, '$1' ) )?.join();
- if ( reqURL.pathname === '/login' ) {
- let action = '';
- if ( reqURL.searchParams.get('action') === 'failed' ) action = 'loginfail';
- return pages.login(res, dashboardLang, themeCookie, state, action);
- }
- if ( reqURL.pathname === '/logout' ) {
- sessionData.delete(state);
- res.setHeader('Set-Cookie', [
- ...( res.getHeader('Set-Cookie') || [] ),
- 'wikibot=""; HttpOnly; SameSite=Lax; Path=/; Max-Age=0'
- ]);
- return pages.login(res, dashboardLang, themeCookie, state, 'logout');
- }
- if ( reqURL.pathname === '/oauth/mw' ) {
- return pages.verify(res, reqURL.searchParams, sessionData.get(state)?.user_id);
- }
- if ( !state ) {
- let action = '';
- if ( reqURL.pathname !== '/' ) action = 'unauthorized';
- if ( reqURL.pathname.startsWith( '/guild/' ) ) {
- let pathGuild = reqURL.pathname.split('/').slice(2, 5).join('/');
- if ( /^\d+\/(?:settings|verification|rcscript|slash)(?:\/(?:\d+|new|notice))?$/.test(pathGuild) ) {
- res.setHeader('Set-Cookie', [`guild="${pathGuild}"; SameSite=Lax; Path=/`]);
- }
- }
- else if ( reqURL.pathname === '/user' ) {
- if ( reqURL.searchParams.get('oauth') === 'success' ) action = 'oauth';
- if ( reqURL.searchParams.get('oauth') === 'failed' ) action = 'oauthfail';
- if ( reqURL.searchParams.get('oauth') === 'verified' ) action = 'oauthverify';
- if ( reqURL.searchParams.get('oauth') === 'other' ) action = 'oauth';
- res.setHeader('Set-Cookie', ['guild="user"; SameSite=Lax; Path=/']);
- }
- return pages.login(res, dashboardLang, themeCookie, state, action);
- }
- if ( reqURL.pathname === '/oauth' ) {
- return pages.oauth(res, state, reqURL.searchParams, lastGuild);
- }
- if ( !sessionData.has(state) || !settingsData.has(sessionData.get(state).user_id) ) {
- let action = '';
- if ( reqURL.pathname !== '/' ) action = 'unauthorized';
- if ( reqURL.pathname.startsWith( '/guild/' ) ) {
- let pathGuild = reqURL.pathname.split('/').slice(2, 5).join('/');
- if ( /^\d+\/(?:settings|verification|rcscript|slash)(?:\/(?:\d+|new|notice))?$/.test(pathGuild) ) {
- res.setHeader('Set-Cookie', [`guild="${pathGuild}"; SameSite=Lax; Path=/`]);
- }
- }
- else if ( reqURL.pathname === '/user' ) {
- if ( reqURL.searchParams.get('oauth') === 'success' ) action = 'oauth';
- if ( reqURL.searchParams.get('oauth') === 'failed' ) action = 'oauthfail';
- if ( reqURL.searchParams.get('oauth') === 'verified' ) action = 'oauthverify';
- if ( reqURL.searchParams.get('oauth') === 'other' ) action = 'oauth';
- res.setHeader('Set-Cookie', ['guild="user"; SameSite=Lax; Path=/']);
- }
- return pages.login(res, dashboardLang, themeCookie, state, action);
- }
- if ( reqURL.pathname === '/refresh' ) {
- let returnLocation = reqURL.searchParams.get('return');
- if ( !/^\/(?:user|guild\/\d+\/(?:settings|verification|rcscript|slash)(?:\/(?:\d+|new|notice))?)$/.test(returnLocation) ) {
- returnLocation = '/';
- }
- return pages.refresh(res, sessionData.get(state), returnLocation);
- }
- if ( reqURL.pathname === '/api' ) {
- let wiki = reqURL.searchParams.get('wiki');
- if ( wiki ) return pages.api(res, wiki);
- }
- let action = '';
- if ( reqURL.searchParams.get('refresh') === 'success' ) action = 'refresh';
- if ( reqURL.searchParams.get('refresh') === 'failed' ) action = 'refreshfail';
- if ( reqURL.searchParams.get('slash') === 'noverify' && reqURL.pathname.split('/')[3] === 'slash' ) action = 'noverify';
- if ( reqURL.pathname === '/user' ) {
- if ( reqURL.searchParams.get('oauth') === 'success' ) action = 'oauth';
- if ( reqURL.searchParams.get('oauth') === 'failed' ) action = 'oauthfail';
- if ( reqURL.searchParams.get('oauth') === 'verified' ) action = 'oauthverify';
- if ( reqURL.searchParams.get('oauth') === 'other' ) action = 'oauthother';
- }
- return dashboard(res, dashboardLang, themeCookie, sessionData.get(state), reqURL, action);
- } );
- server.listen( 8080, 'localhost', () => {
- console.log( '- Dashboard: Server running at http://localhost:8080/' );
- } );
- String.prototype.replaceSave = function(pattern, replacement) {
- return this.replace( pattern, ( typeof replacement === 'string' ? replacement.replace( /\$/g, '$$$$' ) : replacement ) );
- };
- /**
- * End the process gracefully.
- * @param {NodeJS.Signals} signal - The signal received.
- */
- function graceful(signal) {
- console.log( '- Dashboard: ' + signal + ': Closing the dashboard...' );
- server.close( () => {
- console.log( '- Dashboard: ' + signal + ': Closed the dashboard server.' );
- db.end().then( () => {
- console.log( '- Dashboard: ' + signal + ': Closed the database connection.' );
- process.exit(0);
- }, dberror => {
- console.log( '- Dashboard: ' + signal + ': Error while closing the database connection: ' + dberror );
- } );
- } );
- }
- process.once( 'SIGINT', graceful );
- process.once( 'SIGTERM', graceful );
|