protocol-proxy 2.3.4 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config-store.js +295 -225
- package/lib/converters/gemini-to-anthropic.js +286 -277
- package/lib/converters/gemini-to-openai.js +255 -240
- package/lib/converters/openai-to-anthropic.js +368 -329
- package/lib/logger.js +58 -0
- package/lib/proxy-manager.js +4 -0
- package/lib/proxy-server.js +636 -357
- package/lib/stats-store.js +3 -5
- package/package.json +51 -51
- package/public/app.js +1296 -972
- package/public/index.html +321 -277
- package/public/style.css +1448 -1189
- package/server.js +767 -655
package/lib/logger.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const fs = require('fs');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const os = require('os');
|
|
4
|
+
|
|
5
|
+
const LOG_DIR = path.join(os.homedir(), '.protocol-proxy');
|
|
6
|
+
const LOG_FILE = path.join(LOG_DIR, 'server.log');
|
|
7
|
+
const MAX_SIZE = 10 * 1024 * 1024; // 10MB
|
|
8
|
+
|
|
9
|
+
let stream = null;
|
|
10
|
+
|
|
11
|
+
function init() {
|
|
12
|
+
try {
|
|
13
|
+
if (!fs.existsSync(LOG_DIR)) fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
14
|
+
stream = fs.createWriteStream(LOG_FILE, { flags: 'w' });
|
|
15
|
+
} catch (err) {
|
|
16
|
+
console.error('[Logger] 初始化失败:', err.message);
|
|
17
|
+
stream = null;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function checkSize() {
|
|
22
|
+
if (!stream) return;
|
|
23
|
+
try {
|
|
24
|
+
const stat = fs.statSync(LOG_FILE);
|
|
25
|
+
if (stat.size > MAX_SIZE) {
|
|
26
|
+
// 截断:保留后半部分
|
|
27
|
+
const content = fs.readFileSync(LOG_FILE, 'utf8');
|
|
28
|
+
const half = content.slice(Math.floor(content.length / 2));
|
|
29
|
+
stream.end();
|
|
30
|
+
fs.writeFileSync(LOG_FILE, half);
|
|
31
|
+
stream = fs.createWriteStream(LOG_FILE, { flags: 'a' });
|
|
32
|
+
}
|
|
33
|
+
} catch {}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function timestamp() {
|
|
37
|
+
return new Date().toLocaleString('zh-CN', { hour12: false });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function log(...args) {
|
|
41
|
+
const line = `[${timestamp()}] ${args.join(' ')}`;
|
|
42
|
+
console.log(line);
|
|
43
|
+
if (stream) {
|
|
44
|
+
stream.write(line + '\n');
|
|
45
|
+
checkSize();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function error(...args) {
|
|
50
|
+
const line = `[${timestamp()}] ${args.join(' ')}`;
|
|
51
|
+
console.error(line);
|
|
52
|
+
if (stream) {
|
|
53
|
+
stream.write(line + '\n');
|
|
54
|
+
checkSize();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = { init, log, error, LOG_FILE };
|