File size: 8,700 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
export const runtime = "nodejs";

import fs from "fs";
import path from "path";
import { z } from "zod";
import { NextResponse } from "next/server";
import { requireManagementAuth } from "@/lib/api/requireManagementAuth";
import { resolveApiKey } from "@/shared/services/apiKeyResolver";
import { resolveMitmDataDir } from "@/mitm/dataDir";
import { KIRO_MITM_PROFILE } from "@/mitm/targets/kiro";
import { ANTIGRAVITY_MITM_PROFILE } from "@/mitm/targets/antigravity";

type MitmTargetRoute = {
  id: string;
  name: string;
  targetHost: string;
  targetPort: number;
  localPort: number;
  endpoints: string[];
  enabled: boolean;
};

type MitmStats = {
  startedAt: string | null;
  totalRequests: number;
  interceptedRequests: number;
  activeConnections: number;
  lastRequestAt: string | null;
  lastInterceptAt: string | null;
};

type MitmConfig = {
  port: number;
  targets: MitmTargetRoute[];
};

const DEFAULT_PORT = 443;
const MITM_PORT_ERROR =
  "Transparent MITM interception currently requires port 443 because DNS override does not redirect destination ports.";

const updateMitmSchema = z.object({
  enabled: z.boolean().optional(),
  apiKey: z.string().optional(),
  keyId: z.string().optional(),
  sudoPassword: z.string().optional(),
  port: z.coerce.number().int().min(1).max(65535).optional(),
});

const regenerateSchema = z.object({
  action: z.literal("regenerate-cert").optional(),
});

function getMitmDir() {
  return path.join(resolveMitmDataDir(), "mitm");
}

function getConfigPath() {
  return path.join(getMitmDir(), "settings.json");
}

function getStatsPath() {
  return path.join(getMitmDir(), "stats.json");
}

function getCertPath() {
  return path.join(getMitmDir(), "server.crt");
}

function getKeyPath() {
  return path.join(getMitmDir(), "server.key");
}

function defaultTargets(port = DEFAULT_PORT): MitmTargetRoute[] {
  const allHosts = [
    ANTIGRAVITY_MITM_PROFILE.targetHost,
    ...(ANTIGRAVITY_MITM_PROFILE.additionalHosts || []),
  ];
  return [
    {
      id: ANTIGRAVITY_MITM_PROFILE.id,
      name: ANTIGRAVITY_MITM_PROFILE.name,
      targetHost: allHosts.join(", "),
      targetPort: ANTIGRAVITY_MITM_PROFILE.targetPort,
      localPort: port,
      endpoints: ANTIGRAVITY_MITM_PROFILE.apiEndpoints,
      enabled: true,
    },
    {
      id: KIRO_MITM_PROFILE.id,
      name: KIRO_MITM_PROFILE.name,
      targetHost: KIRO_MITM_PROFILE.targetHost,
      targetPort: KIRO_MITM_PROFILE.targetPort,
      localPort: KIRO_MITM_PROFILE.localPort,
      endpoints: KIRO_MITM_PROFILE.apiEndpoints,
      enabled: false,
    },
  ];
}

function readConfig(): MitmConfig {
  try {
    JSON.parse(fs.readFileSync(getConfigPath(), "utf8"));
    return {
      port: DEFAULT_PORT,
      targets: defaultTargets(DEFAULT_PORT),
    };
  } catch {
    return {
      port: DEFAULT_PORT,
      targets: defaultTargets(DEFAULT_PORT),
    };
  }
}

function writeConfig() {
  const mitmDir = getMitmDir();
  fs.mkdirSync(mitmDir, { recursive: true });
  fs.writeFileSync(getConfigPath(), JSON.stringify({ port: DEFAULT_PORT }, null, 2));
}

function readStats(): MitmStats {
  try {
    const raw = JSON.parse(fs.readFileSync(getStatsPath(), "utf8"));
    return {
      startedAt: typeof raw.startedAt === "string" ? raw.startedAt : null,
      totalRequests: Number(raw.totalRequests || 0),
      interceptedRequests: Number(raw.interceptedRequests || 0),
      activeConnections: Number(raw.activeConnections || 0),
      lastRequestAt: typeof raw.lastRequestAt === "string" ? raw.lastRequestAt : null,
      lastInterceptAt: typeof raw.lastInterceptAt === "string" ? raw.lastInterceptAt : null,
    };
  } catch {
    return {
      startedAt: null,
      totalRequests: 0,
      interceptedRequests: 0,
      activeConnections: 0,
      lastRequestAt: null,
      lastInterceptAt: null,
    };
  }
}

async function buildMitmResponse() {
  const { getMitmStatus, getCachedPassword } = await import("@/mitm/manager.runtime");
  const status = await getMitmStatus();
  const config = readConfig();
  const stats = readStats();

  return {
    running: status.running,
    pid: status.pid || null,
    dnsConfigured: status.dnsConfigured || false,
    certExists: status.certExists || fs.existsSync(getCertPath()),
    hasCachedPassword: !!getCachedPassword(),
    port: config.port,
    targets: config.targets,
    stats,
  };
}

export async function GET(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  try {
    const { searchParams } = new URL(request.url);
    if (searchParams.get("download") === "cert") {
      const certPath = getCertPath();
      if (!fs.existsSync(certPath)) {
        return NextResponse.json({ error: "MITM certificate not found" }, { status: 404 });
      }
      return new NextResponse(fs.readFileSync(certPath), {
        headers: {
          "Content-Type": "application/x-pem-file",
          "Content-Disposition": 'attachment; filename="omniroute-mitm-ca.crt"',
        },
      });
    }

    return NextResponse.json(await buildMitmResponse());
  } catch (error) {
    const message = error instanceof Error ? error.message : "Failed to load MITM settings";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}

export async function PUT(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  try {
    const rawBody = await request.json().catch(() => ({}));
    const parsed = updateMitmSchema.safeParse(rawBody);
    if (!parsed.success) {
      return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
    }

    const config = readConfig();
    if (parsed.data.port !== undefined && parsed.data.port !== DEFAULT_PORT) {
      return NextResponse.json({ error: MITM_PORT_ERROR }, { status: 400 });
    }

    if (parsed.data.port !== undefined) {
      config.port = DEFAULT_PORT;
      config.targets = defaultTargets(config.port);
      writeConfig();
    }

    if (typeof parsed.data.enabled === "boolean") {
      const { getCachedPassword, setCachedPassword, startMitm, stopMitm } =
        await import("@/mitm/manager.runtime");
      const { isRoot } = await import("@/mitm/systemCommands");
      const isWin = process.platform === "win32";
      const isRootUser = !isWin && isRoot();
      const sudoPassword = parsed.data.sudoPassword || getCachedPassword() || "";

      if (parsed.data.enabled) {
        const apiKey = await resolveApiKey(parsed.data.keyId || null, parsed.data.apiKey || null);
        if (!apiKey || (!isWin && !isRootUser && !sudoPassword)) {
          return NextResponse.json(
            { error: isWin ? "Missing apiKey" : "Missing apiKey or sudoPassword" },
            { status: 400 }
          );
        }
        await startMitm(apiKey, sudoPassword, { port: config.port });
        if (!isWin) setCachedPassword(sudoPassword);
      } else {
        if (!isWin && !isRootUser && !sudoPassword) {
          return NextResponse.json({ error: "Missing sudoPassword" }, { status: 400 });
        }
        await stopMitm(sudoPassword);
        if (!isWin && parsed.data.sudoPassword) setCachedPassword(parsed.data.sudoPassword);
      }
    }

    return NextResponse.json(await buildMitmResponse());
  } catch (error) {
    const message = error instanceof Error ? error.message : "Failed to update MITM settings";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}

export async function POST(request: Request) {
  const authError = await requireManagementAuth(request);
  if (authError) return authError;

  try {
    const rawBody = await request.json().catch(() => ({}));
    const parsed = regenerateSchema.safeParse(rawBody);
    if (!parsed.success) {
      return NextResponse.json({ error: parsed.error.flatten() }, { status: 400 });
    }

    const { getMitmStatus } = await import("@/mitm/manager.runtime");
    const status = await getMitmStatus();
    if (status.running) {
      return NextResponse.json(
        { error: "Stop the MITM proxy before regenerating certificates" },
        { status: 409 }
      );
    }

    for (const filePath of [getCertPath(), getKeyPath()]) {
      try {
        if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
      } catch {
        /* ignore */
      }
    }

    const { generateCert } = await import("@/mitm/cert/generate");
    await generateCert();

    return NextResponse.json(await buildMitmResponse());
  } catch (error) {
    const message =
      error instanceof Error ? error.message : "Failed to regenerate MITM certificate";
    return NextResponse.json({ error: message }, { status: 500 });
  }
}