|
| 1 | +import { randomUUID } from 'node:crypto'; |
| 2 | +import { |
| 3 | + type AnalyticsEvent, |
| 4 | + type BlockedTraffic, |
| 5 | + clickHouse, |
| 6 | +} from '@databuddy/db'; |
| 7 | +import { extractIpFromRequest, getGeo } from '../utils/ip-geo'; |
| 8 | +import { parseUserAgent } from '../utils/user-agent'; |
| 9 | +import { sanitizeString, VALIDATION_LIMITS } from '../utils/validation'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Log blocked traffic for security and monitoring purposes |
| 13 | + */ |
| 14 | +export async function logBlockedTraffic( |
| 15 | + request: Request, |
| 16 | + body: any, |
| 17 | + _query: any, |
| 18 | + blockReason: string, |
| 19 | + blockCategory: string, |
| 20 | + botName?: string, |
| 21 | + clientId?: string |
| 22 | +): Promise<void> { |
| 23 | + try { |
| 24 | + const ip = extractIpFromRequest(request); |
| 25 | + const userAgent = |
| 26 | + sanitizeString( |
| 27 | + request.headers.get('user-agent'), |
| 28 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 29 | + ) || ''; |
| 30 | + |
| 31 | + const { anonymizedIP, country, region, city } = await getGeo(ip); |
| 32 | + const { browserName, browserVersion, osName, osVersion, deviceType } = |
| 33 | + parseUserAgent(userAgent); |
| 34 | + |
| 35 | + const now = Date.now(); |
| 36 | + |
| 37 | + const blockedEvent: BlockedTraffic = { |
| 38 | + id: randomUUID(), |
| 39 | + client_id: clientId || '', |
| 40 | + timestamp: now, |
| 41 | + |
| 42 | + path: sanitizeString(body?.path, VALIDATION_LIMITS.STRING_MAX_LENGTH), |
| 43 | + url: sanitizeString( |
| 44 | + body?.url || body?.href, |
| 45 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 46 | + ), |
| 47 | + referrer: sanitizeString( |
| 48 | + body?.referrer || request.headers.get('referer'), |
| 49 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 50 | + ), |
| 51 | + method: 'POST', |
| 52 | + origin: sanitizeString( |
| 53 | + request.headers.get('origin'), |
| 54 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 55 | + ), |
| 56 | + |
| 57 | + ip: anonymizedIP || ip, |
| 58 | + user_agent: userAgent || '', |
| 59 | + accept_header: sanitizeString( |
| 60 | + request.headers.get('accept'), |
| 61 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 62 | + ), |
| 63 | + language: sanitizeString( |
| 64 | + request.headers.get('accept-language'), |
| 65 | + VALIDATION_LIMITS.STRING_MAX_LENGTH |
| 66 | + ), |
| 67 | + |
| 68 | + block_reason: blockReason, |
| 69 | + block_category: blockCategory, |
| 70 | + bot_name: botName || '', |
| 71 | + |
| 72 | + country: country || '', |
| 73 | + region: region || '', |
| 74 | + city: city || '', |
| 75 | + browser_name: browserName || '', |
| 76 | + browser_version: browserVersion || '', |
| 77 | + os_name: osName || '', |
| 78 | + os_version: osVersion || '', |
| 79 | + device_type: deviceType || '', |
| 80 | + |
| 81 | + payload_size: |
| 82 | + blockReason === 'payload_too_large' |
| 83 | + ? JSON.stringify(body || {}).length |
| 84 | + : undefined, |
| 85 | + |
| 86 | + created_at: now, |
| 87 | + }; |
| 88 | + |
| 89 | + clickHouse |
| 90 | + .insert({ |
| 91 | + table: 'analytics.blocked_traffic', |
| 92 | + values: [blockedEvent], |
| 93 | + format: 'JSONEachRow', |
| 94 | + }) |
| 95 | + .then(() => { |
| 96 | + // Successfully logged blocked traffic |
| 97 | + }) |
| 98 | + .catch((err) => { |
| 99 | + console.error('Failed to log blocked traffic', { error: err as Error }); |
| 100 | + throw err; |
| 101 | + }); |
| 102 | + } catch (error) { |
| 103 | + console.error('Failed to log blocked traffic', { error: error as Error }); |
| 104 | + throw error; |
| 105 | + } |
| 106 | +} |
| 107 | + |
0 commit comments