@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 04:45 UTC

main
ts 99 lines 3.58 KB
Raw
1 import { cachedFunction } from "alchemy/Util/cached-function";
2 import * as Config from "effect/Config";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as ChildProcess from "effect/process/ChildProcess";
7 import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
8 import * as Schema from "effect/Schema";
9 import * as Stream from "effect/Stream";
10
11 export class AccessError extends Schema.TaggedError<AccessError>()("AccessError", {
12 message: Schema.String,
13 cause: Schema.optional(Schema.Defect()),
14 }) {}
15
16 export class Access extends Context.Service<
17 Access,
18 {
19 readonly getAccessHeaders: (
20 domain: string,
21 ) => Effect.Effect<Record<string, string>, AccessError>;
22 }
23 >()("alchemy/Cloudflare/Access") {}
24
25 export const AccessLive = Layer.effect(
26 Access,
27 Effect.gen(function* () {
28 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
29 const domainUsesAccess = yield* cachedFunction((domain: string) =>
30 Effect.promise((signal) => fetch(`https://${domain}`, { redirect: "manual", signal })).pipe(
31 Effect.map(
32 (response) =>
33 response.status === 302 &&
34 (response.headers.get("location")?.includes("cloudflareaccess.com") ?? false),
35 ),
36 Effect.timeout(1000),
37 Effect.orElseSucceed(() => false),
38 ),
39 );
40 const login = (domain: string) =>
41 ChildProcess.make("cloudflared", ["access", "login", domain]).pipe(
42 (command) => spawner.spawn(command),
43 Effect.flatMap((process) => Stream.runCollect(process.stdout)),
44 Effect.mapError((error) =>
45 AccessError.make({
46 message:
47 `The domain "${domain}" uses Cloudflare Access, but \`cloudflared\` is not installed. ` +
48 `Please install it from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation.`,
49 cause: error,
50 }),
51 ),
52 Effect.flatMap((stdout) => {
53 const matches = stdout.toString().match(/fetched your token:\n\n(.*)/m);
54 return matches && matches.length >= 2
55 ? Effect.succeed({ Cookie: `CF_Authorization=${matches[1]}` })
56 : Effect.fail(
57 AccessError.make({
58 message: "Failed to authenticate with Cloudflare Access",
59 }),
60 );
61 }),
62 Effect.scoped,
63 );
64
65 const getEnv = (name: string) =>
66 Config.String(name)
67
68 .pipe(Effect.catchTag("ConfigError", () => Effect.undefined));
69
70 return Access.of({
71 getAccessHeaders: Effect.fn(function* (domain) {
72 if (!(yield* domainUsesAccess(domain))) {
73 return {};
74 }
75 const clientId = yield* getEnv("CLOUDFLARE_ACCESS_CLIENT_ID");
76 const clientSecret = yield* getEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET");
77 if (clientId && clientSecret) {
78 return {
79 "CF-Access-Client-Id": clientId,
80 "CF-Access-Client-Secret": clientSecret,
81 } as Record<string, string>;
82 }
83
84 if (clientId !== undefined || clientSecret !== undefined) {
85 yield* Effect.logWarning(
86 "Both CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET must be set to use Access Service Token authentication. " +
87 `Only ${
88 clientId !== undefined
89 ? "CLOUDFLARE_ACCESS_CLIENT_ID"
90 : "CLOUDFLARE_ACCESS_CLIENT_SECRET"
91 } was found.`,
92 );
93 }
94
95 return yield* login(domain);
96 }),
97 });
98 }),
99 );