Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@

<a href="https://aikefu.tech">
<img src="https://raw.githubusercontent.com/GtxFury/FlyClash-Android/refs/heads/main/screenshots/file_000000009a5c720bbf9028b8da087f14.png" alt="友情推荐" />
</a>
<a href="https://www.stromsend.xyz/#/register?code=tIhaARYV">
<img src="screenshots/unnamed.jpg" alt="友情推荐" />
</a>
Expand Down
22 changes: 22 additions & 0 deletions electron/main-process/bounded-output-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const DEFAULT_MAX_OUTPUT_CHARS = 256 * 1024;

function appendBoundedOutput(currentOutput, nextChunk, maxChars = DEFAULT_MAX_OUTPUT_CHARS) {
if (!Number.isFinite(maxChars) || maxChars <= 0) {
return '';
}

const current = typeof currentOutput === 'string' ? currentOutput : '';
const next = Buffer.isBuffer(nextChunk) ? nextChunk.toString() : String(nextChunk ?? '');
const combined = current + next;

if (combined.length <= maxChars) {
return combined;
}

return combined.slice(-maxChars);
}

module.exports = {
DEFAULT_MAX_OUTPUT_CHARS,
appendBoundedOutput,
};
5 changes: 3 additions & 2 deletions electron/main-process/mihomo-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports = function initMihomoService(context) {
const { applyOverrides } = require('../ipc-handlers/overrides');
const { getMihomoControllerParam, cleanupSocketFile } = require('../utils/socket-path');
const { RunningMode, setRunningMode, getRunningMode, isServiceMode, getSocketPath, getServiceSocketPath, getSidecarSocketPath } = require('../utils/running-mode');
const { appendBoundedOutput } = require('./bounded-output-buffer');
console.log('[mihomo-service] applyOverrides函数已导入:', typeof applyOverrides);

async function waitForProcessExit(proc, timeoutMs = 5000) {
Expand Down Expand Up @@ -1200,7 +1201,7 @@ module.exports = function initMihomoService(context) {

spawnedProcess.stdout.on('data', (data) => {
const logContent = data.toString();
stdoutOutput += logContent; // 收集 stdout 输出
stdoutOutput = appendBoundedOutput(stdoutOutput, logContent); // 收集最近的 stdout 输出
console.log(`mihomo stdout: ${logContent}`);

// 检测 TUN 启动失败
Expand Down Expand Up @@ -1258,7 +1259,7 @@ module.exports = function initMihomoService(context) {

spawnedProcess.stderr.on('data', (data) => {
const errorText = data.toString();
stderrOutput += errorText; // 收集 stderr 输出
stderrOutput = appendBoundedOutput(stderrOutput, errorText); // 收集最近的 stderr 输出
console.error(`mihomo stderr: ${errorText}`);
safeSend('mihomo-error', errorText);
process.stderr.write(data);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build": "next build",
"start": "next start",
"lint": "next lint",
"test": "node --test",
"rebuild:native": "electron-rebuild -f -w better-sqlite3",
"postinstall": "electron-builder install-app-deps",
"prepare": "node scripts/prepare.mjs",
Expand Down
30 changes: 30 additions & 0 deletions test/bounded-output-buffer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('node:assert/strict');
const test = require('node:test');

const {
DEFAULT_MAX_OUTPUT_CHARS,
appendBoundedOutput,
} = require('../electron/main-process/bounded-output-buffer');

test('appendBoundedOutput keeps short output unchanged', () => {
const output = appendBoundedOutput('hello', '\nworld', 32);

assert.equal(output, 'hello\nworld');
});

test('appendBoundedOutput caps retained output and keeps newest content', () => {
let output = '';

output = appendBoundedOutput(output, '12345', 10);
output = appendBoundedOutput(output, '67890', 10);
output = appendBoundedOutput(output, 'abcde', 10);

assert.equal(output, '67890abcde');
assert.equal(output.length, 10);
});

test('appendBoundedOutput truncates a single large chunk to the configured limit', () => {
const output = appendBoundedOutput('', 'x'.repeat(DEFAULT_MAX_OUTPUT_CHARS + 5), 16);

assert.equal(output, 'x'.repeat(16));
});