// enhanced-server.js const http = require('http'); const { ethers } = require('ethers'); class EnhancedENSVisionRouter { constructor() { this.validENSPattern = /^[a-z0-9-]+$/i; this.port = process.env.PORT || 3000; // Initialize Web3 provider for ENS validation this.provider = new ethers.providers.JsonRpcProvider( process.env.RPC_URL || 'https://eth-mainnet.alchemyapi.io/v2/your-api-key' ); // Cache for ENS validation results this.ensCache = new Map(); this.cacheTimeout = 5 * 60 * 1000; // 5 minutes } extractENSName(hostname) { const parts = hostname.split('.'); if (parts.length >= 4 && parts[1] === 'eth' && parts[2] === 'vision' && parts[3] === 'io') { return parts[0]; } return null; } isValidENSName(name) { if (!name || name.length === 0) return false; if (name.length > 63) return false; if (name.startsWith('-') || name.endsWith('-')) return false; return this.validENSPattern.test(name); } // Real ENS validation with caching async checkENSExists(ensName) { const fullENSName = ensName + '.eth'; const cacheKey = fullENSName; // Check cache first const cached = this.ensCache.get(cacheKey); if (cached && Date.now() - cached.timestamp < this.cacheTimeout) { return cached.exists; } try { console.log(`Validating ENS: ${fullENSName}`); // Try to resolve the ENS name const resolver = await this.provider.getResolver(fullENSName); const exists = resolver !== null; // Cache the result this.ensCache.set(cacheKey, { exists: exists, timestamp: Date.now() }); return exists; } catch (error) { console.error(`ENS validation error for ${fullENSName}:`, error.message); // Cache negative result for shorter time this.ensCache.set(cacheKey, { exists: false, timestamp: Date.now() }); return false; } } async handleRequest(req, res) { try { const hostname = req.headers.host; const ensName = this.extractENSName(hostname); const userAgent = req.headers['user-agent'] || ''; console.log(`${new Date().toISOString()} - ${hostname} - ${userAgent}`); if (!ensName) { this.sendErrorPage(res, 'Invalid URL format', 400); return; } if (!this.isValidENSName(ensName)) { this.sendErrorPage(res, 'Invalid ENS name format', 400); return; } // Validate ENS exists const ensExists = await this.checkENSExists(ensName); if (!ensExists) { this.sendErrorPage(res, `ENS name "${ensName}.eth" not found`, 404); return; } // Construct and redirect to Vision.io const visionURL = `https://vision.io/profile/${ensName}.eth`; console.log(`✓ Redirecting ${ensName}.eth.vision.io → ${visionURL}`); res.writeHead(302, { 'Location': visionURL, 'Cache-Control': 'public, max-age=300', 'X-ENS-Name': ensName + '.eth', 'X-Redirect-Type': 'ens-vision' }); res.end(); } catch (error) { console.error('Request handling error:', error); this.sendErrorPage(res, 'Internal server error', 500); } } sendErrorPage(res, message, statusCode = 404) { res.writeHead(statusCode, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' }); const errorPage = ` ENS → Vision.io Router

Error ${statusCode}

${message}

ENS → Vision.io Router

This service redirects ENS names to Vision.io profiles.

Format: {ensname}.eth.vision.io
Example: vitalik.eth.vision.io https://vision.io/profile/vitalik.eth
`; res.end(errorPage); } start() { const server = http.createServer((req, res) => { this.handleRequest(req, res); }); server.listen(this.port, () => { console.log(`🚀 ENS Vision Router started on port ${this.port}`); console.log(`📡 Handling wildcards for *.eth.vision.io`); console.log(`🔗 Redirecting to Vision.io profile format`); }); process.on('SIGTERM', () => { console.log('Shutting down...'); server.close(() => process.exit(0)); }); } } // Start the enhanced router const router = new EnhancedENSVisionRouter(); router.start();