Open History to see the last commit
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;
77}
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 // The web client matches "[ssh_http:500]" to explain version-skewed SSH pairing.
87 override get message() {
88 const prefix = this.sshHttpStatus === null ? "" : `[ssh_http:${this.sshHttpStatus}] `;
89 return `${prefix}SSH remote API request failed during ${this.operation}.`;
90 }
91}
92
93const withLoopbackSshApi =
94 <A, R>(
95 operation: DesktopSshEnvironmentRequestOperation,
96 use: (httpBaseUrl: string) => Effect.Effect<A, RemoteEnvironmentAuthError, R>,
97 ) =>
98 (httpBaseUrl: string): Effect.Effect<A, DesktopSshEnvironmentRequestError, R> =>
99 resolveLoopbackSshHttpBaseUrl(httpBaseUrl).pipe(
100 Effect.flatMap(use),
101 Effect.mapError(
102 (cause) =>
103 new DesktopSshEnvironmentRequestError({
104 operation,
105 cause,
106 sshHttpStatus: readSshHttpStatus(cause),
107 }),
108 ),
109 );
110
111export const discoverSshHosts = DesktopIpc.makeIpcMethod({
112 channel: IpcChannels.DISCOVER_SSH_HOSTS_CHANNEL,
113 payload: Schema.Void,
114 result: Schema.Array(DesktopDiscoveredSshHostSchema),
115 handler: Effect.fn("desktop.ipc.sshEnvironment.discoverHosts")(function* () {
116 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
117 return yield* sshEnvironment.discoverHosts();
118 }),
119});
120
121export const resolveSshHost = DesktopIpc.makeIpcMethod({
122 channel: IpcChannels.RESOLVE_SSH_HOST_CHANNEL,
123 payload: Schema.String,
124 result: DesktopSshEnvironmentTargetSchema,
125 handler: Effect.fn("desktop.ipc.sshEnvironment.resolveHost")(function* (alias) {
126 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
127 return yield* sshEnvironment.resolveHost(alias);
128 }),
129});
130
131export const ensureSshEnvironment = DesktopIpc.makeIpcMethod({
132 channel: IpcChannels.ENSURE_SSH_ENVIRONMENT_CHANNEL,
133 payload: DesktopSshEnvironmentEnsureInputSchema,
134 result: DesktopSshEnvironmentEnsureResultSchema,
135 handler: Effect.fn("desktop.ipc.sshEnvironment.ensureEnvironment")(function* ({
136 target,
137 options,
138 }) {
139 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
140 return yield* sshEnvironment.ensureEnvironment(target, options).pipe(
141 Effect.catchIf(DesktopSshEnvironment.isDesktopSshPasswordPromptCancellation, (error) =>
142 Effect.succeed({
143 type: DesktopSshPasswordPromptCancelledType,
144 message: error.message,
145 }),
146 ),
147 );
148 }),
149});
150
151export const disconnectSshEnvironment = DesktopIpc.makeIpcMethod({
152 channel: IpcChannels.DISCONNECT_SSH_ENVIRONMENT_CHANNEL,
153 payload: DesktopSshEnvironmentTargetSchema,
154 result: Schema.Void,
155 handler: Effect.fn("desktop.ipc.sshEnvironment.disconnectEnvironment")(function* (target) {
156 const sshEnvironment = yield* DesktopSshEnvironment.DesktopSshEnvironment;
157 yield* sshEnvironment.disconnectEnvironment(target);
158 }),
159});
160
161export const fetchSshEnvironmentDescriptor = DesktopIpc.makeIpcMethod({
162 channel: IpcChannels.FETCH_SSH_ENVIRONMENT_DESCRIPTOR_CHANNEL,
163 payload: DesktopSshHttpBaseUrlInputSchema,
164 result: ExecutionEnvironmentDescriptor,
165 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchDescriptor")(function* ({ httpBaseUrl }) {
166 return yield* withLoopbackSshApi("fetch-environment-descriptor", (resolvedHttpBaseUrl) =>
167 fetchRemoteEnvironmentDescriptor({ httpBaseUrl: resolvedHttpBaseUrl }),
168 )(httpBaseUrl);
169 }),
170});
171
172export const bootstrapSshBearerSession = DesktopIpc.makeIpcMethod({
173 channel: IpcChannels.BOOTSTRAP_SSH_BEARER_SESSION_CHANNEL,
174 payload: DesktopSshBearerBootstrapInputSchema,
175 result: AuthAccessTokenResult,
176 handler: Effect.fn("desktop.ipc.sshEnvironment.bootstrapBearerSession")(function* ({
177 httpBaseUrl,
178 credential,
179 }) {
180 return yield* withLoopbackSshApi("bootstrap-bearer-session", (resolvedHttpBaseUrl) =>
181 bootstrapRemoteBearerSession({
182 httpBaseUrl: resolvedHttpBaseUrl,
183 credential,
184 }),
185 )(httpBaseUrl);
186 }),
187});
188
189export const fetchSshSessionState = DesktopIpc.makeIpcMethod({
190 channel: IpcChannels.FETCH_SSH_SESSION_STATE_CHANNEL,
191 payload: DesktopSshBearerRequestInputSchema,
192 result: AuthSessionState,
193 handler: Effect.fn("desktop.ipc.sshEnvironment.fetchSessionState")(function* ({
194 httpBaseUrl,
195 bearerToken,
196 }) {
197 return yield* withLoopbackSshApi("fetch-session-state", (resolvedHttpBaseUrl) =>
198 fetchRemoteSessionState({
199 httpBaseUrl: resolvedHttpBaseUrl,
200 bearerToken,
201 }),
202 )(httpBaseUrl);
203 }),
204});
205
206export const issueSshWebSocketTicket = DesktopIpc.makeIpcMethod({
207 channel: IpcChannels.ISSUE_SSH_WEBSOCKET_TOKEN_CHANNEL,
208 payload: DesktopSshBearerRequestInputSchema,
209 result: AuthWebSocketTicketResult,
210 handler: Effect.fn("desktop.ipc.sshEnvironment.issueWebSocketTicket")(function* ({
211 httpBaseUrl,
212 bearerToken,
213 }) {
214 return yield* withLoopbackSshApi("issue-websocket-ticket", (resolvedHttpBaseUrl) =>
215 issueRemoteWebSocketTicket({
216 httpBaseUrl: resolvedHttpBaseUrl,
217 bearerToken,
218 }),
219 )(httpBaseUrl);
220 }),
221});
222
223export const resolveSshPasswordPrompt = DesktopIpc.makeIpcMethod({
224 channel: IpcChannels.RESOLVE_SSH_PASSWORD_PROMPT_CHANNEL,
225 payload: DesktopSshPasswordPromptResolutionInputSchema,
226 result: Schema.Void,
227 handler: Effect.fn("desktop.ipc.sshEnvironment.resolvePasswordPrompt")(function* ({
228 requestId,
229 password,
230 }) {
231 const prompts = yield* DesktopSshPasswordPrompts.DesktopSshPasswordPrompts;
232 yield* prompts.resolve({ requestId, password });
233 }),
234});