protocol-proxy 2.3.4 → 2.4.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 +36 -1
- package/lib/logger.js +58 -0
- package/lib/proxy-manager.js +4 -0
- package/lib/proxy-server.js +312 -178
- package/lib/stats-store.js +3 -5
- package/package.json +51 -51
- package/public/app.js +272 -22
- package/public/index.html +316 -277
- package/public/style.css +159 -4
- package/server.js +120 -28
package/lib/config-store.js
CHANGED
|
@@ -73,6 +73,35 @@ function normalizeModels(models) {
|
|
|
73
73
|
));
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
+
function normalizeRoutingStrategy(strategy) {
|
|
77
|
+
return ['primary_fallback', 'round_robin', 'weighted', 'fastest'].includes(strategy)
|
|
78
|
+
? strategy
|
|
79
|
+
: 'primary_fallback';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function normalizeProviderPool(pool) {
|
|
83
|
+
if (!Array.isArray(pool)) return [];
|
|
84
|
+
const seen = new Set();
|
|
85
|
+
const result = [];
|
|
86
|
+
|
|
87
|
+
for (const item of pool) {
|
|
88
|
+
if (!item || typeof item !== 'object') continue;
|
|
89
|
+
const providerId = typeof item.providerId === 'string' ? item.providerId.trim() : '';
|
|
90
|
+
if (!providerId) continue;
|
|
91
|
+
const model = typeof item.model === 'string' ? item.model.trim() : '';
|
|
92
|
+
const key = `${providerId}\0${model}`;
|
|
93
|
+
if (seen.has(key)) continue;
|
|
94
|
+
seen.add(key);
|
|
95
|
+
result.push({
|
|
96
|
+
providerId,
|
|
97
|
+
model,
|
|
98
|
+
weight: Math.max(1, parseInt(item.weight, 10) || 1),
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return result;
|
|
103
|
+
}
|
|
104
|
+
|
|
76
105
|
function normalizeProvider(provider) {
|
|
77
106
|
if (!provider) return provider;
|
|
78
107
|
return {
|
|
@@ -83,7 +112,11 @@ function normalizeProvider(provider) {
|
|
|
83
112
|
|
|
84
113
|
function normalizeProxy(proxy) {
|
|
85
114
|
if (!proxy) return proxy;
|
|
86
|
-
return
|
|
115
|
+
return {
|
|
116
|
+
...proxy,
|
|
117
|
+
routingStrategy: normalizeRoutingStrategy(proxy.routingStrategy),
|
|
118
|
+
providerPool: normalizeProviderPool(proxy.providerPool),
|
|
119
|
+
};
|
|
87
120
|
}
|
|
88
121
|
|
|
89
122
|
function normalizeConfig(config) {
|
|
@@ -222,4 +255,6 @@ module.exports = {
|
|
|
222
255
|
addProxy,
|
|
223
256
|
updateProxy,
|
|
224
257
|
removeProxy,
|
|
258
|
+
normalizeRoutingStrategy,
|
|
259
|
+
normalizeProviderPool,
|
|
225
260
|
};
|
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 };
|