okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1import test from 'node:test' ; 2import assert from 'node:assert/strict' ; 3import { chmod , mkdir , mkdtemp , rm , writeFile } from 'node:fs/promises' ; 4import { tmpdir } from 'node:os' ; 5import { join } from 'node:path' ; 6import { connectorCandidates , describeConnector , findConnector , classifyRunning , findRunningConnector , MIN_VERSION , otherTunnelMessage , ownTunnelMessage , parseVersion , skippedMessage , versionAtLeast } from '../server/cloudflared-find.mjs' ; 7import { createConnectorInstaller } from '../server/cloudflared-install.mjs' ; 8import { createCloudflare } from '../server/cloudflare.mjs' ; 9 10test ( 'version text is read and compared against the minimum' , () => { 11assert . deepEqual ( parseVersion ( 'cloudflared version 2026.5.0 (built 2026-05-12 )' ), [ 2026 , 5 , 0 ]); 12assert . equal ( parseVersion ( 'no digits here' ), null ); 13assert . equal ( versionAtLeast ( '2026.5.0' ), true ); 14assert . equal ( versionAtLeast ( '2022.1.0' ), true ); 15assert . equal ( versionAtLeast ( '2021.12.2' ), false ); 16assert . equal ( versionAtLeast ( null ), true , 'a build with no readable version is not refused' ); 17assert . equal ( MIN_VERSION , '2022.1.0' ); 18}); 19 20test ( 'Linux search order: own folder, component, PATH, then the usual places' , () => { 21const list = connectorCandidates ({ directory :'/h/engine/cloudflare' , componentDirectory :'/h/components/cloudflared' , platform :'linux' , env :{ PATH :'/usr/bin:/opt/x/bin:/usr/bin' }, home :'/home/tester' }); 22assert . deepEqual ( list . slice ( 0 , 4 ). map ( c => [ c . path , c . source ]), [ 23[ '/h/engine/cloudflare/cloudflared' , 'strata' ], [ '/h/components/cloudflared/cloudflared' , 'component' ], 24[ '/usr/bin/cloudflared' , 'path' ], [ '/opt/x/bin/cloudflared' , 'path' ]]); 25const places = list . filter ( c => c . source === 'system' ). map ( c => c . path ); 26for ( const want of [ '/usr/local/bin/cloudflared' , '/opt/homebrew/bin/cloudflared' , '/home/tester/.local/bin/cloudflared' ]) assert . ok ( places . includes ( want ), want ); 27assert . equal ( list . filter ( c => c . path === '/usr/bin/cloudflared' ). length , 1 , '/usr/bin is listed once (PATH wins)' ); 28}); 29 30test ( 'Windows and Mac usual places' , () => { 31const win = connectorCandidates ({ directory :'C:\\h\\cf' , platform :'win32' , env :{ Path :'C:\\tools' , ProgramFiles :'C:\\Program Files' , 'ProgramFiles(x86)' :'C:\\Program Files (x86)' , LOCALAPPDATA :'C:\\Users\\a\\AppData\\Local' }, home :'C:\\Users\\a' }). map ( c => c . path ); 32assert . ok ( win . includes ( 'C:\\tools\\cloudflared.exe' )); 33assert . ok ( win . includes ( 'C:\\Program Files\\cloudflared\\cloudflared.exe' )); 34assert . ok ( win . includes ( 'C:\\Program Files (x86)\\cloudflared\\cloudflared.exe' )); 35assert . ok ( win . includes ( 'C:\\Users\\a\\AppData\\Local\\Microsoft\\WinGet\\Links\\cloudflared.exe' )); 36const mac = connectorCandidates ({ directory :'/h/cf' , platform :'darwin' , env :{ PATH :'' }, home :'/Users/a' }). map ( c => c . path ); 37assert . ok ( mac . includes ( '/opt/homebrew/bin/cloudflared' ) && mac . includes ( '/usr/local/bin/cloudflared' )); 38}); 39 40test ( 'a system cloudflared 2026.5.0 in /usr/bin is used, with its version and where it came from' , async () => { 41const there = new Set ([ '/usr/bin/cloudflared' ]); 42const result = await findConnector ({ directory :'/h/engine/cloudflare' , platform :'linux' , env :{ PATH :'/usr/bin:/bin' }, home :'/home/t' , usable :async p => there . has ( p ), run :async () => 'cloudflared version 2026.5.0 (built 2026-05-12-1234 UTC)' }); 43assert . deepEqual ( result . found , { path :'/usr/bin/cloudflared' , version :'2026.5.0' , source :'path' , label :'found on your PATH' }); 44assert . match ( describeConnector ( result . found ), / cloudflared 2026\.5\.0 · \/usr\/bin\/cloudflared \(found on your PATH\) / ); 45}); 46 47test ( 'Strata’s own copy wins over a system one; an old or broken copy is skipped, with a plain reason' , async () => { 48const there = new Set ([ '/h/cf/cloudflared' , '/usr/local/bin/cloudflared' ]); 49const versions = { '/h/cf/cloudflared' :'cloudflared version 2021.3.0' , '/usr/local/bin/cloudflared' :'cloudflared version 2025.8.1' }; 50const both = await findConnector ({ directory :'/h/cf' , platform :'linux' , env :{ PATH :'' }, home :'/h' , usable :async p => there . has ( p ), run :async p => versions [ p ] }); 51assert . equal ( both . found . path , '/usr/local/bin/cloudflared' , 'the too-old own copy is passed over' ); 52assert . equal ( both . skipped [ 0 ]. reason , 'too-old' ); 53const onlyOld = await findConnector ({ directory :'/h/cf' , platform :'linux' , env :{ PATH :'' }, home :'/h' , usable :async p => p === '/h/cf/cloudflared' , run :async () => 'cloudflared version 2021.3.0' }); 54assert . equal ( onlyOld . found , null ); 55assert . match ( skippedMessage ( onlyOld . skipped ), / version 2021\.3\.0, which is too old\. Strata needs 2022\.1\.0 or newer / ); 56const broken = await findConnector ({ directory :'/h/cf' , platform :'linux' , env :{ PATH :'' }, home :'/h' , usable :async p => p === '/h/cf/cloudflared' , run :async () => { throw new Error ( 'exec format error' ); } }); 57assert . equal ( broken . found , null ); 58assert . match ( skippedMessage ( broken . skipped ), / did not run as cloudflared / ); 59const none = await findConnector ({ directory :'/h/cf' , platform :'linux' , env :{ PATH :'' }, home :'/h' , usable :async () => false , run :async () => '' }); 60assert . deepEqual ( none , { found :null , skipped :[] }); 61assert . equal ( skippedMessage ( none . skipped ), null ); 62}); 63 64test ( 'STRATA_CLOUDFLARED picks exactly one file; STRATA_CLOUDFLARED_SEARCH=own keeps to Strata’s folders' , async () => { 65const one = connectorCandidates ({ directory :'/h/cf' , platform :'linux' , env :{ PATH :'/usr/bin' , STRATA_CLOUDFLARED :'/opt/mine/cloudflared' }, home :'/h' }); 66assert . deepEqual ( one , [{ path :'/opt/mine/cloudflared' , source :'setting' }]); 67const own = connectorCandidates ({ directory :'/h/cf' , componentDirectory :'/h/c' , platform :'linux' , env :{ PATH :'/usr/bin' , STRATA_CLOUDFLARED_SEARCH :'own' }, home :'/h' }); 68assert . deepEqual ( own . map ( c => c . source ), [ 'strata' , 'component' ]); 69}); 70 71test ( 'a real executable on PATH is found and run (a user-local copy, never a system install)' , { skip :process . platform === 'win32' && 'needs a POSIX executable file to run as cloudflared (not on Windows)' }, async () => { 72const dir = await mkdtemp ( join ( tmpdir (), 'strata-cf-path-' )); 73try { 74const bin = join ( dir , 'bin' ); await mkdir ( bin ); 75await writeFile ( join ( bin , 'cloudflared' ), '#!/bin/sh\necho "cloudflared version 2026.5.0 (built 2026-05-12)"\n' ); 76await chmod ( join ( bin , 'cloudflared' ), 0o755 ); 77const installer = createConnectorInstaller ({ directory :join ( dir , 'own' ), platform :process . platform , env :{ PATH :bin }, home :dir , search :undefined }); 78assert . equal ( await installer . locate (), join ( bin , 'cloudflared' )); 79assert . equal ( installer . connector (). version , '2026.5.0' ); 80assert . equal ( await installer . ensure (), join ( bin , 'cloudflared' ), 'nothing is fetched when one is found' ); 81} finally { await rm ( dir , { recursive :true , force :true }); } 82}); 83 84const ps = lines => async ( cmd ) => cmd === 'ps' ?lines . join ( '\n' ) + '\n' :cmd === 'systemctl' ?'active\n' :'' ; 85 86test ( 'running detection lists every cloudflared with its pid; our own child is ignored; a missing ps is not a block' , async () => { 87const run = ps ([ ' 4242 cloudflared /usr/bin/cloudflared --no-autoupdate tunnel run --token SECRETSECRETSECRETSECRET' , ' 77 cloudflared cloudflared tunnel --url http://127.0.0.1:9000' , ' 90 bash bash -c cloudflared' , ' 100 sshd sshd' ]); 88const r = await findRunningConnector ({ platform :'linux' , run}); 89assert . deepEqual ([ r . running , r . kind , r . pids ], [ true , 'service' , [ 4242 , 77 ]]); 90assert . equal (( await findRunningConnector ({ platform :'linux' , run, exclude :[ 4242 , 77 ] })). running , false ); 91assert . equal (( await findRunningConnector ({ platform :'darwin' , run :async () => ' 9 cloudflared cloudflared tunnel --url http://127.0.0.1:1\n' })). kind , 'process' ); 92assert . equal (( await findRunningConnector ({ platform :'linux' , run :async () => '' })). running , false ); 93const win = async cmd => cmd === 'powershell.exe' ?'8864\tC:\\x\\cloudflared.exe tunnel run --token ABCDEFGHIJKLMNOPQRSTUV\r\n' :'STATE : 4 RUNNING' ; 94const w = await findRunningConnector ({ platform :'win32' , run :win }); 95assert . deepEqual ([ w . running , w . kind , w . pids ], [ true , 'service' , [ 8864 ]]); 96}); 97 98test ( 'only a tunnel that is clearly Strata’s own (same port, or same token) counts as Strata’s; the rest are other people’s' , () => { 99const running = { running :true , procs :[ 100{ pid :1 , command :'cloudflared tunnel --no-autoupdate --url http://127.0.0.1:8793' }, 101{ pid :2 , command :'cloudflared tunnel --url http://127.0.0.1:18793' }, 102{ pid :3 , command :'cloudflared tunnel run --token TOKENTOKENTOKENTOKENTOKEN' }, 103{ pid :4 , command :'cloudflared tunnel --url=http://localhost:8793/' }, 104{ pid :5 , command :'' }] }; 105assert . deepEqual ( classifyRunning ( running , { originUrl :'http://127.0.0.1:8793' }), { own :[ 1 , 4 ], others :[ 2 , 3 , 5 ] }); 106assert . deepEqual ( classifyRunning ( running , { originUrl :'http://127.0.0.1:8793' , token :'TOKENTOKENTOKENTOKENTOKEN' }), { own :[ 1 , 3 , 4 ], others :[ 2 , 5 ] }); 107assert . deepEqual ( classifyRunning ( running , { originUrl :'http://127.0.0.1:8793' , token :'short' }), { own :[ 1 , 4 ], others :[ 2 , 3 , 5 ] }, 'a short string is not a token' ); 108assert . equal ( otherTunnelMessage ([ 4242 ]), 'cloudflared is already running for another tunnel (PID 4242).' ); 109assert . equal ( otherTunnelMessage ([ 1 , 2 ]), 'cloudflared is already running for other tunnels (PIDs 1, 2).' ); 110assert . equal ( otherTunnelMessage ([]), null ); 111assert . match ( ownTunnelMessage ([ 7 ]), / Strata’s tunnel for this address is already running \(PID 7\), so Strata did not start a second one / ); 112}); 113 114function fakeOrigin () { return { started :0 , async start () { this . started ++ ; }, async stop () {} }; } 115const info = () => ({ path :'/usr/bin/cloudflared' , version :'2026.5.0' , source :'path' , label :'found on your PATH' }); 116const fake = () => ({ stdout :{ on () {} }, stderr :{ on () {} }, once () {}, kill () {}, pid :55 }); 117const mk = ( originUrl , procs , launch , origin = fakeOrigin ()) => createCloudflare ({ origin, originUrl, hasKey :() => true , locate :async () => '/usr/bin/cloudflared' , connectorInfo :info , 118findRunning :async () => procs . length ?{ running :true , kind :'process' , pids :procs . map ( p => p . pid ), procs} :{ running :false , kind :null , pids :[], procs :[] }, launch, timeoutMs :50 }); 119 120test ( 'another tunnel running (a service for something else) is listed in plain words and a quick tunnel still starts' , async () => { 121const origin = fakeOrigin (); const spawned = []; 122const cf = mk ( 'http://127.0.0.1:8793' , [{ pid :12 , command :'cloudflared tunnel run --token SECRETSECRETSECRETSECRET' }], ( bin , args ) => { spawned . push ([ bin , args ]); return fake (); }, origin ); 123const seen = await cf . inspect (); 124assert . equal ( seen . connector . version , '2026.5.0' ); 125assert . deepEqual ([ seen . external . others , seen . external . own ], [[ 12 ], []]); 126assert . equal ( seen . external . message , 'cloudflared is already running for another tunnel (PID 12).' ); 127assert . ok ( ! JSON . stringify ( seen ). includes ( 'SECRETSECRET' ), 'a token in another connector’s command line is never in the state' ); 128await cf . start ({ mode :'quick' }); 129assert . equal ( spawned . length , 1 ); 130assert . equal ( origin . started , 1 ); 131await cf . stop (); 132}); 133 134test ( 'Strata’s own tunnel (same port) already running is not doubled; the same token is the same tunnel; another token is not' , async () => { 135const origin = fakeOrigin (); let launched = 0 ; 136const same = mk ( 'http://127.0.0.1:8793' , [{ pid :31 , command :'cloudflared tunnel --no-autoupdate --url http://127.0.0.1:8793' }], () => { launched ++ ; return fake (); }, origin ); 137assert . deepEqual (( await same . inspect ()). external . own , [ 31 ]); 138await assert . rejects ( same . start ({ mode :'quick' }), / Strata’s tunnel for this address is already running \(PID 31\), so Strata did not start a second one / ); 139assert . equal ( launched , 0 ); 140assert . equal ( origin . started , 0 , 'the private origin is not opened either' ); 141const token = 'A' . repeat ( 30 ); 142const sameToken = mk ( 'http://127.0.0.1:8793' , [{ pid :32 , command :`cloudflared tunnel run --token ${ token } ` }], () => { launched ++ ; return fake (); }, origin ); 143await assert . rejects ( sameToken . start ({ mode :'domain' , hostname :'ai.example.com' , token}), / already running \(PID 32\) / ); 144assert . equal ( launched , 0 ); 145const spawned = []; 146const other = mk ( 'http://127.0.0.1:8793' , [{ pid :33 , command :'cloudflared tunnel run --token ' + 'B' . repeat ( 30 ) }], () => { spawned . push ( 1 ); return fake (); }, origin ); 147await other . start ({ mode :'domain' , hostname :'ai.example.com' , token}); 148assert . equal ( spawned . length , 1 , 'a different tunnel token is somebody else’s tunnel' ); 149await other . stop (); 150}); 151 152test ( 'with nothing else running the tunnel starts as before and the state names the connector in use' , async () => { 153const spawned = []; 154const cf = mk ( 'http://127.0.0.1:1' , [], ( bin , args ) => { spawned . push ([ bin , args ]); return fake (); }); 155await cf . start ({ mode :'quick' }); 156assert . equal ( spawned [ 0 ][ 0 ], '/usr/bin/cloudflared' ); 157assert . equal ( cf . state (). external , null ); 158await cf . stop (); 159});