Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

sshEnvironment.ts

214 lines7.7 KB

1import {
2 bootstrapRemoteBearerSession,
3 fetchRemoteSessionState,
4 RemoteEnvironmentAuthUndeclaredStatusError,
5 type RemoteEnvironmentAuthError,
6} from "@t3tools/client-runtime/authorization";
7import { fetchRemoteEnvironmentDescriptor } from "@t3tools/client-runtime/environment";
8import {
9 EnvironmentAuthInvalidError,
10 DesktopDiscoveredSshHostSchema,
11 DesktopSshBearerBootstrapInputSchema,
12 DesktopSshBearerRequestInputSchema,
13 DesktopSshEnvironmentEnsureInputSchema,
14 DesktopSshEnvironmentEnsureResultSchema,
15 DesktopSshEnvironmentTargetSchema,
16 DesktopSshHttpBaseUrlInputSchema,
17 DesktopSshPasswordPromptCancelledType,
18 DesktopSshPasswordPromptResolutionInputSchema,
19 ExecutionEnvironmentDescriptor,
20 EnvironmentInternalError,
21 EnvironmentOperationForbiddenError,
22 EnvironmentRequestInvalidError,
23 EnvironmentScopeRequiredError,
24 AuthAccessTokenResult,
25 AuthSessionState,
26} from "@t3tools/contracts";
27import { SshHttpBridgeError } from "@t3tools/ssh/errors";
28import { resolveLoopbackSshHttpBaseUrl } from "@t3tools/ssh/tunnel";
29import * as Data from "effect/Data";
30import * as Effect from "effect/Effect";
31import * as Schema from "effect/Schema";
32​
33import * as IpcChannels from "../channels.ts";
34import * as DesktopIpc from "../DesktopIpc.ts";
35import * as DesktopSshEnvironment from "../../ssh/DesktopSshEnvironment.ts";
36import * as DesktopSshPasswordPrompts from "../../ssh/DesktopSshPasswordPrompts.ts";
37​
38type DesktopSshEnvironmentRequestOperation =
39 | "fetch-environment-descriptor"
40 | "bootstrap-bearer-session"
41 | "fetch-session-state";
42​
43type DesktopSshEnvironmentRequestCause = RemoteEnvironmentAuthError | SshHttpBridgeError;
44​
45const isEnvironmentAuthInvalidError = Schema.is(EnvironmentAuthInvalidError);
46const isEnvironmentInternalError = Schema.is(EnvironmentInternalError);
47const isEnvironmentOperationForbiddenError = Schema.is(EnvironmentOperationForbiddenError);
48const isEnvironmentRequestInvalidError = Schema.is(EnvironmentRequestInvalidError);
49const isEnvironmentScopeRequiredError = Schema.is(EnvironmentScopeRequiredError);
50​
51function readSshHttpStatus(cause: DesktopSshEnvironmentRequestCause): number | null {
52 if (
53 cause instanceof RemoteEnvironmentAuthUndeclaredStatusError ||
54 cause instanceof SshHttpBridgeError
55 ) {
56 return cause.status ?? null;
57 }
58 if (isEnvironmentRequestInvalidError(cause)) {
59 return 400;
60 }
61 if (isEnvironmentAuthInvalidError(cause)) {
62 return 401;
63 }
64 if (isEnvironmentScopeRequiredError(cause)) {
65 return 403;
66 }
67 if (isEnvironmentOperationForbiddenError(cause)) {
68 return 403;
69 }
70 if (isEnvironmentInternalError(cause)) {
71 return 500;
72 }
73 return null;
75​
76export class DesktopSshEnvironmentRequestError extends Data.TaggedError(
77 "DesktopSshEnvironmentRequestError",
78)<{
79 readonly operation: DesktopSshEnvironmentRequestOperation;
80 readonly cause: DesktopSshEnvironmentRequestCause;
81 readonly sshHttpStatus: number | null;
82}> {
83 // The web client matches "[ssh_http:500]" to explain version-skewed SSH pairing.
84 override get message() {
85 const prefix = this.sshHttpStatus === null ? "" : `[ssh_http:${this.sshHttpStatus}] `;
86 return `${prefix}SSH remote API request failed during ${this.operation}.`;
87 }
89​
90const withLoopbackSshApi =
91 <A, R>(
92 operation: DesktopSshEnvironmentRequestOperation,
93 use: (httpBaseUrl: string) => Effect.Effect<A, RemoteEnvironmentAuthError, R>,
94 ) =>
95 (httpBaseUrl: string): Effect.Effect<A, DesktopSshEnvironmentRequestError, R> =>
96 resolveLoopbackSshHttpBaseUrl(httpBaseUrl).pipe(
97 Effect.flatMap(use),
98 Effect.mapError(
99 (cause) =>
100 new DesktopSshEnvironmentRequestError({
101 operation,
102 cause,
103 sshHttpStatus: readSshHttpStatus(cause),
104 }),
105 ),
106 );
107​
108export const discoverSshHosts = DesktopIpc.makeIpcMethod({
109 channel: IpcChannels.DISCOVER_SSH_HOSTS_CHANNEL,
110 payload: Schema.Void,
111 result: Schema.Array(DesktopDiscoveredSshHostSchema),
112 handler: Effect.fn("desktop.ipc.sshEnvironment.discoverHosts")(function* () {
113 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
114 return yield* sshEnvironment.discoverHosts();
115 }),
116});
117​
118export const resolveSshHost = DesktopIpc.makeIpcMethod({
119 channel: IpcChannels.RESOLVE_SSH_HOST_CHANNEL,
120 payload: Schema.String,
121 result: DesktopSshEnvironmentTargetSchema,
122 handler: Effect.fn("desktop.ipc.sshEnvironment.resolveHost")(function* (alias) {
123 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
124 return yield* sshEnvironment.resolveHost(alias);
125 }),
126});
127​
128export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({
129 channel: IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL,
130 payload: DesktopSshEnvironmentEnsureInputSchema,
131 result: DesktopSshEnvironmentEnsureResultSchema,
132 handler: Effect.fn("desktop.ipc.sshEnvironment.ensureEnvironment")(function* ({
133 target,
134 options,
135 }) {
136 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
137 return yield* sshEnvironment.ensureEnvironment(target, options).pipe(
138 Effect.catchIf(DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation, (error) =>
139 Effect.succeed({
140 type: DesktopSshPasswordPromptCancelledType,
141 message: error.message,
142 }),
143 ),
144 );
145 }),
146});
147​
148export const disconnectSshEnvironment = DesktopIpc.makeIpcMethod({
149 channel: IpcChannels.DISCONNECT_SSH_ENVIRONMENT_CHANNEL,
150 payload: DesktopSshEnvironmentTargetSchema,
151 result: Schema.Void,
152 handler: Effect.fn("desktop.ipc.sshEnvironment.disconnectEnvironment")(function* (target) {
153 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
154 yield* sshEnvironment.disconnectEnvironment(target);
155 }),
156});
157​
158export const fetchSshEnvironmentDescriptor = DesktopIpc.makeIpcMethod({
159 channel: IpcChannels.FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL,
160 payload: DesktopSshHttpBaseUrlInputSchema,
161 result: ExecutionEnvironmentDescriptor,
162 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchDescriptor")(function* ({ httpBaseUrl }) {
163 return yield* withLoopbackSshApi("fetch-environment-descriptor", (resolvedHttpBaseUrl) =>
164 fetchRemoteEnvironmentDescriptor({ httpBaseUrl: resolvedHttpBaseUrl }),
165 )(httpBaseUrl);
166 }),
167});
168​
169export const bootstrapSshBearerSession = DesktopIpc.makeIpcMethod({
170 channel: IpcChannels.BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL,
171 payload: DesktopSshBearerBootstrapInputSchema,
172 result: AuthAccessTokenResult,
173 handler: Effect.fn("desktop.ipc.sshEnvironment.bootstrapBearerSession")(function* ({
174 httpBaseUrl,
175 credential,
176 }) {
177 return yield* withLoopbackSshApi("bootstrap-bearer-session", (resolvedHttpBaseUrl) =>
178 bootstrapRemoteBearerSession({
179 httpBaseUrl: resolvedHttpBaseUrl,
180 credential,
181 }),
182 )(httpBaseUrl);
183 }),
184});
185​
186export const fetchSshSessionState = DesktopIpc.makeIpcMethod({
187 channel: IpcChannels.FETCH_SSH_SESSION_STATE_CHANNEL,
188 payload: DesktopSshBearerRequestInputSchema,
189 result: AuthSessionState,
190 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchSessionState")(function* ({
191 httpBaseUrl,
192 bearerToken,
193 }) {
194 return yield* withLoopbackSshApi("fetch-session-state", (resolvedHttpBaseUrl) =>
195 fetchRemoteSessionState({
196 httpBaseUrl: resolvedHttpBaseUrl,
197 bearerToken,
198 }),
199 )(httpBaseUrl);
200 }),
201});
202​
203export const resolveSshPasswordPrompt = DesktopIpc.makeIpcMethod({
204 channel: IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL,
205 payload: DesktopSshPasswordPromptResolutionInputSchema,
206 result: Schema.Void,
207 handler: Effect.fn("desktop.ipc.sshEnvironment.resolvePasswordPrompt")(function* ({
208 requestId,
209 password,
210 }) {
211 const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
212 yield* prompts.resolve({ requestId, password });
213 }),
214});