UNPKG

code-server

Version:

Run VS Code on a remote server.

4 lines (3 loc) 7.29 kB
"use strict";var I=Object.create;var l=Object.defineProperty;var A=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var E=Object.getPrototypeOf,R=Object.prototype.hasOwnProperty;var _=(r,t)=>{for(var e in t)l(r,e,{get:t[e],enumerable:!0})},b=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of x(t))!R.call(r,n)&&n!==e&&l(r,n,{get:()=>t[n],enumerable:!(s=A(t,n))||s.enumerable});return r};var w=(r,t,e)=>(e=r!=null?I(E(r)):{},b(t||!r||!r.__esModule?l(e,"default",{value:r,enumerable:!0}):e,r)),k=r=>b(l({},"__esModule",{value:!0}),r);var O={};_(O,{TunnelPrivacyId:()=>T,activate:()=>N,deactivate:()=>L});module.exports=k(O);var y=require("child_process"),h=w(require("path")),o=w(require("vscode"));var u=class{completeCallback;errorCallback;outcome;get isRejected(){return this.outcome?.outcome===1}get isResolved(){return this.outcome?.outcome===0}get isSettled(){return!!this.outcome}get value(){return this.outcome?.outcome===0?this.outcome?.value:void 0}p;constructor(){this.p=new Promise((t,e)=>{this.completeCallback=t,this.errorCallback=e})}complete(t){return new Promise(e=>{this.completeCallback(t),this.outcome={outcome:0,value:t},e()})}error(t){return new Promise(e=>{this.errorCallback(t),this.outcome={outcome:1,value:t},e()})}};var P=require("stream"),p=()=>new d(10),d=class extends P.Transform{constructor(e){super();this.splitter=e}buffer;_transform(e,s,n){this.buffer?this.buffer=Buffer.concat([this.buffer,e]):this.buffer=e;let a=0;for(;a<this.buffer.length;){let i=this.buffer.indexOf(this.splitter,a);if(i===-1)break;this.push(this.buffer.subarray(a,i)),a=i+1}this.buffer=a===this.buffer.length?void 0:this.buffer.subarray(a),n()}_flush(e){this.buffer&&this.push(this.buffer),e()}};var T=(e=>(e.Private="private",e.Public="public",e))(T||{}),F=1e4,S=o.env.appCommit?.substring(0,10),m;if(process.env.VSCODE_FORWARDING_IS_DEV)m=h.join(__dirname,"../../../cli/target/debug/code");else{let r;process.platform==="darwin"?r="bin":process.platform==="win32"&&S&&o.env.appRoot.includes(S)?r="../../../bin":r="../../bin";let t=o.env.appQuality==="stable"?"code-tunnel":"code-tunnel-insiders",e=process.platform==="win32"?".exe":"";m=h.join(o.env.appRoot,r,t)+e}var v=class{constructor(t,e,s){this.remoteAddress=t;this.privacy=e;this.protocol=s}disposeEmitter=new o.EventEmitter;onDidDispose=this.disposeEmitter.event;localAddress;setPortFormat(t){this.localAddress=t.replace("{port}",String(this.remoteAddress.port))}dispose(){this.disposeEmitter.fire()}};async function N(r){if(o.env.remoteAuthority)return;let t=new g(o.l10n.t("Port Forwarding")),e=new f(t,r);r.subscriptions.push(o.commands.registerCommand("tunnel-forwarding.showLog",()=>t.show()),o.commands.registerCommand("tunnel-forwarding.restart",()=>e.restart()),e.onDidStateChange(s=>{o.commands.executeCommand("setContext","tunnelForwardingIsRunning",s.state!==2)}),await o.workspace.registerTunnelProvider(e,{tunnelFeatures:{elevation:!1,protocol:!0,privacyOptions:[{themeIcon:"globe",id:"public",label:o.l10n.t("Public")},{themeIcon:"lock",id:"private",label:o.l10n.t("Private")}]}}))}function L(){}var g=class{constructor(t){this.label=t}outputChannel;show(){return this.outputChannel?.show()}clear(){this.outputChannel?.clear()}log(t,e,...s){this.outputChannel||(this.outputChannel=o.window.createOutputChannel(this.label,{log:!0}),o.commands.executeCommand("setContext","tunnelForwardingHasLog",!0)),this.outputChannel[t](e,...s)}},C="didWarnPublic",f=class{constructor(t,e){this.logger=t;this.context=e}tunnels=new Set;stateChange=new o.EventEmitter;_state={state:2};get state(){return this._state}set state(t){this._state=t,this.stateChange.fire(t)}onDidStateChange=this.stateChange.event;async provideTunnel(t){if(t.privacy==="public"&&!await this.consentPublicPort(t.remoteAddress.port))return;let e=new v(t.remoteAddress,t.privacy||"private",t.protocol==="https"?"https":"http");switch(this.tunnels.add(e),e.onDidDispose(()=>{this.tunnels.delete(e),this.updateActivePortsIfRunning()}),this.state.state){case 3:case 2:await this.setupPortForwardingProcess();case 0:return this.updateActivePortsIfRunning(),new Promise((s,n)=>{let a=this.stateChange.event(i=>{i.state===1?(e.setPortFormat(i.portFormat),a.dispose(),s(e)):i.state===3&&(a.dispose(),n(new Error(i.error)))})});case 1:return e.setPortFormat(this.state.portFormat),this.updateActivePortsIfRunning(),e}}async restart(){this.killRunningProcess(),await this.setupPortForwardingProcess(),this.updateActivePortsIfRunning()}async consentPublicPort(t){if(this.context.globalState.get(C,!1))return!0;let s=o.l10n.t("Continue"),n=o.l10n.t("Don't show again"),a=await o.window.showWarningMessage(o.l10n.t("You're about to create a publicly forwarded port. Anyone on the internet will be able to connect to the service listening on port {0}. You should only proceed if this service is secure and non-sensitive.",t),{modal:!0},s,n);if(a!==s)if(a===n)await this.context.globalState.update(C,!0);else return!1;return!0}isInStateWithProcess(t){return(this.state.state===0||this.state.state===1)&&this.state.process===t}killRunningProcess(){(this.state.state===0||this.state.state===1)&&(this.logger.log("info","[forwarding] no more ports, stopping forwarding CLI"),this.state.process.kill(),this.state={state:2})}updateActivePortsIfRunning(){if(this.state.state!==0&&this.state.state!==1)return;let t=[...this.tunnels].map(e=>({number:e.remoteAddress.port,privacy:e.privacy,protocol:e.protocol}));this.state.process.stdin.write(`${JSON.stringify(t)} `),t.length===0&&!this.state.cleanupTimeout?this.state.cleanupTimeout=setTimeout(()=>this.killRunningProcess(),F):t.length>0&&this.state.cleanupTimeout&&(clearTimeout(this.state.cleanupTimeout),this.state.cleanupTimeout=void 0)}async setupPortForwardingProcess(){let t=await o.authentication.getSession("github",["user:email","read:org"],{createIfNone:!0}),e=["--verbose","tunnel","forward-internal","--provider","github"];this.logger.log("info","[forwarding] starting CLI");let s=(0,y.spawn)(m,e,{stdio:"pipe",env:{...process.env,NO_COLOR:"1",VSCODE_CLI_ACCESS_TOKEN:t.accessToken}});this.state={state:0,process:s};let n=new u;o.window.withProgress({location:o.ProgressLocation.Notification,title:o.l10n.t({comment:['do not change link format [Show Log](command), only change the text "Show Log"'],message:"Starting port forwarding system ([Show Log]({0}))",args:["command:tunnel-forwarding.showLog"]})},()=>n.p);let a;s.on("exit",i=>{let c=`[forwarding] exited with code ${i}`;this.logger.log("info",c),n.complete(),this.isInStateWithProcess(s)&&(this.state={state:3,error:c})}),s.on("error",i=>{this.logger.log("error",`[forwarding] ${i}`),n.complete(),this.isInStateWithProcess(s)&&(this.state={state:3,error:String(i)})}),s.stdout.pipe(p()).on("data",i=>this.logger.log("info",`[forwarding] ${i}`)).resume(),s.stderr.pipe(p()).on("data",i=>{try{let c=JSON.parse(i);c.port_format&&c.port_format!==a&&(this.state={state:1,portFormat:c.port_format,process:s,cleanupTimeout:"cleanupTimeout"in this.state?this.state.cleanupTimeout:void 0},n.complete())}catch{this.logger.log("error",`[forwarding] ${i}`)}}).resume(),await new Promise((i,c)=>{s.on("spawn",i),s.on("error",c)})}};0&&(module.exports={TunnelPrivacyId,activate,deactivate}); //# sourceMappingURL=extension.js.map