typescript-virtual-container 1.4.1 → 1.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/settings.json +2 -0
- package/README.md +5 -1
- package/benchmark-virtualshell.ts +3 -11
- package/builds/self-standalone.js +202 -202
- package/builds/self-standalone.js.map +3 -3
- package/builds/standalone-wo-sftp.js +8 -8
- package/builds/standalone-wo-sftp.js.map +3 -3
- package/builds/standalone.js +42 -42
- package/builds/standalone.js.map +3 -3
- package/dist/VirtualShell/shell.d.ts.map +1 -1
- package/dist/VirtualShell/shell.js +6 -10
- package/dist/VirtualUserManager/index.d.ts.map +1 -1
- package/dist/VirtualUserManager/index.js +4 -4
- package/dist/commands/helpers.js +1 -1
- package/dist/commands/history.js +2 -2
- package/dist/modules/linuxRootfs.d.ts +1 -1
- package/dist/modules/linuxRootfs.d.ts.map +1 -1
- package/dist/modules/linuxRootfs.js +5 -5
- package/dist/self-standalone.js +149 -102
- package/package.json +1 -1
- package/src/VirtualShell/shell.ts +6 -11
- package/src/VirtualUserManager/index.ts +4 -4
- package/src/commands/helpers.ts +1 -1
- package/src/commands/history.ts +2 -2
- package/src/modules/linuxRootfs.ts +6 -6
- package/src/self-standalone.ts +190 -141
- package/tests/helpers.test.ts +3 -3
|
@@ -24,7 +24,7 @@ ${e}`}};var Jg=2**32,Xf=(()=>{try{return new Function("return 2n ** 32n")()}catc
|
|
|
24
24
|
`)}
|
|
25
25
|
`,exitCode:0}})},exitCode:0}}};function Jl(n){return Array.isArray(n)?n:[n]}function Us(n,t){if(n===t)return{matched:!0,inlineValue:null};let e=`${t}=`;return n.startsWith(e)?{matched:!0,inlineValue:n.slice(e.length)}:t.length===2&&t.startsWith("-")&&!t.startsWith("--")&&n.startsWith(t)&&n.length>t.length?{matched:!0,inlineValue:n.slice(t.length)}:{matched:!1,inlineValue:null}}function zm(n,t={}){let e=new Set(t.flags??[]),r=new Set(t.flagsWithValue??[]),s=[],i=!1;for(let o=0;o<n.length;o+=1){let a=n[o];if(i){s.push(a);continue}if(a==="--"){i=!0;continue}let c=!1;for(let f of e){let{matched:l}=Us(a,f);if(l){c=!0;break}}if(!c){for(let f of r){let l=Us(a,f);if(l.matched){c=!0,l.inlineValue===null&&o+1<n.length&&(o+=1);break}}c||s.push(a)}}return s}function se(n,t){let e=Jl(t);for(let r of n)for(let s of e)if(Us(r,s).matched)return!0;return!1}function en(n,t){let e=Jl(t);for(let r=0;r<n.length;r+=1){let s=n[r];for(let i of e){let o=Us(s,i);if(!o.matched)continue;if(o.inlineValue!==null)return o.inlineValue;let a=n[r+1];return a!==void 0&&a!=="--"?a:!0}}}function Xn(n,t,e={}){return zm(n,e)[t]}function Yt(n,t={}){let e=new Set,r=new Map,s=[],i=new Set(t.flags??[]),o=new Set(t.flagsWithValue??[]),a=!1;for(let c=0;c<n.length;c+=1){let f=n[c];if(a){s.push(f);continue}if(f==="--"){a=!0;continue}if(i.has(f)){e.add(f);continue}if(o.has(f)){let u=n[c+1];u&&!u.startsWith("-")?(r.set(f,u),c+=1):r.set(f,"");continue}let l=Array.from(o).find(u=>f.startsWith(`${u}=`));if(l){r.set(l,f.slice(l.length+1));continue}s.push(f)}return{flags:e,flagsWithValues:r,positionals:s}}var Zl={name:"alias",description:"Define or display aliases",category:"shell",params:["[name[=value] ...]"],run:({args:n,env:t})=>{if(!t)return{exitCode:0};if(n.length===0)return{stdout:Object.entries(t.vars).filter(([s])=>s.startsWith("__alias_")).map(([s,i])=>`alias ${s.slice(8)}='${i}'`).join(`
|
|
26
26
|
`)||"",exitCode:0};let e=[];for(let r of n){let s=r.indexOf("=");if(s===-1){let i=t.vars[`__alias_${r}`];if(i)e.push(`alias ${r}='${i}'`);else return{stderr:`alias: ${r}: not found`,exitCode:1}}else{let i=r.slice(0,s),o=r.slice(s+1).replace(/^['"]|['"]$/g,"");t.vars[`__alias_${i}`]=o}}return{stdout:e.join(`
|
|
27
|
-
`)||void 0,exitCode:0}}},eu={name:"unalias",description:"Remove alias definitions",category:"shell",params:["<name...> | -a"],run:({args:n,env:t})=>{if(!t)return{exitCode:0};if(se(n,["-a"])){for(let e of Object.keys(t.vars))e.startsWith("__alias_")&&delete t.vars[e];return{exitCode:0}}for(let e of n)delete t.vars[`__alias_${e}`];return{exitCode:0}}};var xn=Vt(require("node:path"),1),Ym=["
|
|
27
|
+
`)||void 0,exitCode:0}}},eu={name:"unalias",description:"Remove alias definitions",category:"shell",params:["<name...> | -a"],run:({args:n,env:t})=>{if(!t)return{exitCode:0};if(se(n,["-a"])){for(let e of Object.keys(t.vars))e.startsWith("__alias_")&&delete t.vars[e];return{exitCode:0}}for(let e of n)delete t.vars[`__alias_${e}`];return{exitCode:0}}};var xn=Vt(require("node:path"),1),Ym=["/.virtual-env-js/.auth"];function le(n,t){return!t||t.trim()===""?n:t.startsWith("/")?xn.posix.normalize(t):xn.posix.normalize(xn.posix.join(n,t))}function jm(n){let t=n.startsWith("/")?xn.posix.normalize(n):xn.posix.normalize(`/${n}`);return Ym.some(e=>t===e||t.startsWith(`${e}/`))}function Oe(n,t,e){if(n!=="root"&&jm(t))throw new Error(`${e}: permission denied: ${t}`)}function tu(n){let e=(n.split("?")[0]?.split("#")[0]??n).split("/").filter(Boolean).pop();return e&&e.length>0?e:"index.html"}function Xm(n,t){let e=Array.from({length:n.length+1},()=>Array(t.length+1).fill(0));for(let r=0;r<=n.length;r+=1)e[r][0]=r;for(let r=0;r<=t.length;r+=1)e[0][r]=r;for(let r=1;r<=n.length;r+=1)for(let s=1;s<=t.length;s+=1){let i=n[r-1]===t[s-1]?0:1;e[r][s]=Math.min(e[r-1][s]+1,e[r][s-1]+1,e[r-1][s-1]+i)}return e[n.length][t.length]}function nu(n,t,e){let r=le(t,e);if(n.exists(r))return r;let s=xn.posix.dirname(r),i=xn.posix.basename(r),o=n.list(s),a=o.filter(f=>f.toLowerCase()===i.toLowerCase());if(a.length===1)return xn.posix.join(s,a[0]);let c=o.filter(f=>Xm(f.toLowerCase(),i.toLowerCase())<=1);return c.length===1?xn.posix.join(s,c[0]):r}function ru(n,t,e){return t.map(r=>{let s=le(n,r);return e(s).type==="directory"?`${r}/`:r}).join(" ")}function Qr(n){return n.packageManager}var iu={name:"apt",aliases:["apt-get"],description:"Package manager",category:"package",params:["<install|remove|update|upgrade|search|show|list> [pkg...]"],run:({args:n,shell:t,authUser:e})=>{let r=Qr(t);if(!r)return{stderr:"apt: package manager not initialised",exitCode:1};let s=n[0]?.toLowerCase(),i=n.slice(1),o=se(i,["-q","--quiet","-qq"]),a=se(i,["--purge"]),c=i.filter(l=>!l.startsWith("-"));if(["install","remove","purge","upgrade","update"].includes(s??"")&&e!=="root")return{stderr:`E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
|
|
28
28
|
E: Unable to acquire the dpkg frontend lock, are you root?`,exitCode:100};switch(s){case"install":{if(c.length===0)return{stderr:"apt: no packages specified",exitCode:1};let{output:l,exitCode:u}=r.install(c,{quiet:o});return{stdout:l||void 0,exitCode:u}}case"remove":case"purge":{if(c.length===0)return{stderr:"apt: no packages specified",exitCode:1};let{output:l,exitCode:u}=r.remove(c,{purge:s==="purge"||a,quiet:o});return{stdout:l||void 0,exitCode:u}}case"update":return{stdout:["Hit:1 fortune://packages.fortune.local aurora InRelease","Hit:2 fortune://security.fortune.local aurora-security InRelease","Reading package lists... Done","Building dependency tree... Done","Reading state information... Done","All packages are up to date."].join(`
|
|
29
29
|
`),exitCode:0};case"upgrade":return{stdout:["Reading package lists... Done","Building dependency tree... Done","Reading state information... Done","Calculating upgrade... Done","0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded."].join(`
|
|
30
30
|
`),exitCode:0};case"search":{let l=c[0];if(!l)return{stderr:"apt: search requires a term",exitCode:1};let u=r.search(l);return u.length===0?{stdout:`Sorting... Done
|
|
@@ -103,8 +103,8 @@ Hint: install it with: apt install gzip
|
|
|
103
103
|
`:"")};if(c.length===0)return{stdout:f(s??""),exitCode:0};let l=[];for(let u of c){let h=le(e,u);try{Oe(n,h,"head"),l.push(f(t.vfs.readFile(h)))}catch{return{stderr:`head: ${u}: No such file or directory`,exitCode:1}}}return{stdout:l.join(`
|
|
104
104
|
`),exitCode:0}}};var Lu=["navigation","files","text","archive","system","package","network","shell","users","misc"],Du={navigation:"Navigation",files:"Files & Filesystem",text:"Text Processing",archive:"Archive & Compression",system:"System",package:"Package Management",network:"Network",shell:"Shell & Scripting",users:"Users & Permissions",misc:"Miscellaneous"},$u="\x1B[1m",Sn="\x1B[0m",nA="\x1B[36m",rA="\x1B[33m",Ri="\x1B[2m",iA="\x1B[32m";function Uu(n,t){return n.length>=t?n:n+" ".repeat(t-n.length)}function sA(n){let t=n.aliases?.length?` ${Ri}(${n.aliases.join(", ")})${Sn}`:"";return` ${nA}${Uu(n.name,16)}${Sn}${t}${Uu("",(n.aliases?.length,0))} ${n.description??""}`}function oA(n){let t={};for(let i of n){let o=i.category??"misc";t[o]||(t[o]=[]),t[o].push(i)}let e=[`${$u}Available commands${Sn}`,`${Ri}Type 'help <command>' for detailed usage.${Sn}`,""],r=[...Lu.filter(i=>t[i]),...Object.keys(t).filter(i=>!Lu.includes(i)).sort()];for(let i of r){let o=t[i];if(!o?.length)continue;e.push(`${rA}${Du[i]??i}${Sn}`);let a=[...o].sort((c,f)=>c.name.localeCompare(f.name));for(let c of a)e.push(sA(c));e.push("")}let s=n.length;return e.push(`${Ri}${s} commands available.${Sn}`),e.join(`
|
|
105
105
|
`)}function aA(n){let t=[];if(t.push(`${$u}${n.name}${Sn} \u2014 ${n.description??"no description"}`),n.aliases?.length&&t.push(`${Ri}Aliases: ${n.aliases.join(", ")}${Sn}`),t.push(""),t.push(`${iA}Usage:${Sn}`),n.params.length)for(let r of n.params)t.push(` ${n.name} ${r}`);else t.push(` ${n.name}`);let e=Du[n.category??"misc"]??n.category??"misc";return t.push(""),t.push(`${Ri}Category: ${e}${Sn}`),t.join(`
|
|
106
|
-
`)}function Ou(n){return{name:"help",description:"List all commands, or show usage for a specific command",category:"shell",params:["[command]"],run:({args:t})=>{let e=ja();if(t[0]){let r=t[0].toLowerCase(),s=e.find(i=>i.name===r||i.aliases?.includes(r));return s?{stdout:aA(s),exitCode:0}:{stderr:`help: no help entry for '${t[0]}'`,exitCode:1}}return{stdout:oA(e),exitCode:0}}}}var Mu={name:"history",description:"Display command history",category:"shell",params:["[n]"],run:({args:n,shell:t})=>{let e
|
|
107
|
-
`).filter(Boolean),
|
|
106
|
+
`)}function Ou(n){return{name:"help",description:"List all commands, or show usage for a specific command",category:"shell",params:["[command]"],run:({args:t})=>{let e=ja();if(t[0]){let r=t[0].toLowerCase(),s=e.find(i=>i.name===r||i.aliases?.includes(r));return s?{stdout:aA(s),exitCode:0}:{stderr:`help: no help entry for '${t[0]}'`,exitCode:1}}return{stdout:oA(e),exitCode:0}}}}var Mu={name:"history",description:"Display command history",category:"shell",params:["[n]"],run:({args:n,shell:t,authUser:e})=>{let r=`/home/${e}/.bash_history`;if(!t.vfs.exists(r))return{stdout:"",exitCode:0};let i=t.vfs.readFile(r).split(`
|
|
107
|
+
`).filter(Boolean),o=n[0],a=o?parseInt(o,10):null,c=a&&!Number.isNaN(a)?i.slice(-a):i,f=i.length-c.length+1;return{stdout:c.map((u,h)=>`${String(f+h).padStart(5)} ${u}`).join(`
|
|
108
108
|
`),exitCode:0}}};var Fu={name:"hostname",description:"Print hostname",category:"system",params:[],run:({hostname:n})=>({stdout:n,exitCode:0})};var Hu={name:"htop",description:"System monitor",category:"system",params:[],run:({mode:n})=>n==="exec"?{stderr:"htop: interactive terminal required",exitCode:1}:{openHtop:!0,exitCode:0}};var Qu={name:"id",description:"Print user identity",category:"system",params:["[user]"],run:({authUser:n,shell:t,args:e})=>{let r=e[0]??n,s=r==="root"?0:1e3,i=s,a=t.users.isSudoer(r)?`${i}(${r}),0(root)`:`${i}(${r})`;return{stdout:`uid=${s}(${r}) gid=${i}(${r}) groups=${a}`,exitCode:0}}};var Wu={name:"kill",description:"Send signal to process",category:"system",params:["[-9] <pid>"],run:({args:n})=>n.find(e=>!e.startsWith("-"))?{stdout:"",exitCode:0}:{stderr:"kill: no pid specified",exitCode:1}};var qu={name:"ln",description:"Create links",category:"files",params:["[-s] <target> <link_name>"],run:({authUser:n,shell:t,cwd:e,args:r})=>{let s=se(r,["-s","--symbolic"]),i=r.filter(l=>!l.startsWith("-")),[o,a]=i;if(!o||!a)return{stderr:"ln: missing operand",exitCode:1};let c=le(e,a),f=s?o:le(e,o);try{if(Oe(n,c,"ln"),s)t.vfs.symlink(f,c);else{let l=le(e,o);if(Oe(n,l,"ln"),!t.vfs.exists(l))return{stderr:`ln: ${o}: No such file or directory`,exitCode:1};let u=t.vfs.readFile(l);t.writeFileAsUser(n,c,u)}return{exitCode:0}}catch(l){return{stderr:`ln: ${l instanceof Error?l.message:String(l)}`,exitCode:1}}}},Ku={name:"readlink",description:"Print resolved path of symbolic link",category:"files",params:["[-f] <path>"],run:({shell:n,cwd:t,args:e})=>{let r=e.includes("-f")||e.includes("-e"),s=e.find(a=>!a.startsWith("-"));if(!s)return{stderr:`readlink: missing operand
|
|
109
109
|
`,exitCode:1};let i=le(t,s);return n.vfs.exists(i)?n.vfs.isSymlink(i)?{stdout:`${n.vfs.resolveSymlink(i)}
|
|
110
110
|
`,exitCode:0}:{stderr:`readlink: ${s}: not a symbolic link
|
|
@@ -381,7 +381,7 @@ echo 'neofetch: virtual stub'
|
|
|
381
381
|
`),exitCode:0}}remove(t,e={}){let r=[],s=[];for(let i of t){let o=this.installed.get(i.toLowerCase());o?s.push(o):r.push(`Package '${i}' is not installed, so not removed`)}if(s.length===0)return{output:r.join(`
|
|
382
382
|
`)||"Nothing to remove.",exitCode:0};e.quiet||r.push("Reading package lists... Done","Building dependency tree... Done","The following packages will be REMOVED:",` ${s.map(i=>i.name).join(" ")}`,`0 upgraded, 0 newly installed, ${s.length} to remove and 0 not upgraded.`);for(let i of s){e.quiet||r.push(`Removing ${i.name} (${i.version}) ...`);for(let a of i.files)if(!(!e.purge&&(a.startsWith("/etc/")||a.endsWith(".conf"))))try{this.vfs.exists(a)&&this.vfs.remove(a)}catch{}this.findInRegistry(i.name)?.onRemove?.(this.vfs),this.installed.delete(i.name),this.log(`remove ${i.name} ${i.version}`)}return this.aptLog("remove",s.map(i=>i.name)),this.persist(),{output:r.join(`
|
|
383
383
|
`),exitCode:0}}search(t){let e=t.toLowerCase();return dc.filter(r=>r.name.includes(e)||r.description.toLowerCase().includes(e)||(r.shortDesc??"").toLowerCase().includes(e)).sort((r,s)=>r.name.localeCompare(s.name))}show(t){let e=this.findInRegistry(t);if(!e)return null;let r=this.installed.get(t);return[`Package: ${e.name}`,`Version: ${e.version}`,`Architecture: ${e.architecture??"amd64"}`,`Maintainer: ${e.maintainer??"Fortune Maintainers <pkg@fortune.local>"}`,`Installed-Size: ${e.installedSizeKb??0}`,`Depends: ${(e.depends??[]).join(", ")||"(none)"}`,`Section: ${e.section??"misc"}`,"Priority: optional",`Description: ${e.description}`,`Status: ${r?"install ok installed":"install ok not-installed"}`].join(`
|
|
384
|
-
`)}};var An=require("node:crypto"),xh=require("node:events"),Sh=Vt(require("node:path"),1);function sE(){let n=process.env.SSH_MIMIC_FAST_PASSWORD_HASH;return!!n&&!["0","false","no","off"].includes(n.toLowerCase())}var Rt=Jn("VirtualUserManager"),Xs=class n extends xh.EventEmitter{constructor(e,r=!0){super();this.vfs=e;this.autoSudoForNewUsers=r;Rt.mark("constructor")}vfs;autoSudoForNewUsers;static recordCache=new Map;static fastPasswordHash=sE();usersPath="/
|
|
384
|
+
`)}};var An=require("node:crypto"),xh=require("node:events"),Sh=Vt(require("node:path"),1);function sE(){let n=process.env.SSH_MIMIC_FAST_PASSWORD_HASH;return!!n&&!["0","false","no","off"].includes(n.toLowerCase())}var Rt=Jn("VirtualUserManager"),Xs=class n extends xh.EventEmitter{constructor(e,r=!0){super();this.vfs=e;this.autoSudoForNewUsers=r;Rt.mark("constructor")}vfs;autoSudoForNewUsers;static recordCache=new Map;static fastPasswordHash=sE();usersPath="/etc/htpasswd";sudoersPath="/etc/sudoers";quotasPath="/etc/quotas";authDirPath="/.virtual-env-js/.auth";users=new Map;sudoers=new Set;quotas=new Map;activeSessions=new Map;nextTty=0;async initialize(){Rt.mark("initialize"),this.loadFromVfs(),this.loadSudoersFromVfs(),this.loadQuotasFromVfs();let e=!1;this.users.has("root")||(this.users.set("root",this.createRecord("root","")),e=!0),this.sudoers.add("root"),e&&await this.persist(),this.emit("initialized")}async setQuotaBytes(e,r){if(Rt.mark("setQuotaBytes"),this.validateUsername(e),!this.users.has(e))throw new Error(`quota: user '${e}' does not exist`);if(!Number.isFinite(r)||r<0)throw new Error("quota: maxBytes must be a non-negative number");this.quotas.set(e,Math.floor(r)),await this.persist()}async clearQuota(e){Rt.mark("clearQuota"),this.validateUsername(e),this.quotas.delete(e),await this.persist()}getQuotaBytes(e){return Rt.mark("getQuotaBytes"),this.quotas.get(e)??null}getUsageBytes(e){Rt.mark("getUsageBytes");let r=`/home/${e}`;return this.vfs.exists(r)?this.vfs.getUsageBytes(r):0}assertWriteWithinQuota(e,r,s){Rt.mark("assertWriteWithinQuota");let i=this.quotas.get(e);if(i===void 0)return;let o=bh(r),a=bh(`/home/${e}`);if(!(o===a||o.startsWith(`${a}/`)))return;let f=this.getUsageBytes(e),l=0;if(this.vfs.exists(o)){let p=this.vfs.stat(o);p.type==="file"&&(l=p.size)}let u=Buffer.isBuffer(s)?s.length:Buffer.byteLength(s,"utf8"),h=f-l+u;if(h>i)throw new Error(`quota exceeded for '${e}': ${h}/${i} bytes`)}verifyPassword(e,r){Rt.mark("verifyPassword");let s=this.users.get(e);if(!s)return this.hashPassword(r,""),!1;let i=this.hashPassword(r,s.salt),o=s.passwordHash;try{let a=Buffer.from(i,"hex"),c=Buffer.from(o,"hex");return a.length!==c.length?!1:(0,An.timingSafeEqual)(a,c)}catch{return i===o}}async addUser(e,r){if(Rt.mark("addUser"),this.validateUsername(e),this.validatePassword(r),this.users.has(e))return;this.users.set(e,this.createRecord(e,r)),this.autoSudoForNewUsers&&this.sudoers.add(e);let s=`/home/${e}`;this.vfs.exists(s)||(this.vfs.mkdir(s,493),this.vfs.writeFile(`${s}/README.txt`,`Welcome to the virtual environment, ${e}`)),await this.persist(),this.emit("user:add",{username:e})}getPasswordHash(e){Rt.mark("getPasswordHash");let r=this.users.get(e);return r?r.passwordHash:null}async setPassword(e,r){if(Rt.mark("setPassword"),this.validateUsername(e),this.validatePassword(r),!this.users.has(e))throw new Error(`passwd: user '${e}' does not exist`);this.users.set(e,this.createRecord(e,r)),await this.persist()}async deleteUser(e){if(Rt.mark("deleteUser"),this.validateUsername(e),e==="root")throw new Error("deluser: cannot delete root");if(!this.users.delete(e))throw new Error(`deluser: user '${e}' does not exist`);this.sudoers.delete(e),this.emit("user:delete",{username:e}),await this.persist()}isSudoer(e){return Rt.mark("isSudoer"),this.sudoers.has(e)}async addSudoer(e){if(Rt.mark("addSudoer"),this.validateUsername(e),!this.users.has(e))throw new Error(`sudoers: user '${e}' does not exist`);this.sudoers.add(e),await this.persist()}async removeSudoer(e){if(Rt.mark("removeSudoer"),this.validateUsername(e),e==="root")throw new Error("sudoers: cannot remove root");this.sudoers.delete(e),await this.persist()}registerSession(e,r){Rt.mark("registerSession");let s={id:(0,An.randomUUID)(),username:e,tty:`pts/${this.nextTty++}`,remoteAddress:r,startedAt:new Date().toISOString()};return this.activeSessions.set(s.id,s),this.emit("session:register",{sessionId:s.id,username:e,remoteAddress:r}),s}unregisterSession(e){if(Rt.mark("unregisterSession"),!e)return;let r=this.activeSessions.get(e);this.activeSessions.delete(e),r&&this.emit("session:unregister",{sessionId:e,username:r.username}),this.activeSessions.delete(e)}updateSession(e,r,s){if(Rt.mark("updateSession"),!e)return;let i=this.activeSessions.get(e);i&&this.activeSessions.set(e,{...i,username:r,remoteAddress:s})}listActiveSessions(){return Rt.mark("listActiveSessions"),Array.from(this.activeSessions.values()).sort((e,r)=>e.startedAt.localeCompare(r.startedAt))}listUsers(){return Array.from(this.users.keys()).sort()}loadFromVfs(){if(this.users.clear(),!this.vfs.exists(this.usersPath))return;let e=this.vfs.readFile(this.usersPath);for(let r of e.split(`
|
|
385
385
|
`)){let s=r.trim();if(s.length===0)continue;let i=s.split(":");if(i.length<3)continue;let[o,a,c]=i;!o||!a||!c||this.users.set(o,{username:o,salt:a,passwordHash:c})}}loadSudoersFromVfs(){if(this.sudoers.clear(),!this.vfs.exists(this.sudoersPath))return;let e=this.vfs.readFile(this.sudoersPath);for(let r of e.split(`
|
|
386
386
|
`)){let s=r.trim();s.length>0&&this.sudoers.add(s)}}loadQuotasFromVfs(){if(this.quotas.clear(),!this.vfs.exists(this.quotasPath))return;let e=this.vfs.readFile(this.quotasPath);for(let r of e.split(`
|
|
387
387
|
`)){let s=r.trim();if(s.length===0)continue;let[i,o]=s.split(":"),a=Number.parseInt(o??"",10);!i||!Number.isFinite(a)||a<0||this.quotas.set(i,a)}}async persist(){this.vfs.exists(this.authDirPath)||this.vfs.mkdir(this.authDirPath,448);let e=Array.from(this.users.values()).sort((o,a)=>o.username.localeCompare(a.username)).map(o=>[o.username,o.salt,o.passwordHash].join(":")).join(`
|
|
@@ -394,7 +394,7 @@ echo 'neofetch: virtual stub'
|
|
|
394
394
|
`).replace(/\r/g,`
|
|
395
395
|
`).replace(/\n/g,`\r
|
|
396
396
|
`)}function Ih(n,t){let e=Number.isFinite(t.cols)&&t.cols>0?Math.floor(t.cols):80,r=Number.isFinite(t.rows)&&t.rows>0?Math.floor(t.rows):24;return`stty cols ${e} rows ${r} 2>/dev/null; ${n}`}function Ch(n,t){return!t||t.trim()===""||t==="."?n:t.startsWith("/")?Js.posix.normalize(t):Js.posix.normalize(Js.posix.join(n,t))}async function kh(n){try{let e=(await(0,wh.readFile)(`/proc/${n}/task/${n}/children`,"utf8")).trim().split(/\s+/).filter(Boolean).map(s=>Number.parseInt(s,10)).filter(s=>Number.isInteger(s)&&s>0),r=await Promise.all(e.map(s=>kh(s)));return[...e,...r.flat()]}catch{return[]}}async function vh(n=process.pid){let t=await kh(n),e=Array.from(new Set(t)).sort((r,s)=>r-s);return e.length===0?null:e.join(",")}function Rh(n,t,e){let r=Ih(n,t),s=(0,Bh.spawn)("script",["-qfec",r,"/dev/null"],{stdio:["pipe","pipe","pipe"],env:{...process.env,TERM:process.env.TERM??"xterm-256color"}});return s.stdout.on("data",i=>{e.write(i.toString("utf8"))}),s.stderr.on("data",i=>{e.write(i.toString("utf8"))}),s}function Nh(n,t,e){return Rh(`nano -- ${hc(n)}`,t,e)}function Ph(n,t,e){return Rh(`htop -p ${hc(n)}`,t,e)}function Th(n,t,e){let r=[`Linux ${n} ${t.kernel} ${t.arch}`,"","The programs included with the Fortune GNU/Linux system are free software;","the exact distribution terms for each program are described in the","individual files in /usr/share/doc/*/copyright.","","Fortune GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent","permitted by applicable law."];if(e){let s=new Date(e.at),i=Number.isNaN(s.getTime())?e.at:Vs(s);r.push(`Last login: ${i} from ${e.from||"unknown"}`)}return r.push(""),`${r.map(s=>`${s}\r
|
|
397
|
-
`).join("")}`}function Lh(n,t,e){let r=n==="root",s=r?"\x1B[31;1m":"\x1B[35;1m",i="\x1B[37;1m",o="\x1B[34;1m",a="\x1B[0m";return`${i}[${s}${n}${i}@${o}${t}${a} ${e}${i}]${a}${r?"#":"$"} `}function Uh(n,t,e,r,s,i="unknown",o={cols:80,rows:24},a){let c="",f=0,l=oE(a.vfs),u=null,h="",p=`/home/${e}`,y=Kr(e,r),E=null,x=null,k=()=>{let B=`/home/${e}`,T=p===B?"~":pc.posix.basename(p)||"/";return Lh(e,r,T)},D=Array.from(new Set(nc())).sort();console.log(`[${s}] Shell started for user '${e}' at ${i}`),(async()=>{let B=`/home/${e}/.bashrc`;if(a.vfs.exists(B))try{let T=a.vfs.readFile(B);for(let Q of T.split(`
|
|
397
|
+
`).join("")}`}function Lh(n,t,e){let r=n==="root",s=r?"\x1B[31;1m":"\x1B[35;1m",i="\x1B[37;1m",o="\x1B[34;1m",a="\x1B[0m";return`${i}[${s}${n}${i}@${o}${t}${a} ${e}${i}]${a}${r?"#":"$"} `}function Uh(n,t,e,r,s,i="unknown",o={cols:80,rows:24},a){let c="",f=0,l=oE(a.vfs,e),u=null,h="",p=`/home/${e}`,y=Kr(e,r),E=null,x=null,k=()=>{let B=`/home/${e}`,T=p===B?"~":pc.posix.basename(p)||"/";return Lh(e,r,T)},D=Array.from(new Set(nc())).sort();console.log(`[${s}] Shell started for user '${e}' at ${i}`),(async()=>{let B=`/home/${e}/.bashrc`;if(a.vfs.exists(B))try{let T=a.vfs.readFile(B);for(let Q of T.split(`
|
|
398
398
|
`)){let ee=Q.trim();!ee||ee.startsWith("#")||await bt(ee,e,r,"shell",p,a,void 0,y)}}catch{}})();function H(){let B=k();t.write(`\r${B}${c}\x1B[K`);let T=c.length-f;T>0&&t.write(`\x1B[${T}D`)}function C(){t.write("\r\x1B[K")}function F(B){x={...B,buffer:""},C(),t.write(B.prompt)}async function V(B){if(!x)return;let T=x;if(x=null,!B){t.write(`\r
|
|
399
399
|
Sorry, try again.\r
|
|
400
400
|
`),H();return}if(!T.commandLine){e=T.targetUser,T.loginShell&&(p=`/home/${e}`),a.users.updateSession(s,e,i),t.write(`\r
|
|
@@ -409,7 +409,7 @@ Sorry, try again.\r
|
|
|
409
409
|
`),t.write(`${Ye.join(" ")}\r
|
|
410
410
|
`),H()}}function Y(B){if(B.length===0)return;l.push(B),l.length>500&&(l=l.slice(l.length-500));let T=l.length>0?`${l.join(`
|
|
411
411
|
`)}
|
|
412
|
-
`:"";a.vfs.writeFile(
|
|
412
|
+
`:"";a.vfs.writeFile(`/home/${e}/.bash_history`,T)}function te(){let B=`/home/${e}/.lastlog.json`;if(!a.vfs.exists(B))return null;try{return JSON.parse(a.vfs.readFile(B))}catch{return null}}function ce(B){let T=`/home/${e}/.lastlog`;a.vfs.exists(T)||a.vfs.mkdir(T,448);let Q=`${T}/${e}.json`;a.vfs.writeFile(Q,JSON.stringify({at:B,from:i}))}function $(){let B=te(),T=new Date().toISOString();t.write(Th(r,n,B)),ce(T)}$(),H(),t.on("data",async B=>{if(E){E.process.stdin.write(B);return}if(x){let Q=B.toString("utf8");for(let ee=0;ee<Q.length;ee+=1){let fe=Q[ee];if(fe===""){x=null,t.write(`^C\r
|
|
413
413
|
`),H();return}if(fe==="\x7F"||fe==="\b"){x.buffer=x.buffer.slice(0,-1);continue}if(fe==="\r"||fe===`
|
|
414
414
|
`){let ie=x.buffer;if(x.buffer="",x.onPassword){let{result:ct,nextPrompt:tn}=await x.onPassword(ie,a);t.write(`\r
|
|
415
415
|
`),ct!==null?(x=null,ct.stdout&&t.write(ct.stdout.replace(/\n/g,`\r
|
|
@@ -422,8 +422,8 @@ Sorry, try again.\r
|
|
|
422
422
|
`),fe.length>0){let ie=await Promise.resolve(bt(fe,e,r,"shell",p,a,void 0,y));if(Y(fe),ie.openEditor){await b(ie.openEditor.targetPath,ie.openEditor.initialContent,ie.openEditor.tempPath);return}if(ie.openHtop){await S();return}if(ie.sudoChallenge){F(ie.sudoChallenge);return}if(ie.clearScreen&&t.write("\x1B[2J\x1B[H"),ie.stdout&&t.write(`${$i(ie.stdout)}\r
|
|
423
423
|
`),ie.stderr&&t.write(`${$i(ie.stderr)}\r
|
|
424
424
|
`),ie.closeSession){t.write(`logout\r
|
|
425
|
-
`),t.exit(ie.exitCode??0),t.end();return}ie.nextCwd&&(p=ie.nextCwd),ie.switchUser&&(e=ie.switchUser,p=ie.nextCwd??`/home/${e}`,a.users.updateSession(s,e,i),c="",f=0),await a.vfs.flushMirror()}H();continue}if(ee==="\x7F"||ee==="\b"){f>0&&(c=`${c.slice(0,f-1)}${c.slice(f)}`,f-=1,H());continue}v(ee)}}),t.on("close",()=>{E&&(E.process.kill("SIGTERM"),E=null)})}function oE(n){let t
|
|
426
|
-
`).map(
|
|
425
|
+
`),t.exit(ie.exitCode??0),t.end();return}ie.nextCwd&&(p=ie.nextCwd),ie.switchUser&&(e=ie.switchUser,p=ie.nextCwd??`/home/${e}`,a.users.updateSession(s,e,i),c="",f=0),await a.vfs.flushMirror()}H();continue}if(ee==="\x7F"||ee==="\b"){f>0&&(c=`${c.slice(0,f-1)}${c.slice(f)}`,f-=1,H());continue}v(ee)}}),t.on("close",()=>{E&&(E.process.kill("SIGTERM"),E=null)})}function oE(n,t){let e=`/home/${t}/.bash_history`;return n.exists(e)?n.readFile(e).split(`
|
|
426
|
+
`).map(s=>s.trim()).filter(s=>s.length>0):(n.writeFile(e,""),[])}function aE(n){return typeof n=="object"&&n!==null&&"vfsInstance"in n&&$h(n.vfsInstance)}function $h(n){if(typeof n!="object"||n===null)return!1;let t=n;return typeof t.restoreMirror=="function"&&typeof t.flushMirror=="function"&&typeof t.writeFile=="function"&&typeof t.readFile=="function"&&typeof t.mkdir=="function"&&typeof t.exists=="function"&&typeof t.stat=="function"&&typeof t.list=="function"&&typeof t.remove=="function"&&typeof t.copy=="function"&&typeof t.move=="function"&&typeof t.touch=="function"}var cE={kernel:"1.0.0+itsrealfortune+1-amd64",os:"Fortune GNU/Linux x64",arch:"x86_64"},Oi=Jn("VirtualShell");function fE(){let n=process.env.SSH_MIMIC_AUTO_SUDO_NEW_USERS;return n?!["0","false","no","off"].includes(n.toLowerCase()):!0}var hr=class extends Dh.EventEmitter{vfs;users;packageManager;hostname;properties;startTime;initialized;constructor(t,e,r){super(),Oi.mark("constructor"),this.hostname=t,this.properties=e||cE,this.startTime=Date.now(),$h(r)?this.vfs=r:aE(r)?this.vfs=r.vfsInstance:this.vfs=new yh(r??{}),this.users=new Xs(this.vfs,fE()),this.packageManager=new js(this.vfs,this.users);let s=this.vfs,i=this.users,o=this.packageManager,a=this.properties,c=this.hostname,f=this.startTime;this.initialized=(async()=>{await s.restoreMirror(),await i.initialize(),fh(s,i,c,a,f),o.load(),this.emit("initialized")})()}async ensureInitialized(){Oi.mark("ensureInitialized"),await this.initialized}addCommand(t,e,r){let s=t.trim().toLowerCase();if(s.length===0||/\s/.test(s))throw new Error("Command name must be non-empty and contain no spaces");ec(tc(s,e,r))}executeCommand(t,e,r){Oi.mark("executeCommand"),bt(t,e,this.hostname,"shell",r,this),this.emit("command",{command:t,user:e,cwd:r})}startInteractiveSession(t,e,r,s,i){Oi.mark("startInteractiveSession"),this.emit("session:start",{user:e,sessionId:r,remoteAddress:s}),Uh(this.properties,t,e,this.hostname,r,s,i,this),this.refreshProcSessions()}refreshProcFs(){zs(this.vfs,this.properties,this.hostname,this.startTime,this.users.listActiveSessions())}refreshProcSessions(){zs(this.vfs,this.properties,this.hostname,this.startTime,this.users.listActiveSessions())}syncPasswd(){ac(this.vfs,this.users)}getVfs(){return this?.vfs??null}getUsers(){return this?.users??null}getHostname(){return this?.hostname}writeFileAsUser(t,e,r){Oi.mark("writeFileAsUser"),this.users.assertWriteWithinQuota(t,e,r),this.vfs.writeFile(e,r)}};function Oh(n){return n.replace(/\r\n/g,`
|
|
427
427
|
`).replace(/\r/g,`
|
|
428
428
|
`).replace(/\n/g,`\r
|
|
429
429
|
`)}function Mh(n,t,e,r,s){Promise.resolve(bt(t,e,r,"exec",`/home/${e}`,s,void 0,Kr(e,r))).then(i=>{i.stdout&&n.write(`${Oh(i.stdout)}\r
|