okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
261145baa
main
1/** Install the official connector into Strata's own data folder, without sudo. */ 2import { createHash } from 'node:crypto' ; 3import { execFile } from 'node:child_process' ; 4import { mkdir , mkdtemp , lstat , rename , rm , chmod , writeFile , access } from 'node:fs/promises' ; 5import { constants } from 'node:fs' ; 6import { join } from 'node:path' ; 7import { promisify } from 'node:util' ; 8import { require as requireAcquisition } from './acquisition.mjs' ; 9import { findConnector , skippedMessage } from './cloudflared-find.mjs' ; 10 11const run = promisify ( execFile ); 12const RELEASES = 'https://api.github.com/repos/cloudflare/cloudflared/releases/latest' ; 13const LIMIT = 128 * 1024 * 1024 ; 14 15export function connectorAsset ( platform , arch ) { 16const cpu = { x64 :'amd64' , arm64 :'arm64' , ia32 :'386' , arm :'arm' }[ arch ]; 17if ( ! cpu || ! [ 'darwin' , 'linux' , 'win32' ]. includes ( platform ) || platform === 'win32' && arch === 'arm64' ) { 18throw new Error ( 'Automatic connector setup is not available for this operating system and processor.' ); 19} 20return `cloudflared- ${ platform === 'win32' ? 'windows' : platform } - ${ cpu } ${ platform === 'darwin' ? '.tgz' : platform === 'win32' ? '.exe' : '' } ` ; 21} 22 23export function verifyConnector ( bytes , digest ) { 24if ( ! /^sha256: [ a - f0 - 9 ]{ 64 } $ / . test ( digest ?? '' ) || `sha256: ${ createHash ( 'sha256' ). update ( bytes ). digest ( 'hex' )} ` !== digest ) { 25throw new Error ( 'The connector download could not be verified. Retry the automatic setup.' ); 26} 27} 28 29/** 30* `componentDirectory` is where an optional, explicitly installed connector component would live (the local 31* component folder). A connector found there is used as it is; one that is not there is never fetched while the app 32* runs (acquisition.mjs), it is a notice. 33*/ 34export function createConnectorInstaller ({ directory, componentDirectory= null , platform= process . platform , arch= process . arch , fetchImpl= fetch , env= process . env , home, run :runVersion , usable, search}) { 35const name = platform === 'win32' ?'cloudflared.exe' :'cloudflared' ; 36const binary = join ( directory , name ); 37let installing = null , last = { found :null , skipped :[] }; 38/** Strata's own folder first, then the component folder, PATH and the usual install places (cloudflared-find.mjs). */ 39async function look () { 40last = await findConnector ({ directory, componentDirectory, platform, env, home, run :runVersion , usable, search}); 41return last ; 42} 43async function locate () { return ( await look ()). found ?. path ?? null ; } 44/** What is in use ({ path, version, source, label }), or null; set by the last look. */ 45const connector = () => last . found ; 46async function install () { 47const existing = await locate (); 48if ( existing ) return existing ; 49const unusable = skippedMessage ( last . skipped ); 50if ( unusable ) throw new Error ( unusable ); 51requireAcquisition ( 'The Cloudflare connector (cloudflared)' , 'the optional connector component' ); 52const name = connectorAsset ( platform , arch ); 53await mkdir ( directory , { recursive :true }); 54const stage = await mkdtemp ( join ( directory , '.install-' )); 55const request = url => fetchImpl ( url , { signal :AbortSignal . timeout ( 120000 ), headers :{ 'user-agent' :'Strata-connector-setup' , accept :'application/vnd.github+json' } }); 56try { 57const metadata = await request ( RELEASES ); 58if ( ! metadata . ok ) throw new Error ( 'Cloudflare’s download service is unavailable. Try again in a moment.' ); 59const release = await metadata . json (); 60const asset = release . assets ?. find ( a => a . name === name ); 61if ( ! asset || ! asset . browser_download_url ?. startsWith ( 'https://github.com/cloudflare/cloudflared/releases/download/' )) throw new Error ( 'No official connector download was found for this computer.' ); 62if ( asset . size > LIMIT ) throw new Error ( 'The connector download is unexpectedly large.' ); 63const response = await request ( asset . browser_download_url ); 64if ( ! response . ok || ! response . body ) throw new Error ( 'The connector could not be downloaded. Check your connection and retry.' ); 65const chunks = []; let size = 0 ; 66for await ( const chunk of response . body ) { 67size += chunk . byteLength ; 68if ( size > LIMIT ) throw new Error ( 'The connector download is unexpectedly large.' ); 69chunks . push ( chunk ); 70} 71const bytes = Buffer . concat ( chunks ); 72verifyConnector ( bytes , asset . digest ); 73const download = join ( stage , name ); 74await writeFile ( download , bytes , { mode :0o600 }); 75let candidate = download ; 76if ( platform === 'darwin' ) { 77const { stdout} = await run ( 'tar' , [ '-tzf' , download ], { timeout :10000 }); 78const entry = stdout . split ( / \r ? \n / ). find ( p => p === 'cloudflared' || p === './cloudflared' ); 79if ( ! entry ) throw new Error ( 'The connector package did not contain the expected executable.' ); 80await run ( 'tar' , [ '-xzf' , download , '-C' , stage , entry ], { timeout :10000 }); 81candidate = join ( stage , 'cloudflared' ); 82} 83if ( ! ( await lstat ( candidate )). isFile ()) throw new Error ( 'The connector package contained an invalid executable.' ); 84await chmod ( candidate , 0o700 ); 85await run ( candidate , [ '--version' ], { timeout :10000 , windowsHide :true }); 86await rename ( candidate , binary ); 87return binary ; 88} finally { await rm ( stage , { recursive :true , force :true }); } 89} 90return { locate, connector, async inspect () { await look (); return last ; }, ensure () { 91if ( ! installing ) installing = install (). finally (() => { installing = null ; }); 92return installing ; 93} }; 94}