|
1340 | 1340 | // 处理单行格式的节点: - {name: ..., server: ..., ...} |
1341 | 1341 | // 需要正确处理嵌套的花括号(如 ws-opts: {path: "...", headers: {Host: ...}}) |
1342 | 1342 | clashConfig = clashConfig.split('\n').map(line => { |
1343 | | - // 检查是否是节点行(以 " - {" 开头) |
1344 | | - if (/^\s*-\s*\{/.test(line)) { |
| 1343 | + // 检查是否是节点行(以 " - {" 开头,且包含 name: 和 server:) |
| 1344 | + if (/^\s*-\s*\{/.test(line) && line.includes('name:') && line.includes('server:')) { |
1345 | 1345 | // 检查是否已经有 ech-opts |
1346 | 1346 | if (line.includes('ech-opts')) { |
1347 | 1347 | return line; // 已有 ech-opts,不修改 |
1348 | 1348 | } |
1349 | 1349 | // 找到最后一个 } 的位置(从右往左查找,处理嵌套花括号) |
1350 | 1350 | const lastBraceIndex = line.lastIndexOf('}'); |
1351 | 1351 | if (lastBraceIndex > 0) { |
1352 | | - // 在最后一个 } 之前添加 , ech-opts: {enable: true} |
1353 | | - return line.substring(0, lastBraceIndex) + ', ech-opts: {enable: true}' + line.substring(lastBraceIndex); |
| 1352 | + // 检查最后一个 } 之前是否有内容,确保格式正确 |
| 1353 | + const beforeBrace = line.substring(0, lastBraceIndex).trim(); |
| 1354 | + if (beforeBrace.length > 0) { |
| 1355 | + // 在最后一个 } 之前添加 , ech-opts: {enable: true} |
| 1356 | + // 确保在逗号前有空格 |
| 1357 | + const needsComma = !beforeBrace.endsWith(',') && !beforeBrace.endsWith('{'); |
| 1358 | + return line.substring(0, lastBraceIndex) + (needsComma ? ', ' : ' ') + 'ech-opts: {enable: true}' + line.substring(lastBraceIndex); |
| 1359 | + } |
1354 | 1360 | } |
1355 | 1361 | } |
1356 | 1362 | return line; |
1357 | 1363 | }).join('\n'); |
1358 | 1364 |
|
1359 | 1365 | // 处理多行格式的节点(如果存在) |
1360 | | - // 匹配格式: |
1361 | | - // - name: ... |
1362 | | - // server: ... |
1363 | | - // type: ... |
1364 | | - // ... |
1365 | | - clashConfig = clashConfig.replace(/^(\s*-\s*name:[^\n]*(?:\n\s+[^\n-]+)*)/gm, (match) => { |
1366 | | - // 检查是否已经有 ech-opts |
1367 | | - if (match.includes('ech-opts')) { |
1368 | | - return match; // 已有 ech-opts,不修改 |
1369 | | - } |
1370 | | - // 获取缩进级别(通常是 2 或 4 个空格) |
1371 | | - const indentMatch = match.match(/^(\s*)/); |
1372 | | - const baseIndent = indentMatch ? indentMatch[1] : ' '; |
1373 | | - const propIndent = baseIndent + ' '; |
1374 | | - // 在节点末尾添加 ech-opts |
1375 | | - return match + '\n' + baseIndent + 'ech-opts:\n' + propIndent + 'enable: true'; |
1376 | | - }); |
| 1366 | + // 只处理单行格式,多行格式由订阅转换服务处理,不需要额外修改 |
| 1367 | + // 如果订阅转换服务返回多行格式,通常已经是正确的格式 |
1377 | 1368 | } |
1378 | 1369 |
|
1379 | 1370 | // 替换 DNS nameserver 为阿里的加密 DNS |
|
0 commit comments