1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| exports.handler = async (event) => { console.log('Full path received:', event.path); const relativePath = event.path.replace('/pinglun', ''); const finalPath = relativePath || '/'; const targetUrl = `你项目的域名/.netlify/functions/comment${finalPath}`; const queryString = event.queryStringParameters ? '?' + new URLSearchParams(event.queryStringParameters).toString() : ''; const fullUrl = targetUrl + queryString; const clientIP = event.headers['x-nf-client-connection-ip'] || event.headers['x-forwarded-for'] || event.headers['client-ip'] || 'unknown'; console.log('Client IP:', clientIP); console.log('Proxying to:', fullUrl); if (event.httpMethod === 'OPTIONS') { return { statusCode: 204, headers: { 'access-control-allow-origin': '*', 'access-control-allow-headers': 'Content-Type, X-Waline-Token, Authorization', 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS', }, body: '', }; } try { const fetchOptions = { method: event.httpMethod, headers: { 'content-type': event.headers['content-type'] || 'application/json', 'x-waline-token': event.headers['x-waline-token'] || '', 'authorization': event.headers['authorization'] || '', 'user-agent': event.headers['user-agent'] || 'Netlify-Proxy', 'x-forwarded-for': clientIP, 'x-real-ip': clientIP, 'x-client-ip': clientIP, 'x-forwarded-proto': event.headers['x-forwarded-proto'] || 'https', 'x-forwarded-host': event.headers['host'] || '', 'referer': event.headers['referer'] || '', 'accept-language': event.headers['accept-language'] || '', }, }; if (!['GET', 'HEAD'].includes(event.httpMethod) && event.body) { fetchOptions.body = event.body; } const response = await fetch(fullUrl, fetchOptions); const data = await response.text(); console.log('Response status:', response.status); return { statusCode: response.status, headers: { 'content-type': response.headers.get('content-type') || 'application/json', 'access-control-allow-origin': '*', 'access-control-allow-headers': 'Content-Type, X-Waline-Token, Authorization', 'access-control-allow-methods': 'GET, POST, PUT, DELETE, OPTIONS', 'set-cookie': response.headers.get('set-cookie') || '', }, body: data, }; } catch (error) { console.error('Proxy error:', error); return { statusCode: 502, headers: { 'content-type': 'application/json', }, body: JSON.stringify({ error: 'Proxy error', message: error.message }), }; } };
|