web/src/utils/fetch-website-info.ts
156 lines · 3160 bytes
1import { error } from './logger';
2import { safeFetch } from './safe-fetch';
3import { apiBase, enrichHeaders } from './api-config';
4
5export const fetchWebsiteInfo = async (
6 url: string,
7): Promise<WebsiteData | null> => {
8 const endpoint = `${apiBase}/v1/enrich/website?url=${encodeURIComponent(url)}`;
9 try {
10 const res = await safeFetch(endpoint, { headers: enrichHeaders() });
11 if (!res.ok) {
12 error('Website', `HTTP ${res.status} for ${url} (${endpoint})`);
13 return null;
14 }
15 return await res.json();
16 } catch (err) {
17 error('Website', `Network error for ${url}: ${err}`);
18 return null;
19 }
20};
21
22interface DNSRecord {
23 target: string;
24 ip: string;
25 country_code: string;
26 country_name: string;
27 isp: string;
28}
29
30interface DNSRecords {
31 ns: {
32 records: DNSRecord[];
33 };
34 mx: {
35 records: DNSRecord[];
36 };
37}
38
39interface Engine {
40 name: string;
41 reference: string;
42 detected: boolean;
43}
44
45interface DomainBlacklist {
46 engines: Engine[];
47 detections: number;
48}
49
50interface FileType {
51 signature: string;
52 extension: string;
53 headers: string;
54}
55
56interface GeoLocation {
57 countries: string[];
58}
59
60interface HtmlForms {
61 number_of_total_forms: number;
62 number_of_total_input_fields: number;
63 two_text_inputs_in_a_form: boolean;
64 credit_card_field_present: boolean;
65 password_field_present: boolean;
66 email_field_present: boolean;
67}
68
69interface Redirection {
70 found: boolean;
71 external: boolean;
72 url: string;
73 redirects: string[];
74}
75
76interface ResponseHeaders {
77 code: number;
78 status: string;
79 date: string;
80 last_modified: string;
81 etag: string;
82 accept_ranges: string;
83 vary: string;
84 content_encoding: string;
85 cache_control: string;
86 content_length: string;
87 content_type: string;
88 age: string;
89 content_security_policy_report_only: string;
90 strict_transport_security: string;
91 public_key_pins_report_only: string;
92 x_frame_options: string;
93 x_content_type_options: string;
94 x_xss_protection: string;
95 referrer_policy: string;
96 x_permitted_cross_domain_policies: string;
97 onion_location: string;
98}
99
100interface RiskScore {
101 result: number;
102}
103
104interface SecurityChecks {
105 [key: string]: boolean | string;
106}
107
108interface ServerDetails {
109 ip: string;
110 hostname: string;
111 continent_code: string;
112 continent_name: string;
113 country_code: string;
114 country_name: string;
115 region_name: string;
116 city_name: string;
117 latitude: number;
118 longitude: number;
119 isp: string;
120 asn: string;
121}
122
123interface SiteCategory {
124 [key: string]: boolean;
125}
126
127interface UrlParts {
128 scheme: string;
129 host: string;
130 host_nowww: string;
131 port: null | number;
132 path: null | string;
133 query: null | string;
134}
135
136interface WebPage {
137 title: string;
138 description: string;
139 keywords: string;
140}
141
142export interface WebsiteData {
143 dns_records: DNSRecords;
144 domain_blacklist: DomainBlacklist;
145 file_type: FileType;
146 geo_location: GeoLocation;
147 html_forms: HtmlForms;
148 redirection: Redirection;
149 response_headers: ResponseHeaders;
150 risk_score: RiskScore;
151 security_checks: SecurityChecks;
152 server_details: ServerDetails;
153 site_category: SiteCategory;
154 url_parts: UrlParts;
155 web_page: WebPage;
156}