forked from Zhanli-Li/Auto-Tutor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_server.js
More file actions
577 lines (506 loc) · 21.9 KB
/
email_server.js
File metadata and controls
577 lines (506 loc) · 21.9 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
const express = require('express');
const nodemailer = require('nodemailer');
const cors = require('cors');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
const rateLimit = require('express-rate-limit');
// 新增:HTTP/HTTPS 与 DNS
const http = require('http');
const https = require('https');
const dns = require('dns');
// 加载环境变量
require('dotenv').config();
const app = express();
const port = process.env.PORT || 3000;
// 配置常量
const CONFIG = {
PORT: port,
RATE_LIMIT_WINDOW_MS: parseInt(process.env.RATE_LIMIT_WINDOW_MS) || 15 * 60 * 1000,
RATE_LIMIT_MAX_REQUESTS: parseInt(process.env.RATE_LIMIT_MAX_REQUESTS) || 10,
MAX_FILE_SIZE_MB: parseInt(process.env.MAX_FILE_SIZE_MB) || 10,
MAX_EMAIL_CONTENT_LENGTH: parseInt(process.env.MAX_EMAIL_CONTENT_LENGTH) || 50000,
MAX_EMAIL_SUBJECT_LENGTH: parseInt(process.env.MAX_EMAIL_SUBJECT_LENGTH) || 200,
FETCH_TIMEOUT_MS: parseInt(process.env.FETCH_TIMEOUT_MS) || 30000,
MAX_CONTENT_SIZE_MB: parseInt(process.env.MAX_CONTENT_SIZE_MB) || 5,
MAX_CONTENT_LENGTH: parseInt(process.env.MAX_CONTENT_LENGTH) || 3000,
UPLOAD_DIR: process.env.UPLOAD_DIR || 'uploads',
LOG_DIR: process.env.LOG_DIR || 'log',
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
ENABLE_ACCESS_LOG: process.env.ENABLE_ACCESS_LOG === 'true'
};
// 新增:IPv4 优先 & Keep-Alive Agent(减少握手、提高稳定性)
try { if (dns.setDefaultResultOrder) dns.setDefaultResultOrder('ipv4first'); } catch (e) {}
// 使用默认 lookup;仅通过 dns.setDefaultResultOrder 优先 IPv4,避免部分场景 hostname 为空触发错误
const keepAliveAgentHttp = new http.Agent({ keepAlive: true, maxSockets: 10 });
const keepAliveAgentHttps = new https.Agent({ keepAlive: true, maxSockets: 10 });
// 日志函数
function log(level, message, ...args) {
const timestamp = new Date().toISOString();
const levels = { error: 0, warn: 1, info: 2, debug: 3 };
const currentLevel = levels[CONFIG.LOG_LEVEL] || 2;
if (levels[level] <= currentLevel) {
console[level](`[${timestamp}] [${level.toUpperCase()}]`, message, ...args);
}
}
// 创建uploads目录
if (!fs.existsSync(CONFIG.UPLOAD_DIR)) {
fs.mkdirSync(CONFIG.UPLOAD_DIR, { recursive: true });
log('info', `创建上传目录: ${CONFIG.UPLOAD_DIR}`);
}
// 创建log目录
if (!fs.existsSync(CONFIG.LOG_DIR)) {
fs.mkdirSync(CONFIG.LOG_DIR, { recursive: true });
log('info', `创建日志目录: ${CONFIG.LOG_DIR}`);
}
// 速率限制
const limiter = rateLimit({
windowMs: CONFIG.RATE_LIMIT_WINDOW_MS,
max: CONFIG.RATE_LIMIT_MAX_REQUESTS,
message: { success: false, message: '请求过于频繁,请稍后再试' },
standardHeaders: true,
legacyHeaders: false
});
// 中间件
app.use(cors());
app.use(express.json({ limit: '10mb' }));
app.use(express.static('.'));
app.use('/api/', limiter);
// 文件上传配置
const upload = multer({
dest: CONFIG.UPLOAD_DIR + '/',
limits: {
fileSize: CONFIG.MAX_FILE_SIZE_MB * 1024 * 1024,
files: 20
},
fileFilter: (req, file, cb) => {
const allowedTypes = (process.env.ALLOWED_FILE_TYPES || 'application/pdf').split(',');
if (allowedTypes.includes(file.mimetype)) {
cb(null, true);
} else {
cb(new Error(`只允许上传以下格式的文件: ${allowedTypes.join(', ')}`));
}
}
});
// 输入验证函数
function validateEmailInput(data) {
const { to, subject, body, senderEmail, senderPassword, smtpServer, smtpPort } = data;
if (!to || !subject || !body || !senderEmail || !senderPassword || !smtpServer || !smtpPort) {
throw new Error('缺少必要的邮件参数');
}
// 邮箱格式验证
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(to) || !emailRegex.test(senderEmail)) {
throw new Error('邮箱格式不正确');
}
// 端口验证
const port = parseInt(smtpPort);
if (isNaN(port) || port < 1 || port > 65535) {
throw new Error('SMTP端口号无效');
}
// 主题和内容长度限制
if (subject.length > CONFIG.MAX_EMAIL_SUBJECT_LENGTH) {
throw new Error(`邮件主题过长(最多${CONFIG.MAX_EMAIL_SUBJECT_LENGTH}字符)`);
}
if (body.length > CONFIG.MAX_EMAIL_CONTENT_LENGTH) {
throw new Error(`邮件内容过长(最多${CONFIG.MAX_EMAIL_CONTENT_LENGTH}字符)`);
}
return true;
}
// 安全处理文件/目录名
function sanitizeForPath(name) {
return (name || '')
.replace(/[\\/:*?"<>|]/g, '')
.replace(/\s+/g, ' ')
.trim();
}
// 修复浏览器上传中文文件名被按 Latin1 解读导致的乱码(例如显示为 “ä¸å…”)
// 思路:尝试用 latin1 -> utf8 纠正;若纠正后含中日韩字符且原字符串疑似乱码,则采用纠正结果
function fixFilenameEncoding(name) {
if (!name) return name;
try {
const decoded = Buffer.from(name, 'latin1').toString('utf8');
const hasCJK = /[\u3400-\u9FFF]/.test(decoded);
const origHasCJK = /[\u3400-\u9FFF]/.test(name);
const looksMojibake = /[ÃÂåäæçèéíóú]/.test(name); // 常见 UTF-8 被当作 Latin1 的伪影
if ((hasCJK && !origHasCJK) || looksMojibake) {
return decoded;
}
} catch (e) {
// 忽略ncorrect
}
return name;
}
// 邮件发送接口
app.post('/api/send-email', upload.fields([
{ name: 'resume', maxCount: 1 },
{ name: 'transcript', maxCount: 1 },
{ name: 'attachments', maxCount: 15 }
]), async (req, res) => {
const startTime = Date.now();
// 记录所有上传的临时文件路径,便于统一清理
const uploadedFiles = [];
try {
// 输入验证
validateEmailInput(req.body);
const {
to,
subject,
body,
senderEmail,
senderPassword,
smtpServer,
smtpPort
} = req.body;
// 新增:可选的代理端口解析与校验(HTTP 代理)
const proxyPortRaw = (req.body.proxyPort || '').trim();
const proxyPort = proxyPortRaw ? parseInt(proxyPortRaw) : null;
if (proxyPortRaw && (isNaN(proxyPort) || proxyPort < 1 || proxyPort > 65535)) {
throw new Error('代理端口无效');
}
const senderName = (req.body.senderName || '').trim();
const tutorNameRaw = (req.body.tutorName || '').trim();
const emailLanguage = (req.body.emailLanguage || '').trim();
// 收集上传的文件(简历、成绩单、其他附件)
const files = req.files || {};
const resumeFiles = files.resume || [];
const transcriptFiles = files.transcript || [];
const otherFiles = files.attachments || [];
[ ...resumeFiles, ...transcriptFiles, ...otherFiles ].forEach(f => {
if (f && f.path) uploadedFiles.push(f.path);
});
log('info', `开始发送邮件: ${senderEmail} -> ${to}`);
// 创建邮件传输器(新增:支持可选HTTP代理)
const transportOptions = {
host: smtpServer,
port: parseInt(smtpPort),
secure: parseInt(smtpPort) === 465, // 465端口使用SSL
auth: {
user: senderEmail,
pass: senderPassword
},
tls: {
rejectUnauthorized: false
},
connectionTimeout: 60000, // 60秒连接超时
greetingTimeout: 30000, // 30秒问候超时
socketTimeout: 60000 // 60秒socket超时
};
if (proxyPort) {
transportOptions.proxy = `http://127.0.0.1:${proxyPort}`; // 仅当提供代理端口时启用
log('info', `已启用SMTP代理: ${transportOptions.proxy}`);
}
const transporter = nodemailer.createTransport(transportOptions);
// 验证SMTP连接
await transporter.verify();
log('debug', 'SMTP连接验证成功');
// 将纯文本换行转换为HTML段落与换行,避免“没有格式”。
const escapeHtml = (s) => s
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>');
const toHtml = (text) => {
const escaped = escapeHtml(text || '');
// 先按双换行切段,段内的单换行转成 <br>
return escaped
.split(/\n\s*\n/g)
.map(p => `<p style="margin:0 0 12px 0; line-height:1.6;">${p.replace(/\n/g, '<br>')}</p>`)
.join('');
};
const htmlBody = toHtml(body);
// RFC5987 编码工具,保障中文等非ASCII文件名在多数客户端正确显示
const encodeRFC5987 = (str) => encodeURIComponent(str)
.replace(/['()]/g, escape)
.replace(/\*/g, '%2A');
const asciiFallback = (str, fallback = 'attachment.pdf') => {
const ascii = str.replace(/[\u0080-\uFFFF]/g, '');
return ascii && /[\w\.-]/.test(ascii) ? ascii : fallback;
};
// 邮件选项
const mailOptions = {
from: `${senderName ? '"' + senderName + '" ' : ''}<${senderEmail}>`,
to: to,
subject: subject,
html: htmlBody,
text: body
};
// 组装附件:简历、成绩单、其他PDF(带 contentDisposition,包含 filename* 为 UTF-8)
const attachments = [];
if (resumeFiles[0]) {
const name = fixFilenameEncoding(resumeFiles[0].originalname || 'resume.pdf');
attachments.push({
filename: name,
path: resumeFiles[0].path,
contentType: 'application/pdf',
contentDisposition: `attachment; filename="${asciiFallback(name, 'resume.pdf')}"; filename*=UTF-8''${encodeRFC5987(name)}`
});
log('debug', `已添加简历: ${name}`);
}
if (transcriptFiles[0]) {
const name = fixFilenameEncoding(transcriptFiles[0].originalname || 'transcript.pdf');
attachments.push({
filename: name,
path: transcriptFiles[0].path,
contentType: 'application/pdf',
contentDisposition: `attachment; filename="${asciiFallback(name, 'transcript.pdf')}"; filename*=UTF-8''${encodeRFC5987(name)}`
});
log('debug', `已添加成绩单: ${name}`);
}
if (otherFiles && otherFiles.length) {
otherFiles.forEach((f, idx) => {
const name = fixFilenameEncoding(f.originalname || `attachment_${idx + 1}.pdf`);
attachments.push({
filename: name,
path: f.path,
contentType: 'application/pdf',
contentDisposition: `attachment; filename="${asciiFallback(name, `attachment_${idx + 1}.pdf`)}"; filename*=UTF-8''${encodeRFC5987(name)}`
});
});
log('debug', `已添加其他PDF数量: ${otherFiles.length}`);
}
if (attachments.length) {
mailOptions.attachments = attachments;
}
// 发送邮件
const info = await transporter.sendMail(mailOptions);
const duration = Date.now() - startTime;
log('info', `邮件发送成功: ${info.messageId}, 耗时: ${duration}ms`);
// 写入本地日志:每位导师一个文件夹 log/xx导师
try {
let tutorDirName = sanitizeForPath(tutorNameRaw) || sanitizeForPath(to.split('@')[0]);
if (!/导师$/.test(tutorDirName)) {
tutorDirName += '导师';
}
tutorDirName = tutorDirName || '未知导师';
const tutorDir = path.join(CONFIG.LOG_DIR, tutorDirName);
fs.mkdirSync(tutorDir, { recursive: true });
const ts = new Date().toISOString().replace(/[:]/g, '-');
const safeSubject = sanitizeForPath(subject).slice(0, 60) || '邮件';
const logFilename = `${ts}_${safeSubject}.json`;
const logPath = path.join(tutorDir, logFilename);
const logData = {
timestamp: new Date().toISOString(),
messageId: info.messageId || '',
durationMs: duration,
to,
tutorName: tutorNameRaw,
from: mailOptions.from,
language: emailLanguage,
subject,
body,
html: htmlBody,
attachments: (attachments || []).map(a => ({ filename: a.filename }))
};
fs.writeFileSync(logPath, JSON.stringify(logData, null, 2), 'utf8');
log('info', `已保存邮件日志: ${logPath}`);
} catch (logErr) {
log('warn', '写入邮件日志失败:', logErr.message);
}
// 清理所有上传的临时文件
uploadedFiles.forEach(p => {
if (p && fs.existsSync(p)) {
try { fs.unlinkSync(p); } catch (e) { log('warn', `清理临时文件失败: ${p} -> ${e.message}`); }
}
});
res.json({
success: true,
message: '邮件发送成功',
messageId: info.messageId,
duration: duration
});
} catch (error) {
const duration = Date.now() - startTime;
log('error', `邮件发送失败 (耗时: ${duration}ms):`, error.message);
// 清理所有上传的临时文件
uploadedFiles.forEach(p => {
if (p && fs.existsSync(p)) {
try { fs.unlinkSync(p); } catch (e) { log('warn', `清理临时文件失败: ${p} -> ${e.message}`); }
}
});
// 根据错误类型返回不同的状态码
let statusCode = 500;
let errorMessage = error.message;
if (error.message.includes('缺少必要') || error.message.includes('格式不正确') || error.message.includes('无效')) {
statusCode = 400; // 客户端错误
} else if (error.message.includes('认证失败') || error.message.includes('Authentication')) {
statusCode = 401; // 认证错误
errorMessage = 'SMTP认证失败,请检查邮箱和密码';
} else if (error.message.includes('连接') || error.message.includes('timeout')) {
statusCode = 503; // 服务不可用
errorMessage = 'SMTP服务器连接失败,请检查服务器设置';
}
res.status(statusCode).json({
success: false,
message: errorMessage,
duration: duration
});
}
});
// 网页内容获取接口
app.get('/api/fetch-website', async (req, res) => {
const startTime = Date.now();
try {
const { url } = req.query;
// 输入验证
if (!url) {
return res.status(400).json({
success: false,
message: '缺少URL参数'
});
}
// URL格式验证
let validUrl;
try {
validUrl = new URL(url);
if (!['http:', 'https:'].includes(validUrl.protocol)) {
throw new Error('只支持HTTP和HTTPS协议');
}
} catch (urlError) {
return res.status(400).json({
success: false,
message: 'URL格式无效: ' + urlError.message
});
}
log('info', `开始获取网页内容: ${url}`);
const fetch = (await import('node-fetch')).default;
// 使用带重试和超时的抓取逻辑
const response = await fetchWithRetry(
fetch,
url,
{
// 仍然限制最大重定向次数
follow: 5,
// 限制响应体最大尺寸,避免过大
size: CONFIG.MAX_CONTENT_SIZE_MB * 1024 * 1024,
// 使用 Keep-Alive + IPv4 优先的 Agent(在封装中默认提供,可覆盖)
agent: (parsedUrl) => parsedUrl.protocol === 'http:' ? keepAliveAgentHttp : keepAliveAgentHttps
},
{
maxRetries: parseInt(process.env.FETCH_MAX_RETRIES) || 4,
timeoutMs: CONFIG.FETCH_TIMEOUT_MS,
backoffBaseMs: 400,
backoffFactor: 2,
maxBackoffMs: 5000
}
);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const contentType = response.headers.get('content-type') || '';
if (!contentType.includes('text/html') && !contentType.includes('text/plain')) {
throw new Error('不支持的内容类型: ' + contentType);
}
const text = await response.text();
// 简单提取文本内容
const textContent = text
.replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '') // 移除script标签
.replace(/<style[^>]*>[\s\S]*?<\/style>/gi, '') // 移除style标签
.replace(/<[^>]*>/g, ' ') // 移除HTML标签
.replace(/\s+/g, ' ') // 合并空白字符
.trim()
.substring(0, CONFIG.MAX_CONTENT_LENGTH);
const duration = Date.now() - startTime;
log('info', `网页内容获取成功, 内容长度: ${textContent.length}, 耗时: ${duration}ms`);
res.json({
success: true,
content: textContent,
url: url,
contentLength: textContent.length,
duration: duration
});
} catch (error) {
const duration = Date.now() - startTime;
log('error', `网页获取失败 (耗时: ${duration}ms):`, error.message);
let statusCode = 500;
let errorMessage = error.message;
if (error.message.includes('timeout') || error.message.includes('ETIMEDOUT')) {
statusCode = 504; // 网关超时
errorMessage = '网页请求超时,请稍后重试';
} else if (error.message.includes('ENOTFOUND') || error.message.includes('ECONNREFUSED')) {
statusCode = 502; // 网关错误
errorMessage = '无法连接到目标网站';
} else if (error.message.includes('HTTP 4')) {
statusCode = 404; // 客户端错误
errorMessage = '网页不存在或无法访问';
}
res.status(statusCode).json({
success: false,
message: errorMessage,
duration: duration
});
}
});
app.listen(CONFIG.PORT, () => {
log('info', `邮件服务器运行在 http://localhost:${CONFIG.PORT}`);
log('info', `请在浏览器中打开 http://localhost:${CONFIG.PORT}/tutor_email_system.html`);
log('info', `环境: ${process.env.NODE_ENV || 'development'}`);
log('info', `日志级别: ${CONFIG.LOG_LEVEL}`);
log('info', `上传目录: ${CONFIG.UPLOAD_DIR}`);
log('info', `速率限制: ${CONFIG.RATE_LIMIT_MAX_REQUESTS}次/${CONFIG.RATE_LIMIT_WINDOW_MS/1000/60}分钟`);
});
// 优雅关闭
process.on('SIGTERM', () => {
log('info', '收到SIGTERM信号,正在关闭服务器...');
process.exit(0);
});
process.on('SIGINT', () => {
log('info', '收到SIGINT信号,正在关闭服务器...');
process.exit(0);
});
// 简易退避等待
function sleep(ms) { return new Promise(res => setTimeout(res, ms)); }
// 新增:带超时与重试的抓取封装(指数退避 + 抖动)
async function fetchWithRetry(fetchImpl, url, opts = {}, retryOpts = {}) {
const {
maxRetries = 4,
timeoutMs = 15000,
backoffBaseMs = 400,
backoffFactor = 2,
maxBackoffMs = 5000
} = retryOpts;
const defaultHeaders = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8'
};
const baseOptions = {
redirect: 'follow',
// node-fetch 支持 agent 为函数:根据协议返回对应 Agent
agent: (parsedUrl) => parsedUrl.protocol === 'http:' ? keepAliveAgentHttp : keepAliveAgentHttps,
...opts,
headers: { ...defaultHeaders, ...(opts.headers || {}) }
};
let lastErr;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
try {
const res = await fetchImpl(url, { ...baseOptions, signal: controller.signal });
// 对 429/5xx 做重试
if (!res.ok && (res.status === 429 || res.status >= 500)) {
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
}
clearTimeout(timer);
return res;
} catch (err) {
clearTimeout(timer);
lastErr = err;
const msg = (err && err.message) || '';
const code = err && (err.code || err.errno);
const retryable = (
msg.includes('timeout') || msg.includes('aborted') ||
msg.includes('network') || msg.includes('fetch failed') ||
(code && ['ECONNRESET','ETIMEDOUT','ECONNREFUSED','EAI_AGAIN','ENOTFOUND'].includes(code)) ||
/HTTP\s(429|5\d\d)/.test(msg)
);
if (attempt < maxRetries && retryable) {
const backoff = Math.min(maxBackoffMs, backoffBaseMs * Math.pow(backoffFactor, attempt)) * (0.5 + Math.random());
log('warn', `抓取失败,准备重试 ${attempt + 1}/${maxRetries},原因: ${msg},等待 ${Math.round(backoff)}ms`);
await sleep(backoff);
continue;
}
break;
}
}
throw lastErr;
}