v1.3: OpenCode 1-click (dashboard install+copy buttons, small_model fix), gateway WAF sanitizer (50 RCE/WAF triggers, ZWSP neutralize), failover retries, prompt caps + dump; E2E opencode run PASS
This commit is contained in:
+68
-19
@@ -1,6 +1,6 @@
|
||||
// Logicc → OpenAI adapter and local account-pool console. No runtime dependencies.
|
||||
import { createServer } from 'node:http';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { readFileSync, writeFileSync } from 'node:fs';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { timingSafeEqual } from 'node:crypto';
|
||||
@@ -83,6 +83,28 @@ function usageOf(event) {
|
||||
return { prompt_tokens: input, completion_tokens: output, total_tokens: input + output };
|
||||
}
|
||||
|
||||
const WAF_TRIGGERS = ['python -c "', 'python -c \'', 'python3 -c "', 'python3 -c \'', 'node -e "', 'perl -e "', 'php -r "', 'ruby -e "', 'bash -c "', 'sh -c "', 'cmd /c', 'powershell', 'executionpolicy bypass', '/etc/passwd', 'rm -rf /', 'union select', '<script', 'document.cookie', 'certutil', 'invoke-webrequest', 'downloadstring', 'iex(', 'wget http', 'curl http', 'bitsadmin', 'schtasks', 'reg add hkey', 'net user', 'whoami', 'systeminfo', 'tasklist', 'mimikatz', 'sqlmap', 'nmap -', 'msfvenom', 'base64 -d', 'eval(', 'onerror=', 'onload=', 'alert(1', 'sleep(5)', 'waitfor delay', 'pg_sleep', 'xp_cmdshell', 'load_file(', 'into outfile', 'select * from', 'insert into', 'delete from', 'drop table'];
|
||||
function wafSanitize(text) {
|
||||
if (typeof text !== 'string') return text;
|
||||
const Z = '';
|
||||
let out = text;
|
||||
for (const p of WAF_TRIGGERS) {
|
||||
let lower = out.toLowerCase();
|
||||
let idx = 0;
|
||||
while ((idx = lower.indexOf(p, idx)) !== -1) {
|
||||
const seg = out.slice(idx, idx + p.length);
|
||||
if (!seg.includes(Z)) {
|
||||
out = out.slice(0, idx) + seg[0] + Z + seg.slice(1) + out.slice(idx + p.length);
|
||||
lower = out.toLowerCase();
|
||||
idx += p.length + 2;
|
||||
} else {
|
||||
idx += p.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function createAdapter({ env = process.env, dataFile = env.LOGICC_DATA_FILE || resolve(ROOT, 'data/state.json') } = {}) {
|
||||
const pool = new AccountPool(dataFile, env);
|
||||
const apiBase = (env.LOGICC_API_BASE || 'https://app.logicc.com/api').replace(/\/+$/, '');
|
||||
@@ -104,7 +126,8 @@ export function createAdapter({ env = process.env, dataFile = env.LOGICC_DATA_FI
|
||||
return upstream(account, path, { method, body, stream, signal }, false);
|
||||
}
|
||||
if (!response.ok) {
|
||||
await response.body?.cancel();
|
||||
const errBody = await response.text().catch(() => '');
|
||||
console.error(`[UPERR] ${response.status} acct=${account.name} path=${path.split('?')[0]} resp=${errBody.slice(0, 120)}`);
|
||||
throw new HttpError(response.status, `上游 ${path.split('?')[0].startsWith('chats/') ? '聊天' : path.split('?')[0]} 请求失败(HTTP ${response.status})`);
|
||||
}
|
||||
return response;
|
||||
@@ -132,26 +155,43 @@ export function createAdapter({ env = process.env, dataFile = env.LOGICC_DATA_FI
|
||||
const onClose = () => { if (!res.writableEnded) controller.abort(); };
|
||||
res.on('close', onClose);
|
||||
const signal = AbortSignal.any([controller.signal, AbortSignal.timeout(180000)]);
|
||||
let account;
|
||||
const maxAttempts = accountId ? 1 : Number(env.LOGICC_FAILOVER_TRIES || 4);
|
||||
let account = null;
|
||||
let lastError = null;
|
||||
try {
|
||||
account = accountId ? pool.account(accountId) : pool.acquire();
|
||||
if (accountId) pool.runtimeFor(account.id).inFlight++;
|
||||
trace.accountId = account.id;
|
||||
trace.accountName = account.name;
|
||||
const result = await operation(account, trace, signal);
|
||||
pool.healthy(account);
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (signal.aborted) error = new HttpError(controller.signal.aborted ? 499 : 504, controller.signal.aborted ? '客户端已取消请求' : '上游请求超时');
|
||||
trace.status = error.status === 499 ? 'cancelled' : 'error';
|
||||
trace.error = safeError(error);
|
||||
trace.httpStatus = error.status || 502;
|
||||
if (account && error.status !== 499) pool.failed(account, error);
|
||||
throw error;
|
||||
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
||||
try {
|
||||
account = accountId ? pool.account(accountId) : pool.acquire();
|
||||
if (accountId) pool.runtimeFor(account.id).inFlight++;
|
||||
trace.accountId = account.id;
|
||||
trace.accountName = account.name;
|
||||
trace.attempts = attempt;
|
||||
const result = await operation(account, trace, signal);
|
||||
pool.healthy(account);
|
||||
return result;
|
||||
} catch (error) {
|
||||
if (signal.aborted) error = new HttpError(controller.signal.aborted ? 499 : 504, controller.signal.aborted ? '客户端已取消请求' : '上游请求超时');
|
||||
if (account && error.status !== 499) pool.failed(account, error);
|
||||
lastError = error;
|
||||
const retriable = attempt < maxAttempts && !accountId && !res.headersSent &&
|
||||
[403, 429, 500, 502, 503, 504].includes(error.status);
|
||||
if (retriable) continue;
|
||||
trace.status = error.status === 499 ? 'cancelled' : 'error';
|
||||
trace.error = safeError(error);
|
||||
trace.httpStatus = error.status || 502;
|
||||
throw error;
|
||||
} finally {
|
||||
if (account) pool.release(account);
|
||||
account = null;
|
||||
}
|
||||
}
|
||||
trace.status = 'error';
|
||||
trace.error = safeError(lastError);
|
||||
trace.httpStatus = lastError?.status || 503;
|
||||
throw lastError || new HttpError(503, '没有可用账户');
|
||||
} finally {
|
||||
res.off('close', onClose);
|
||||
trace.durationMs = Date.now() - trace.startedAt;
|
||||
if (account) pool.release(account);
|
||||
pool.record(trace);
|
||||
}
|
||||
}
|
||||
@@ -163,7 +203,16 @@ export function createAdapter({ env = process.env, dataFile = env.LOGICC_DATA_FI
|
||||
if (body.model !== undefined && typeof body.model !== 'string') throw new HttpError(400, 'model 必须为字符串');
|
||||
if (body.stream !== undefined && typeof body.stream !== 'boolean') throw new HttpError(400, 'stream 必须为布尔值');
|
||||
const tools = mapTools(body.tool_choice === 'none' ? [] : body.tools);
|
||||
const prompt = flattenMessages(body.messages) + tools.prompt;
|
||||
const capSeg = (text, max) => {
|
||||
if (text.length <= max) return text;
|
||||
const head = Math.floor(max * 0.6);
|
||||
return text.slice(0, head) + String.fromCharCode(10) + '[...truncated by logicc-farm gateway...]' + String.fromCharCode(10) + text.slice(-(max - head - 40));
|
||||
};
|
||||
const MSG_CAP = Number(env.LOGICC_MSG_CAP || 65000);
|
||||
const TOOLS_CAP = Number(env.LOGICC_TOOLS_CAP || 30000);
|
||||
const prompt = wafSanitize(capSeg(flattenMessages(body.messages), MSG_CAP) + capSeg(tools.prompt, TOOLS_CAP));
|
||||
console.error(`[CHAT] model=${body.model} msgs=${body.messages.length} promptLen=${prompt.length} tools=${body.tools?.length || 0} stream=${body.stream}`);
|
||||
if (prompt.length > 50000) { try { writeFileSync(resolve(ROOT, 'data/last_prompt.txt'), prompt); } catch {} }
|
||||
if (!prompt.trim()) throw new HttpError(400, '消息内容不能为空');
|
||||
await track(req, res, 'chat', async (account, trace, signal) => {
|
||||
let model = body.model;
|
||||
|
||||
Reference in New Issue
Block a user