Repository navigation
feat(server): persist outgoing links to peer environments #21672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
juliusmarminge
wants to merge
4
commits into
takeover/ssh-relay-remote-15-launch-retry
from
takeover/ssh-relay-remote-15-peer-links
+2,015
−3
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
250757c
feat(server): persist links to peer environments
juliusmarminge 3466f49
fix(server): prefer compatible peer link addresses
juliusmarminge e06c97f
docs(server): clarify supported peer link addresses
juliusmarminge 84a4f73
fix(server): handle peer response and cleanup failures
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| import { AuthMcpClientAccess, EnvironmentId, PeerLink, PeerLinkSummary } from "@t3tools/contracts"; | ||
| import * as Console from "effect/Console"; | ||
| import * as DateTime from "effect/DateTime"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Option from "effect/Option"; | ||
| import * as Layer from "effect/Layer"; | ||
| import * as References from "effect/References"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Argument from "effect/cli/Argument"; | ||
| import * as Command from "effect/cli/Command"; | ||
| import * as Flag from "effect/cli/Flag"; | ||
| import * as GlobalFlag from "effect/cli/GlobalFlag"; | ||
| import * as FetchHttpClient from "effect/http/FetchHttpClient"; | ||
|
|
||
| import * as ServerSecretStore from "../auth/ServerSecretStore.ts"; | ||
| import * as ServerConfig from "../config.ts"; | ||
| import * as ServerEnvironment from "../environment/ServerEnvironment.ts"; | ||
| import * as PeerLinks from "../peer/PeerLinks.ts"; | ||
| import * as SqlitePersistence from "../persistence/Sqlite.ts"; | ||
| import { authLocationFlags, type CliAuthLocationFlags, resolveCliAuthConfig } from "./config.ts"; | ||
|
|
||
| /** | ||
| * Links live in this environment's database and secret store, so these | ||
| * commands work whether or not its server is running. | ||
| */ | ||
| const runWithPeerLinks = <A, E>( | ||
| flags: CliAuthLocationFlags, | ||
| run: (links: PeerLinks.PeerLinks["Service"]) => Effect.Effect<A, E>, | ||
| options?: { readonly quietLogs?: boolean }, | ||
| ) => | ||
| Effect.gen(function* () { | ||
| const logLevel = yield* GlobalFlag.LogLevel; | ||
| const config = yield* resolveCliAuthConfig(flags, logLevel); | ||
| return yield* PeerLinks.PeerLinks.pipe( | ||
| Effect.flatMap(run), | ||
| Effect.provide( | ||
| PeerLinks.layer.pipe( | ||
| Layer.provide( | ||
| Layer.mergeAll( | ||
| ServerSecretStore.layer, | ||
| SqlitePersistence.layerConfig, | ||
| FetchHttpClient.layer, | ||
| ), | ||
| ), | ||
| Layer.provide(ServerEnvironment.layer.pipe(Layer.provide(ServerSecretStore.layer))), | ||
| Layer.provide(ServerConfig.layer(config)), | ||
| Layer.provide( | ||
| Layer.succeed( | ||
| References.MinimumLogLevel, | ||
| options?.quietLogs ? "Error" : config.logLevel, | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| const jsonFlag = Flag.Boolean("json").pipe( | ||
| Flag.withDescription("Emit JSON instead of human-readable output."), | ||
| Flag.withDefault(false), | ||
| ); | ||
|
|
||
| const encodeLinks = Schema.encodeEffect(Schema.fromJsonString(Schema.Array(PeerLinkSummary))); | ||
|
|
||
| const encodeLink = Schema.encodeEffect(Schema.fromJsonString(Schema.NullOr(PeerLink))); | ||
|
|
||
| const formatLinks = (links: ReadonlyArray<PeerLinkSummary>) => | ||
| links.length === 0 | ||
| ? "No linked environments.\n" | ||
| : `${links | ||
| .map( | ||
| (link) => | ||
| `${link.label} (${link.environmentId})\n ${link.status}, access ${link.access}, expires ${DateTime.formatIso(link.expiresAt)}\n ${link.urls.join(", ")}${link.lastError === null ? "" : `\n last error: ${link.lastError}`}`, | ||
| ) | ||
| .join("\n\n")}\n`; | ||
|
|
||
| const environmentLinkCommand = Command.make("link", { | ||
| ...authLocationFlags, | ||
| url: Argument.String("url").pipe( | ||
| Argument.withDescription( | ||
| "Where the other environment answers: HTTPS, or HTTP with loopback, a Tailscale .ts.net name, or Tailscale IPv6.", | ||
| ), | ||
| ), | ||
| alternateUrl: Flag.String("url").pipe( | ||
| Flag.withDescription("Another address for the same environment, tried after the first."), | ||
| Flag.atLeast(0), | ||
| ), | ||
| pairingCode: Flag.String("pairing-code").pipe( | ||
| Flag.withDescription( | ||
| "A pairing code from the other environment (`t3 auth pairing create` there).", | ||
| ), | ||
| ), | ||
| access: Flag.Literals("access", AuthMcpClientAccess.literals).pipe( | ||
| Flag.withDescription( | ||
| "Access ceiling for this outgoing link: read-only, or the broadest runtime mode.", | ||
| ), | ||
| Flag.withDefault("approval-required" as const), | ||
| ), | ||
| }).pipe( | ||
| Command.withDescription( | ||
| "Create an outgoing link to another T3 Code environment. Revoke its session in that environment's Settings → Connections.", | ||
| ), | ||
| Command.withHandler((flags) => | ||
| runWithPeerLinks(flags, (links) => | ||
| Effect.gen(function* () { | ||
| const linked = yield* links.link({ | ||
| url: flags.url, | ||
| alternateUrls: flags.alternateUrl, | ||
| pairingCode: flags.pairingCode, | ||
| access: flags.access, | ||
| }); | ||
| yield* Console.log( | ||
| `Linked ${linked.label} (${linked.environmentId}) with ${linked.access} access, until ${DateTime.formatIso(linked.expiresAt)}.\n`, | ||
| ); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const environmentListCommand = Command.make("list", { ...authLocationFlags, json: jsonFlag }).pipe( | ||
| Command.withDescription("List linked environments and whether each answers now."), | ||
| Command.withHandler((flags) => | ||
| runWithPeerLinks( | ||
| flags, | ||
| (links) => | ||
| Effect.gen(function* () { | ||
| const listed = yield* links.list; | ||
| yield* Console.log(flags.json ? `${yield* encodeLinks(listed)}\n` : formatLinks(listed)); | ||
| }), | ||
| { quietLogs: flags.json }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const environmentGetCommand = Command.make("get", { | ||
| ...authLocationFlags, | ||
| json: jsonFlag, | ||
| environmentId: Argument.String("environment-id"), | ||
| }).pipe( | ||
| Command.withDescription("Read a stored link without contacting the peer."), | ||
| Command.withHandler((flags) => | ||
| runWithPeerLinks( | ||
| flags, | ||
| (links) => | ||
| Effect.gen(function* () { | ||
| const linked = yield* links.get(EnvironmentId.make(flags.environmentId)); | ||
| yield* Console.log( | ||
| flags.json | ||
| ? `${yield* encodeLink(Option.getOrNull(linked))}\n` | ||
| : Option.isNone(linked) | ||
| ? `No linked environment ${flags.environmentId}.\n` | ||
| : `${linked.value.label} (${linked.value.environmentId})\n access ${linked.value.access}, expires ${DateTime.formatIso(linked.value.expiresAt)}\n ${linked.value.urls.join(", ")}\n`, | ||
| ); | ||
| }), | ||
| { quietLogs: flags.json }, | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| const environmentUnlinkCommand = Command.make("unlink", { | ||
| ...authLocationFlags, | ||
| environmentId: Argument.String("environment-id").pipe( | ||
| Argument.withDescription("The linked environment's id, from `t3 environment list`."), | ||
| ), | ||
| }).pipe( | ||
| Command.withDescription( | ||
| "Forget a linked environment here. Revoke it in that environment's Connections to end its session there too.", | ||
| ), | ||
| Command.withHandler((flags) => | ||
| runWithPeerLinks(flags, (links) => | ||
| Effect.gen(function* () { | ||
| const removed = yield* links.unlink(EnvironmentId.make(flags.environmentId)); | ||
| yield* Console.log( | ||
| removed | ||
| ? `Forgot ${flags.environmentId}. Revoke it in that environment's Connections to end its session there.\n` | ||
| : `No linked environment ${flags.environmentId}.\n`, | ||
| ); | ||
| }), | ||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| export const environmentCommand = Command.make("environment").pipe( | ||
| Command.withDescription("Manage outgoing links to other T3 Code environments."), | ||
| Command.withSubcommands([ | ||
| environmentLinkCommand, | ||
| environmentListCommand, | ||
| environmentGetCommand, | ||
| environmentUnlinkCommand, | ||
| ]), | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.