husteno-o feat: code review system, type safety, error handling, AI provider fallback · 9/9/2026
5ccaeae
283 lines · 8.0 KB
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
283
import * as dns from "node:dns/promises";
import * as tls from "node:tls";
import { ValidationError } from "@devpulse/core";
import { validateSafeUrl } from "./ssrf.js";

export async function lookupDns(
  hostname: string,
  recordType: "A" | "AAAA" | "CNAME" | "MX" | "TXT" | "NS" = "A",
): Promise<{ hostname: string; recordType: string; records: string[] }> {
  const cleanHost = hostname
    .replace(/^https?:\/\//, "")
    .split("/")[0]
    .split(":")[0]
    .trim();
  if (!cleanHost) throw new ValidationError("Invalid domain name.");

  try {
    let records: string[] = [];
    switch (recordType.toUpperCase()) {
      case "A": {
        const res = await dns.resolve4(cleanHost);
        records = res;
        break;
      }
      case "AAAA": {
        const res = await dns.resolve6(cleanHost);
        records = res;
        break;
      }
      case "CNAME": {
        const res = await dns.resolveCname(cleanHost);
        records = res;
        break;
      }
      case "MX": {
        const res = await dns.resolveMx(cleanHost);
        records = res.map((r) => `${r.priority} ${r.exchange}`);
        break;
      }
      case "TXT": {
        const res = await dns.resolveTxt(cleanHost);
        records = res.map((r) => r.join(" "));
        break;
      }
      case "NS": {
        const res = await dns.resolveNs(cleanHost);
        records = res;
        break;
      }
      default:
        throw new ValidationError(`Unsupported record type: ${recordType}`);
    }

    return { hostname: cleanHost, recordType, records };
  } catch (err: unknown) {
    throw new ValidationError(
      `DNS lookup failed for ${cleanHost} (${recordType}): ${err instanceof Error ? err.message : String(err)}`,
    );
  }
}

export interface SslCertInfo {
  domain: string;
  issuer: string;
  subject: string;
  validFrom: string;
  validTo: string;
  daysRemaining: number;
  isValid: boolean;
  subjectAltNames: string[];
}

export async function inspectSslCertificate(domain: string, port = 443): Promise<SslCertInfo> {
  const cleanDomain = domain
    .replace(/^https?:\/\//, "")
    .split("/")[0]
    .split(":")[0]
    .trim();
  if (!cleanDomain) throw new ValidationError("Invalid domain name.");

  // SSRF check on domain
  await validateSafeUrl(`https://${cleanDomain}`);

  return new Promise((resolve, reject) => {
    const socket = tls.connect(
      {
        host: cleanDomain,
        port,
        servername: cleanDomain,
        rejectUnauthorized: false,
        timeout: 5000,
      },
      () => {
        const cert = socket.getPeerCertificate(true);
        socket.end();

        if (!cert || !cert.valid_to) {
          return reject(new ValidationError(`No SSL certificate found for ${cleanDomain}`));
        }

        const validToDate = new Date(cert.valid_to);
        const validFromDate = new Date(cert.valid_from);
        const now = Date.now();
        const daysRemaining = Math.max(
          0,
          Math.floor((validToDate.getTime() - now) / (1000 * 60 * 60 * 24)),
        );
        const isValid = socket.authorized && daysRemaining > 0;

        const sans = cert.subjectaltname
          ? cert.subjectaltname.split(",").map((s: string) => s.trim().replace(/^DNS:/, ""))
          : [];

        resolve({
          domain: cleanDomain,
          issuer:
            typeof cert.issuer === "object"
              ? cert.issuer.O || cert.issuer.CN || "Unknown"
              : "Unknown",
          subject: typeof cert.subject === "object" ? cert.subject.CN || cleanDomain : cleanDomain,
          validFrom: validFromDate.toISOString(),
          validTo: validToDate.toISOString(),
          daysRemaining,
          isValid,
          subjectAltNames: sans.slice(0, 10),
        });
      },
    );

    socket.on("error", (err: Error) => {
      reject(new ValidationError(`SSL connection failed: ${err.message}`));
    });

    socket.on("timeout", () => {
      socket.destroy();
      reject(new ValidationError(`SSL connection timed out for ${cleanDomain}`));
    });
  });
}

export interface HttpRequestResult {
  url: string;
  method: string;
  statusCode: number;
  statusText: string;
  responseTimeMs: number;
  headers: Record<string, string>;
  bodySnippet: string;
  contentLength: number;
}

export async function testHttpRequest(
  rawUrl: string,
  method: "GET" | "HEAD" | "POST" = "GET",
  body?: string,
): Promise<HttpRequestResult> {
  const safeUrl = await validateSafeUrl(rawUrl);
  const startTime = Date.now();

  const controller = new AbortController();
  const timeout = setTimeout(() => controller.abort(), 6000);

  try {
    const response = await fetch(safeUrl.toString(), {
      method,
      headers: {
        "User-Agent": "DevPulse-Bot/1.0 (+https://github.com/swadhin/discordbot)",
        Accept: "*/*",
      },
      body: method === "POST" ? body : undefined,
      signal: controller.signal,
    });

    const responseTimeMs = Date.now() - startTime;
    const headers: Record<string, string> = {};
    response.headers.forEach((val, key) => {
      headers[key] = val;
    });

    let bodySnippet = "";
    if (method !== "HEAD") {
      const text = await response.text();
      bodySnippet = text.slice(0, 1000);
    }

    return {
      url: safeUrl.toString(),
      method,
      statusCode: response.status,
      statusText: response.statusText,
      responseTimeMs,
      headers,
      bodySnippet,
      contentLength: bodySnippet.length,
    };
  } finally {
    clearTimeout(timeout);
  }
}

export const HttpStatusCodes: Record<number, { name: string; description: string }> = {
  200: { name: "OK", description: "The request has succeeded." },
  201: {
    name: "Created",
    description: "The request has been fulfilled and a new resource was created.",
  },
  204: {
    name: "No Content",
    description: "The server successfully processed the request and is not returning any content.",
  },
  301: {
    name: "Moved Permanently",
    description: "The target resource has been assigned a new permanent URI.",
  },
  302: {
    name: "Found",
    description: "The target resource resides temporarily under a different URI.",
  },
  304: { name: "Not Modified", description: "The client can use cached data." },
  400: {
    name: "Bad Request",
    description: "The server could not understand the request due to invalid syntax.",
  },
  401: {
    name: "Unauthorized",
    description: "The request requires user authentication credentials.",
  },
  403: {
    name: "Forbidden",
    description: "The server understood the request but refuses to authorize it.",
  },
  404: { name: "Not Found", description: "The server can not find the requested resource." },
  405: {
    name: "Method Not Allowed",
    description: "The request method is not supported for the requested resource.",
  },
  409: {
    name: "Conflict",
    description: "The request conflicts with the current state of the server.",
  },
  422: {
    name: "Unprocessable Entity",
    description:
      "The server understands the content type and syntax, but was unable to process the contained instructions.",
  },
  429: {
    name: "Too Many Requests",
    description: "The user has sent too many requests in a given amount of time.",
  },
  500: {
    name: "Internal Server Error",
    description:
      "The server encountered an unexpected condition that prevented it from fulfilling the request.",
  },
  502: {
    name: "Bad Gateway",
    description:
      "The server, while acting as a gateway or proxy, received an invalid response from the inbound server.",
  },
  503: {
    name: "Service Unavailable",
    description:
      "The server is currently unable to handle the request due to temporary overloading or maintenance.",
  },
  504: {
    name: "Gateway Timeout",
    description:
      "The server, while acting as a gateway or proxy, did not receive a timely response from an upstream server.",
  },
};

export function lookupHttpStatus(code: number): {
  code: number;
  name: string;
  description: string;
} {
  const found = HttpStatusCodes[code];
  if (!found) {
    throw new ValidationError(`Unknown or uncataloged HTTP status code: ${code}`);
  }
  return { code, ...found };
}