Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
272 lines (237 loc) · 11.4 KB
/
Copy pathserver.js
File metadata and controls
272 lines (237 loc) · 11.4 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* server.js — Portfolio Backend
* Contact form handled by Nodemailer + Gmail SMTP (port 587 / STARTTLS).
*
* KEY FIX FOR RENDER FREE TIER:
* Render's infrastructure returns only IPv6 addresses from DNS.
* Render free instances have NO IPv6 outbound routing → ENETUNREACH.
* Solution: use dns.resolve4() which explicitly fetches A records (IPv4 only),
* then pass that raw IP as the SMTP host. TLS still validates against
* 'smtp.gmail.com' via tls.servername so the certificate check passes.
*/
'use strict';
const dns = require('dns').promises;
const express = require('express');
const cors = require('cors');
const path = require('path');
const nodemailer = require('nodemailer');
require('dotenv').config();
const app = express();
const PORT = process.env.PORT || 3000;
// ── Env vars ─────────────────────────────────────────────────────
const SMTP_USER = process.env.SMTP_USER;
const SMTP_PASS = process.env.SMTP_PASS;
const CONTACT_EMAIL = process.env.CONTACT_EMAIL || SMTP_USER;
if (!SMTP_USER || !SMTP_PASS) {
console.warn('[smtp] ⚠️ SMTP_USER / SMTP_PASS not set — emails will not send.');
console.warn('[smtp] → On Render: Dashboard → Environment → add SMTP_USER, SMTP_PASS, CONTACT_EMAIL');
}
// ── Build a nodemailer transporter using a resolved IPv4 address ──
// Render free instances have NO IPv6 outbound routing → ENETUNREACH when connecting to IPv6.
// We resolve to an IPv4 address via dns.lookup(family: 4) or dns.resolve4(),
// and set tls.servername so Gmail's TLS certificate validates against 'smtp.gmail.com'.
async function resolveIpv4Host(hostname) {
try {
const res = await dns.lookup(hostname, { family: 4 });
if (res && res.address) {
console.log(`[smtp] dns.lookup(${hostname}) → ${res.address} (IPv4 ✅)`);
return res.address;
}
} catch (err) {
console.warn(`[smtp] dns.lookup(${hostname}) failed: ${err.message}`);
}
try {
const addresses = await dns.resolve4(hostname);
if (addresses && addresses.length > 0) {
console.log(`[smtp] dns.resolve4(${hostname}) → ${addresses[0]} (IPv4 ✅)`);
return addresses[0];
}
} catch (err) {
console.warn(`[smtp] dns.resolve4(${hostname}) failed: ${err.message}`);
}
return hostname;
}
async function buildTransporter() {
const smtpHost = await resolveIpv4Host('smtp.gmail.com');
return nodemailer.createTransport({
host: smtpHost,
port: 587,
secure: false, // STARTTLS (upgrades automatically)
auth: {
user: SMTP_USER,
pass: SMTP_PASS
},
tls: {
servername: 'smtp.gmail.com',
rejectUnauthorized: true
},
connectionTimeout: 15000,
greetingTimeout: 15000,
socketTimeout: 20000
});
}
let isSmtpVerified = false;
let lastSmtpError = null;
let lastEmailSentAt = null;
// ── Helpers ──────────────────────────────────────────────────────
function escapeHtml(str) {
return String(str)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
// ── Middleware ───────────────────────────────────────────────────
const corsOptions = {
origin: true,
methods: ['GET', 'POST', 'OPTIONS'],
allowedHeaders: ['Content-Type'],
optionsSuccessStatus: 204
};
app.use(cors(corsOptions));
app.options('/{*splat}', cors(corsOptions));
app.use(express.json({ limit: '20kb' }));
app.use(express.urlencoded({ extended: true, limit: '20kb' }));
// ── Serve static frontend ────────────────────────────────────────
app.use(express.static(path.join(__dirname, 'public')));
// ── GET /api/health ──────────────────────────────────────────────
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
service: 'portfolio-api',
smtpConfigured: !!(SMTP_USER && SMTP_PASS),
smtpVerified: isSmtpVerified,
smtp: !!(SMTP_USER && SMTP_PASS),
lastSmtpError: lastSmtpError,
lastEmailSentAt: lastEmailSentAt
});
});
// ── Rate limiter (5 req / IP / 15 min) ──────────────────────────
const ipRequestLog = new Map();
const RATE_LIMIT = 5;
const RATE_WINDOW = 15 * 60 * 1000;
function isRateLimited(ip) {
const now = Date.now();
const entry = ipRequestLog.get(ip);
if (!entry || now - entry.start > RATE_WINDOW) {
ipRequestLog.set(ip, { start: now, count: 1 });
return false;
}
entry.count++;
return entry.count > RATE_LIMIT;
}
setInterval(() => {
const now = Date.now();
for (const [ip, entry] of ipRequestLog) {
if (now - entry.start > RATE_WINDOW) ipRequestLog.delete(ip);
}
}, RATE_WINDOW).unref();
// ── POST /api/contact ────────────────────────────────────────────
app.post('/api/contact', async (req, res) => {
if (!SMTP_USER || !SMTP_PASS) {
return res.status(503).json({ error: 'Email service not configured.' });
}
const clientIp = req.headers['x-forwarded-for']?.split(',')[0].trim() || req.ip || 'unknown';
if (isRateLimited(clientIp)) {
return res.status(429).json({ error: 'Too many requests. Please try again later.' });
}
const { name, email, subject, message } = req.body || {};
if (typeof name !== 'string' || typeof email !== 'string' || typeof message !== 'string') {
return res.status(400).json({ error: 'Name, email, and message are required.' });
}
const trimmedName = name.trim();
const trimmedEmail = email.trim();
const trimmedSubject = typeof subject === 'string' ? subject.trim() : '';
const trimmedMessage = message.trim();
if (!trimmedName || !trimmedEmail || !trimmedMessage) {
return res.status(400).json({ error: 'Name, email, and message cannot be empty.' });
}
if (trimmedName.length > 100 || trimmedEmail.length > 100 || trimmedSubject.length > 200 || trimmedMessage.length > 5000) {
return res.status(400).json({ error: 'Input exceeds maximum allowed length.' });
}
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(trimmedEmail)) {
return res.status(400).json({ error: 'Invalid email address.' });
}
// Reject header injection
if (/[\r\n]/.test(trimmedName) || /[\r\n]/.test(trimmedEmail) || /[\r\n]/.test(trimmedSubject)) {
return res.status(400).json({ error: 'Invalid characters in input.' });
}
try {
const transporter = await buildTransporter();
const cleanName = trimmedName.replace(/["\\]/g, '');
const mailOptions = {
from: `"${cleanName} via Portfolio" <${SMTP_USER}>`,
replyTo: `"${cleanName}" <${trimmedEmail}>`,
to: CONTACT_EMAIL,
subject: trimmedSubject ? `[Portfolio] ${trimmedSubject}` : '[Portfolio] New message',
html: `
<div style="font-family:monospace;background:#0d0d0d;color:#e0e0e0;padding:24px;border-radius:8px;max-width:600px;">
<h2 style="color:#00d4ff;margin-top:0;">📨 New Portfolio Message</h2>
<p><strong>Name:</strong> ${escapeHtml(trimmedName)}</p>
<p><strong>Email:</strong> <a href="mailto:${escapeHtml(trimmedEmail)}" style="color:#00d4ff;">${escapeHtml(trimmedEmail)}</a></p>
<p><strong>Subject:</strong> ${escapeHtml(trimmedSubject || '(none)')}</p>
<hr style="border-color:#333;margin:16px 0;">
<p><strong>Message:</strong><br>${escapeHtml(trimmedMessage).replace(/\n/g, '<br>')}</p>
</div>`
};
const info = await Promise.race([
transporter.sendMail(mailOptions),
new Promise((_, reject) => setTimeout(() => reject(new Error('SMTP timeout')), 25000))
]);
console.log(`[contact] ✅ Email sent successfully (id: ${info.messageId})`);
isSmtpVerified = true;
lastSmtpError = null;
lastEmailSentAt = new Date().toISOString();
res.json({ message: 'success' });
} catch (err) {
lastSmtpError = { message: err.message, code: err.code, time: new Date().toISOString() };
console.error('[contact] ❌ Error:', err.message, '| code:', err.code);
res.status(500).json({ error: 'Failed to send email. Please try again later.', details: err.message });
}
});
// Reject unsupported methods on /api/contact
app.all('/api/contact', (req, res) => {
res.set('Allow', 'POST, OPTIONS');
res.status(405).json({ error: 'Method not allowed.' });
});
// ── Global error handler ─────────────────────────────────────────
app.use((err, req, res, next) => {
if (err.type === 'entity.parse.failed' || err.status === 400) {
return res.status(400).json({ error: 'Invalid JSON in request body.' });
}
if (err.status === 413) {
return res.status(413).json({ error: 'Request body exceeds maximum allowed size.' });
}
console.error('[server] Unhandled error:', err.message);
res.status(500).json({ error: 'Internal server error.' });
});
// ── Start ────────────────────────────────────────────────────────
if (require.main === module) {
app.listen(PORT, '0.0.0.0', () => {
console.log(`✅ Server running on port ${PORT}`);
if (SMTP_USER && SMTP_PASS) {
// Warm up: verify SMTP using an IPv4-resolved transporter
buildTransporter()
.then(t => t.verify())
.then(() => {
isSmtpVerified = true;
console.log('[smtp] ✅ Gmail SMTP ready — emails will work');
})
.catch(err => {
console.error('[smtp] ❌ SMTP verify failed:', err.message, '| code:', err.code);
if (err.code === 'ENETUNREACH') {
console.error('[smtp] → Network unreachable. Render may be blocking this connection.');
} else if (err.responseCode === 535) {
console.error('[smtp] → Auth failed. Check SMTP_USER and SMTP_PASS on Render.');
console.error('[smtp] → SMTP_PASS must be a Gmail App Password (not your login password).');
console.error('[smtp] → Generate one: https://myaccount.google.com/apppasswords');
}
});
} else {
console.warn('[smtp] ⚠️ No credentials — set SMTP_USER, SMTP_PASS, CONTACT_EMAIL on Render');
}
});
}
// Export the Express app for Vercel serverless function support
module.exports = app;