#!/usr/bin/env node // Fixed exits must survive profile changes, preserve provider identity and chain // suffixes, and fail closed if a node/source disappears. Render actual shell code. const fs = require('fs'), os = require('os'), path = require('path'); const { spawnSync } = require('child_process'); const assert = require('assert'); const S = require('../luci/servers.js'), G = require('../luci/graph.js'); const root = path.resolve(__dirname, '..'); const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'kit-fixed-')); const q = v => "'" + String(v).replace(/'/g, "'\\''") + "'"; function render(values, ok = true) { const env = path.join(tmp, 'profile.env'); fs.writeFileSync(env, Object.entries(values).map(([k,v])=>k+'='+q(v)).join('\n')+'\n'); const r = spawnSync('sh', ['router/mihomo-kit.sh','render','tests/fixtures/common.env',env], {cwd:root,encoding:'utf8'}); if (!ok) { assert.notEqual(r.status,0,'invalid settings accepted'); return; } assert.equal(r.status,0,r.stderr); const cfg = S.yamlLoad(r.stdout); assert.deepEqual(G.model(cfg).warnings,[]); if (process.env.MIHOMO) { // The renderer's config is real YAML; validate the fixed groups with the actual // core as well. Avoid unrelated remote rule sets/geodata in this small config. const config = path.join(tmp,'core.yaml'); fs.writeFileSync(config,JSON.stringify({proxies:cfg.proxies,'proxy-groups':cfg['proxy-groups'],'proxy-providers':cfg['proxy-providers'],rules:['MATCH,DIRECT']})); const t=spawnSync(process.env.MIHOMO,['-t','-d',tmp,'-f',config],{encoding:'utf8'}); assert.equal(t.status,0,t.stdout+'\n'+t.stderr); } return cfg; } const nodeName = '[MY] 🇨🇭 one (a).+$ [x] `q`'; const group = (cfg,n) => cfg['proxy-groups'].find(g=>g.name===n); const defaults = { MY_SERVERS_FILE:'tests/fixtures/own.servers', SERVICES:'fixed sub chain gone normal', SVC_FIXED_DOMAINS:'fixed.example', SVC_FIXED_SERVER:'tr "q"', SVC_SUB_DOMAINS:'subfixed.example', SVC_SUB_SERVER:nodeName, SVC_SUB_PROVIDER:'example-my', SVC_CHAIN_DOMAINS:'chain.example', SVC_CHAIN_SERVER:nodeName, SVC_CHAIN_PROVIDER:'example-my', SVC_CHAIN_CHAIN:'FOREIGN', SVC_GONE_DOMAINS:'gone.example', SVC_GONE_SERVER:'server removed', SVC_NORMAL_DOMAINS:'normal.example', SVC_NORMAL_VIA:'MY' }; try { for (const [TARGET,LOCATION] of [['router','main'],['router','whitelist'],['router','foreign'],['mobile','main']]) { const c=render({...defaults,TARGET,LOCATION}); assert.deepEqual(group(c,'FIXED').proxies,['SVC-FIXED-FIXED']); assert.deepEqual(group(c,'SVC-FIXED-FIXED').proxies,['tr "q"']); assert.equal(group(c,'NORMAL').type,'fallback'); assert.deepEqual(group(c,'SVC-FIXED-GONE').proxies,['REJECT']); const pg=group(c,'SVC-FIXED-SUB'); assert.deepEqual(pg.use,['example-my']); assert.equal(pg['empty-fallback'],'REJECT'); const regex = new RegExp(pg.filter); assert(regex.test(nodeName)); assert(!regex.test(nodeName+' copy')); assert(!regex.test('copy '+nodeName)); assert(!regex.test(nodeName.replace('.','X'))); const cg=group(c,'SVC-FIXED-CHAIN ⟶ FOREIGN'); assert.deepEqual(group(c,'CHAIN').proxies,[cg.name]); assert.deepEqual(cg.use,['example-my@FOREIGN']); assert.equal(cg['empty-fallback'],'REJECT'); assert(new RegExp(cg.filter).test(nodeName+' ⟶ FOREIGN')); assert(!new RegExp(cg.filter).test(nodeName+' copy ⟶ FOREIGN')); const own=c.proxies.find(p=>p.name==='tr "q"'); assert.equal(own['dialer-proxy'], TARGET==='mobile'||LOCATION==='whitelist'?'MY-EXIT':undefined); console.log('ok fixed service '+TARGET+'/'+LOCATION); } const c=render({...defaults,SUB_EXAMPLE_DISABLED:'1'}); assert.deepEqual(group(c,'SVC-FIXED-SUB').proxies,['REJECT']); assert.deepEqual(group(c,'SVC-FIXED-CHAIN ⟶ FOREIGN').proxies,['REJECT']); const ownChain=render({...defaults,CSQTT:'1',SVC_FIXED_CHAIN:'DIRECT'}); const og=group(ownChain,'SVC-FIXED-FIXED ⟶ DIRECT'); assert.deepEqual(og.proxies,['tr "q" ⟶ DIRECT']); assert.equal(ownChain.proxies.find(p=>p.name==='tr "q" ⟶ DIRECT')['dialer-proxy'],undefined); render({...defaults,SVC_SUB_PROVIDER:'bad|source'},false); render({...defaults,SVC_SUB_SERVER:''},false); render({...defaults,POOLS:'svc_fixed_fixed',POOL_SVC_FIXED_FIXED_SERVERS:'hy-own'},false); // Shell metacharacters remain data; they must not be evaluated by generated keys. const injection='$(touch '+path.join(tmp,'injected')+') "quote"'; const missing=render({...defaults,SVC_FIXED_SERVER:injection}); assert(!fs.existsSync(path.join(tmp,'injected'))); assert.deepEqual(group(missing,'SVC-FIXED-FIXED').proxies,['REJECT']); console.log('ok fixed service missing source, explicit entry, invalid settings and shell quoting'); } finally { fs.rmSync(tmp,{recursive:true,force:true}); }