@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 108 lines 4.15 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as NetworkFirewall from "@alchemy.run/aws/NetworkFirewall";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class NetworkFirewallBindingsFunction extends Lambda.Function<Lambda.Function>()(
13 "NetworkFirewallBindingsFunction",
14 ) {}
15
16 export default NetworkFirewallBindingsFunction.make(
17 {
18 main,
19 functionUrl: true,
20 timeout: Duration.seconds(120),
21 },
22 Effect.gen(function* () {
23 // A policy and a stateful rule group are cheap (seconds to provision)
24 // and exercise the policy- and rule-group-scoped grants + ARN injection.
25 const ruleGroup = yield* NetworkFirewall.RuleGroup("BindingsRuleGroup", {
26 type: "STATEFUL",
27 capacity: 10,
28 rules: 'pass tcp any any -> any 443 (msg:"allow https"; sid:100001; rev:1;)',
29 summaryConfiguration: { RuleOptions: ["SID", "MSG"] },
30 });
31 const policy = yield* NetworkFirewall.FirewallPolicy("BindingsPolicy", {
32 firewallPolicy: {
33 StatelessDefaultActions: ["aws:forward_to_sfe"],
34 StatelessFragmentDefaultActions: ["aws:forward_to_sfe"],
35 StatefulRuleGroupReferences: [{ ResourceArn: ruleGroup.ruleGroupArn }],
36 },
37 });
38
39 const bound = {
40 describeFirewallPolicy: yield* NetworkFirewall.DescribeFirewallPolicy(policy),
41 describeRuleGroup: yield* NetworkFirewall.DescribeRuleGroup(ruleGroup),
42 describeRuleGroupSummary: yield* NetworkFirewall.DescribeRuleGroupSummary(ruleGroup),
43 describeRuleGroupMetadata: yield* NetworkFirewall.DescribeRuleGroupMetadata(ruleGroup),
44 };
45
46 return {
47 fetch: Effect.gen(function* () {
48 const request = yield* HttpServerRequest;
49 const url = new URL(request.originalUrl);
50 const pathname = url.pathname;
51
52 if (request.method === "GET" && pathname === "/bindings") {
53 return yield* HttpServerResponse.json({
54 bound: Object.keys(bound),
55 });
56 }
57
58 // Policy-scoped describe — the policy ARN is injected.
59 if (request.method === "GET" && pathname === "/policy") {
60 const response = yield* bound.describeFirewallPolicy();
61 return yield* HttpServerResponse.json({
62 status: response.FirewallPolicyResponse.FirewallPolicyStatus,
63 statelessDefaultActions: response.FirewallPolicy?.StatelessDefaultActions ?? [],
64 });
65 }
66
67 // Rule-group-scoped describe — the rule group ARN is injected.
68 if (request.method === "GET" && pathname === "/rule-group") {
69 const response = yield* bound.describeRuleGroup();
70 return yield* HttpServerResponse.json({
71 status: response.RuleGroupResponse.RuleGroupStatus,
72 type: response.RuleGroupResponse.Type,
73 rulesString: response.RuleGroup?.RulesSource?.RulesString ?? "",
74 });
75 }
76
77 if (request.method === "GET" && pathname === "/rule-group/summary") {
78 const response = yield* bound.describeRuleGroupSummary();
79 return yield* HttpServerResponse.json({
80 ruleCount: (response.Summary?.RuleSummaries ?? []).length,
81 });
82 }
83
84 if (request.method === "GET" && pathname === "/rule-group/metadata") {
85 const response = yield* bound.describeRuleGroupMetadata();
86 return yield* HttpServerResponse.json({
87 capacity: response.Capacity ?? 0,
88 type: response.Type ?? "",
89 });
90 }
91
92 return yield* HttpServerResponse.json(
93 { error: "Not found", method: request.method, pathname },
94 { status: 404 },
95 );
96 }).pipe(Effect.orDie),
97 };
98 }).pipe(
99 Effect.provide(
100 Layer.mergeAll(
101 NetworkFirewall.DescribeFirewallPolicyHttp,
102 NetworkFirewall.DescribeRuleGroupHttp,
103 NetworkFirewall.DescribeRuleGroupSummaryHttp,
104 NetworkFirewall.DescribeRuleGroupMetadataHttp,
105 ),
106 ),
107 ),
108 );