import {z} from 'zod' export type CreateApiThreatFeedsC2Args = {body: {date?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, host?: string | undefined, family?: string | undefined, country?: string | undefined, feedType?: string | undefined, reviewStatus?: string | undefined, asn?: number | undefined, sort?: ('date' | 'seen' | 'host') | undefined, dir?: ('asc' | 'desc') | undefined, cursor?: string | undefined, limit?: number | undefined, withTotal?: boolean | undefined}}; export type CreateApiThreatFeedsC2Options = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiThreatFeedsC2Response = z.object({data: z.array(z.object({_id: z.string().optional(), host: z.string().optional(), dayUtc: z.string().optional(), feedType: z.string().nullable().optional(), families: z.array(z.string()).optional(), tags: z.array(z.string()).optional(), infraType: z.string().nullable().optional(), country: z.string().nullable().optional(), asn: z.number().int().nullable().optional(), asOrg: z.string().nullable().optional(), resolvedIps: z.array(z.string()).optional(), ports: z.array(z.number().int()).optional(), sha256: z.array(z.string()).optional(), dnsActivity: z.number().int().optional(), dnsActivityByWindow: z.array(z.object({windowDays: z.number().int().optional(), count: z.number().int().optional()})).optional(), passiveDnsHits: z.number().int().optional(), trancoRank: z.number().int().nullable().optional(), firstSeen: z.string().nullable().optional(), lastSeen: z.string().nullable().optional(), addedAt: z.string().nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional(), compromised_data: z.object({domain: z.string().optional(), compromised_employees: z.number().int().optional(), last_employee_compromised: z.string().nullable().optional(), compromised_users: z.number().int().optional(), last_user_compromised: z.string().nullable().optional()}).optional()})).optional(), pageInfo: z.object({limit: z.number().int().optional(), count: z.number().int().optional(), hasMore: z.boolean().optional(), nextCursor: z.string().nullable().optional(), total: z.number().int().optional(), sort: z.object({key: z.string().optional(), dir: z.enum(["asc", "desc"]).optional()}).optional()}).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * C2 Host Feed * * POST /threat-feeds/c2 */ export const createApiThreatFeedsC2 = async (args: CreateApiThreatFeedsC2Args, options?: CreateApiThreatFeedsC2Options) => { const {baseUrl = "https://api.hudsonrock.com/json/v3", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/threat-feeds/c2${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiThreatFeedsC2Response, await res.json()) };