UNPKG

@fntio/infra-client

Version:

Self-hosted AWS access portal: credential vending, target-account trust, and grant-driven private networking as CDK stacks and constructs.

2 lines (1 loc) • 6.66 kB
import{a as p}from"./chunk-BG475WXG.js";import{c as f}from"./chunk-ZRDXKVPG.js";import{F as g,I as u,R as i,i as d,qb as a}from"./chunk-FYA6QUPZ.js";import{e as o}from"./chunk-WFIHWVQO.js";var l="network:autoReconnect",y="network:firstConnectShown",h="network:lastSite",m=class c{bridge=u(f);api=u(p);enabled=i(!1);busyOp=i(null);daemon=i("unknown");installed=i(!0);foreignTailnet=i(!1);controlUrl=i(null);tailnetIp=i(null);lastError=i(null);autoReconnect=i(localStorage.getItem(l)==="1");setupOpen=i(!1);sites=i([]);activeSiteId=i(null);pendingSiteId=i(null);siteName(e){return e?this.sites().find(t=>t.id===e)?.displayName||e:""}siteOfAccount=a(()=>{let e=new Map;for(let t of this.sites())for(let n of t.accounts)e.set(n,t.id);return e});isAccountOnActiveSite(e){if(this.sites().length<=1)return!0;let t=this.siteOfAccount().get(e);if(!t)return!0;let n=this.activeSiteId();return n===null||n===t}setupReason=a(()=>this.installed()?this.daemon()==="daemon-off"?"daemon-off":null:"not-installed");state=a(()=>!this.bridge.isDesktop||!this.bridge.networkSupported||!this.enabled()?"hidden":this.busyOp()==="connect"?"connecting":this.busyOp()==="disconnect"?"disconnecting":this.setupReason()?"setup":this.daemon()==="connected"&&!this.foreignTailnet()?"connected":"off");controlHost=a(()=>{let t=this.sites().find(n=>n.id===this.activeSiteId())?.controlUrl??this.controlUrl();try{return t?new URL(t).host:null}catch{return null}});shellSiteAware=i(!1);leftover=i(!1);init(){!this.bridge.isDesktop||!this.bridge.networkSupported||(this.bridge.onNetworkChanged(()=>{this.refresh()}),this.bridge.onTrayConnect(e=>{e&&!(this.state()==="connected"&&this.activeSiteId()===e)?this.connect({userInitiated:!0,siteId:e}):!e&&this.state()!=="connected"&&this.connect({userInitiated:!0})}),this.tryInfo(3))}tryInfo(e){this.api.info().subscribe({next:t=>o(this,null,function*(){this.enabled.set(t.enabled),this.controlUrl.set(t.controlUrl),this.sites.set(t.sites??[]),t.enabled&&(yield this.hydratePrefs(),yield this.refresh(),this.autoReconnect()&&this.state()==="off"&&!this.foreignTailnet()?this.connect({userInitiated:!1}):!this.autoReconnect()&&this.state()==="connected"&&this.leftover()&&(yield this.disconnect()))}),error:()=>{e>1?setTimeout(()=>this.tryInfo(e-1),3e3):this.enabled.set(!1)}})}hydratePrefs(){return o(this,null,function*(){try{let e=yield d(this.api.networkPrefs());this.autoReconnect.set(e.autoReconnect),e.autoReconnect?localStorage.setItem(l,"1"):localStorage.removeItem(l),e.lastSiteId&&localStorage.setItem(h,e.lastSiteId)}catch{}})}refresh(){return o(this,null,function*(){try{let e=yield this.bridge.networkStatus();this.installed.set(e.installed),this.daemon.set(e.state),this.tailnetIp.set(e.self?.ips?.[0]??null),this.shellSiteAware.set("activeSiteId"in e),this.foreignTailnet.set(e.foreignTailnet===!0),this.leftover.set(e.ownedThisRun===!1),this.activeSiteId.set(e.activeSiteId??(!e.foreignTailnet&&this.sites().length===1?this.sites()[0].id:null))}catch{this.daemon.set("unknown"),this.foreignTailnet.set(!1)}})}defaultSiteId(){let e=this.sites();if(!e.length)return;let t=localStorage.getItem(h);return e.some(n=>n.id===t)?t:e[0].id}connect(e){if(this.busyOp())return;this.busyOp.set("connect"),this.lastError.set(null);let t=e?.siteId??this.defaultSiteId();this.pendingSiteId.set(t??null),this.doConnect(e?.userInitiated??!0,t).finally(()=>{this.busyOp.set(null),this.pendingSiteId.set(null)})}switchTo(e){this.state()==="connected"&&this.activeSiteId()===e||this.connect({siteId:e})}doConnect(e,t){return o(this,null,function*(){if(t&&this.sites().length>1&&t!==this.sites()[0].id&&!this.shellSiteAware()){this.lastError.set("Switching networks needs the updated desktop app \u2014 quit it via the tray icon and reopen, then try again.");return}try{let n=yield this.attempt(t);if(!n.ok&&n.code==="key-rejected"&&(n=yield this.attempt(t)),n.ok)t&&(localStorage.setItem(h,t),this.api.saveNetworkPrefs({lastSiteId:t}).subscribe({error:()=>{}})),yield this.awaitRunning(),e&&this.daemon()==="connected"&&!localStorage.getItem(y)&&(localStorage.setItem(y,"1"),this.setupOpen.set(!0));else{let s=n.code??n.error,r=n.detail??(n.code?void 0:n.error);r&&console.warn("network connect failed:",r),this.lastError.set(this.friendly(s)),e&&(s==="not-installed"||s==="daemon-not-running")&&this.setupOpen.set(!0)}yield this.refresh()}catch(n){this.lastError.set(n?.status===501?"Network access is not enabled on this deployment.":"Could not get a network key. Try again.")}})}attempt(e){return o(this,null,function*(){let t=s=>this.bridge.networkConnect(s,e).catch(r=>({ok:!1,code:"cli-error",detail:String(r?.message??r)}));if(this.bridge.networkLazyKey){let s=yield t("");if(s.code!=="needs-key")return s}let{key:n}=yield d(this.api.mintDeviceKey(e));return t(n)})}awaitRunning(e=15e3){return o(this,null,function*(){let t=Date.now()+e;for(;;){if(yield this.refresh(),this.daemon()==="connected")return;if(Date.now()>=t){this.lastError.set("Connected the client, but the network hasn\u2019t confirmed yet \u2014 give it a few seconds and check the pill.");return}yield new Promise(n=>setTimeout(n,1e3))}})}disconnect(){return o(this,null,function*(){this.busyOp()||(this.busyOp.set("disconnect"),yield this.bridge.networkDisconnect().catch(()=>{}),yield this.refresh(),this.busyOp.set(null))})}setAutoReconnect(e){this.autoReconnect.set(e),e?localStorage.setItem(l,"1"):localStorage.removeItem(l),this.api.saveNetworkPrefs({autoReconnect:e}).subscribe({error:()=>{}})}friendly(e){switch(e){case"not-installed":return"Tailscale isn\u2019t installed on this machine.";case"daemon-not-running":return"Tailscale is installed but not running. Open the Tailscale app, then try again.";case"key-rejected":return"The network rejected the connection key, even after retrying with a fresh one. Wait a minute and try again \u2014 if it keeps happening, tell your admin.";case"timeout":return"Connecting took too long. Check your internet connection and try again.";case"cli-error":return"Couldn\u2019t reach the private network. Make sure Tailscale is running, then try again \u2014 if it keeps happening, tell your admin.";case"bad-key":return"Could not get a valid network key. Try again.";case"no-network-config":return"This backend doesn\u2019t offer network access.";case"unknown-site":return"The desktop app doesn\u2019t know that network yet \u2014 quit it via the tray icon and relaunch, then try again.";case"insecure-control-url":return"Refused: control plane is not HTTPS.";default:return"Could not connect."}}static \u0275fac=function(t){return new(t||c)};static \u0275prov=g({token:c,factory:c.\u0275fac,providedIn:"root"})};export{m as a};