The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as magicTransit from "@distilled.cloud/cloudflare/magic-transit"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Predicate from "effect/Predicate"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { CloudflareEnvironment } from "../CloudflareEnvironment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | |
| 12 | const TypeId = "Cloudflare.MagicTransit.SiteAcl" as const; |
| 13 | type TypeId = typeof TypeId; |
| 14 | |
| 15 | /** |
| 16 | * Protocols a Magic WAN site ACL can match. |
| 17 | */ |
| 18 | export type MagicSiteAclProtocol = "tcp" | "udp" | "icmp"; |
| 19 | |
| 20 | /** |
| 21 | * One side of a Magic WAN site ACL — selects a LAN and optionally narrows |
| 22 | * the match to specific ports, port ranges, and subnets. |
| 23 | */ |
| 24 | export interface MagicSiteAclLan { |
| 25 | /** The LAN identifier this side of the ACL applies to. */ |
| 26 | lanId: string; |
| 27 | /** Display name of the LAN (informational). */ |
| 28 | lanName?: string; |
| 29 | /** Specific ports to match. */ |
| 30 | ports?: number[]; |
| 31 | /** Port ranges to match, e.g. `"8080-8090"`. */ |
| 32 | portRanges?: string[]; |
| 33 | /** Subnets to match (IPv4 CIDR or address). */ |
| 34 | subnets?: string[]; |
| 35 | } |
| 36 | |
| 37 | export interface MagicSiteAclProps { |
| 38 | /** |
| 39 | * The site this ACL belongs to. Changing it triggers a replacement. |
| 40 | */ |
| 41 | siteId: string; |
| 42 | /** |
| 43 | * The name of the ACL. |
| 44 | */ |
| 45 | name: string; |
| 46 | /** |
| 47 | * The first LAN of the ACL pair. |
| 48 | */ |
| 49 | lan1: MagicSiteAclLan; |
| 50 | /** |
| 51 | * The second LAN of the ACL pair. |
| 52 | */ |
| 53 | lan2: MagicSiteAclLan; |
| 54 | /** |
| 55 | * Description for the ACL. |
| 56 | */ |
| 57 | description?: string; |
| 58 | /** |
| 59 | * If `true`, traffic is forwarded locally on the Magic Connector; if |
| 60 | * `false`, traffic is forwarded to Cloudflare. |
| 61 | * @default false |
| 62 | */ |
| 63 | forwardLocally?: boolean; |
| 64 | /** |
| 65 | * Protocols the ACL matches. Omit to match all protocols. |
| 66 | */ |
| 67 | protocols?: MagicSiteAclProtocol[]; |
| 68 | /** |
| 69 | * If `true`, the policy allows traffic in one direction only |
| 70 | * (lan1 → lan2); if `false`, traffic is bidirectional. |
| 71 | * @default false |
| 72 | */ |
| 73 | unidirectional?: boolean; |
| 74 | } |
| 75 | |
| 76 | export interface MagicSiteAclAttributes { |
| 77 | /** Cloudflare-assigned identifier of the ACL. */ |
| 78 | aclId: string; |
| 79 | /** The site the ACL belongs to. */ |
| 80 | siteId: string; |
| 81 | /** The Cloudflare account the ACL belongs to. */ |
| 82 | accountId: string; |
| 83 | /** The name of the ACL. */ |
| 84 | name: string; |
| 85 | /** The ACL description, if set. */ |
| 86 | description: string | undefined; |
| 87 | /** Whether traffic is forwarded locally on the connector. */ |
| 88 | forwardLocally: boolean | undefined; |
| 89 | /** Protocols the ACL matches, if narrowed. */ |
| 90 | protocols: MagicSiteAclProtocol[] | undefined; |
| 91 | /** Whether the ACL is unidirectional. */ |
| 92 | unidirectional: boolean | undefined; |
| 93 | } |
| 94 | |
| 95 | export type MagicSiteAcl = Resource< |
| 96 | TypeId, |
| 97 | MagicSiteAclProps, |
| 98 | MagicSiteAclAttributes, |
| 99 | never, |
| 100 | Providers |
| 101 | >; |
| 102 | |
| 103 | /** |
| 104 | * An ACL between two LANs of a Magic WAN site — allows traffic between |
| 105 | * LAN segments behind a Magic WAN Connector (all inter-LAN traffic is |
| 106 | * denied by default). |
| 107 | * |
| 108 | * Requires a Magic WAN subscription — accounts without it receive a typed |
| 109 | * `MagicWanUnauthorized` error (Cloudflare code 1025) or `MagicWanNotEnabled` |
| 110 | * (code 1101, "forbidden: feature not enabled"). |
| 111 | * |
| 112 | * `siteId` is create-only — changing it triggers a replacement. Everything |
| 113 | * else is updated in place. |
| 114 | * ### Creating an ACL |
| 115 | * **Example:** Allow TCP between two LANs |
| 116 | * ```typescript |
| 117 | * yield* Cloudflare.MagicTransit.MagicSiteAcl("lan-to-lan", { |
| 118 | * siteId: site.siteId, |
| 119 | * name: "office-to-lab", |
| 120 | * lan1: { lanId: officeLan.lanId, ports: [443] }, |
| 121 | * lan2: { lanId: labLan.lanId }, |
| 122 | * protocols: ["tcp"], |
| 123 | * }); |
| 124 | * ``` |
| 125 | * |
| 126 | * **Example:** Unidirectional ACL forwarded locally |
| 127 | * ```typescript |
| 128 | * yield* Cloudflare.MagicTransit.MagicSiteAcl("one-way", { |
| 129 | * siteId: site.siteId, |
| 130 | * name: "sensors-to-collector", |
| 131 | * lan1: { lanId: sensorsLan.lanId }, |
| 132 | * lan2: { lanId: collectorLan.lanId, ports: [9000] }, |
| 133 | * unidirectional: true, |
| 134 | * forwardLocally: true, |
| 135 | * }); |
| 136 | * ``` |
| 137 | * |
| 138 | * @see https://developers.cloudflare.com/magic-wan/configuration/connector/network-options/site-acls/ |
| 139 | * |
| 140 | * @resource |
| 141 | * @product Magic Transit |
| 142 | * @category Network |
| 143 | */ |
| 144 | export const MagicSiteAcl = Resource<MagicSiteAcl>(TypeId); |
| 145 | |
| 146 | /** |
| 147 | * Returns true if the given value is a MagicSiteAcl resource. |
| 148 | */ |
| 149 | export const isMagicSiteAcl = (value: unknown): value is MagicSiteAcl => |
| 150 | Predicate.hasProperty(value, "Type") && value.Type === TypeId; |
| 151 | |
| 152 | export const MagicSiteAclProvider = () => |
| 153 | Provider.succeed(MagicSiteAcl, { |
| 154 | stables: ["aclId", "siteId", "accountId"], |
| 155 | |
| 156 | diff: Effect.fn(function* ({ olds, news }) { |
| 157 | if (!isResolved(news)) return undefined; |
| 158 | if (olds === undefined) return undefined; |
| 159 | // ACLs cannot move between sites. |
| 160 | if ( |
| 161 | typeof olds.siteId === "string" && |
| 162 | typeof news.siteId === "string" && |
| 163 | olds.siteId !== news.siteId |
| 164 | ) { |
| 165 | return { action: "replace" } as const; |
| 166 | } |
| 167 | return undefined; |
| 168 | }), |
| 169 | |
| 170 | read: Effect.fn(function* ({ output, olds }) { |
| 171 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 172 | const acct = output?.accountId ?? accountId; |
| 173 | const siteId = output?.siteId ?? (typeof olds?.siteId === "string" ? olds.siteId : undefined); |
| 174 | if (!siteId) return undefined; |
| 175 | |
| 176 | if (output?.aclId) { |
| 177 | const observed = yield* getAcl(acct, siteId, output.aclId); |
| 178 | if (observed) return toAttributes(observed, siteId, acct); |
| 179 | } |
| 180 | // Cold read — match by name within the site. ACLs carry no |
| 181 | // ownership markers; report as Unowned so takeover is gated behind |
| 182 | // the adopt policy. |
| 183 | const name = output?.name ?? olds?.name; |
| 184 | if (name) { |
| 185 | const observed = yield* findByName(acct, siteId, name); |
| 186 | if (observed) return Unowned(toAttributes(observed, siteId, acct)); |
| 187 | } |
| 188 | return undefined; |
| 189 | }), |
| 190 | |
| 191 | reconcile: Effect.fn(function* ({ news, output }) { |
| 192 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 193 | // Inputs have been resolved to concrete strings by Plan. |
| 194 | const siteId = news.siteId as string; |
| 195 | const lan1 = toLanRequest(news.lan1); |
| 196 | const lan2 = toLanRequest(news.lan2); |
| 197 | |
| 198 | // Observe — the id on `output` is a hint; fall back to a name scan. |
| 199 | let observed = output?.aclId ? yield* getAcl(accountId, siteId, output.aclId) : undefined; |
| 200 | if (!observed) { |
| 201 | observed = yield* findByName(accountId, siteId, news.name); |
| 202 | } |
| 203 | |
| 204 | // Ensure — create when missing. |
| 205 | if (!observed) { |
| 206 | const created = yield* magicTransit.createSiteAcl({ |
| 207 | accountId, |
| 208 | siteId, |
| 209 | name: news.name, |
| 210 | lan_1: lan1, |
| 211 | lan_2: lan2, |
| 212 | description: news.description, |
| 213 | forwardLocally: news.forwardLocally, |
| 214 | protocols: news.protocols, |
| 215 | unidirectional: news.unidirectional, |
| 216 | }); |
| 217 | return toAttributes(created, siteId, accountId); |
| 218 | } |
| 219 | |
| 220 | // Sync — the update API is a PUT; send the full desired state, but |
| 221 | // skip the call entirely on a no-op. |
| 222 | const dirty = |
| 223 | (observed.name ?? undefined) !== news.name || |
| 224 | (news.description !== undefined && |
| 225 | (observed.description ?? undefined) !== news.description) || |
| 226 | (news.forwardLocally !== undefined && |
| 227 | (observed.forwardLocally ?? false) !== news.forwardLocally) || |
| 228 | (news.unidirectional !== undefined && |
| 229 | (observed.unidirectional ?? false) !== news.unidirectional) || |
| 230 | (news.protocols !== undefined && !sameList(observed.protocols, news.protocols)) || |
| 231 | lanDirty(observed.lan_1, lan1) || |
| 232 | lanDirty(observed.lan_2, lan2); |
| 233 | if (dirty) { |
| 234 | const updated = yield* magicTransit.updateSiteAcl({ |
| 235 | accountId, |
| 236 | siteId, |
| 237 | aclId: observed.id!, |
| 238 | name: news.name, |
| 239 | lan_1: lan1, |
| 240 | lan_2: lan2, |
| 241 | description: news.description, |
| 242 | forwardLocally: news.forwardLocally, |
| 243 | protocols: news.protocols, |
| 244 | unidirectional: news.unidirectional, |
| 245 | }); |
| 246 | observed = { ...updated, id: updated.id ?? observed.id }; |
| 247 | } |
| 248 | |
| 249 | return toAttributes(observed, siteId, accountId); |
| 250 | }), |
| 251 | |
| 252 | delete: Effect.fn(function* ({ output }) { |
| 253 | yield* magicTransit |
| 254 | .deleteSiteAcl({ |
| 255 | accountId: output.accountId, |
| 256 | siteId: output.siteId, |
| 257 | aclId: output.aclId, |
| 258 | }) |
| 259 | .pipe(Effect.catchTag("SiteAclNotFound", () => Effect.void)); |
| 260 | }), |
| 261 | |
| 262 | // Parent fan-out: ACLs are sub-resources keyed by site, and there is no |
| 263 | // account-wide ACL enumeration API. Enumerate every Magic site (account |
| 264 | // scope), then list ACLs per site with bounded concurrency, paginating |
| 265 | // each list exhaustively. Magic WAN-gated accounts (and partial-scope |
| 266 | // tokens) reject these routes with the typed `MagicWanUnauthorized` |
| 267 | // (code 1025) / `Forbidden` tags — treat those as "nothing to list". |
| 268 | list: Effect.fn(function* () { |
| 269 | const { accountId } = yield* yield* CloudflareEnvironment; |
| 270 | |
| 271 | const siteIds = yield* magicTransit.listSites.pages({ accountId }).pipe( |
| 272 | Stream.runCollect, |
| 273 | Effect.map((chunk) => |
| 274 | Array.from(chunk).flatMap((page) => |
| 275 | (page.result ?? []).flatMap((site) => (site.id ? [site.id] : [])), |
| 276 | ), |
| 277 | ), |
| 278 | Effect.catchTag(["MagicWanUnauthorized", "MagicWanNotEnabled", "Forbidden"], () => |
| 279 | Effect.succeed([] as string[]), |
| 280 | ), |
| 281 | ); |
| 282 | |
| 283 | const rows = yield* Effect.forEach( |
| 284 | siteIds, |
| 285 | (siteId) => |
| 286 | magicTransit.listSiteAcls.pages({ accountId, siteId }).pipe( |
| 287 | Stream.runCollect, |
| 288 | Effect.map((chunk) => |
| 289 | Array.from(chunk).flatMap((page) => |
| 290 | (page.result ?? []).map((acl) => toAttributes(acl, siteId, accountId)), |
| 291 | ), |
| 292 | ), |
| 293 | // Site vanished or became inaccessible mid-enumeration — skip it. |
| 294 | Effect.catchTag(["MagicWanUnauthorized", "MagicWanNotEnabled", "Forbidden"], () => |
| 295 | Effect.succeed([] as MagicSiteAclAttributes[]), |
| 296 | ), |
| 297 | ), |
| 298 | { concurrency: 10 }, |
| 299 | ); |
| 300 | |
| 301 | return rows.flat(); |
| 302 | }), |
| 303 | }); |
| 304 | |
| 305 | interface ObservedAclLan { |
| 306 | lanId: string; |
| 307 | lanName?: string | null; |
| 308 | ports?: number[] | null; |
| 309 | portRanges?: string[] | null; |
| 310 | subnets?: string[] | null; |
| 311 | } |
| 312 | |
| 313 | interface ObservedAcl { |
| 314 | id?: string | null; |
| 315 | name?: string | null; |
| 316 | description?: string | null; |
| 317 | forwardLocally?: boolean | null; |
| 318 | protocols?: string[] | null; |
| 319 | unidirectional?: boolean | null; |
| 320 | lan_1?: ObservedAclLan | null; |
| 321 | lan_2?: ObservedAclLan | null; |
| 322 | } |
| 323 | |
| 324 | interface AclLanRequest { |
| 325 | lanId: string; |
| 326 | lanName?: string; |
| 327 | ports?: number[]; |
| 328 | portRanges?: string[]; |
| 329 | subnets?: string[]; |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Read an ACL by id, mapping "gone" (`SiteAclNotFound`, HTTP 404) to |
| 334 | * `undefined`. |
| 335 | */ |
| 336 | const getAcl = (accountId: string, siteId: string, aclId: string) => |
| 337 | magicTransit.getSiteAcl({ accountId, siteId, aclId }).pipe( |
| 338 | Effect.map((acl): ObservedAcl | undefined => acl), |
| 339 | Effect.catchTag("SiteAclNotFound", () => Effect.undefined), |
| 340 | ); |
| 341 | |
| 342 | /** |
| 343 | * Find an ACL by exact name within a site. Names are not enforced unique; |
| 344 | * pick the first match deterministically by id. |
| 345 | */ |
| 346 | const findByName = (accountId: string, siteId: string, name: string) => |
| 347 | magicTransit.listSiteAcls({ accountId, siteId }).pipe( |
| 348 | Effect.map((r): ObservedAcl | undefined => |
| 349 | r.result |
| 350 | .filter((acl) => acl.name === name) |
| 351 | .sort((a, b) => (a.id ?? "").localeCompare(b.id ?? "")) |
| 352 | .at(0), |
| 353 | ), |
| 354 | ); |
| 355 | |
| 356 | const toLanRequest = (lan: MagicSiteAclLan): AclLanRequest => ({ |
| 357 | // Inputs have been resolved to concrete strings by Plan. |
| 358 | lanId: lan.lanId as string, |
| 359 | lanName: lan.lanName, |
| 360 | ports: lan.ports, |
| 361 | portRanges: lan.portRanges, |
| 362 | subnets: lan.subnets, |
| 363 | }); |
| 364 | |
| 365 | const byString = (x: string | number, y: string | number) => { |
| 366 | const a = String(x); |
| 367 | const b = String(y); |
| 368 | return a < b ? -1 : a > b ? 1 : 0; |
| 369 | }; |
| 370 | |
| 371 | const sameList = ( |
| 372 | a: readonly (string | number)[] | null | undefined, |
| 373 | b: readonly (string | number)[] | undefined, |
| 374 | ): boolean => [...(a ?? [])].sort(byString).join(",") === [...(b ?? [])].sort(byString).join(","); |
| 375 | |
| 376 | const lanDirty = (observed: ObservedAclLan | null | undefined, desired: AclLanRequest): boolean => |
| 377 | (observed?.lanId ?? undefined) !== desired.lanId || |
| 378 | (desired.ports !== undefined && !sameList(observed?.ports, desired.ports)) || |
| 379 | (desired.portRanges !== undefined && !sameList(observed?.portRanges, desired.portRanges)) || |
| 380 | (desired.subnets !== undefined && !sameList(observed?.subnets, desired.subnets)); |
| 381 | |
| 382 | const toAttributes = ( |
| 383 | acl: ObservedAcl, |
| 384 | siteId: string, |
| 385 | accountId: string, |
| 386 | ): MagicSiteAclAttributes => ({ |
| 387 | aclId: acl.id ?? "", |
| 388 | siteId, |
| 389 | accountId, |
| 390 | name: acl.name ?? "", |
| 391 | description: acl.description ?? undefined, |
| 392 | forwardLocally: acl.forwardLocally ?? undefined, |
| 393 | protocols: acl.protocols ? acl.protocols.map((p) => p as MagicSiteAclProtocol) : undefined, |
| 394 | unidirectional: acl.unidirectional ?? undefined, |
| 395 | }); |