import type { WeSParams } from './ajax'; /** * Demo answers for test mode — the WordPress Playground demo * (`Captcha::is_test_mode()`), where the site cannot be connected to the * Web-Engine cloud. * * Every route that would reach the cloud is answered here instead, from made-up * data held in memory for the life of the page, so the settings screen can be * explored without an account. The addresses are from the documentation * ranges (RFC 5737). Routes that only read or write the site's own options are * left to the server's `local_call()`. */ /** A demo visitor: one row of the firewall's activity summary */ type DemoVisitor = { ip: string; country: string; language: string; signedIn: number; failed: number; /** Minutes before page load it was last seen */ minutesAgo: number; }; type DemoBan = { source: 'manual' | 'automatic'; reason: string; minutesAgo: number; }; /** The address the demo admin is "signed in from" — it gets no Block action */ const DEMO_CURRENT_IP = '192.0.2.10'; const visitors: DemoVisitor[] = [ { ip: DEMO_CURRENT_IP, country: 'GB', language: 'en', signedIn: 14, failed: 1, minutesAgo: 3, }, { ip: '192.0.2.44', country: 'GB', language: 'en', signedIn: 6, failed: 0, minutesAgo: 125, }, { ip: '198.51.100.23', country: 'US', language: 'en', signedIn: 3, failed: 2, minutesAgo: 1440, }, { ip: '203.0.113.7', country: 'DE', language: 'de', signedIn: 1, failed: 0, minutesAgo: 310, }, { ip: '192.0.2.201', country: 'FR', language: 'fr', signedIn: 2, failed: 1, minutesAgo: 380, }, { ip: '198.51.100.150', country: 'RU', language: 'ru', signedIn: 0, failed: 27, minutesAgo: 40, }, { ip: '203.0.113.91', country: 'CN', language: 'zh', signedIn: 0, failed: 12, minutesAgo: 190, }, { ip: '203.0.113.200', country: 'BR', language: 'pt', signedIn: 0, failed: 4, minutesAgo: 540, }, { ip: '198.51.100.77', country: 'NL', language: 'nl', signedIn: 0, failed: 2, minutesAgo: 2900, }, ]; /** What the demo's settings have been changed to on this page */ const demo = { mode: 'monitor', level: 1, countries: [ 'GB' ], languages: [ 'en' ], removeData: false, modes: [ 'puzzle' ], allowed: new Set< string >( [ DEMO_CURRENT_IP ] ), bans: new Map< string, DemoBan >( [ [ '198.51.100.150', { source: 'automatic', reason: 'too_many_failures', minutesAgo: 35, }, ], [ '203.0.113.91', { source: 'automatic', reason: 'too_many_failures', minutesAgo: 185, }, ], [ '198.51.100.77', { source: 'manual', reason: 'manual_block', minutesAgo: 2880 }, ], ] ), }; const loadedAt = Date.now(); /** * A time some minutes before page load, as the server writes it * @param minutes */ function ago( minutes: number ): string { const date = new Date( loadedAt - minutes * 60000 ); const pad = ( value: number ) => String( value ).padStart( 2, '0' ); return ( `${ date.getFullYear() }-${ pad( date.getMonth() + 1 ) }-${ pad( date.getDate() ) } ` + `${ pad( date.getHours() ) }:${ pad( date.getMinutes() ) }:${ pad( date.getSeconds() ) }` ); } /** * A body after a short wait, so the demo's spinners show as they would * @param body */ function answer( body: unknown ): Promise< unknown > { return new Promise( ( resolve ) => window.setTimeout( () => resolve( body ), 250 ) ); } function unique( values: string[] ): string[] { return values.filter( ( value, index ) => values.indexOf( value ) === index ); } const flag = ( value: boolean ) => ( value ? '1' : '0' ); /** * One page of rows, sorted and sliced as `Login::get_activity()` does * @param rows * @param params */ function page< Row extends { [ key: string ]: string } >( rows: Row[], params: WeSParams ): Row[] { const sort = String( params.sort ?? '' ); const desc = 'ASC' !== String( params.dir ?? '' ).toUpperCase(); const start = Number( params.start ?? 0 ) || 0; const size = Number( params.size ?? 20 ) || 20; const sorted = [ ...rows ]; if ( sorted.length > 0 && sort in sorted[ 0 ] ) { sorted.sort( ( a, b ) => { const numeric = ! isNaN( Number( a[ sort ] ) ); const order = numeric ? Number( a[ sort ] ) - Number( b[ sort ] ) : a[ sort ].localeCompare( b[ sort ] ); return desc ? -order : order; } ); } return sorted.slice( start, start + size ); } function firewall() { return { code: 200, payload: { countries: demo.countries, languages: demo.languages, logged_countries: unique( visitors.map( ( v ) => v.country ) ), logged_languages: unique( visitors.map( ( v ) => v.language ) ), mode: demo.mode, level: demo.level, allowed_ips_count: demo.allowed.size, blocked_count: demo.bans.size, remove_data_on_uninstall: demo.removeData, }, }; } function activity( params: WeSParams ) { // Row actions ride along with the read, as on the server. const allow = String( params.allow ?? '' ); if ( allow !== '' ) { demo.allowed.add( allow ); demo.bans.delete( allow ); } const ban = String( params.ban ?? '' ); if ( ban !== '' ) { if ( ban === DEMO_CURRENT_IP ) { return { success: false, code: 409, description: "You can't block the address you're signed in from.", }; } demo.bans.set( ban, { source: 'manual', reason: 'manual_block', minutesAgo: 0, } ); } const unblock = String( params.unblock ?? '' ); if ( unblock !== '' ) { demo.bans.delete( unblock ); } const filter = String( params.filter ?? '' ); const rows = visitors .filter( ( v ) => { if ( 'successful' === filter ) { return v.signedIn > 0; } if ( 'failed' === filter ) { return v.failed > 0; } return true; } ) .map( ( v ) => ( { ip_address: v.ip, signed_in_count: String( v.signedIn ), failed_count: String( v.failed ), last_seen: ago( v.minutesAgo ), country: v.country, language: v.language, is_allowed: flag( demo.allowed.has( v.ip ) ), has_current_ban: flag( demo.bans.has( v.ip ) ), has_manual_ban: flag( 'manual' === demo.bans.get( v.ip )?.source ), has_warnings: '0', } ) ); return { code: 200, payload: { activity: page( rows, params ), current_ip: DEMO_CURRENT_IP, }, }; } function logs( params: WeSParams ) { const ip = String( params.ip ?? '' ); const visitor = visitors.find( ( v ) => v.ip === ip ); const ban = demo.bans.get( ip ); const rows: { [ key: string ]: string }[] = []; if ( visitor ) { const base = { ip_address: ip, is_banned: '0', is_warning: '0', country: visitor.country, language: visitor.language, }; // Failures first (most recent), then the sign-ins before them, a few // minutes apart; capped so the drill-down stays readable. const failed = Math.min( visitor.failed, 8 ); for ( let i = 0; i < failed; i++ ) { const last = i === 0 && ban && 'automatic' === ban.source; rows.push( { ...base, request_at: ago( visitor.minutesAgo + i * 4 ), is_successful: '0', is_banned: flag( !! last ), reason: last ? ( 'monitor' === demo.mode ? 'would_block_' : '' ) + ban.reason : '', } ); } const signedIn = Math.min( visitor.signedIn, 5 ); for ( let i = 0; i < signedIn; i++ ) { rows.push( { ...base, request_at: ago( visitor.minutesAgo + ( failed + i ) * 4 + 60 ), is_successful: '1', reason: demo.allowed.has( ip ) ? 'allow_list' : '', } ); } } return { code: 200, payload: { logs: page( rows, params ), ban: ban ? { banned_at: ago( ban.minutesAgo ), expires_at: null, reason: ban.reason, source: ban.source, } : null, ip_info: 'A documentation address, made up for this demo.', }, }; } /** * `update_option` for the settings the cloud holds; undefined for the site's * own, which `local_call()` stores * @param params */ function updateOption( params: WeSParams ): unknown { const value = String( params.value ?? '' ); switch ( params.option ) { case 'we360cloud_sentry_firewall_protection': if ( [ 'off', 'monitor', 'protect' ].includes( value ) ) { demo.mode = value; } return { code: 200, applied: demo.mode }; case 'we360cloud_sentry_allowed_countries': case 'we360cloud_sentry_allowed_languages': { let list: unknown; try { list = JSON.parse( value ); } catch { list = null; } if ( ! Array.isArray( list ) ) { return { success: false, code: 400, description: 'Invalid list', }; } if ( 'we360cloud_sentry_allowed_countries' === params.option ) { demo.countries = list.map( String ); } else { demo.languages = list.map( String ); } return { code: 200, applied: true }; } case 'we360cloud_sentry_remove_data_on_uninstall': demo.removeData = 'true' === value; return { code: 200, applied: demo.removeData }; case 'we360cloud_sentry_modes': demo.modes = demo.modes.includes( value ) ? demo.modes.filter( ( mode ) => mode !== value ) : [ ...demo.modes, value ]; return { code: 200, applied: demo.modes }; default: return undefined; } } /** * The demo's answer to one of the *account* layer's routes. All of them are * the cloud's, so all are answered: the site reads as connected with sentry * enabled. * @param route */ export function weSDemoAccount( route: string ): Promise< unknown > { switch ( route ) { case 'connect/status': return answer( { success: true, connected: true, pending: false, products: [ 'sentry' ], dash_url: '', email: 'demo@example.com', } ); case 'balance': return answer( { success: true, code: 200, payload: { balance: 2500 }, } ); default: // The cloud's own pages (sign-in, My account, disconnect) open the // demo's advert instead; reaching here is a missed hand-off. return answer( { success: false, description: 'Not available in the demo.', } ); } } /** * The demo's answer to one of *sentry's* routes, or undefined to let the * server's `local_call()` answer it. * @param route * @param params * @param local - the request to the server, for routes the demo adds to */ export function weSDemoSentry( route: string, params: WeSParams, local: () => Promise< unknown > ): Promise< unknown > | undefined { switch ( route ) { case 'get_firewall': return answer( firewall() ); case 'get_firewall_activity': return answer( activity( params ) ); case 'get_firewall_logs': return answer( logs( params ) ); case 'sentrysetup': return answer( { success: true, code: 200 } ); case 'options': // The site's own options, plus the verification methods the cloud // would hold. return local().then( ( body ) => { const typed = body as { payload?: object }; return { ...typed, payload: { ...typed.payload, we360cloud_sentry_modes: demo.modes, }, }; } ); case 'update_option': { const body = updateOption( params ); return undefined === body ? undefined : answer( body ); } default: return undefined; } }