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

sshEnvironment.ts

225 lines7.9 KB

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