-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple-https-server.js
More file actions
387 lines (344 loc) · 13.7 KB
/
simple-https-server.js
File metadata and controls
387 lines (344 loc) · 13.7 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
const express = require('express');
const https = require('https');
const fs = require('fs');
const path = require('path');
// 导入现有的server.js中的所有路由和中间件
const app = express();
// 中间件配置
app.use(express.json({ limit: '50mb' }));
app.use(express.urlencoded({ extended: true, limit: '50mb' }));
app.use(express.static('public'));
// CORS配置
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
// 安全中间件
const helmet = require('helmet');
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
styleSrc: ["'self'", "'unsafe-inline'"],
scriptSrc: ["'self'", "'unsafe-inline'"],
connectSrc: ["'self'", "https://openai.qiniu.com"],
mediaSrc: ["'self'", "blob:"],
imgSrc: ["'self'", "data:", "https:"],
},
},
}));
// 导入所有现有的路由
const baziCalculator = require('./bazi_calculator');
const aiPipeline = require('./ai_pipeline');
const liuyaoPipeline = require('./liuyao_pipeline');
const hebanPipeline = require('./heban_pipeline');
const ttsClient = require('./tts_client');
const { LiuyaoGenerator } = require('./liuyao_generator');
// 健康检查
app.get('/health', (req, res) => {
res.json({
status: 'ok',
timestamp: new Date().toISOString(),
https: true
});
});
// 八字计算API
app.post('/api/calculate_bazi', async (req, res) => {
try {
const { year, month, day, hour } = req.body;
const result = baziCalculator.calculateBazi(year, month, day, hour);
res.json({ success: true, data: result });
} catch (error) {
console.error('八字计算错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 问道API (天乙贵人)
app.post('/api/ask', async (req, res) => {
try {
const { year, month, day, hour, question } = req.body;
const result = await aiPipeline.analyzeWithOptionalTts(year, month, day, hour, question, false);
res.json(result);
} catch (error) {
console.error('问道分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/ask_tts', async (req, res) => {
try {
const { year, month, day, hour, question, tts } = req.body;
const result = await aiPipeline.analyzeWithOptionalTts(year, month, day, hour, question, true, tts);
if (result.success && result.audioBuffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': result.audioBuffer.length,
'X-Text': result.text || ''
});
res.send(result.audioBuffer);
} else {
res.json(result);
}
} catch (error) {
console.error('问道TTS分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 姻缘分析API
app.post('/api/ask_marriage', async (req, res) => {
try {
const { year, month, day, hour, question } = req.body;
const result = await aiPipeline.analyzeMarriageWithOptionalTts(year, month, day, hour, question, false);
res.json(result);
} catch (error) {
console.error('姻缘分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/ask_marriage_tts', async (req, res) => {
try {
const { year, month, day, hour, question, tts } = req.body;
const result = await aiPipeline.analyzeMarriageWithOptionalTts(year, month, day, hour, question, true, tts);
if (result.success && result.audioBuffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': result.audioBuffer.length,
'X-Text': result.text || ''
});
res.send(result.audioBuffer);
} else {
res.json(result);
}
} catch (error) {
console.error('姻缘TTS分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 时运分析API
app.post('/api/ask_fortune', async (req, res) => {
try {
const { year, month, day, hour, question } = req.body;
const result = await aiPipeline.analyzeFortuneWithOptionalTts(year, month, day, hour, question, false);
res.json(result);
} catch (error) {
console.error('时运分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/ask_fortune_tts', async (req, res) => {
try {
const { year, month, day, hour, question, tts } = req.body;
const result = await aiPipeline.analyzeFortuneWithOptionalTts(year, month, day, hour, question, true, tts);
if (result.success && result.audioBuffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': result.audioBuffer.length,
'X-Text': result.text || ''
});
res.send(result.audioBuffer);
} else {
res.json(result);
}
} catch (error) {
console.error('时运TTS分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 六爻占卜API (太极贵人)
app.post('/api/ask_liuyao', async (req, res) => {
try {
const { question, hexagramData } = req.body;
const result = await liuyaoPipeline.analyzeLiuyaoWithOptionalTts(question, false, hexagramData);
res.json(result);
} catch (error) {
console.error('六爻分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/ask_liuyao_tts', async (req, res) => {
try {
const { question, hexagramData, tts } = req.body;
const result = await liuyaoPipeline.analyzeLiuyaoWithOptionalTts(question, true, hexagramData, tts);
if (result.success && result.audioBuffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': result.audioBuffer.length,
'X-Text': result.text || ''
});
res.send(result.audioBuffer);
} else {
res.json(result);
}
} catch (error) {
console.error('六爻TTS分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 六爻卦象生成API
app.post('/api/generate_hexagram', async (req, res) => {
try {
const { throwResults } = req.body;
if (throwResults && Array.isArray(throwResults) && throwResults.length === 6) {
// 使用前端传来的投掷结果生成卦象
const generator = new LiuyaoGenerator();
const hexagramData = generator.generateHexagramFromThrows(throwResults);
res.json({ success: true, data: hexagramData });
} else {
// 如果没有投掷结果,随机生成
const generator = new LiuyaoGenerator();
const hexagramData = generator.generateHexagram();
res.json({ success: true, data: hexagramData });
}
} catch (error) {
console.error('六爻卦象生成错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 合盘分析API (红鸾天喜)
app.post('/api/ask_heban', async (req, res) => {
try {
const { person1, person2, question } = req.body;
const result = await hebanPipeline.analyzeHebanWithOptionalTts(person1, person2, question, false);
res.json(result);
} catch (error) {
console.error('合盘分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
app.post('/api/ask_heban_tts', async (req, res) => {
try {
const { person1, person2, question, tts } = req.body;
const result = await hebanPipeline.analyzeHebanWithOptionalTts(person1, person2, question, true, tts);
if (result.success && result.audioBuffer) {
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': result.audioBuffer.length,
'X-Text': result.text || ''
});
res.send(result.audioBuffer);
} else {
res.json(result);
}
} catch (error) {
console.error('合盘TTS分析错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 纯TTS API
app.post('/api/tts_audio', async (req, res) => {
try {
const { text, voiceType = 'qiniu_zh_male_ybxknjs', format = 'mp3', speechRate = 1.0, pitch = 1 } = req.body;
if (!text) {
return res.status(400).json({ success: false, error: '缺少文本内容' });
}
const audioBuffer = await ttsClient.synthesize(text, voiceType, format, speechRate, pitch);
res.set({
'Content-Type': 'audio/mpeg',
'Content-Length': audioBuffer.length
});
res.send(audioBuffer);
} catch (error) {
console.error('TTS合成错误:', error);
res.status(500).json({ success: false, error: error.message });
}
});
// 创建自签名证书
function createSelfSignedCert() {
const crypto = require('crypto');
const forge = require('node-forge');
try {
// 生成私钥
const keyPair = forge.pki.rsa.generateKeyPair(2048);
const privateKey = forge.pki.privateKeyToPem(keyPair.privateKey);
// 创建证书
const cert = forge.pki.createCertificate();
cert.publicKey = keyPair.publicKey;
cert.serialNumber = '01';
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setFullYear(cert.validity.notBefore.getFullYear() + 1);
const attrs = [{
name: 'commonName',
value: 'localhost'
}, {
name: 'countryName',
value: 'CN'
}, {
shortName: 'ST',
value: 'Beijing'
}, {
name: 'localityName',
value: 'Beijing'
}, {
name: 'organizationName',
value: '道求职'
}, {
shortName: 'OU',
value: 'IT'
}];
cert.setSubject(attrs);
cert.setIssuer(attrs);
cert.sign(keyPair.privateKey);
const certPem = forge.pki.certificateToPem(cert);
// 保存证书和私钥
fs.writeFileSync('server.key', privateKey);
fs.writeFileSync('server.crt', certPem);
console.log('✅ SSL证书生成成功');
return true;
} catch (error) {
console.error('❌ SSL证书生成失败:', error.message);
return false;
}
}
// 启动HTTPS服务器
function startHttpsServer() {
try {
// 检查证书是否存在
if (!fs.existsSync('server.key') || !fs.existsSync('server.crt')) {
console.log('🔐 正在生成SSL证书...');
if (!createSelfSignedCert()) {
console.log('💡 使用HTTP模式启动(语音识别功能受限)');
const http = require('http');
const httpServer = http.createServer(app);
const port = process.env.PORT || 3000;
httpServer.listen(port, () => {
console.log('🚀 HTTP服务器启动成功!');
console.log(`📱 访问地址: http://localhost:${port}`);
console.log('⚠️ 语音识别功能需要HTTPS环境,建议使用Chrome的--unsafely-treat-insecure-origin-as-secure参数');
});
return;
}
}
const options = {
key: fs.readFileSync('server.key'),
cert: fs.readFileSync('server.crt')
};
const httpsServer = https.createServer(options, app);
const port = process.env.PORT || 3443;
httpsServer.listen(port, () => {
console.log('🚀 HTTPS服务器启动成功!');
console.log(`📱 访问地址: https://localhost:${port}`);
console.log('🔐 使用HTTPS协议,语音识别功能已启用');
console.log('⚠️ 首次访问时浏览器会提示证书不安全,请点击"高级"->"继续访问"');
console.log('🎤 现在可以正常使用语音输入功能了!');
});
} catch (error) {
console.error('❌ HTTPS服务器启动失败:', error.message);
console.log('💡 回退到HTTP模式');
const http = require('http');
const httpServer = http.createServer(app);
const port = process.env.PORT || 3000;
httpServer.listen(port, () => {
console.log('🚀 HTTP服务器启动成功!');
console.log(`📱 访问地址: http://localhost:${port}`);
console.log('⚠️ 语音识别功能需要HTTPS环境');
});
}
}
// 启动服务器
startHttpsServer();